Download presentation
Presentation is loading. Please wait.
1
1 Tirgul no. 3 Topics covered: H Iteration. H Defining and using classes.
2
2 for loop for ( statement1; condition; statement2) { statements; } Where statement1 the condition and statement2 may be empty or contain multiple statements.
3
3 while loop while (boolean-condition) { statements; } do { statements; } while(boolean-condition);
4
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); }
5
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; }
6
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; }
7
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); }
8
8 Nested 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:
9
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 Method boolean checkValue (int col, int row, int limit) returned type of method name of method three formal parameters of method, each of type int
10
10 Magic 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 1 3 + 3 3 + 5 3 = 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); }
11
11 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); }
12
12 Defining 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; }
13
13 Using 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); }
14
14 Extending 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); }
15
15 Get 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. */
16
16 Using 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(); }
17
17 Circle 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 <= radius) { return true; } else { return false; }
18
18 Using 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"); }
19
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 only interested in the file MyClass.html. Javadoc (from sun’s javadoc documentation)
20
20 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 radius. * @param radius new circle radius. */ public setRadius(double radius) { this.radius = radius; }
21
21 Example of javadoc documentation /** * The Point class represents a 2-dimensional integral 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; }
22
22
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.