Question

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

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

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

please help me fixing the mistake ?

public class Exercise09_01
{
     public static void main (String[] args)
      {
             Rectangle rectangle1 = new Rectangle(4,40);

             Rectangle rectangle2 = new Rectangle(3.5,3.59);
           
             System.out.println("The details of the first rectangel...");
             System.out.println("Width of Rectangle1:" + rectangle1.width);
             System.out.println("Height of Rectangle1:" + rectangle1.height);
             System.out.println("Area of Rectangle1:" + df.format(rectangle1.getArea()));
             System.out.println("Perimeter of Rectangle1:"+ rectangle1.getPerimeter());
             System.out.println();

             System.out.println("The details of the second rectangel...");
             System.out.println("Width of Rectangle2:" + rectangle2.width);
             System.out.println("Height of Rectangle2:" + rectangle2.height);
             System.out.println("Area of Rectangle2:" + df.format(rectangle2.getArea()));
             System.out.println("Perimeter of Rectangle2:"+ rectangle2.getPerimeter());
       }
}
           
public class Rectangle
{
        double width = 1.0;
        double height = 1.0;
   Rectangel() {
       }
   Rectangel(double newWidth, double newHeight)
      {
          width = newWidth;
          height = newHeight;
       }
   double getArea()
     {
   return width * height;
     }
   double getPerimeter()
     {
   return 2*(width + height);
}
}

    

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

UML Rectangle class digaram

Rectangle width: double height: double +Rectangle () +Rectangle (newWidth:double, newHeight: double) +getArea) double +getPer

-------------------------------*-------------------------------*-------------------------------*
/**Test java program for Rectangle class*/
//Exercise09_01.java
import java.text.DecimalFormat;
public class Exercise09_01
{
   public static void main (String[] args)
   {
       Rectangle rectangle1 = new Rectangle(4,40);
       Rectangle rectangle2 = new Rectangle(3.5,3.59);

       /**Create an instace of DecimalFormat class*/
       DecimalFormat df=new DecimalFormat("#,##.##");
      
       System.out.println("The details of the first rectangel...");
       System.out.println("Width of Rectangle1:" + rectangle1.width);
       System.out.println("Height of Rectangle1:" + rectangle1.height);
       System.out.println("Area of Rectangle1:" + df.format(rectangle1.getArea()));
       System.out.println("Perimeter of Rectangle1:"+ rectangle1.getPerimeter());
       System.out.println();

       System.out.println("The details of the second rectangel...");
       System.out.println("Width of Rectangle2:" + rectangle2.width);
       System.out.println("Height of Rectangle2:" + rectangle2.height);
       System.out.println("Area of Rectangle2:" + df.format(rectangle2.getArea()));
       System.out.println("Perimeter of Rectangle2:"+ rectangle2.getPerimeter());
   }
}

-------------------------------*-------------------------------*-------------------------------*
//Rectangle.java
public class Rectangle
{
   /**Instance variables */
   double width ;
   double height;
   /*Name of the constructor must match with
   * the name of the class, Rectangle*/
   public Rectangle()
   {
       width=1;
       height=1;
   }
   /*Name of the constructor must match with
   * the name of the class, Rectangle*/
   public Rectangle(double newWidth, double newHeight)
   {
       width = newWidth;
       height = newHeight;
   }
   /**Returns area */
   public double getArea()
   {
       return width * height;
   }
   /**Returns perimeter */
   public double getPerimeter()
   {
       return 2*(width + height);
   }
}/**End of Rectangle class*/

-------------------------------*-------------------------------*-------------------------------*

Sample Output:

The details of the first rectangel...
Width of Rectangle1:4.0
Height of Rectangle1:40.0
Area of Rectangle1:1,60
Perimeter of Rectangle1:88.0

The details of the second rectangel...
Width of Rectangle2:3.5
Height of Rectangle2:3.59
Area of Rectangle2:12.56
Perimeter of Rectangle2:14.18

Add a comment
Know the answer?
Add Answer to:
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...
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
  • 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...

  • 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()...

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

  • Creating a Class in C++ Summary In this lab, you create a programmer-defined class and then...

    Creating a Class in C++ Summary In this lab, you create a programmer-defined class and then use it in a C++ program. The program should create two Rectangle objects and find their area and perimeter. Instructions Ensure the class file named Rectangle.cpp is open in your editor. In the Rectangle class, create two private attributes named length and width. Bothlength and width should be data type double. Write public set methods to set the values for lengthand width. Write public...

  • java language Problem 3: Shape (10 points) (Software Design) Create an abstract class that represents a Shape and contains abstract method: area and perimeter. Create concrete classes: Circle, Rectang...

    java language Problem 3: Shape (10 points) (Software Design) Create an abstract class that represents a Shape and contains abstract method: area and perimeter. Create concrete classes: Circle, Rectangle, Triangle which extend the Shape class and implements the abstract methods. A circle has a radius, a rectangle has width and height, and a triangle has three sides. Software Architecture: The Shape class is the abstract super class and must be instantiated by one of its concrete subclasses: Circle, Rectangle, or...

  • Design a class named Triangle that extends GeometricObject class. The class contains: Three double data fields...

    Design a class named Triangle that extends GeometricObject class. The class contains: Three double data fields named side1, side2, and side3 with default values 1.0 to denote three sides of the triangle. A no-arg constructor that creates a default triangle with color = "blue", filled = true. A constructor that creates a triangle with the specified side1, side2, side3 and color = "blue", filled = true. The accessor functions for all three data fields, named getSide1(), getSide2(), getSide3(). A function...

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

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

  • Programming Language: Java Write a class named ColoredRectangle that extends the Rectangle class (given below). The...

    Programming Language: Java Write a class named ColoredRectangle that extends the Rectangle class (given below). The ColoredRectangle class will have an additional private String field named Color. The ColoredRectangle class should have four constructors: a no-arg constructor; a three-arg constructor; a two-arg constructor that accepts a Rectangle object and a color; and a copy constructor. The ColoredRectangle class should have an Equals and toString methods. The ColoredRectangle class should have mutators and accessors for all three fields. public class Rectangle...

  • Creating a Programmer-Defined Class in Java Summary In this lab, you will create a programmer-defined class...

    Creating a Programmer-Defined Class in Java Summary In this lab, you will create a programmer-defined class and then use it in a Java program. The program should create two Rectangle objects and find their area and perimeter. Instructions Make sure the class file named Rectangle.java is open. In the Rectangle class, create two private attributes named lengthand width. Both length and width should be data type double. Write public set methods to set the values for length and width. Write...

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
ADVERTISEMENT