Question

Go to the Java API and review the Rectangle class briefly. As directed below, create a...

Go to the Java API and review the Rectangle class briefly. As directed below, create a subclass of the Rectangle class called BetterRectangle as described below. Create another class called RectangleTester, which fully tests the BetterRectangle, by displaying a menu that allows a user to process data for multiple rectangles until they choose to quit. Be sure to include sample runs as block comments in your tester source code file.

P9.10 The Rectangle class of the standard Java library does not supply a method to compute the area or perimeter of a rectangle. Provide a subclass BetterRectangle of the Rectangle class that has getPerimeter and getArea methods. Do not add any instance variables. In the constructor, call the setLocation and setSize methods of the Rectangle class. Provide a program that tests the methods that you supplied.

​P9.11 Repeat excersize P9.10, but in the BetterRectangle constructor, invoke the super class constuctor.

Use Dia to create a UML class diagram for BetterRectangle, being sure to show the parent class and relationship to Rectangle (do not need variables/methods for this class).

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

Please find my implementation.

import java.awt.Rectangle;

public class BetterRectangle extends Rectangle {

  

   // constructor

   public BetterRectangle(int x, int y, int width, int height){

      

       // calling setLocation

       setLocation(x, y);

      

       // calling setSize

       setSize(width, height);

   }

  

   // method to return perimeter

   public double getPerimeter(){

       return 2*(getHeight() + getWidth());

   }

  

   // function to return area

   public double getArea(){

       return getHeight()*getWidth();

   }

  

   // main method

   public static void main(String[] args) {

      

       // Creating two objects of type BetterRectangle

       BetterRectangle r1 = new BetterRectangle(3, 4, 6, 7);

      

       BetterRectangle r2 = new BetterRectangle(5, 6, 12, 15);

      

       System.out.println("R1: Area: "+r1.getArea()+", Perimeter: "+r1.getPerimeter());

       System.out.println("R2: Area: "+r2.getArea()+", Perimeter: "+r2.getPerimeter());

   }

}

/*

Sample Run:

R1: Area: 42.0, Perimeter: 26.0

R2: Area: 180.0, Perimeter: 54.0

*/

Add a comment
Know the answer?
Add Answer to:
Go to the Java API and review the Rectangle class briefly. As directed below, create a...
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
  • 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...

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

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

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

  • Hey, I have this homework from my data structures class using java that I am having...

    Hey, I have this homework from my data structures class using java that I am having difficulties coding. I had to create an abstract class Shape, which contains two protected instance variables color(String) and filled(boolean). Getter and setter for all the instance variables, and toString(). Two abstract methods getArea() and getPerimeter(). The part that I am not sure how to code is: for the subclasses of Shape which are Circle and Rectangle I have to override the abstract methods getArea()...

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

  • In Java programming language. For this assignment, you must write a class Rectangle and a tester...

    In Java programming language. For this assignment, you must write a class Rectangle and a tester RectangleTest. The Rectangle class should have only the following public methods (you can add other non-public methods): Write a constructor that creates a rectangle using the x, y coordinates of its lower left corner, its width and its height in that order. Creating a rectangle with non-positive width or height should not be allowed, although x and y are allowed to be negative. Write...

  • ****Here is the assignment **** Purpose: To write an Object-Oriented application that creates a Java class...

    ****Here is the assignment **** Purpose: To write an Object-Oriented application that creates a Java class with several instance variables, a constructor to initialize the instance variables, several methods to access and update the instance variables’ values, along with other methods to perform calculations. Also, write a test class that instantiates the first class and tests the class’s constructor and methods. Details: Create a class called Rectangle containing the following: Two instance variables, An instance variable of type double used...

  • Java Object Oriented Create a class that models a rectangle located in the first Cartesian quadrant....

    Java Object Oriented Create a class that models a rectangle located in the first Cartesian quadrant. The class should store information about a rectangle such that it can be drawn in an X,Y cartesian coordinate system, including rectangles which are not aligned parallel to the X-Y axes, that is, they are oriented making an angle with the X axis. Your class should provide one constructor methods for specifying the rectangle The constructor should receive the rectangle area, the length of...

  • please do in java and comments the code so i can understand Requirements: Create a Java...

    please do in java and comments the code so i can understand Requirements: Create a Java class named “MyRectangle2D.java”. Your class will have double two variables named x and y. These will represent the center point of your rectangle. Your class will have two double variables named width and height. These will represent the width and height of your rectangle. Create getter and setter methods for x, y, width, and height. Create a “no argument” constructor for your class that...

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