Top Banner
1 Tirgul no. 3 Tirgul no. 3 Topics covered : Iteration. Defining and using classes.
22

1 Tirgul no. 3 Topics covered: H Iteration. H Defining and using classes.

Dec 21, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: 1 Tirgul no. 3 Topics covered: H Iteration. H Defining and using classes.

1

Tirgul no. 3Tirgul no. 3

Topics covered:

Iteration. Defining and using classes.

Page 2: 1 Tirgul no. 3 Topics covered: H Iteration. H Defining and using classes.

2

for loop

for ( statement1; condition; statement2) { statements;}

•Where statement1 the condition and statement2 may be empty or contain multiple statements.

Page 3: 1 Tirgul no. 3 Topics covered: H Iteration. H Defining and using classes.

3

while loop

while (boolean-condition) {

statements;

}

do { statements;} while(boolean-condition);

Page 4: 1 Tirgul no. 3 Topics covered: H Iteration. H Defining and using classes.

4

for 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(i=0,s1=new String("a"),s2=new String("b"); i<3; i++,s1+="q",s2+="z") { System.out.println(s1); System.out.println(s2); } }}

Page 5: 1 Tirgul no. 3 Topics covered: H Iteration. H Defining and using classes.

5

Compute e ( 1/n!)

public double computeE1() { double answer = 0; int fact = 1; final int iterations = 20;

for(int i=0; i<iterations; i++) { answer += 1.0/fact; fact *= i+1; } return answer; }

Page 6: 1 Tirgul no. 3 Topics covered: H Iteration. H Defining and using classes.

6

Compute e ( 1/n!) (cont.)

public double computeE2() { final double epsilon = 0.00001; double answer = 0; int i=0,fact = 1; while(1.0/fact > epsilon) { answer += 1.0/fact; i++; fact *= i; } return answer; }

Page 7: 1 Tirgul no. 3 Topics covered: H Iteration. H Defining and using classes.

7

Reversing 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= (rev*10) + (src%10); src/=10; } System.out.println(rev); }}

Page 8: 1 Tirgul no. 3 Topics covered: H Iteration. H Defining and using classes.

8

Nested LoopsNested Loops

public static void main(String[] args) { for(int i=1; i<=9; i++) { for(int j=0; j<=Math.abs(i-5); j++) System.out.print("*"); System.out.println(); } }

*****************************

Print this pattern:

Page 9: 1 Tirgul no. 3 Topics covered: H Iteration. H Defining and using classes.

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

Formal Parameters of a MethodFormal 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

Page 10: 1 Tirgul no. 3 Topics covered: H Iteration. H Defining and using classes.

10

Magic NumbersMagic 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("Enter a value N to determine the range 1..N ”); int n = EasyInput.readInt(); for (int i=1; i<n; i++) { if (isMagic(i)) {

System.out.println(i); } } }

Page 11: 1 Tirgul no. 3 Topics covered: H Iteration. H Defining and using classes.

11

Magic Numbers (cont.)Magic 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 (number>0) {int digit = number%10;sumOfCubes += digit*digit*digit;number /= 10;

} return (n == sumOfCubes); }

Page 12: 1 Tirgul no. 3 Topics covered: H Iteration. H Defining and using classes.

12

Defining New ClassesDefining 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; }}

Page 13: 1 Tirgul no. 3 Topics covered: H Iteration. H Defining and using classes.

13

Using the Class We WroteUsing 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); }}

Page 14: 1 Tirgul no. 3 Topics covered: H Iteration. H Defining and using classes.

14

Extending the Classes FunctionalityExtending 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); }}

Page 15: 1 Tirgul no. 3 Topics covered: H Iteration. H Defining and using classes.

15

Get and Set methodsGet 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. */

Page 16: 1 Tirgul no. 3 Topics covered: H Iteration. H Defining and using classes.

16

Using Our ClassUsing Our Classpublic 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(); }}

Page 17: 1 Tirgul no. 3 Topics covered: H Iteration. H Defining and using classes.

17

Circle ClassCircle Classpublic 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 <= radius) { return true; } else { return false; } }

Page 18: 1 Tirgul no. 3 Topics covered: H Iteration. H Defining and using classes.

18

Using our Circle ClassUsing our Circle Classpublic 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"); } }}

Page 19: 1 Tirgul no. 3 Topics covered: H Iteration. H Defining and using classes.

19

•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 onlyinterested in the file MyClass.html .

JavadocJavadoc(from sun’s javadoc documentation)(from sun’s javadoc documentation)

Page 20: 1 Tirgul no. 3 Topics covered: H Iteration. H Defining and using classes.

20

Javadoc (cont.)Javadoc (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: @author - author of code. @param - parameter description. @return - return value description.

Example: /** * This method receives the new circle <b>radius</b>. * @param radius new circle radius. */ public setRadius(double radius) { this.radius = radius; }

Page 21: 1 Tirgul no. 3 Topics covered: H Iteration. H Defining and using classes.

21

Example of javadoc documentationExample of javadoc documentation/** * The Point class represents a <B> 2-dimensional integral</B> point. * @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. * @param x The x coordinate. * @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. * @param newX The new x-coordinate. */ public void setX(int newX) { x = newX; }

Page 22: 1 Tirgul no. 3 Topics covered: H Iteration. H Defining and using classes.

22