Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

How do I modify this rectangle correctly?

+5 votes
asked Jun 2 by Cameron Carpenter (170 points)
public class Rectangle {
    private double height, width;
    public Rectangle(int x, int y) {
        height=x;
        width=y;
        corner1=a;
        corner2=b;
    }
    public void printDims() {
        System.out.println("This rectangle has height " + height + " and width " + width);
    }
    
    public void printArea() {
            System.out.println("This rectangle has area of " + height*width + " units squared.");
    }

Here is what I have so far. This is what I have to do I am unsure how to begin please help.

ii. Modify the Rectangle class to permit construction of rectangles using a single input argument (in which case both height and width are to the input value).
iii. Modify the Rectangle class to have the following methods:
– PrintDiag, which (in format similar to PrintDim and PrintArea) prints the length of a diagonal from two non-adjacent corners of the rectangle (computed as , where a and b are the two distinct lengths of the rectangle)    
– PrintPerimeter, which (in format similar to PrintDim and PrintArea) prints the perimeter of the rectangle.

1 Answer

+1 vote
answered Jun 2 by Peter Minarik (54,560 points)

Existing Code

First of all, the existing code you shared has a few problems.

Missing Closing Brace for Rectangle

The scope of the Rectangle class is not closed (you're missing a closing curly brace "}")  after the last function.

Undefined Variables

The corner1 and corner2 variables in the constructor are undefined. Remove them and now your Rectangle class (placed in Rectangle.java) should compile.

New Code

Constructor For a Square

The instruction

Modify the Rectangle class to permit construction of rectangles using a single input argument (in which case both height and width are to the input value).

describes a constructor for a square (where both width and height are the same).

Look at the existing constructor (public Rectangle(int x, int y)), make a copy of it and remove one of the parameters. Then in the body of your freshly created constructor set both the width and height to be the only parameter of the new constructor.

Printing The Perimeter

I suggest doing this first as it is easier than the diagonal.

Make a copy of one of the existing functions other than the constructor, e.g.: public void printDims(). Change the name to the desired printPerimeter. Then in the body of the function, change the text to be printed to say it is the perimeter (instead of the width and height).

Calculate the perimeter (2 * (width + height)) of the rectangle and print that instead of printing the height and width.

Printing The Length of The Diagonal

Make a copy of one of the existing functions other than the constructor, e.g.: public void printDims(). Change the name to the desired printDiag. Then in the body of the function, change the text to be printed to say it is the diameter (instead of the width and height).

You will need to create a new variable to store the diameter. I suggest using a type that supports fractions, so use a floating-point number (double).

You will need to use the Pythagoras Theorem to calculate the diagonal. If you need help with this, check this out. For calculating the square of a number, just multiply it by itself, i.e., x2 can be written like this: x * x. To calculate the square root of x, you can use the Math.sqrt(x) expression.

It's Your Turn

I hope these instructions can get you started.

Get your hands dirty, and write some code! Give it your best shot. If you get stuck, post what you have so far and people will be happy to help you move forward.

Good luck!

Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...