THIS IS A JAVA PROGRAM Some objects can be doubled, for example a bank account.
ID: 3574829 • Letter: T
Question
THIS IS A JAVA PROGRAM
Some objects can be doubled, for example a bank account. The result would be a bank account with twice the balance. Some objects can't be doubled. For example, there is no double of a dime coin—no twenty cent coin exists. You must provide an interface called Doubler that contains the method declaration for makeDouble. Your must also provide a class RectangleDoubler that implements Doubler . makeDouble should double the width and height, but the same x and y positions of the top left corner. makeDouble should have one parameter that is a rectangle object; the object to be doubled. It should also return the reference to a rectangle object. Do not change the original rectangle, but create a new rectangle to hold the doubled dimensions. Use the following RectangleDoublerTester: (Be sure to add comments)
Note: The output will state java.awt.Rectangle. This is ok. It is the format of the toString method in the Rectangle class.
Explanation / Answer
Hi,
Please see below all the Java Classes and Sample output. Please comment for any queries/feeddback.
Thanks,
Anita Joseph
Doubler.java
import java.awt.Rectangle;
/**
* Interface Doubler
*/
public interface Doubler {
public Rectangle makeDouble(Rectangle rectangle);
}
RectangleDoubler.java
import java.awt.Rectangle;
/**
* Class RectangleDoubler which implements Doubler
*/
public class RectangleDoubler implements Doubler {
/**
* overriding makeDouble of Doubler Interface
* This method will double the width and height and keep x and y as it it and returns a Reactangle object
* input: Rectangle rectangle
* output:Rectangle
*/
public Rectangle makeDouble(Rectangle rectangle) {
//Creating a new Rectangle
Rectangle newRect= new Rectangle();
//Setting x and y as same as that of input rectangle
newRect.x = rectangle.x;
newRect.y =rectangle.y;
//Setting width and height as double as that of input rectangle
newRect.width = 2* rectangle.width;
newRect.height = 2* rectangle.height;
//Returning the new Rectangle object
return newRect;
}
}
RectangleDoublerTester.java
import java.awt.Rectangle;
public class RectangleDoublerTester {
public static void main(String[] args)
{
Rectangle box = new Rectangle(5, 10, 20, 30);
RectangleDoubler doubler = new RectangleDoubler();
Rectangle box2 = doubler.makeDouble(box);
System.out.println(box);
System.out.println("Box Expected: x=5, y=10, width=20, height=30");
System.out.println(box2);
System.out.println("Box2 Expected: x=5, y=10, width=40, height=60");
}
}
Sample output:
java.awt.Rectangle[x=5,y=10,width=20,height=30]
Box Expected: x=5, y=10, width=20, height=30
java.awt.Rectangle[x=5,y=10,width=40,height=60]
Box2 Expected: x=5, y=10, width=40, height=60
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.