Question
The Rectangle Class ackage lab08; public class Rectangle { //Declare instance variables private double length; private double width; //Class constructors //Create a new Rectangle with
The Rectangle Class
ackage lab08;
public class Rectangle {
//Declare instance variables
private double length;
private double width;
//Class constructors
//Create a new Rectangle with no width or length set
public Rectangle(){
length=0;
width=0;
}
//Create a new Rectangle with the width and length as the provided values
public Rectangle(double aWidth, double aLength){
width=aWidth;
length=aLength;
}
//Get or return the width of the Rectangle
double getWidth(){
return width;
}
//Get or return the width of the Rectangle
double getLength(){
return length;
}
-
Rectangle
- width: double
- length: double
Rectangle()
Rectangle(aWidth:double, aLength:double)
+ setLength(aLength: double): void
+ setWidth(aWidth: double): void
+ getLength(): double
+ getWidth(): double
+ calculateArea(): double
-
- How many constructors does the class Rectangle provide?
-
- How many methods does the class Rectangle provide?
-
- How many instance variables does the class Rectangle have?
-
- Briefly explain the purpose of the following two lines of code:
Rectangle aRectangle;
aRectangle = new Rectangle();
-
- The two lines of code can be combined into on Provide this one line of code:
-
- What method is used to set the width of an instance of the Rectangle class? What data type does it expect as an argument?
-
- What method is used to set the length of an instance of the Rectangle class? What data type does it expect as an argument?
-
- Consider the following code:
Rectangle aRectangle = new Rectangle();
aRectangle.setLength(5.5);
aRectangle.setWidth(10.2);
-
- Draw a rough diagram to represent the rectangle that has been created:
-
- Provide the line of code that you would use to change the width of the Rectangle from 10.2 to 11.5 (ie how would you set the width to be 11.5):
- Briefly explain the term class. In your explanation include details on the difference between an object and a class.
- How do you declare a class?
- Briefly explain the function of each part (5 of them) of the following statement:
Student aStudent = new Student();
- Create a new test class which will have main method in it and do as follows:
Create five objects named R1, R2, R3, R4 and R5 of Rectangle Class.
Dimensions of R1 should be default dimensions. Which constructor will you use?
Dimensions of R2- length 10 and width 6.5.
Dimensions of R3- length 2.5 and width 1.5.
Dimensions of R4- length 5.5 and width 3.5.
Dimensions of R4- length 8.5 and width 4.5.
Which constructor will you use for these rectangles?
Now set the length and width of R3 to 5.5 and 6.5 respectively using set methods.
Print the details of rectangle using get methods.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started