Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Design and implement a Java program (name it Rectangle.java). - Design a class n

ID: 3568093 • Letter: D

Question

Design and implement a Java program (name it Rectangle.java). - Design a class named Rectangle to represent a rectangle. The class contains:
? Two double data fields named width and height that specify the width and
height of the rectangle. The default values are 1 for both width and height.
? A no-arg constructor that creates a default rectangle.
? A constructor that creates a rectangle with the specified width and height.
? A method named getArea() that returns the area of this rectangle.
? A method named getPerimeter() that returns the perimeter.
Draw the UML diagram for the class and then implement the class. Write a test
program that creates two Rectangle objects

Explanation / Answer

public class Rectangle{

//Our rectangle is defined by 2 values:
//We make the variables private because that is generally best practice within classes
private double height; //height variable
private double width; //variable to store the width


//First the no argument constructor
public Rectangle(){
height = 1; //Set our variables to their default values
width = 1;
}

//Now the constructor that takes arguments:
public Rectangle(double startHeight, double startWidth){
width = startWidth;
height = startHeight;
}

//Now we write our fetching functions:
public double getWidth(){
return width; //simply return the width
}
public double getHeight(){
return height; //simply return the height
}
//Now we write the functions that return area and perimiter
public double getPerimeter(){
return (2*height+2*width); //The Perimeter of a rectangle is twice the width
//plus twice the height
}
public double getArea{
return (width*height); //The area of a rectangle is height times width
}
//Thus ends the class, now for the main function

}

public static void main(String[] args) {
//Note: I do not know what input method you use, i am assuming if you are
//studing classes you are comfortable with some type of java input...

char custom; //a control variable for determining default

System.out.println("Would you like to customize your rectangle?");
custom = input.Method();

if(custom == "y"){
double width=1, height=1; //create and initialize input variables
System.out.print("Please enter a height: "); //Accept the inputs that we need
height = input.Method();
System.out.print("Please enter a width: ");
width = input.Method();
myRectangle = new Rectangle(height, width); //Create a rectangle object
// using our constructor
}
else{
myRectangle = new Rectangle(); //If no values are set use default constr.
}

System.out.println("The height is: " + myRectangle.getHeight());
System.out.println("The width is: " + myRectangle.getWidth());
System.out.println("The perimeter is: " + myRectangle.getPerimeter());
System.out.println("The area is : " + myRectangle.getArea());
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote