Question #46863

Write a Java statement to initialize a variable square with a rectangle object whose top left corner is (10, 20) and whose sides all have length 40. Then write a statement that replaces square with a rectangle of the same size and top left corner (20, 20).

Expert's answer

Answer on Question #46863, Programming, Java

Problem.

Use Java code to convert the command line arguments to an array of integers.

Solution.

Code (Rectangle.java)


public class Rectangle {
    private float topLeftX;
    private float topLeftY;
    private float bottomRightX;
    private float bottomRightY;
    /**
     * Create square by top left corner and side.
     * @param topLeftX x coordinate of top left corner.
     * @param topLeftY y coordinate of top left corner.
     * @param side length of side.
     */
    Rectangle(float topLeftX, float topLeftY, float side) {
        this.topLeftX = topLeftX;
        this.topLeftY = topLeftY;
        this.bottomRightX = topLeftX + side;
        this.bottomRightY = topLeftY + side;
    }
    /**
     * Move top left corner to another point.
     * @param topLeftX x coordinate of top left corner.
     * @param topLeftY y coordinate of top left corner.
     */
    void moveTo(float topLeftX, float topLeftY) {
        bottomRightX = topLeftX + (bottomRightX - this.topLeftX);
        bottomRightY = topLeftY + (bottomRightY - this.topLeftY);
        this.topLeftX = topLeftX;
        this.topLeftY = topLeftY;
    }
}


Code (Main.java)


public class Main {
    public static void main(String[] args) {
        Rectangle square = new Rectangle(10, 20, 40);
        square.moveTo(20, 20);
    }
}


http://www.AssignmentExpert.com/

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

LATEST TUTORIALS
APPROVED BY CLIENTS