(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: Brief Version (11th Global Edition)
Additional Engineering Textbook Solutions
Starting Out with Programming Logic and Design (4th Edition)
Problem Solving with C++ (10th Edition)
Computer Systems: A Programmer's Perspective (3rd Edition)
Java How to Program, Early Objects (11th Edition) (Deitel: How to Program)
Java: An Introduction to Problem Solving and Programming (8th Edition)
- (WRITE JAVA CODE AND SEND SCREENSHOTS) You need to develop a system in Java for a Juice company. Create a class Juice with data member’s sugar, flavour, size, amount of water, and price. Write required Constructors (no argument and parameterize) for this class. Write a member method showJuice() to display the contents of a juice object though you may have to write some more methods as per need. In main() method you need to create different juices with the following specifications: 1) Sugar = 5g, flavor = mango, size = ½ (half liter), water = ½ liters, price = $1 2) Sugar = 2g, flavor = orange, size = ½ (half liter), water = ½ liters, price = $2 3) Sugar = 3g, flavor = graps, size = ½ (half liter), water = ½ liters, price = $3 You also need to create 1 litter juices for the company by adding objects of two half litter juices. While adding two half litter flavours to make a 1 litter juice, make sure you can only add same flavour juices. If two…arrow_forward(The ComparableSquare class) Define a class named ComparableSquare thatextends Square and implements Comparable. Implement the compareTo methodto compare the Squares on the basis of area. Write a test class to find the larger oftwo instances of ComparableSquareobjects.arrow_forwardProblem (Circle.java) Implement a class called Circle for representing a circle. The circle has two data members, a Point object representing the center of the circle and a float value representing the radius. Include appropriate constructors for your Circle class. Encapsulate it. Also include methods for finding the area and circumference of the circle. area = pi * radius * radius circumference = 2 * pi * radius. (You may use the static constant pi in the Math class) 5. Override the toString() and equals().arrow_forward
- Please help me with this , I am stuck ! PLEASE WRITE IT IN C++ Thanks 1) bagUnion: The union of two bags is a new bag containing the combined contents of the original two bags. Design and specify a method union for the ArrayBag that returns as a new bag the union of the bag receiving the call to the method and the bag that is the method's parameter. The method signature (which should appear in the .h file and be implemented in the .cpp file is: ArrayBag<T> bagUnion(const ArrayBag<T> &otherBag) const; This method would be called in main() with: ArrayBag<int> bag1u2 = bag1.bagUnion(bag2); Note that the union of two bags might contain duplicate items. For example, if object x occurs five times in one bag and twice in another, the union of these bags contains x seven times. The union does not affect the contents of the original bags. Here is the main file: #include <cstdlib>#include <iostream>#include "ArrayBag.h"using namespace std; template…arrow_forwardPlease include doctring, and write only in python: First, write a class named Movie that has four data members: title, genre, director, and year. It should have: an init method that takes as arguments the title, genre, director, and year (in that order) and assigns them to the data members. The year is an integer and the others are strings. get methods for each of the data members (get_title, get_genre, get_director, and get_year). Next write a class named StreamingService that has two data members: name and catalog. the catalog is a dictionary of Movies, with the titles as the keys and the Movie objects as the corresponding values (you can assume there aren't any Movies with the same title). The StreamingService class should have: an init method that takes the name as an argument, and assigns it to the name data member. The catalog data member should be initialized to an empty dictionary. get methods for each of the data members (get_name and get_catalog). a method named add_movie…arrow_forwardWrite a simple cash register class. (Use Java) Each cash register should keep track of the number of items checked out and the total cash in the register. We are only concerned with the number of items to check out and their prices. No need to handle item names, SKUs, taxes, etc... It should have the following methods (use static when appropriate): checkout( ) Takes two forms. If there is only one parameter that is the cash received and there is only one item checked out. If it has two parameters, the first parameter should have the number of items checked out and the second parameter is the price per item. cashout( ) Reset the number of items and total cash to zero for a cash register getItems( ) Returns the number of items in the cash register getTotalCash( ) Returns the total cash in the cash register getAveragePricePerItem( ) Returns the average price per item checked out by the cash register getRegisterCount( ) Returns the number of cash registers created getAllItems(…arrow_forward
- (It must be done on python) Create a “Rectangle” class, which is a geometric figure. The rectangle must have two attributes: “side1” and “side2” and methods that calculate area and perimeter must be implemented.Also define a class “Square”, which is an equal-sided rectangle.arrow_forwardQuestion 7 (a) Suppose the class Spaghetti implements the Edible interface and the base class of Spaghetti is Food class. You have given the following variables declaration: Food food; new Spaghetti (); Spaghetti spa Edible e = null; %3D Identify the following statements as valid or invalid. i) e spa; spa = food; = new Food(): ii) spa food (Spaghetti) e: e = new Spaghetti(): CS Scanned with CamScannerarrow_forward(Conversions between feet and meters) Write a class that contains the following two methods: /** Convert from feet to meters */ public static double footTOMeter(double foot) /** Convert from meters to feet */ public static double meterToFoot(double meter) The formula for the conversion is: meter = 0.305 * foot foot = 3.279 * meter Write a test program that invokes these methods to display the following tables: Feet Meters Meters Feet 1.0 0.305 20.0 65.574 2.0 0.610 25.0 81.967 ... 9.0 2.745 60.0 196.721 10.0 3.050 65.0 213.115arrow_forward
- (Java) Create an HourlyEmployee class that inherits from Employee and has two new instance variables: hours, which represents the hours worked, and wage, which represents the employee's pay per hour. (Both are doubles.) Create a constructor that takes the arguments first name, last name, social security number, hourly wage, and the number of hours worked. Also create accessors, mutators, an earnings method that returns the money earned by the employee this week, and a toString method that returns information about the employee in the form of a String. The setWage method should ensure that the wage is nonnegative, and the setHours method should ensure that the value of hours is between 0 and 168 (the number of hours in a week).Create a Driver class with a main method that prompts the user to enter a first name, last name, social security number, hours, and wage for an employee. Then, the program should create an HourlyEmployee object and use its toString method to print information…arrow_forward(You must complete Self-Check Problem 7 before answering this question.) Encapsulate the Name class. Make its fields private and add appropriate accessor methods to the class.arrow_forwardNeed help in Java programming. Write the class definitions for all classes. Be sure to include all properties and two getters and setters for each class. One of the getters and setters should be for a string datatype, and one for either a numeric or Boolean data type. At a minimum the following classes need to be defined: Customer (CustID, LastName, FirstName, Street, City, State, Zip, TaxExempt) Inventory (ItemID, Description, Cost, Selling Price, Quantity On Hand) Order Header (OrderID, CustID, OrderDate, Shipto, Amount Due) Order Detail (OrderID, Line Number, ItemID, Quantity Ordered, Selling Price, Total)arrow_forward
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education