Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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;

}

  1. 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

    The class Rectangle has been provided for testing above. Use the code provided and the following class diagram to answer the questions below:
    1. How many constructors does the class Rectangle provide?
    1. How many methods does the class Rectangle provide?
    1. How many instance variables does the class Rectangle have?
    1. Briefly explain the purpose of the following two lines of code:

Rectangle aRectangle;

aRectangle = new Rectangle();

    1. The two lines of code can be combined into on Provide this one line of code:
    1. What method is used to set the width of an instance of the Rectangle class? What data type does it expect as an argument?
    1. What method is used to set the length of an instance of the Rectangle class? What data type does it expect as an argument?
    1. Consider the following code:

Rectangle aRectangle = new Rectangle();

aRectangle.setLength(5.5);

aRectangle.setWidth(10.2);

    1. Draw a rough diagram to represent the rectangle that has been created:
    1. 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):

  1. Briefly explain the term class. In your explanation include details on the difference between an object and a class.

  1. How do you declare a class?
  2. Briefly explain the function of each part (5 of them) of the following statement:

Student aStudent = new Student();

  1. 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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Processing Fundamentals Design And Implementation

Authors: David M. Kroenke

5th Edition

B000CSIH5A, 978-0023668814

More Books

Students also viewed these Databases questions

Question

13-6 How will MIS help my career?

Answered: 1 week ago