Introduction to Java Programming and Data Structures, Comprehensive Version (11th Edition)
Introduction to Java Programming and Data Structures, Comprehensive Version (11th Edition)
11th Edition
ISBN: 9780134670942
Author: Y. Daniel Liang
Publisher: PEARSON
bartleby

Videos

Textbook Question
Book Icon
Chapter 9, Problem 9.1PE

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

Expert Solution & Answer
Check Mark
Program Plan Intro

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()”
  • 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.
Program Description Answer

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);

}

}

Sample Output

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?

Subscribe now to access step-by-step solutions to millions of textbook problems written by subject matter experts!
03:28
Students have asked these similar questions
(already solved on bartleby) You are required to build a mini FunTime application for the kids to playwith their electronic toys in a virtual world. Follow the given steps   Build a class Toy having the following data members   Name(String) Color (String) Type (String) Price (float) batteryHealth (int)   Provide constructor with arguments for name, color, type, and price. Initialize batteryHealth to 5 (which means full).   Provide getter for each of these but no setters. Provide a function charge(String time). This function would increase the batteryHealth of the toy according to following rules   Time   Increase in battery health   15 mins   20% charging (Increase health by 1)   30 mins   40%   45 mins   60%   1 hour   80%   1 hour 15 mins   100%   If the toy is already fully charged, then no more charging should be done and appropriate message to be displayed. The charge function returns the new health of the battery of toy object.   Provide another function play() that reduces the…
This program extends the earlier "Online shopping cart" program. (Consider first saving your earlier program). (1) Extend the ItemToPurchase class to contain a new attribute.  item_description (string) - Set to "none" in default constructorImplement the following method for the ItemToPurchase class. print_item_description() - Prints item_description attribute for an ItemToPurchase object. Has an ItemToPurchase parameter. Ex. of print_item_description() output: Bottled Water: Deer Park, 12 oz. (2) Build the ShoppingCart class with the following data attributes and related methods. Note: Some can be method stubs (empty methods) initially, to be completed in later steps. Parameterized constructor which takes the customer name and date as parameters Attributescustomer_name (string) - Initialized in default constructor to "none"current_date (string) - Initialized in default constructor to "January 1, 2016"cart_items (list)Methodsadd_item()Adds an item to cart_items list. Has a parameter of…
(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…

Chapter 9 Solutions

Introduction to Java Programming and Data Structures, Comprehensive Version (11th Edition)

Ch. 9.5 - Prob. 9.5.5CPCh. 9.5 - What is wrong in the following code? 1. class Test...Ch. 9.5 - Prob. 9.5.7CPCh. 9.6 - Prob. 9.6.1CPCh. 9.6 - Prob. 9.6.2CPCh. 9.6 - Which packages contain the classes Date, Random,...Ch. 9.7 - Suppose the class F is defined in (a). Let f be an...Ch. 9.7 - Prob. 9.7.2CPCh. 9.7 - Can you invoke an instance method or reference an...Ch. 9.9 - Prob. 9.9.1CPCh. 9.9 - Prob. 9.9.2CPCh. 9.9 - In the following code, radius is private in the...Ch. 9.10 - Prob. 9.10.1CPCh. 9.10 - Show the output of the following program: public...Ch. 9.10 - Show the output of the following code:Ch. 9.10 - Prob. 9.10.4CPCh. 9.11 - What is wrong in the following code? 1public class...Ch. 9.12 - If a class contains only private data fields and...Ch. 9.12 - If all the data fields in a class are private and...Ch. 9.12 - Is the following class immutable? public class A {...Ch. 9.13 - What is the output of the following program?...Ch. 9.14 - Prob. 9.14.2CPCh. 9.14 - Prob. 9.14.3CPCh. 9 - (The Rectangle class) Following the example of the...Ch. 9 - (The Stock class) Following the example of the...Ch. 9 - (Use the Date class) Write a program that creates...Ch. 9 - Prob. 9.4PECh. 9 - (Use the GregorianCa1endar class) Java API has the...Ch. 9 - (Stopwatch) Design a class named StopWatch. The...Ch. 9 - (The Account class) Design a class named Account...Ch. 9 - (The Fan class) Design a class named Fan to...Ch. 9 - (Geometry: n-sided regular polygon) In an n-sided...Ch. 9 - Prob. 9.10PECh. 9 - (Algebra: 2 2linear equations) Design a class...Ch. 9 - (Geometry: intersecting point) Suppose two line...Ch. 9 - (The Location class) Design a class named Location...
Knowledge Booster
Background pattern image
Computer Science
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Text book image
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Text book image
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
Text book image
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Text book image
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Text book image
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education
Introduction to Variables; Author: Neso Academy;https://www.youtube.com/watch?v=fO4FwJOShdc;License: Standard YouTube License, CC-BY