Question

Following the example of the Circle class in Section 8.2, design a class named Rectangle to...

Following the example of the Circle class in Section 8.2, 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.

Write a test program that creates two Rectangle objects —one with width 4 and height 40 and the other with width 3.5 and height 35.9. Display the width, height, area, and perimeter of each rectangle in this order.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

RectangleTest.java


public class RectangleTest {
   public static void main(String a[]){
       Rectangle r1 = new Rectangle(4,40);
       Rectangle r2 = new Rectangle(3.5,35.9);
       System.out.println("Rectangle 1 details:");
       System.out.println("Width: "+r1.getWidth());
       System.out.println("Height: "+r1.getHeight());
       System.out.println("Area: "+r1.getArea());
       System.out.println("Perimeter: "+r1.getPerimeter());
       System.out.println();
       System.out.println("Rectangle 2 details:");
       System.out.println("Width: "+r2.getWidth());
       System.out.println("Height: "+r2.getHeight());
       System.out.println("Area: "+r2.getArea());
       System.out.println("Perimeter: "+r2.getPerimeter());
   }
}

Rectangle.java


public class Rectangle {
   private double width ;
   private double height;
   public Rectangle(){
       width = 1;
       height = 1;
   }
   public Rectangle(double w, double h){
       width = w;
       height = h;
   }
   public double getArea(){
       return 2 * width * height;
   }
   public double getPerimeter(){
       return 2 * (width + height);
   }
   public double getWidth() {
       return width;
   }
   public void setWidth(double width) {
       this.width = width;
   }
   public double getHeight() {
       return height;
   }
   public void setHeight(double height) {
       this.height = height;
   }
  
}

Output:

Rectangle 1 details:
Width: 4.0
Height: 40.0
Area: 320.0
Perimeter: 88.0

Rectangle 2 details:
Width: 3.5
Height: 35.9
Area: 251.29999999999998
Perimeter: 78.8

Add a comment
Know the answer?
Add Answer to:
Following the example of the Circle class in Section 8.2, design a class named Rectangle to...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • (The Rectangle class) Following the example of the Circle class in Section 9.2, design a class...

    (The Rectangle class) Following the example of the Circle class in Section 9.2, 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()...

  • Please use Java to answer the question. (The Rectangle class) Following the example of the Circle...

    Please use Java to answer the question. (The Rectangle class) Following the example of the Circle class in Section 8.2, 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...

  • JAVA design a class named Rectangle to represent a rectangle. The class contains: A method named getPerimeter() that returns the perimeter. Two double data fields named width and height that specify...

    JAVA design a class named Rectangle to represent a rectangle. The class contains: A method named getPerimeter() that returns the perimeter. 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. design...

  • Within NetBeans, write a Java program for EACH of the following problems. (The Rectangle class) Following...

    Within NetBeans, write a Java program for EACH of the following problems. (The Rectangle class) Following the example of the Circle class in Section 9.2, 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...

  • NEEDS TO BE IN PYTHON: (The Rectangle class) Following the example of the Circle class in...

    NEEDS TO BE IN PYTHON: (The Rectangle class) Following the example of the Circle class in Section 9.2, design a class named Rectangle to represent a rectangle. The class contains: - Two data fields named width and height. - A constructor that creates a rectangle with the specified width and height. The default values are 1 and 2 for the width and height, respectively. - A method named getArea() that returns the area of this rectangle. - A method named...

  • Do exercise 9.1 in the book (page 362, or page 384 of the Comprehensive Version of...

    Do exercise 9.1 in the book (page 362, or page 384 of the Comprehensive Version of the book) Also add a method named getDiagonal which computes and returns the diagonal of the rectangle. For example the distance from the top left corner to the bottom right corner. Or equivalently the distance from the top right corner to the bottom left corner. Call this method and display it's output in your test program. for the two Rectangle objects. Here is exercise...

  • using c++ E2b:Design a class named Rectangle to represent a rectangle . The class contains: (1)...

    using c++ E2b:Design a class named Rectangle to represent a rectangle . The class contains: (1) Two double data members named width and height which specifies the width and height of the rectangle . (2) A no-arg constructor that creates a rectangle with width 1 and height 1. (3) A constructor that creates a rectangle with the specified width and height . (4) A function named getArea() that returns the area of this rectangle . (5) A function named getPerimeter()...

  • Design a general class GeometricObject can be used to model all geometric objects. This class contains...

    Design a general class GeometricObject can be used to model all geometric objects. This class contains the properties color and filled and their appropriate get and set methods. Assume that this class also contains toString() methods. The toString() method returns a string representation of the object. Define the Triangle, Circle, and Rectangle classes that extend the GeometricObject class. The Triangle class inherits all accessible data fields and methods from the GeometricObject class.In addition, it has three double data fields named...

  • Introduction to Java:Design a class named Triangle that extends GeometricObject.

    Please include comments in the program .The program must be able to be compiled.Design a class named Triangle that extends GeometricObject. This class contains:* Three double data fields named side1, side2, and side3 with default values 1.0 to denote the three sides of a triangle.* A no-arg constructor that creates a default triangle.* A constructor that creates a triangle with the specified side1, side2, and side3.* The accessor methods for all three data fields.* A method named getArea() that returns...

  • Code a rectangle class extending the geometric obi.

    rectangle class contains tge following; -three data fields name recname,  height and width with default values"Ricky" for recName, 5 for the height and 15 for the width.-default constructor with no arguments -constructor that accept width  and height to create  a rectangle  -the accessor of all data feilds-methods that returns the area of the rectangle called getArea()-methods that returns the perimeter of thr rectangle called getPerimeter()-methods that returns the spring description of the rectangle called toString()    Note: the toString() method is...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT