Presentation is loading. Please wait.

Presentation is loading. Please wait.

Overloading Having more than one method with the same name is known as overloading. Overloading is legal in Java as long as each version takes different.

Similar presentations

Presentation on theme: "Overloading Having more than one method with the same name is known as overloading. Overloading is legal in Java as long as each version takes different."— Presentation transcript:

1 Overloading Having more than one method with the same name is known as overloading. Overloading is legal in Java as long as each version takes different parameters. 1. public static double area(double length, double width) { 2. double result = length * width; 3. return result; 4. } 5. public static double area(double side){ 6. double result = side * side; 7. return result; 8. }

2 Logical Operators There are three logical operators in Java. AND && OR || NOT ! 1. boolean even = (x%2 == 0 && x != 0);

3 while Java provides language features that make repetition processes easier to write. The while loop will continue to run the statements in its curly braces as long as the condition in the parentheses is true. 1. int x = 2; 2. while (x < 100) { 3. System.out.println(x); 4. x = x * x; 5. }

4 while loop - flow of execution 1. Evaluate the condition in the parentheses, yielding true or false 2. If the condition is false, exit the while statement and continue execution at the next statement. 3. If the condition is true, execute the statements between the squiggly brackets, and then go back to step 1.

5 infinite loops We have to be careful when using loops to make sure that the condition will become false at some point and terminate the loop. When the condition remains true forever, it is called an infinite loop. 1. while(x < 0) { 2. System.out.println(x); 3. x++; 4. }

6 Exercise 3-1 Recreate the countdown method which uses a while loop to decrement a number down to 0 and then print out Blastoff! Try to do this without if statements. 1. public static void(int x) { 2. while (x > 0){ 3. System.out.println(x); 4. x--; 5. } 6. System.out.println(Blastoff!); 7. }

7 Encapsulation & Generalization Encapsulation is taking a piece of code and wrapping it up in a method, allowing you to take advantage of all the things methods are good for. Generalization means takings something specific, like printing multiples of 2, and making it more general, like printing the multiples of any integer.

8 Encapsulation & Generalization multiples of two 1. int i = 1; 2. while (i <= 6) { 3. System.out.print(2*i + ); 4. i = i + 1; 5. } 6. System.out.println();

9 Encapsulation & Generalization printMultiples method 1. public static void printMultiples(int n) { 2. int i = 1; 3. while (i <= 6) { 4. System.out.print(n*i + ); 5. i = i + 1; 6. } 7. System.out.println(); 8. }

10 Exercise 3-2 Create a new method called printMultTable that has one parameter x. Make printMultTable print out an x by x multiplication table. Hint: Make printMultTable call the printMultiples function x times.

11 Exercise 3-3 Create a recursive method called isPalindrome that takes a String and returns a boolean indicating whether the word is a palindrome or not. Create a method called iterPalindrome that does the same thing but iteratively (using a while loop). Hint: A palindrome is a word that is the same forwards and backwards.

12 Importing Packages Java libraries are divided into packages Java.lang contains most of the classes we have used so far and is imported automatically. Java.awt, or Abstract Window Toolkit contains classes for windows, buttons, graphics, etc. All import statements appear at the beginning of the program, outside the class definition. To import a package, we use the import statement. For example, Point and Rectangle are in the java.awt package and would be imported like this: 1. import java.awt.Point; 2. import java.awt.Rectangle;

13 Point objects A point is two numbers (coordinates) that we treat collectively as a single object. In math, points are often written in parentheses with a comma separating the coordinates. (0,0) indicates the origin and (x,y) indicates the point x units to the right and y units up from the origin. In Java, a point is represented by a Point object. To create a new point, you have to use new: 1. Point blank; 2. blank = new Point(3, 4); The first line is a conventional variable declaration: blank has type Point The second line invokes new, specifies the type of the new object, and provides arguments. The arguments are the coordinates of the new point (3,4).

14 new The result of new is a reference to the new point. In the previous example, blank contains a reference to the newly-created object. This is the standard way to diagram the assignment. As usual, the name of the variable blank appears outside the box and its value appears inside the box. In this case, the value is a reference which is shown graphically with an arrow. The arrow points to the object were referring to

15 state diagrams These diagrams are called state diagrams and are commonly used to show the state of the program. A state diagram can be thought of as a snapshot of the particular point in the execution. The names x and y are the names of the instance variables.

16 Instance Variables The pieces of data that make up and object are called instance variables because each object, which is an instance of its type, has its own copy of the instance variables. An instance could be thought of as a glove compartment in a car. Each car is an instance of the type car and each car has its own glove compartment. If you want to get something from the glove compartment of your car, you need to specify which car is yours. Java uses the dot notation to specify the object you want to get a value from. 1. int x = blank.x; // go to the object blank refers to and get the value of x

17 Dot Notation You can use the dot notation as part of any Java expression. 1. System.out.println(blank.x +, + blank.y); You can also pass objects as parameters in the usual way. For example: 1. public static void printPoint(Point p) { 2. System.out.println(( + p.x +, + p.y + )); 3. }

18 Rectangles Rectangles are similar to points except that they have four instance variables: x, y, width, height. Other than that everything is pretty much the same 1. Rectangle box = new Rectangle(0, 0, 100, 200);

19 Objects as return types You can write methods that return objects. For example, findCenter takes a Rectangle as an argument and returns a Point that contains the coordinates of the center of the Rectangle. 1. public static Point findCenter(Rectangle box) { 2. int x = box.x + box.width/2; 3. int y = box.y + box.height.2; 4. return new Point(x, y); 5. }

Download ppt "Overloading Having more than one method with the same name is known as overloading. Overloading is legal in Java as long as each version takes different."

Similar presentations

Ads by Google