Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
91 views

Tutorial

The document provides instructions for creating classes Point and Rectangle with fields and methods. It defines: 1. A Point class with x and y double fields. 2. A Rectangle class with x, y, width, and height int fields and a constructor to initialize them. 3. Methods for the Rectangle class to show position as a String and calculate/print area as a double. It then provides examples of using a Rectangle object, calling its methods to show initial position and area, and later updating the position and size before showing the new values.

Uploaded by

jakalabric
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
91 views

Tutorial

The document provides instructions for creating classes Point and Rectangle with fields and methods. It defines: 1. A Point class with x and y double fields. 2. A Rectangle class with x, y, width, and height int fields and a constructor to initialize them. 3. Methods for the Rectangle class to show position as a String and calculate/print area as a double. It then provides examples of using a Rectangle object, calling its methods to show initial position and area, and later updating the position and size before showing the new values.

Uploaded by

jakalabric
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Tutorial;

1. Define a class called Point with two double fields named x and y. Hint: Refer to the "fields" example above!

2. class Point 3. { 4. double x; 5. double y; 6. }


2. Define a class called Rectangle with 4 int fields named x, y, width and height

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

4. Rectangle() 5. { 6. X = 0; y = 0; 7. width = 10; 8. height = 5; 9. }

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

void showPosition() { System.out.println("The position is (" + x + , + y + ) ); }


6. Inside class Lab3, fill in the code for method rectangleArea to do the following steps: 1. Create a new Rectangle() called r. 2. Show its position. 3. Show its area. If you don't know how to do this, remember that this is the kind of exercise that you learnt how to do in week 1: declare/create/use an object.

Rectangle r = new Rectangle(); r.showPosition(); r.showArea(); Dot notation method

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.

You might also like