Question
import java.awt.Rectangle; import java.awt.Point; public class HW3 { public static void main(String[] args) { //Creates a rectangle with top left hand point at location 0,0.
import java.awt.Rectangle; import java.awt.Point;
public class HW3 { public static void main(String[] args) { //Creates a rectangle with top left hand point at location 0,0. // and width=100 and height=200 Rectangle box = new Rectangle(0,0,100,200); //TASK #1 - Object as an input parameter System.out.println("Task #1, AREA is: " + getArea(box) ); //TASK #2 - Object as a return type Point[] pts = getPoints(box); if (pts != null) { System.out.println("Task #2, Lower Left Point is: " + pts[0] ); System.out.println("Task #2, Top Left Point is: " + pts[1] ); System.out.println("Task #2, Top Right Point is: " + pts[2] ); System.out.println("Task #2, Lower Right Point is: " + pts[3] ); } else System.out.println("Task #2, the Rectangle points are unknown"); //Task #3 - Mutable Object int old_width = box.width; int old_height = box.height; shrink(box); System.out.println("Task #3, width changed from " + old_width + " to " + box.width); System.out.println("Task #3, height changed from " + old_height + " to " + box.height); //Task #4 - Aliasing Rectangle new_box = copy_and_shrink(box); System.out.println("Task #4, the area of the old box is: " + getArea(box) ); System.out.println("Task #4, the area of the new box is: "+ getArea(new_box) ); } //TASK #1 - Object as an input parameter // - Complete the method to calculate the area of a rectangle. public static double getArea(Rectangle rect) { //-----YOUR TASK #1 CODE GOES HERE!----- return 0.0; //Do not forget to change the return value } //TASK #2 - Object as a return type // - Complete the method to return all four points that define the rectangle public static Point[] getPoints(Rectangle rect) { Point[] pts = new Point[4]; pts[0] = new Point(rect.x,rect.y - rect.height); // Assigning the first point (lower left point pt0) //-----YOUR TASK #2 CODE GOES HERE!----- return null; //Do not forget to change the return value } //TASK #3 - Mutable Object (notice that we do not need to return the object) public static void shrink(Rectangle rect) { //-----YOUR TASK #3 CODE GOES HERE!----- //Notice - we do not have to return anything! } //TASK #4 - Object Aliasing public static Rectangle copy_and_shrink(Rectangle original_rectangle) { Rectangle new_rectangle = new Rectangle(original_rectangle); //-----YOUR TASK #4 CODE GOES HERE!----- return null; //Do not forget to change the return value } }
Programming Assignment 3 - Methods and Objects (16 points). TASK #1-Objects as Parameters (4 pts). Edit the method getArea to calculate the area of a Rectangle class. //TASK #1 -Object as an input parameter- - Complete the method to calculate the area of a rectangle.+ public static double getArea (Rectanqle rect) YOUR TASK #1 CODE GOES HERE ' +' return 0.0 L/Do not forget to change the return value TASK #2-Objects as Return Types(4 pts). Edit the method called getPoints that returns all four points that define the rectangle. The points should be returned in clockwise order. Notice that the return type is a Point array (Point[]). ptl pt2 pto pt3 // TASK #2 - Object as a return type- // Complete the method to return all four points that define the rectangle public static PointI] getRoints (Rectangle rect) . Pointl] pts new Point[4] // Assigning the first point (lower left point pto) pts[0] = new Point (rect.x, rect.v- rect height); YOUR TASK #2 CODE GOES HERE! return null; //Do not forget to change the return value
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started