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

Concept explainers

bartleby

Videos

Textbook Question
Book Icon
Chapter 11, Problem 11.1PE

Sections 11.2–11.4

11.1    (The Triangle class) Design a class named Triangle that extends GeometricObject. The class contains:

■    Three double data fields named side1, side2, and side3 with default values 1.0 to denote three sides of a triangle.

■    A no-arg constructor that creates a default triangle.

■    A constructor that creates a triangle with the specified side1, side2, and side3.

■    The accessor methods for all three data fields.

■    A method named getArea() that returns the area of this triangle.

■    A method named getPerimeter() that returns the perimeter of this triangle.

■    A method named toString() that returns a string description for the triangle.

For the formula to compute the area of a triangle, see Programming Exercise 2.19. The toString () method is implemented as follows:

return "Triangle: side1 = " + side1 + " side2 = " + side2 + " side3 = " + side3;

Draw the UML diagrams for the classes Triangle and GeometricObject and implement the classes. Write a test program that prompts the user to enter three sides of the triangle, a color, and a Boolean value to indicate whether the triangle is filled. The program should create a Triangle object with these sides and set the color and filled properties using the input. The program should display the area, perimeter, color, and true or false to indicate whether it is filled or not.

Expert Solution & Answer
Check Mark
Program Plan Intro

Program Plan:

  • Include the required import statement.
  • Define the main class.
    • Define the main method using public static main.
      • Declare the input scanner.
      • Get the three sides of the triangle from the user.
      • Create an object for the “Triangle” class.
      • Get the color from the user and call the “setColor” method with the parameter “color”.
      • Get the Boolean value for filled the triangle and call the “setFilled” method with the parameter “filled”.
      • Display the output.
  • Define the “GeometricObject” class.
    • Declare the required variables.
    • Define the default constructor and constructor for the class.
    • Define the accessor and matator.
    • The “isFilled” and “setColor” method will return the value to the main class.
  • Define the derived class “Triangle” from the “GeometricObject” class.
    • Declare the required variables.
    • Define the default constructor and constructor for the class.
    • Define the accessor.
    • The “getArea()” method will calculate the area of triangle and then return the result.
    • The “getPerimeter()” method will return the perimeter of the triangle.
    • The “toString()” method will return the three sides of the triangle.
Program Description Answer

The below program is used to display the area, perimeter and sides of the triangle as follows:

Explanation of Solution

Program:

//import statement

import java.util.Scanner;

//class Excersise11_01

public class Exercise11_01

{

// main method

public static void main(String[] args)

{

// declare the scanner variable

Scanner input = new Scanner(System.in);

//get the input from the user

System.out.print("Enter three sides: ");

//declare the variables

double side1 = input.nextDouble();

double side2 = input.nextDouble();

double side3 = input.nextDouble();

//create an object for the "Triangle" class

Triangle triangle = new Triangle(side1, side2, side3);

//get the input from the user

System.out.print("Enter the color: ");

String color = input.next();

//call the "setColor" function

triangle.setColor(color);

//get the input from the user

System.out.print("Enter a boolean value for filled: ");

boolean filled = input.nextBoolean();

//call the "setFilled" function

triangle.setFilled(filled);

//print the output

System.out.println("The area is " + triangle.getArea());

System.out.println("The perimeter is "

+ triangle.getPerimeter());

System.out.println(triangle);

}

}

//definition of class "GeometricObject"

class GeometricObject

{

/* declare the required variables and initialize it */

private String color = "white";

private boolean filled;

private java.util.Date dateCreated;

//definition of default constructor

public GeometricObject()

{

//create an object

dateCreated = new java.util.Date();

}

//definition of constructor

public GeometricObject(String color, boolean filled)

{

//create an object

dateCreated = new java.util.Date();

//set the value

this.color = color;

this.filled = filled;

}

//definition of accessor

public String getColor()

{

//return the color

return color;

}

//definition of mutator

public void setColor (String color)

{

//set the color

this.color = color;

}

//definition of the "isFilled" method

public boolean isFilled()

{

//return the value

return filled;

}

//definition of the "setFilled" method

public void setFilled(boolean filled)

{

//set the value

this.filled = filled;

}

//definition of the "getDateCreated" method

public java.util.Date getDateCreated()

{

//return the value

return dateCreated;

}

//definition of the "toString" method

public String toString()

{

//return the value

return "created on " + dateCreated + "\ncolor: " + color + " and filled: " + filled;

}

}

