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: 9780134700144
Author: Liang
Publisher: PEARSON
bartleby

Videos

Textbook Question
Book Icon
Chapter 13, Problem 13.1PE

(Triangle class) Design a new Triangle class that extends the abstract GeometricObject class. Draw the UML diagram for the classes Triangle and GeometricObject then implement the Triangle class. 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 to create Triangle class

Program Plan:

  • Include a class name named “Exercise_13_01”.
    • Include the scanner package.
    • Declare the main() function.
    • Create a scanner object.
    • Prompt the user to enter three sides of the triangle.
    • Prompt the user to enter a color.
    • Prompt the user to enter a Boolean value on whether the triangle is filled.
    • Create triangle object with the entered user input.
    • Set color and set filled in the triangle.
    • Print the output.
    • Close the main() function.
    • Close the class “Exercise_13_01”.
  • Include an abstract class “GeometricObject”.
    • Declare all the data types of the variables.
    • Construct a default geometric object.
    • Construct a geometric object with color and filled value.
    • Assign color and fill to the object.
    • Set a new color.
    • Define an isFilled method returns filled and since filled is Boolean; the get method is named isFilled.
    • Set a new filled and getDateCreated method.
    • Override toString function to return output.
    • Define Abstract method getArea.
    • Define Abstract method getPerimeter.
    • Close class.
  • Include a class “Triangle” that extends the class GeometricObject.
    • Declare the three sides of the triangle.
    • Include a triangle constructor.
    • Assign values to all three sides of the triangle.
    • Assign triangle values to all attributes.
    • Return side1 and Set side1 to a new length.
    • Return side2 and Set side2 to a new length.
    • Return side3 and Set side3 to a new length.
    • Create a Function to return area of the Triangle.
    • Create a function to return a string description of the object.
    • Close class.
Program Description Answer

The java code to design a new Triangle class that extends the abstract GeometricObject class and to design 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 or not.

Explanation of Solution

Program:

File name: Exercise_13_01.java

//package to have user input

import java.util.Scanner;

//class Definition

public class Exercise_13_01

{

// Main method

public static void main(String[] args)

{

// Create a Scanner object

Scanner input = new Scanner(System.in);

/* Prompt the user to enter three sides of the triangle */

System.out.print("Enter three side of the triangle: ");

double side1 = input.nextDouble();

double side2 = input.nextDouble();

double side3 = input.nextDouble();

// Prompt the user to enter a color

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

String color = input.next();

/*Prompt the user to enter a boolean value on whether the triangle is filled*/

System.out.print("Is the triangle filled (true / false)? ");

boolean filled = input.nextBoolean();

/* Create triangle object with the entered user input */

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

// set color

triangle.setColor(color);

// set filled

triangle.setFilled(filled);

// print the output

System.out.println(triangle.toString());

System.out.println("Area: " + triangle.getArea());

System.out.println("Perimeter: " + triangle.getPerimeter());

System.out.println("Color: " + triangle.getColor());

System.out.println("Triangle is" + (triangle.isFilled() ? "" : " not ")

+ "filled");

}

}

Filename: GeometricObject.java

//abstract class GeometricObject definition

public abstract class GeometricObject

{

//data type declaration

private String color = "while";

private boolean filled;

private java.util.Date dateCreated;

// Construct a default geometric object

protected GeometricObject()

{

dateCreated = new java.util.Date();

}

/* Construct a geometric object with color and filled value */

protected GeometricObject(String color, boolean filled)

{

dateCreated = new java.util.Date();

//assign color and fill

this.color = color;

this.filled = filled;

}

// Return color

public String getColor()

{

return color;

}

// Set a new color

public void setColor(String color)

{

this.color = color;

}

/* isFilled method returns filled and Since filled is boolean,the get method is named isFilled */

public boolean isFilled()

{

return filled;

}

// Set a new filled

public void setFilled(boolean filled)

{

this.filled = filled;

}

// Get dateCreated

public java.util.Date getDateCreated()

{

return dateCreated;

}

//override toString function to return output

@Override

public String toString()

{

return "created on " + dateCreated + "\ncolor: " + color +

" and filled: " + filled;

}

// define Abstract method getArea

public abstract double getArea();

//Define Abstract method getPerimeter

public abstract double getPerimeter();

}

