Design a class named Rectangle that represents a rectangle. The class should con
ID: 3632882 • Letter: D
Question
Design a class named Rectangle that represents a rectangle. The class should contain the following:
• Two private double data fields named “width” and “height” where the default width and height values are 1
• 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
• Two methods to get the width and height of the rectangle, named getWidth() and getHeight()
Using the newly created rectangle class, write a separate program that does the following:
• Ask user if they would like to create a default rectangle or if they want to enter width and height
• If they want to enter width and height, prompt them for these values
• Print the created rectangle's width, height, area, and perimeter
Explanation / Answer
**Please note, i have not compiled this and do not know for sure it will compile, this is written in correct java but i haven't been super careful about spelling/syntax/etc. It may compile, but even if it doesn't this should be an excellent guide for how basic classes work**
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());
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.