//definition of derived class "Triangle"

class Triangle extends GeometricObject

{

/* declare the required variables and initialize it */

private double side1 = 1.0, side2 = 1.0, side3 = 1.0;

/*definition of Constructor */

public Triangle()

{

}

/* definition of Constructor */

public Triangle(double side1, double side2, double side3)

{

this.side1 = side1;

this.side2 = side2;

this.side3 = side3;

}

//definition of accessor

public double getSide1()

{

//return the value

return side1;

}

//definition of accessor

public double getSide2()

{

//return the value

return side2;

}

//definition of accessor

public double getSide3()

{

//return the value

return side3;

}

/*override method of "getArea" in GeometricObject */

public double getArea()

{

//declare and calculate the value

double s = (side1 + side2 + side3) / 2;

//return the value

return Math.sqrt(s * (s - side1) * (s - side2) * (s - side3));

}

/*override method of "getPerimeter" in GeometricObject */

public double getPerimeter()

{

//return the value

return side1 + side2 + side3;

}

//definition of "toString" method

public String toString()

{

// return the three sides

return "Triangle: side1 = " + side1 + " side2 = " + side2 +" side3 = " + side3;

}

}

UML diagram:

Introduction to Java Programming and Data Structures, Comprehensive Version (11th Edition), Chapter 11, Problem 11.1PE

Explanation:

The above UML diagram the “GeometricObject” is a parent class which contains “color”, “filled”, “dateCreated” variables and “GeometricObject()”, “GeometricObject(String color, boolean filled)”, “getColor()”, “getColor(String color)”, “isFilled()”, “setFilled(boolean filled)”, “getDateCreated()” and “toString()” methods.

The “Triangle” is the child class extended from “GeometricObject” class it contains “side1”, “side2” and “side3” variables and “Triangle()”, “Triangle(double side1, double side2, double side3)”, “getSide1()”, “getSide2()”, “getSide3()”, “getArea()”, “getPerimeter()” and “toString()” methods.

Sample Output

Enter three sides: 2

3

4

Enter the color: black

Enter a boolean value for filled: true

The area is 2.9047375096555625

The perimeter is 9.0