Filename: Triangle.Java

/*Class definition of Triangle that extends the class GeometricObject */

public class Triangle extends GeometricObject

{

//declare the three sides of the triangle

private double side1;

private double side2;

private double side3;

//triangle constructor

public Triangle()

{

}

//assign values to all three sides of the triangle

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

{

this.side1 = side1;

this.side2 = side2;

this.side3 = side3;

}

//assign triangle values to all attributes

public Triangle(double side1, double side2, double side3, String color, boolean filled)

{

this(side1, side2, side3);

setColor(color);

setFilled(filled);

}

// Return side1

public double getSide1()

{

return side1;

}

// Set side1 to a new length

public void setSide1(double side1)

{

this.side1 = side1;

}

// Return side2

public double getSide2()

{

return side2;

}

// Set side2 to a new length

public void setSide2(double side2)

{

this.side2 = side2;

}

// Return side3

public double getSide3()

{

return side3;

}

// Set side3 to a new length

public void setSide3(double side3)

{

this.side3 = side3;

}

// Function to Return area of the Triangle

@Override

public double getArea()

{

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

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

}

/* override function to Return perimeter of the triangle */

@Override

public double getPerimeter()

{

return side1 + side2 + side3;

}

/*override function to Return a string description of the object*/

@Override

public String toString()

{

return super.toString() + "\nArea: " + getArea() + "\nPerimeter: " + getPerimeter();

}

}

UML Diagram:

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

Explanation:

  • Here, GeometricObject is an abstract class that extends the triangle class which is represented using an arrow. The triangle class then contains a rectangle that represents the objects used to input the three sides of the triangle.
  • The remaining part is used to represent all the methods and functions used in the triangle class.
  • The Triangle() method creates a triangle with default sides.  The method Triangle(side1:double, side2; double,side3;double) creates a triangle of the specified sides.
  • Then the respective sides of the triangles are returned using getSide() methods.
  • Then the method getArea() returns the area of the triangle and the method getPerimeter() returns the perimeter of the triangle and the function toString() returns a string description of the object.
Sample Output

Enter three side of the triangle: 3

4

5

Enter a color: red

Is the triangle filled (true / false)? false

created on Sun Jul 08 13:24:03 IST 2018

color: red and filled: false

Area: 6.0

Perimeter: 12.0

Area: 6.0

Perimeter: 12.0

Color: red

Triangle is not filled

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
(Triangle class) Design a new Triangle class that extends the abstract Geometricobject class. Draw the UML diagram for the classes Triangle and Geometricobject then implement the Triangle class. 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 filed. The program should create a Tri- angle object with the se 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.
(The Time class) Design a class named Time. The class contains: - The data fields hour, minute, and second that represent a time. - A no-arg constructor that creates a Time object for the current time. (The values of the data fields will represent the current time.) -A constructor that constructs a Time object with a specified elapsed time since midnight, January 1, 1970, in milliseconds. (The values of the data fields will represent this time.) -A constructor that constructs a Time object with the specified hour, minute, and second. - Three getter methods for the data fields hour, minute, and second, respectively. -A method named setTime (long elapseTime) that sets a new time for the object using the elapsed time. For example, if the elapsed time is 555550000 milliseconds, the hour is 10, the minute is 19, and the second is 10. Draw the UML diagram for the class and then implement the class. Write a test program that creates two Time objects (using new Time (), new Time(555550000),…
(Java) The Abstract Art Class Write an abstract class as follows: The class is named Art It inherits from the Comparable interface It has a private String member variable named name It has a private String member variable named artist It has a private int member variable called year It has a default constructor that assigns the values "No name" to name, "No artist" to artist and -1 to the year. This default constructor calls the three argument constructor. It has a three-argument constructor to assign values to the name, artist and year variables. It has a copy constructor that makes a copy of another non-null Art object It has getters and setters for the name, artist and year variables It has a toString() method that creates a string of artist, with name and year tabbed once on subsequent lines (see sample output) It has an equals method that compares this Art to another Object It has a compareTo method that compares in this order: 1) artist, 2) name, 3) year This class contains no…

