240 likes | 516 Views
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.
E N D
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: • import java.awt.Point; • import java.awt.Rectangle;
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: • Point blank; • 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).
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
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.
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. • int x = blank.x; // go to the object blank refers to and get the value of x
Dot Notation • You can use the dot notation as part of any Java expression. • System.out.println(blank.x + “, “ + blank.y); • You can also pass objects as parameters in the usual way. For example: • public static void printPoint(Point p) { • System.out.println(“(“ + p.x + “, “ + p.y + “)”); • }
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 • Rectangle box = new Rectangle(0, 0, 100, 200);
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. • public static Point findCenter(Rectangle box) { • int x = box.x + box.width/2; • int y = box.y + box.height.2; • return new Point(x, y); • }
Constructors • Constructors are special methods which initialize instance variables. • The name of the constructor is the same as the name of the class. • Constructors have no return type and no return value. • The keyword static is omitted. This is how the compiler can tell that this is a constructor. • public Time() { • this.hour = 0; • this.minute = 0; • this.second = 0.0; • }
this • The name this is a special keyword that refers to the object we are creating. You can use this the same way you use the name of any other object. • You do not declare this and you can’t make an assignment to it. this is created by the system. All you have to do is initialize its instance variables.
Overloading Constructors • Constructors can be overloaded just like other methods. • You can provide multiple constructors with different parameters. Java will know which constructor to invoke by matching the arguments of new with the parameters of the constructor. • It is common to have one constructor that takes no arguments and one that takes a parameter list identical to the list of instance variables. • public Time(int hour, int minute, double second) { • this.hour = hour; • this.minute = minute; • this.second = second; • }
Exercise 4-1 Create a new object called Time which has three local variables: • integer hour • integer minute • double second This object should have two constructors: • No parameters, Assigns instance variables to 0. • 3 parameters (int hour, int minute, double second) and assigns the values to the local variables. Assigns the time 10:02:37.457 to two new Time objects using the two different constructors.
Printing Objects • When Java prints the value of a user-defines object type, it prints the name of the type and a special hexadecimal (base 16) code that is unique for each object. • Exercise 4-2 • Create a new method called printTime that prints out the time in the following format: HH:MM:SS making sure to round off the seconds.
Modifiers • Unlike pure functions, modifiers edit the value of an object. • public static void increment (double secs) { • this.second += secs; • while (this.second >= 60.0) { • this.second -= 60.0; • this.minute++; • } • while (this.minute >= 60) { • this.minute -= 60; • this.hour++; • } • }
Exercise 4-3 Create a method incrementSecond(Time t) which increases the time by one second.
Pure Functions • A method is considered a pure function if the result depends only on the arguments and has no effects like modifying an argument or printing something. The only result of invoking a pure function is the return value. • public static int getHour() { • return this.hour; • }
Exercise 4-4 Create a method named isBefore which takes one parameter Time t1 and returns whether or not the object time is before it or not. • Time start = new Time(8, 0, 0.0); • Time nap_time = new Time(9, 30, 0.0); • System.out.println(start.isBefore(nap_time)); • System.out.println(nap_time.isBefore(start));
Exercise 4-5 Create a new method called addTime which takes a single Parameter Time and adds it to the object’s time. The function should return a Time object and the code below should print out the time this class ends. • Time start = new Time(10, 02, 37.457); • Time duration = new Time(3, 15, 0.0); • Time end = addTime(start, duration); • end.printTime();
Arrays • An array is a set of values where each value is identified by an index. You can make an array of ints, doubles, or any other type, but all the values in an array have to be the same type. • Array types look like other Java types except they are followed by []. • int [] count; • double[] values; • Until these values are initialized, they are set to null.
Arrays • To create an array itself, use new. • count = new int[4]; • values = new double[size]; • You can use any integer expression as an array size. • State diagram of an array: • The large numbers inside the boxes are the elements. The small numbers outside the boxes are the indices used to identify each box. When you allocate an array of ints, the elements are initialized to zero.
Accessing elements • To store values in the array, use the [] operator. • count [0] = 7; // 7 • count [1] = count [0] * 2; // 14 • count [2]++; // 1 • count[3] -= 60; // -60
looping through arrays • You can use any expression as an index, as long as it has type int. • int i = 0; • while (i < 4) { • System.out.println(count[i]); • i++; • }
Copying Arrays • When you copy an array variable, remember that you are copying a reference to the array. • double [] a = new double [3]; • double [] b = a; // a and b refer to the same array • To allocate a new array and copy the elements from one to the other, use a loop. • double [] b = new double [3]; • inti = 0; • while (i < 4) { • b[i] = a[i]; • i++; • }
Exercise 4-6 Create a String hello which stores the value “Hello, World!” and store each character as an element of a char array named forward. Make a new char array named reverse and store the characters of hello in reverse order. Print out forward and backward.