Introduction to Java Programming and Data Structures, Comprehensive Version Plus MyProgrammingLab with Pearson EText -- Access Card Package
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:

Introduction to Java Programming and Data Structures, Comprehensive Version Plus MyProgrammingLab with Pearson EText -- Access Card Package, 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
1. Level the resources (R) for the following network. Show exactly which activity is being moved at each cycle and how many days it is being moved. Show all cycles required to utilize the free float and the back float. B H 3 3 L 2 0-0-0 A C F G K N P Q T 0 3 2 2 1 2-2-2 7R 8R 4R 6R 4R 2R 5R 4R D 1 2R 2 M 000 4R 2 4R 1 2 3 4 B5 B BE B 5 5 7 D 2003 C NO C MBSCM В H 5 2 F 7 7 8 SH2F80 5 Н Н 6 7 7L3G4+ 6H2G4 J 4 4 14 8 L K 00 36 9 10 11 12 13 14 15 P 2 Z+ N N 4 4 Z t 2334 4 Σ + M M 4 +
2. Perform resource allocation for the following project. Resource limits are 6 labors and 2 helpers. Legend: Activity Dur Resources G H 2 3 2L 1H 2L OH A 1 3L 1H + B D F J K 3 4 6 2 4 4L 2H 3L OH 4L 1H 2L 2H 4L 2H C E 2 2 I 1 2L 1H 3L 1H 5L 1H
Need Java method please. Thank you.

Chapter 11 Solutions

Introduction to Java Programming and Data Structures, Comprehensive Version Plus MyProgrammingLab with Pearson EText -- Access Card Package

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