Chapter 13 Solutions

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

Ch. 13.4 - How do you create a Calendar object for the...Ch. 13.4 - For a Calendar object c, how do you get its year,...Ch. 13.5 - Prob. 13.5.1CPCh. 13.5 - Prob. 13.5.2CPCh. 13.5 - Prob. 13.5.3CPCh. 13.5 - Prob. 13.5.4CPCh. 13.6 - Prob. 13.6.1CPCh. 13.6 - Prob. 13.6.2CPCh. 13.6 - Can the following code be compiled? Why? Integer...Ch. 13.6 - Prob. 13.6.4CPCh. 13.6 - What is wrong in the following code? public class...Ch. 13.6 - Prob. 13.6.6CPCh. 13.6 - Listing 13.5 has an error. If you add list.add...Ch. 13.7 - Can a class invoke the super.clone() when...Ch. 13.7 - Prob. 13.7.2CPCh. 13.7 - Show the output of the following code:...Ch. 13.7 - Prob. 13.7.4CPCh. 13.7 - What is wrong in the following code? public class...Ch. 13.7 - Show the output of the following code: public...Ch. 13.8 - Prob. 13.8.1CPCh. 13.8 - Prob. 13.8.2CPCh. 13.8 - Prob. 13.8.3CPCh. 13.9 - Show the output of the following code: Rational r1...Ch. 13.9 - Prob. 13.9.2CPCh. 13.9 - Prob. 13.9.3CPCh. 13.9 - Simplify the code in lines 8285 in Listing 13.13...Ch. 13.9 - Prob. 13.9.5CPCh. 13.9 - The preceding question shows a bug in the toString...Ch. 13.10 - Describe class-design guidelines.Ch. 13 - (Triangle class) Design a new Triangle class that...Ch. 13 - (Shuffle ArrayList) Write the following method...Ch. 13 - (Sort ArrayList) Write the following method that...Ch. 13 - (Display calendars) Rewrite the PrintCalendar...Ch. 13 - (Enable GeometricObject comparable) Modify the...Ch. 13 - Prob. 13.6PECh. 13 - (The Colorable interface) Design an interface...Ch. 13 - (Revise the MyStack class) Rewrite the MyStack...Ch. 13 - Prob. 13.9PECh. 13 - Prob. 13.10PECh. 13 - (The Octagon class) Write a class named Octagon...Ch. 13 - Prob. 13.12PECh. 13 - Prob. 13.13PECh. 13 - (Demonstrate the benefits of encapsulation)...Ch. 13 - Prob. 13.15PECh. 13 - (Math: The Complex class) A complex number is a...Ch. 13 - (Use the Rational class) Write a program that...Ch. 13 - (Convert decimals to fractious) Write a program...Ch. 13 - (Algebra: solve quadratic equations) Rewrite...Ch. 13 - (Algebra: vertex form equations) The equation of a...

Additional Engineering Textbook Solutions

Find more solutions based on key concepts
10.5" Balance the angles in Problem 9.22 El. Compute the preliminary azimuths for each course.

Elementary Surveying: An Introduction To Geomatics (15th Edition)

BankAccount and SavingsAccount Classes Design an abstract class named BankAccount to hold the following data fo...

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

Summarize the booting process.

Computer Science: An Overview (13th Edition) (What's New in Computer Science)

Explain the difference between overloading a function and redefining a function.

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

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
Call By Value & Call By Reference in C; Author: Neso Academy;https://www.youtube.com/watch?v=HEiPxjVR8CU;License: Standard YouTube License, CC-BY