Triangle: side1 = 2.0 side2 = 3.0 side3 = 4.0

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!
Students have asked these similar questions
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 and then implement the class. Write a test program that creates two Rectangle objects—one with width 4 and height 40and the other with width 3.5 and height 35.9. Display the width, height, area, and perimeter of each rectangle in this order.
Task 2: Delivery Vehicle We want to develop a delivery vehicle that can carry a limited number of equally weighted boxes. To do this we can build off of our previous task by extending the BasicVehicle class. Instructions 1. Create a new class called DeliveryVehicle that is a subclass of BasicVehicle: • a private attribute cargoList of type String[] • a private attribute numItems of type int 2. Create a constructor that takes two parameters: a String for registration, and an int for maxCapacity: • pass on the registration parameter to the super constructor along with 0.0 for altitude, longitude and latitude. • use the maxCapacity parameter to create a new array for cargoList 3. Create a boolean method called isFull that returns if the cargoList is full. 4. Create a boolean method called isEmpty that returns if there are no boxes loaded. 5. Create a method called loadCargo that takes a String parameter description and adds the cargo description to the cargo list (if it is not already…
Information: Java Script   There are two types of accounts - S for short term contract and L for long term contract. 3.1 Create an Employee objects project. Create an Employee class and use the provided UML diagram to code the instance fields, constructors and methods. 3.1.1 Both constructors must increment the static numberOfEmp field by one. Both constructors must call the setEmpCode() method to compile a code for the employee. Both constructors must call the setCommission() method. 3.1.2 setEmpCode() method:   The empCode must be constructed in the following way: First character is the type of contract Next three characters a random 3 digits Add a backslash and the number of objects value to the code NOTE: You have to use the Random class in your code (See Ch 9).  Do NOT use the Math.random() method.   Example of code: S781/1 Explanation: Short term contracts (S), 781 is the 3-digit random value, first employee object. 3.1.3 setCommission() method:   Assign a basic amount to the…

Chapter 11 Solutions

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

Ch. 11.5 - Identify the problems in the following code:...Ch. 11.5 - Prob. 11.5.2CPCh. 11.5 - If a method in a subclass has the same signature...Ch. 11.5 - If a method in a subclass has the same signature...Ch. 11.5 - If a method in a subclass has the same name as a...Ch. 11.5 - Prob. 11.5.6CPCh. 11.7 - Prob. 11.7.1CPCh. 11.8 - Prob. 11.8.1CPCh. 11.8 - Prob. 11.8.2CPCh. 11.8 - Can you assign new int[50], new Integer [50], new...Ch. 11.8 - Prob. 11.8.4CPCh. 11.8 - Show the output of the following code:Ch. 11.8 - Show the output of following program: 1public...Ch. 11.8 - Show the output of following program: public class...Ch. 11.9 - Indicate true or false for the following...Ch. 11.9 - For the GeometricObject and Circle classes in...Ch. 11.9 - Suppose Fruit, Apple, Orange, GoldenDelicious, and...Ch. 11.9 - What is wrong in the following code? 1public class...Ch. 11.10 - Prob. 11.10.1CPCh. 11.11 - Prob. 11.11.1CPCh. 11.11 - Prob. 11.11.2CPCh. 11.11 - Prob. 11.11.3CPCh. 11.11 - Prob. 11.11.4CPCh. 11.11 - Prob. 11.11.5CPCh. 11.12 - Correct errors in the following statements: int[]...Ch. 11.12 - Correct errors in the following statements: int[]...Ch. 11.13 - Prob. 11.13.1CPCh. 11.14 - What modifier should you use on a class so a class...Ch. 11.14 - Prob. 11.14.2CPCh. 11.14 - In the following code, the classes A and B are in...Ch. 11.14 - In the following code, the classes A and B are in...Ch. 11.15 - Prob. 11.15.1CPCh. 11.15 - Indicate true or false for the following...Ch. 11 - Sections 11.211.4 11.1(The Triangle class) Design...Ch. 11 - (Subclasses of Account) In Programming Exercise...Ch. 11 - (Maximum element in ArrayList) Write the following...Ch. 11 - Prob. 11.5PECh. 11 - (Use ArrayList) Write a program that creates an...Ch. 11 - (Shuffle ArrayList) Write the following method...Ch. 11 - (New Account class) An Account class was specified...Ch. 11 - (Largest rows and columns) Write a program that...Ch. 11 - Prob. 11.10PECh. 11 - (Sort ArrayList) Write the following method that...Ch. 11 - (Sum ArrayList) Write the following method that...Ch. 11 - (Remove duplicates) Write a method that removes...Ch. 11 - (Combine two lists) Write a method that returns...Ch. 11 - (Area of a convex polygon) A polygon is convex if...Ch. 11 - Prob. 11.16PECh. 11 - (Algebra: perfect square) Write a program that...Ch. 11 - (ArrayList of Character) Write a method that...Ch. 11 - (Bin packing using first fit) The bin packing...

Additional Engineering Textbook Solutions

Find more solutions based on key concepts
Name the steps in the programming process.

Digital Fundamentals (11th Edition)

Why is it critical that accumulator variables be properly initialized?

Starting Out with C++ from Control Structures to Objects (9th Edition)

Star Pattern Write a program that displays the following pattern:

Starting Out with Java: From Control Structures through Objects (7th Edition) (What's New in Computer Science)

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
    EBK JAVA PROGRAMMING
    Computer Science
    ISBN:9781337671385
    Author:FARRELL
    Publisher:CENGAGE LEARNING - CONSIGNMENT
    Text book image
    Microsoft Visual C#
    Computer Science
    ISBN:9781337102100
    Author:Joyce, Farrell.
    Publisher:Cengage Learning,
Text book image
EBK JAVA PROGRAMMING
Computer Science
ISBN:9781337671385
Author:FARRELL
Publisher:CENGAGE LEARNING - CONSIGNMENT
Text book image
Microsoft Visual C#
Computer Science
ISBN:9781337102100
Author:Joyce, Farrell.
Publisher:Cengage Learning,
Introduction to Classes and Objects - Part 1 (Data Structures & Algorithms #3); Author: CS Dojo;https://www.youtube.com/watch?v=8yjkWGRlUmY;License: Standard YouTube License, CC-BY