Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

My program 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

image text in transcribedMy program

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!----- double area = rect.getHeight() * rect.getWidth(); return area; // 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!----- pts[1] = new Point(rect.x, rect.y); // Assigning the first point (top left point pt1) pts[2] = new Point(rect.x + rect.width, rect.y); // Assigning the third point (top right point pt2) pts[3] = new Point(rect.x + rect.width, rect.y - rect.height); // Assigning the last point (lower right point pt3) return pts; // 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! rect.height = (int) ((rect.getHeight()*50)/100); rect.width = (int) ((rect.getWidth()*50)/100); }

// 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!----- new_rectangle.height = (int) ((original_rectangle.getHeight()*50)/100); new_rectangle.width = (int) ((original_rectangle.getWidth()*50)/100); return original_rectangle; // Do not forget to change the return value } }

But The result is

image text in transcribed

The old box has to be 20000.0

and the new box has to be 5000.0 which is right.

What should I change to get 20000.0 for old box.

TASK #3-Mutable Objects (4 pts). Edit the method called shrink that reduces the size of rectangles width and height by 50% pt1 pt2 Height-50% width-50% pt0 pt3 //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-Aliasing Objects (4 pts). Edit the method called copy and shrink. The method should perform the same functionality as TASK #3, but it should make a copy of the rectangle. In other words, it should not change or edit the original rectangle that is passed in as an input parameter.- pt1 pt2 Height-50% pt2 width-50% pt3 pt0 Copy-width & Height are reduced by 50% pt0 pt3 Original - Does not change //TASK #4 -Object Aliasing.. public static Rectanglecopy and shrink (Rectangle original rectangle) Rectangle new rec angle = new Rectangle (original re angle):- YOUR TASK #4 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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Machine Learning And Knowledge Discovery In Databases European Conference Ecml Pkdd 2014 Nancy France September 15 19 2014 Proceedings Part I Lnai 8724

Authors: Toon Calders ,Floriana Esposito ,Eyke Hullermeier ,Rosa Meo

2014th Edition

3662448475, 978-3662448472

More Books

Students also viewed these Databases questions

Question

Project management skills and/or experience desirable

Answered: 1 week ago