(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 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 then implement the class. Write a test
Rectangle
Program plan:
- Import required packages.
- Declare the main class named “Main”.
- Give the main method “public static void main ()”.
- Create an object “myRectangle” and pass the arguments.
- Print the area of the first rectangle along with width and height by calling the function “getArea()”
- Print the perimeter of the first rectangle by calling the function “getPerimeter()”
- Create another object “yourRectangle” and also pass the arguments.
- Print the area of the second rectangle along with width and height by calling the function “getArea()”
- Print the perimeter of the second rectangle by calling the function “getPerimeter()”
- Give the main method “public static void main ()”.
- Define class named “MyRectangle”
- Declare the data members and default constructor.
- Declare a parameterized constructor.
- Define a method “getArea()” to calculate the area of the rectangle.
- Return the result.
- Define a method “getPerimeter()” to calculate the perimeter of the rectangle.
- Return the result.
The below program is used to display the area and perimeter of the two rectangles along with the width and height of the rectangles.
Explanation of Solution
Program:
// Class definition
public class One {
// Define main function
public static void main(String[] args) {
/*Create object for the class and pass the arguments */
MyRectangle myRectangle = new MyRectangle(4, 40);
// Print the area of the first rectangle
System.out.println("The area of a rectangle with width " + myRectangle.width + " and height "
+ myRectangle.height + " is " + myRectangle.getArea());
// Print the perimeter of the first rectangle
System.out.println("The perimeter of a rectangle is " + myRectangle.getPerimeter());
/* Create object for the class and pass the arguments */
MyRectangle yourRectangle = new MyRectangle(3.5, 35.9);
// Print the area of the second rectangle
System.out.println("The area of a rectangle with width " + yourRectangle.width + " and height "
+ yourRectangle.height + " is " + yourRectangle.getArea());
// Print the perimeter of the second rectangle
System.out.println("The perimeter of a rectangle is " + yourRectangle.getPerimeter());
}
}
// Class definition
class MyRectangle {
// Data members
double width = 1;
double height = 1;
// Default Constructor
public MyRectangle() {
}
// Parameterized Constructor
public MyRectangle(double newWidth, double newHeight) {
width = newWidth;
height = newHeight;
}
// Method to get the area of rectangle
public double getArea() {
/* Return the area value by multiplying width and height */
return width * height;
}
// Method to get the perimeter of rectangle
public double getPerimeter() {
/*Return the perimeter value by multiplying 2 with width and height */
return 2 * (width + height);
}
}
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
Want to see more full solutions like this?
Chapter 9 Solutions
Introduction to Java Programming and Data Structures, Comprehensive Version (11th Edition)
Additional Engineering Textbook Solutions
Web Development and Design Foundations with HTML5 (8th Edition)
Database Concepts (8th Edition)
Java How to Program, Early Objects (11th Edition) (Deitel: How to Program)
Starting Out with Programming Logic and Design (5th Edition) (What's New in Computer Science)
Mechanics of Materials (10th Edition)
Introduction To Programming Using Visual Basic (11th Edition)
- Scenario You work for a small company that exports artisan chocolate. Although you measure your products in kilograms, you often get orders in both pounds and ounces. You have decided that rather than have to look up conversions all the time, you could use Python code to take inputs to make conversions between the different units of measurement. You will write three blocks of code. The first will convert kilograms to pounds and ounces. The second will convert pounds to kilograms and ounces. The third will convert ounces to kilograms and pounds. The conversions are as follows: 1 kilogram = 35.274 ounces 1 kilogram = 2.20462 pounds 1 pound = 0.453592 kilograms 1 pound = 16 ounces 1 ounce = 0.0283 kilograms 1 ounce = 0.0625 pounds For the purposes of this activity the template for a function has been provided. You have not yet covered functions in the course, but they are a way of reusing code. Like a Python script, a function can have zero or more parameters. In the code window you…arrow_forwardmake a screen capture showing the StegExpose resultsarrow_forwardWhich of the following is not one of the recommended criteria for strategic objectives? Multiple Choice a) realistic b) appropriate c) sustainable d) measurablearrow_forward
- Management innovations such as total quality, benchmarking, and business process reengineering always lead to sustainable competitive advantage because everyone else is doing them. a) True b) Falsearrow_forwardVision statements are more specific than strategic objectives. a) True b) Falsearrow_forwardThe three components of the __________ approach to corporate accounting include financial, environmental, and social performance measures. Multiple Choice a) stakeholder b) triple dimension c) triple bottom line d) triple efficiencyarrow_forward
- Competitors, as internal stakeholders, should be included in the stakeholder management consideration of a company and in its mission statement. a) True b) Falsearrow_forwardAt what level in the organization should the strategic management perspective be emphasized? Multiple Choice a) throughout the organization b) from the bottom up in an organization c) at the top of the organization d) at the middle of the organizationarrow_forwardA good manager can be flexible when it comes to sticking to the original plan; to get good results, the intended strategy has to become the realized strategy. a) True b) Falsearrow_forward
- ________ tend to be quite enduring and seldom change. Multiple Choice a) Strategic objectives b) Vision statements c) Strategic plans d) Mission statementsarrow_forwardThe idea that organizations are not only accountable to stockholders but also to the community-at-large is known as social responsibility. a) True b) Falsearrow_forwardAmong the leaders needed for an effective strategic management process are ________, who, although they have little positional power and formal authority, generate their power through the conviction and clarity of their ideas. Multiple Choice a) executive leaders b) triple bottom line advocates c) internal networkers d) local line leadersarrow_forward
- C++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage LearningProgramming Logic & Design ComprehensiveComputer ScienceISBN:9781337669405Author:FARRELLPublisher:CengageMicrosoft Visual C#Computer ScienceISBN:9781337102100Author:Joyce, Farrell.Publisher:Cengage Learning,
- EBK JAVA PROGRAMMINGComputer ScienceISBN:9781337671385Author:FARRELLPublisher:CENGAGE LEARNING - CONSIGNMENTC++ for Engineers and ScientistsComputer ScienceISBN:9781133187844Author:Bronson, Gary J.Publisher:Course Technology PtrEBK JAVA PROGRAMMINGComputer ScienceISBN:9781305480537Author:FARRELLPublisher:CENGAGE LEARNING - CONSIGNMENT