2. Define a class of rectangles. 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.0 for both width and height. ? A no-arg constructor that creates a default rectangle. ? A constructor that creates a rectangle with a specified width and height. ? A method named getArea() that returns the area of a rectangle. ? A method named getPerimeter() that returns the perimeter. The main method in your program should do the following things: • Create the first rectangle using the no-arg constructor. • Create a second rectangle using the constructor's arguments to set the width to 4.0 and the height to 40.0. • Print out the area of the first rectangle. • Print out the perimeter of the second rectangle.
Added by Christopher G.
Close
Step 1
First, make sure you have BlueJ installed on your computer. If not, you can download it from the official BlueJ website. Show more…
Show all steps
Your feedback will help us improve your experience
K S and 81 other AP CS educators are ready to help you.
Ask a new question
Labs
Want to see this concept in action?
Explore this concept interactively to see how it behaves as you change inputs.
Key Concepts
Recommended Videos
Using Java, create a class named RunQuad. This class shall contain the main method. Add three (3) classes named Quadrilateral, Rectangle, and Square. Rectangle shall inherit from Quadrilateral while Square shall inherit from Rectangle. Declare a public method named showDescription() in all three (3) classes except in RunSquad. Customize each method by creating different println() statements. a. For Quadrilateral: "- is a quadrilateral". b. For Rectangle: "- has 4 right angles". Add a super() statement to call Quadrilateral's method. c. For Square: "- has 4 equal sides". Add a super() statement to call Rectangle's method. Code the main method of the RunQuad class. The output shall ask the user to press R or S to choose between rectangle and square. Display appropriate description(s). Add three (3) more classes named Parallelogram, Rhombus, and Trapezoid. a. Parallelogram shall inherit from Quadrilateral and shall be the new parent class of Rectangle. Its println() statement shall be "- has 2 pairs of parallel sides". b. Rhombus shall inherit from Parallelogram. Its println() statement shall be "- has 4 congruent sides". c. Trapezoid shall inherit from Quadrilateral. Its println() statement shall be "- has 1 pair of parallel sides". Modify the main method of the RunQuad class. The choices will now be R, S, P, H, and T. Display appropriate description(s).
Akash M.
Title: Geometric Objects Program Design a new Triangle class that extends the abstract GeometricObject class. Write a test program that prompts the user to enter three sides of the triangle (make sure they define an actual triangle), a color, and a Boolean value to indicate whether the triangle is filled. The program should then create a Triangle object with these sides and set the color and filled properties using the input. In addition, have the program make use of constructors to add circle and rectangle objects. The program should display, for each of these geometric objects, the area, perimeter, color, and true or false to indicate whether it is filled or not. Place all of the classes in a single Java source file named TestGeometricObjects.java. Make sure that the class that contains the main method is also named public class TestGeometricObjects, and that the Triangle, Circle, and Rectangle classes are not public. The output for this program might be formatted to look something like the following: Please enter the sides of a triangle: 2 4 78 Unable to create a triangle with those sides. Please enter the sides of a triangle: 3 4 5 What is the color of the triangle: red Is the triangle filled (true/false)?: true Triangle: Area: 6.0 Perimeter: 12.0 Color: red Filled: true Circle: Area: 12.56 Perimeter: 12.56 Color: yellow Filled: true Rectangle: Area: 8.0 Perimeter: 12.0 Color: red Filled: false
Chapter 8: Programming Project 1 For this exercise, you will write a class named Area. The Area class should provide static methods that calculate the areas of different geometric shapes. The class should have three overloaded static methods named getArea. Here is a description of each method: 1. The first version of the static getArea method will calculate the area of a circle. It should accept the circle's radius as a double and return the circle's area as a double. See the formula below for calculating the area of a circle. 2. The second version of the static getArea method will calculate the area of a rectangle. It should accept the rectangle's length and width as doubles and return the rectangle's area as a double. See the formula below for calculating the area of a rectangle. 3. The third version of the static getArea method will calculate the area of a trapezoid. It should accept the trapezoid's base #1 length, base #2 length, and height as doubles and return the trapezoid's area as a double. See the formula below for calculating the area of a trapezoid. The Area class should also have a main method that calls each of the overloaded getArea methods. It should display the following values, each on a separate line: 1. The area of a circle with a radius of 3. The value should be rounded to two decimal places. 2. The area of a rectangle with a length of 2 and a width of 4. The value should be rounded to one decimal place. 3. The area of a trapezoid with base lengths of 3 and 4, and a height of 5. The value should be rounded to one decimal place. Here are the formulas for calculating the areas of the shapes: - Area of a circle: Area = πr², where π is Math.PI and r is the circle's radius. - Area of a rectangle: Area = Width × Length - Area of a trapezoid: Area = (base1 + base2) × height / 2
Supreeta N.
Recommended Textbooks
Computer Science and Information Technology
Introduction to Programming Using Python
Computer Science - An Overview
Watch the video solution with this free unlock.
EMAIL
PASSWORD