Tutorial
Tutorial
1. Define a class called Point with two double fields named x and y. Hint: Refer to the "fields" example above!
7. class Rectangle 8. { 9. int x; int y; //could join together int width; int height; }
3. Define a constructor for class Rectangle that initialises x and y to zero, initialises width to 10 and height to 5. Hint: Refer to the "constructor" example above! CHECK PLACEMENT INOCCURENCE WITH OTHERS
10.
4. Define a method for class Rectangle called showArea that calculates the area of the rectangle as a double, and prints it out in the format "The area is XYZ" where XYZ is the computed area. Hint: Well, by now you know what I'm going to say! Look for an example method above that prints something out on the screen, e.g. the sayHello() example. Remember, there is an example above for everything.
5. void showArea() 6. { 7. double area = width * height; // initialize in one line 8. System.out.println("The area is + area); 9. 10. }
5. Define a method for class Rectangle called showPosition that prints out the position of the rectangle in the format "The position is (X,Y)" where X and Y are the current x and y positions of the rectangle
7. Inside class Lab3, fill in the code for method rectangleUpdate to do the following steps: a. Create a new Rectangle() called r. b. Show its position. c. Show its area. d. Change its position to (3,4). e. Change its size to have width=4 and height=7. f. Show its position again. g. Show its area again.