Question

(The Rectangle class) (WOULD APPRECIATE IT IF THE PROGRAM/ANSWER COULD BE DIRECTLY COPY AND PASTED, also...

(The Rectangle class) (WOULD APPRECIATE IT IF THE PROGRAM/ANSWER COULD BE DIRECTLY COPY AND PASTED, also this should be in java)

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() that returns the area of this rectangle.

- A method named getPerimeter() that returns the perimeter.


Draw the UML diagram for the class and then implement the class.

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.

Sample Run

The area of a rectangle with width 4.0 and height 40.0 is 160.0
The perimeter of a rectangle is 88.0
The area of a rectangle with width 3.5 and height 35.9 is 125.64999999999999
The perimeter of a rectangle is 78.8

Class Name: Exercise09_01

Homework Answers

Answer #1
import java.util.*;
//class Rectangle
class Rectangle
{
  //data fields
  private double width,height;
  //no-arg constructor
  public Rectangle()
  {
    this.width = 1;
    this.height = 1;
  }
  //argument constructor
  public Rectangle(double w,double h)
  {
    this.width = w;
    this.height = h;
  }
  //get width
  public double getWidth()
  {
    return width;
  }
  //get height
  public double getHeight()
  {
    return height;
  }
  //function to calculate area
  double getArea()
  {
    double area = width*height;
    return area;
  }
  //function to calculate perimeter
  double getPerimeter()
  {
    double perimeter = 2*(height+width);
    return perimeter;
  }
}
//driver code
class Main {
  public static void main(String[] args) {
    //created object of class Rectangle rec1
    Rectangle rec1 = new Rectangle(4,40);
    double area = rec1.getArea();
    double perimeter = rec1.getPerimeter();
    System.out.println("The area of a rectangle with width  "+rec1.getWidth()+" and height "+rec1.getHeight()+" is "+area);
    System.out.println("The perimeter of rectangle is "+perimeter);

    //created object of class Rectangle rec2
    Rectangle rec2 = new Rectangle(3.5,35.9);
    double area1 = rec2.getArea();
    double perimeter1 = rec2.getPerimeter();
    System.out.println("The area of a rectangle with width  "+rec2.getWidth()+" and height "+rec2.getHeight()+" is "+area1);
    System.out.println("The perimeter of rectangle is "+perimeter1);



  }
}

Output::

Note:: If any queries please comment.

Know the answer?
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
Similar Questions
Lab: RectClass (constructor) Code in C++ This program creates a Rectangle object, then displays the rectangle's...
Lab: RectClass (constructor) Code in C++ This program creates a Rectangle object, then displays the rectangle's length, width, and area Define an overloaded constructor and use it when creating the Rectangle object instead of using the setters. Change this program to calculate and display the rectangle's perimeter. Example: In feet, how wide is your house? 20 In feet, how long is your house? 25 The house is 20.00 feet wide. The house is 25.00 feet long. The house has 500.00...
Design a Java class named Polygon that contains:  A private int data field named numSides...
Design a Java class named Polygon that contains:  A private int data field named numSides that defines the number of sides of the polygon. The default value should be 4.  A private double data field named sideLength that defines the length of each side. The default value should be 5.0.  A private double data field named xCoord that defines the x-coordinate of the center of the polygon. The default value should be 0.0.  A private double...
c++ program. can you please do commenting. Question 1 [25]. (The Account Class) Design a class...
c++ program. can you please do commenting. Question 1 [25]. (The Account Class) Design a class named Account that contains (keep the data fields private): a) An int data field named id for the account. b) A double data field named balance for the account. c) A double data field named annualInterestRate that stores the current interest rate. d) A no-arg constructor that creates a default account with id 0, balance 0, and annualInterestRate 0. e) The accessor and mutator...
Whenever I try to run this program a window appears with Class not Found in Main...
Whenever I try to run this program a window appears with Class not Found in Main project. Thanks in Advance. * * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package Assignment10; /** * * @author goodf */ public class Assignment10{ public class SimpleGeometricObject { private String color = "white"; private boolean filled; private java.util.Date dateCreated;    /**...
The following is for a Java Program Create UML Class Diagram for these 4 java classes....
The following is for a Java Program Create UML Class Diagram for these 4 java classes. The diagram should include: 1) All instance variables, including type and access specifier (+, -); 2) All methods, including parameter list, return type and access specifier (+, -); 3) Include Generalization and Aggregation where appropriate. Java Classes description: 1. User Class 1.1 Subclass of Account class. 1.2 Instance variables __ 1.2.1 username – String __ 1.2.2 fullName – String __ 1.2.3 deptCode – int...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT