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
Java - OOP
Class: Square Create a class called "Square", which is inherited from“TwoDimensionShape” class. The "Square" class is used to calculate theArea and the Perimeter of square shapes when their side-length is given, or in contrast,finding the side-length of the square when the Area or the Perimeter of the square is given.The specifications of this class are below. Data Members (Attributes): All are private. sideLength: the sidelight (L) of the square. Methods/Operations/Getters/Setters: Default Constructor: when creating an object this constructor must set the side-length of the square by calling the method setSideLength(). Also, this constructor willcall the findArea() and the findPerimeter() in addition to summaryPrint() to calculateand print all needed information. User-Defined Constructor: when creating an object this constructor shouldfind the side-length of the square by calling the method findSquareSideLength()and pass the Area or the…
Calculator Class In the file Calculator.java, write a class called Calculator that emulates basic functions of a calculator: add, subtract, multiply, divide, and clear. The class has one private member field called value for the calculator's current value. Implement the following Constructor and instance methods as listed below:   public Calculator() - Constructor method to set the member field to 0.0 public void add(double val) - add the parameter to the member field public void subtract(double val) - subtract the parameter from the member field public void multiply(double val) - multiply the member field by the parameter public void divide(double val) - divide the member field by the parameter public void clear( ) - set the member field to 0.0 public double getValue( ) - return the member field Given two double input values num1 and num2, the program outputs the following values: The initial value of the instance field, value The value after adding num1 The value after…

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