Write a Rectangle class. A Rectangle has properties of width and length. You construct a Rectangle object by providing the width and length in that order. If no width and length are provided to the constructor, construct a Rectangle with width 0.0 and length 0.0. We want to be able to get and set both the width and the length independently. We also want to be able to ask for the area of the rectangle and the perimeter of the rectangle. What will the object need to remember? width and length - those are the instance variables. Rectangle class has these constructors : public Rectangle() - Constructs a new rectangle with width and length of 0.0. public Rectangle(double width, double length) - Constructs a new rectangle with the given width and length. Remember that the job of the constructor is to initialize the instance variables. It has these methods. public double getWidth() - Gets the width of this Rectangle public double getLength() - Gets the length of this Rectangle public void setWidth(double aWidth) - Sets a new width for this Rectangle public void setLength(double aLength) - Sets a new length for this Rectangle   public double area() - Calculates and returns this Rectangle's area (product of width and length)   public double perimeter() - Calculates and returns this Rectangle's perimeter (sum of all four sides) public void printDimensions() - Displays the width and length of this Rectangle in the following format (to two decimal places): Width: 3.48 Height: 7.27   The methods and constructor are provided as stubs in the starter file. A stub has a method header and a body with no implementation.  The stub for an accessor returns 0 for numbers or null for objects like strings.  A stub for a mutator method has no body at all.  A class with stubs for methods will compile, but it does not yet behave correctly. You still need to supply implementation and the correct return values. The idea is that you can implement one method at a time and test it since the class will compile. This technique is frequently used in development of applications. You are given a RectangleTester class in Codecheck along with the starter for the Rectangle class. Copy both into your BlueJ project. RectangleTester is an application with a main method. To run your application, right click on it and execute its main method.

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

In java language

Write a Rectangle class. A Rectangle has properties of width and length. You construct a Rectangle object by providing the width and length in that order. If no width and length are provided to the constructor, construct a Rectangle with width 0.0 and length 0.0. We want to be able to get and set both the width and the length independently. We also want to be able to ask for the area of the rectangle and the perimeter of the rectangle.

What will the object need to remember? width and length - those are the instance variables.

Rectangle class has these constructors :

  • public Rectangle() - Constructs a new rectangle with width and length of 0.0.
  • public Rectangle(double width, double length) - Constructs a new rectangle with the given width and length. Remember that the job of the constructor is to initialize the instance variables.

It has these methods.

    • public double getWidth() - Gets the width of this Rectangle
    • public double getLength() - Gets the length of this Rectangle
    • public void setWidth(double aWidth) - Sets a new width for this Rectangle
    • public void setLength(double aLength) - Sets a new length for this Rectangle

 

  • public double area() - Calculates and returns this Rectangle's area (product of width and length)

 

  • public double perimeter() - Calculates and returns this Rectangle's perimeter (sum of all four sides)
  • public void printDimensions() - Displays the width and length of this Rectangle in the following format (to two decimal places):
    • Width: 3.48
    • Height: 7.27

 

The methods and constructor are provided as stubs in the starter file. A stub has a method header and a body with no implementation. 

  • The stub for an accessor returns 0 for numbers or null for objects like strings. 
  • A stub for a mutator method has no body at all. 

A class with stubs for methods will compile, but it does not yet behave correctly. You still need to supply implementation and the correct return values. The idea is that you can implement one method at a time and test it since the class will compile. This technique is frequently used in development of applications.

You are given a RectangleTester class in Codecheck along with the starter for the Rectangle class. Copy both into your BlueJ project. RectangleTester is an application with a main method. To run your application, right click on it and execute its main method.

RectangleTester.java

public class RectangleTester { public static void main(String[] args) { Rectangle r1 = new Rectangle(); r1.setWidth(3.0); r1.setLength(5.2); System.out.printf("r1 Width: %.2f%n", r1.getWidth()); System.out.println("Expected: 3.00"); System.out.printf("r1 Length: %.2f%n", r1.getLength()); System.out.println("Expected: 5.20"); Rectangle r2 = new Rectangle(2.6, 5.4); System.out.printf("r2 Width: %.2f%n", r2.getWidth()); System.out.println("Expected: 2.60"); System.out.printf("r2 Length: %.2f%n", r2.getLength()); System.out.println("Expected: 5.40"); System.out.printf("r1 Area: %.2f%n", r1.area()); System.out.println("Expected: 15.60"); System.out.printf("r1 Perimeter: %.2f%n", r1.perimeter()); System.out.println("Expected: 16.40"); System.out.printf("r2 Area: %.2f%n", r2.area()); System.out.println("Expected: 14.04"); System.out.printf("r2 Perimeter: %.2f%n", r2.perimeter()); System.out.println("Expected: 16.00"); System.out.println(); r1.printDimensions(); } }

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps with 1 images

Blurred answer
Knowledge Booster
Unreferenced Objects
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education