bartleby

Concept explainers

bartleby

Videos

Question
Book Icon
Chapter 11, Problem 11.1PE
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:

Instructor Solutions Manual For 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
Create an original network topology consisting of at least seven routers and twelve links, assigning arbitrary positive weights to each link. Using this topology, apply Dijkstra's Link-State Algorithm to compute the shortest paths from a source router of your choice to all other routers in the network. Your topology must be entirely your own design and should not resemble any examples from the textbook, lecture slides, or other students' work. Al-generated topologies are not permitted. Create a PowerPoint presentation that follows the format and style of slides 11 to 23 from Lecture Slide Set 06 (LS06). You should copy those slides and make any necessary changes, additions, or deletions to reflect your own topology, shortest-path calculations, and update tables. Do not alter the original slide style, layout, or formatting.
Create an original network topology consisting of at least seven routers and twelve links, assigning arbitrary positive weights to each link. Using this topology, apply Dijkstra's Link-State Algorithm to compute the shortest paths from a source router of your choice to all other routers in the network. Your topology must be entirely your own design and should not resemble any examples from the textbook, lecture slides, or other students' work. Al-generated topologies are not permitted. Create
x3003 x3008 1110 0000 0000 1100 1110 0010 0001 0000 0101 0100 1010 0000 x3004 0010 0100 0001 0011 x3005 0110 0110 0000 0000 X3006 0110 1000 0100 0000 x3007 0001 0110 1100 0100 0111 0110 0000 What does the following LC-3 program do? Trace Step by Step, SHOW ALL YOUR WORK. x3001 x3002 0000 x3009 0001 0000 0010 0001 X300A 0001 0010 0110 0001 x300B 0001 0100 1011 1111 x300C 0000 0011 1111 1000 X300D 1111 0000 0010 0101 x300E 0000 0000 0000 0101 x300F 0000 0000 0000 0100 x3010 0000 0000 0000 0011 x3011 0000 0000 0000 0110 x3012 0000 0000 0000 0010 x3013 x3014 0000 0000 0000 0000 0000 0100 0000 0111 x3015 0000 0000 0000 0110 x3016 0000 0000 0000 1000 x3017 0000 0000 0000 0111 x3018 0000 0000 0000 0101

Chapter 11 Solutions

Instructor Solutions Manual For Introduction To Java Programming And Data Structures, Comprehensive Version, 11th Edition

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
Programming Logic & Design Comprehensive
Computer Science
ISBN:9781337669405
Author:FARRELL
Publisher:Cengage
Text book image
C++ Programming: From Problem Analysis to Program...
Computer Science
ISBN:9781337102087
Author:D. S. Malik
Publisher:Cengage Learning
Text book image
C++ for Engineers and Scientists
Computer Science
ISBN:9781133187844
Author:Bronson, Gary J.
Publisher:Course Technology Ptr
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