Top Banner
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. }
19

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.

Mar 26, 2015

Download

Documents

Isabel Owens
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: 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.

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. }

Page 2: 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.

Logical Operators

There are three logical operators in Java. AND &&

OR ||

NOT !

1. boolean even = (x%2 == 0 && x != 0);

Page 3: 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.

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. }

Page 4: 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.

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.

Page 5: 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.

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. }

Page 6: 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.

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.

public static void(int x) {

while (x > 0){

System.out.println(x);

x--;

}

System.out.println(“Blastoff!”);

}

Page 7: 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.

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.

Page 8: 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.

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(“”);

Page 9: 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.

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. }

Page 10: 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.

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.

Page 11: 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.

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.

Page 12: 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.

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;

Page 13: 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.

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).

Page 14: 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.

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 we’re referring to

Page 15: 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.

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.

Page 16: 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.

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

Page 17: 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.

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. }

Page 18: 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.

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);

Page 19: 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.

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. }