CSE114 Spring 2016 Lab Exercise 1 of Week 7 Classes and Objects: Points on a 2-Dimensional Plane Chen-Wei Wang 1 Recap from the Previous Section – Each Java class is used to define a template. e.g., class Person – In each Java class, we define the common attributes and behaviour (i.e., methods) that are shared by all its instances. e.g., In the Person class, we declare attributes age, firstName, lastName, weight, height, nationality, and so on. In the same class, we also define methods: – Mutator Methods: growOlder, changeNationality, gainWeight, and loseWeight. Exercise: Define gainWeightBy and loseWeightBy. – Accessor Methods: getAge, getNationality, and isMiddleAged. Exercise: Define getName and getBMI. – From each Java class, we can instantiate as many objects as we wish. e.g., We may have jim and jonathan both being instances of the Person class. – All objects support the same attribute and methods as defined in its instantiating class. e.g., Both jim and jonathan have a first name, a last name, a weight, a height, and a nationality. – Each object, however, might have instance-specific values for attributes. e.g., jim’s nationality is British, whereas jonathan’s nationality is Canadian. Also, their weights and heights may also be different. – Consequently, calling the same method on different objects might have different effects. e.g., Since jim and jonathan’s weights and heights are different, calling the same getBMI method will give us different BMI values. Similarly, since they might gain weight or lose weight, calling the same getBMI method before and after gain/lose weight will also give us different BMI values. 2 Exercise: Two Dimensional Points 1. Create a new class Point that represents a template for points on a two-dimensional plane. 2. Create a new class PointTester with a main method, where we will create and manipulate instances of the Point class. 3. In the Point class: 3.1 Declare two attributes x and y of type double. 1 3.2 Define a constructor that initializes values of the two attributes x and y as those of two parameters initX and initY (of type double). 4. In the PointTester class: 4.1 Create an object p1 of type Point (i.e., p1 is an instance of the Point template). Let the initial location of p1 be (0, 2). 4.2 Print out the location of p1 by accessing its two attributes x and y. 5. In the Piont class, define the following two methods void moveRight(double unit) { x = x + unit; } void moveUp(double unit) { y = y + unit; } which moves the current point right and up by incrementing values of, respectively, attribute x and attribute y by certain units. 6. In the PointTester class, use the moveRight and moveUp methods in the Point class to first move p1 (currently in the location of (0, 2)) right for 3 units (resulting in a new location of (3, 2)), and then up for 2 units (resulting in a new location of (3, 4)). Between these movements, print out the current location of p1 and observe its changes. 7. In the Point class, define the moveLeft and moveDown methods. 8. In the PointTester class, move p1 left and down using the moveLeft and moveDown methods. 9. In the Point class, define the following method double getDistanceFromOrigin() { double distance = Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2)); return distance; } which calculates the distance between the current point and the origin (at location (0, 0)) by applying the formula √ (x− 0)2 + (y − 0)2. 10. In the PointTester class, calculate, using the getDistanceFromOrigin() method in the Point class, and print the distance between p1 and the origin. 11. In the Point class, define the method getDistanceFrom which calculates the distance between the current point and another point named other using the formula √ (x− other.x)2 + (y − other.y)2. What should be the appropriate signature of getDistanceFrom (e.g., parameters, return type)? 12. In the PointTester class, create a new object p2 of type Point at location (-6, -8). Calculate and print the distance from p1 to p2, and the distance from p2 to p1. 13. In the PointTester class: – Create another three points: p3 at (7, 9), p4 at (8, 1), and p5 at (-2, -4). – Create an array poitns of points of size five to accommodate p1 to p5. – Write a for loop to move each point in points up for 5 units. – Before and after the movements of these points, write loops to print out their locations to observe the changes. 14. In the PointTester class: – For each point in points that is above the y-axis, move it right for 6 units. – Before and after the movements of these points, write loops to print out their locations to observe the changes. 2