Title: Tirgul no. 3
1Tirgul no. 3
- Topics covered
- Iteration.
- Defining and using classes.
2for loop
- for ( statement1 condition statement2)
- statements
-
- Where statement1 the condition and statement2 may
be empty or - contain multiple statements.
3while loop
while (boolean-condition) statements
do statements while(boolean-condition)
4for example
public class For public static void
main(String args) int i String
s1,s2 //endless loop
for() System.out.println("q")
//a very bad example of using a for
loop for(i0,s1new String("a"),s2new
String("b") ilt3 i,s1"q",s2"z")
System.out.println(s1)
System.out.println(s2)
5 Compute e (S 1/n!)
public double computeE1() double answer
0 int fact 1 final int iterations
20 for(int i0 iltiterations i)
answer 1.0/fact fact i1
return answer
6 Compute e (S 1/n!) (cont.)
public double computeE2() final double
epsilon 0.00001 double answer 0 int
i0,fact 1 while(1.0/fact gt epsilon)
answer 1.0/fact i fact
i return answer
7Reversing an integer
public class ReverseInt public static void
main(String args) int src,rev
System.out.print("Please enter an integer )
src EasyInput.readInt() rev 0
while(src ! 0) rev (rev10)
(src10) src/10
System.out.println(rev)
8Nested Loops
Print this pattern
public static void main(String args)
for(int i1 ilt9 i)
for(int j0 jltMath.abs(i-5) j)
System.out.print("")
System.out.println()
9Formal Parameters of a Method
boolean checkValue (int col, int row, int limit)
returned typeof method
nameof method
three formal parametersof method, each of type
int
Call to checkValue( ) (as an expression)
x.checkValue(3,7,9) assigns 3, 7, and 9
to the formal parameters col, row, and limit,
respectively then executes the method
10Magic Numbers
Magic Number A magic number is a number whose
sum of cubes of digits
equals the number itself.
For example, 153 is a magic number since 13
33 53 153.
public class MagicNumbers public static void
main(String args) System.out.print("E
nter a value N to determine the range 1..N )
int n EasyInput.readInt() for (int i1
iltn i) if (isMagic(i))
System.out.println(i)
11Magic Numbers (cont.)
// Checks if a given number is a magic
number static boolean isMagic(int n)
int sumOfCubes 0 int number n //
keep the original number while (numbergt0)
int digit number10 sumOfCubes
digitdigitdigit number / 10
return (n sumOfCubes)
12Defining New Classes
/ This is a basic date object. For now it
cannot do anything except hold the initial
date it is given in creation. / public class
Date private int day // the day in
month, an integer variable private int
month private int year / This
is a constructur. This function is called when a
new object of type Date is created. It
receives three parameters that define the
date. Note that the parameters are
assumed to be valid. / public Date(int
day, int month, int year) this.day
day this.month month
this.year year
13Using the Class We Wrote
/ This is a small application that shows how
to use Date objects. / public class DateTest
/ This is a special method. If this
class is run by java then the excecution
is started here. / public static void
main(String args) Date d1 //
declare a variable that refers to a Date object.
// create a new Date
objecet with the date 23/6/1997.
// and make 'd' refer to it. d1 new
Date(23, 6, 1997)
// we can also declare a variable and
create // the object in
one line Date d2 new Date(14, 4,
1998)
14Extending the Classes Functionality
/ This is a basic date object. For now it
cannot do anything except hold the initial
date it is given in creation. / public class
Date private int day // the day in
month, an integer variable private int
month private int year / Same
as previous example. / public Date(int
day, int month, int year) this.day
day this.month month
this.year year / This method
prints the date to standard output. /
public void print() System.out.println(day
"." month "." year)
15Get and Set methods
/ The following methods are used to set
and get the values of private fields of the
object. / / set the value of the
day. / public void setDay(int newDay)
day newDay / return the
current day in the month. / public int
getDay() return day /
The setMonth(), getMonth(), setYear(), getYear()
methods are similar. /
16Using Our Class
public class DateTest public static void
main(String args) throws Exception Date
d1 new Date(12, 11, 1997) Date d2 new
Date(1, 1, 1998) int day
System.out.print("the date d1 is ") // don't
move to next line d1.print() // ask d1
to print itself System.out.print("the date
d2 is ") d2.print()
System.out.print("Please enter a new day for d1
") day EasyInput.readInt()
d1.setDay(day) // change d1's day. d2 is
unchanged. // print again
System.out.print("the date d1 is ") // don't
move to next line d1.print() // ask d1
to print itself System.out.print("the date
d2 is ") d2.print()
17Circle Class
public class Circle private double centerX
// the center of the circle private double
centerY private double radius // the
radius of the circle public Circle(double x,
double y, double radius) centerX x
centerY y this.radius radius
public boolean isIn(double x, double y)
//use the Math object's power and square
root methods to do //the
calculation. double dist
Math.sqrt(Math.pow((centerX-x), 2.0)
Math.pow((centerY - y),
2.0)) // now check if point is
in circle or not if (dist lt radius)
return true else
return false
18Using our Circle Class
public class PointInCircle public static
void main(String args) throws Exception
double x,y double radius
//read input from user and create
circle System.out.print("Enter the circle
center (x y) ") x EasyInput.readDouble
() y EasyInput.readDouble()
System.out.print("Enter the circle radius ")
radius EasyInput.readlnDouble()
Circle cir new Circle(x,y,radius)
System.out.print("Enter X and Y coordinates (use
space)") x EasyInput.readDouble()
y EasyInput.readlnDouble()
if(cir.isIn(x,y)) System.out.println("
The point is in the circle")
else System.out.println("The point is
not in the circle")
19Javadoc (from suns javadoc documentation)
- Javadoc parses the declarations and documentation
- comments in a set of Java source files and
produces - a corresponding set of HTML pages describing
(by - default) the public and protected classes,
inner - classes, interfaces, constructors, methods, and
fields. - You can include documentation comments in the
- source code, ahead of declarations for any
entity - (classes, interfaces, methods, constructors,
or - fields). These are also known as Javadoc
- comments, and the text must be written in
HTML, - in that they should use HTML entities and can
- use HTML tags.
-
- How to run javadoc MyClass.java
- The result includes several HTML files. We are
only - interested in the file MyClass.html .
20Javadoc (cont.)
- A doc comment consists of the characters between
the - characters / that begin the comment and the
- characters / that end it.
- Javadoc parses special tags when they are
embedded - within a Java doc comment. These doc tags
enable you to - autogenerate a complete, well-formatted API
from your - source code.
- Important Javadoc tags
- _at_author - author of code.
- _at_param - parameter description.
- _at_return - return value description.
- Example
- /
- This method receives the new circle
ltbgtradiuslt/bgt. - _at_param radius new circle radius.
- /
21Example of javadoc documentation
/ The Point class represents a ltBgt
2-dimensional integrallt/Bgt point. _at_author Ziv
Yaniv / public class Point private int x
// x-coordinate of this point private int y
// y-coordinate of this point /
Construct a new point with the specified x y
coordinates. _at_param x The x coordinate.
_at_param y The y coordinate. / public
Point(int x, int y) this.x x this.y
y / Return the x-coordinate of
this point. / public int getX()
return x
/ Set the x-coordinate of this point to be
the given value. _at_param newX The new
x-coordinate. / public void setX(int newX)
x newX
22(No Transcript)