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
Write a Java class called Rectangle that represents a rectangular two-dimensional region. It should have following...
Write a Java class called Rectangle that represents a rectangular two-dimensional region. It should have following 4 fields: int x (x-coordinate for its top left corner) int y (y coordinate for its top left corner) double height (height of the rectangle) double width (width of the rectangle) Your rectangle objects should have the following methods: public Rectangle(int newx, int newy, int newwidth, int newheight) Constructor that initializes the x-coordinate, y-coordinate for the top left corner and its width and height....
Attached is the file GeometricObject.java. Include this in your project, but do not change. Create a...
Attached is the file GeometricObject.java. Include this in your project, but do not change. Create a class named Triangle that extends GeometricObject. The class contains: Three double fields named side1, side2, and side3 with default values = 1.0. These denote the three sides of the triangle A no-arg constructor that creates a default triangle A constructor that creates a triangle with parameters specified for side1, side2, and side3. An accessor method for each side. (No mutator methods for the sides)...
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...
Problem Description: Problem Description: (The Account class) Design a class named Account that contains: A private...
Problem Description: Problem Description: (The Account class) Design a class named Account that contains: A private int data field named id for the account (default 0). A private double data field named balance for the account (default 0). A private double data field named annualInterestRate that stores the current interest rate (default 0). Assume all accounts have the same interest rate. A private Date data field named dateCreated that stores the date when the account was created. A no-arg constructor...
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...
1. Circle: Implement a Java class with the name Circle. It should be in the package...
1. Circle: Implement a Java class with the name Circle. It should be in the package edu.gcccd.csis. The class has two private instance variables: radius (of the type double) and color (of the type String). The class also has a private static variable: numOfCircles (of the type long) which at all times will keep track of the number of Circle objects that were instantiated. Construction: A constructor that constructs a circle with the given color and sets the radius to...
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...
Hi, I'm writing a Java program that prints a grid with circles and squares that have...
Hi, I'm writing a Java program that prints a grid with circles and squares that have letters in them and it is also supposed to print the toString() function to the console window each time the application runs. This toString() function is supposed to show the tile's shape, letter, and color component (or components). Could someone please review and debug my code to help me figure out why my toString() function is not working? import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JButton;...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT