Concept explainers
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
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.
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 main method using public static main.
- 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.
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:
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.
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?
Chapter 11 Solutions
Introduction to Java Programming and Data Structures Comprehensive Version (11th Edition)
Additional Engineering Textbook Solutions
Database Concepts (8th Edition)
Computer Science: An Overview (13th Edition) (What's New in Computer Science)
Starting Out with C++ from Control Structures to Objects (9th Edition)
Elementary Surveying: An Introduction To Geomatics (15th Edition)
Starting Out with Python (4th Edition)
Introduction To Programming Using Visual Basic (11th Edition)
- Translate the following VM commands to Assembly instructions: □ push constant 1 □ push constant 5arrow_forwardSuppose the state of the argument and local memory segments are as follows: argument local stack 0 0 9 sp-> 256 1 257 1 14 2 258 259 Now consider the following VM code: 1 push constant 2 pop local @ 3 push constant 15 4 pop local 1 5 push local 1 6 push argument 1 7 gt 8 pop local 2 9 push local 0 10 push argument 0 11 add 12 pop local 0 13 push local 1 14 push local 1 15 push constant 1 16 sub 17 add 18 pop local 1 What will be the value of local 1 after the VM code has executed?arrow_forwardSuppose the state of the RAM is as follows and the adjacent assembly code will execute: RAM 0 3 1 2 2 0 فيا 3 6 456 5 1 4 1234567 $1 A = M A = M A = M D = M @4 M = D What will be the value of the RAM[4] following the assembly code execution?arrow_forward
- Two industries that use decision trees extensively are lenders (banks, mortgage companies, etc.) and insurance. Discuss how a decision tree is used to solve a business problem.arrow_forwardand some More lab 9 For the last lab of the term, I want you to create a practical application for your database, in which you modify it in some way, for instance, taking a new order. Since the emphasis is on the database connectivity, neither a GUI nor a web application is required. In fact, a GUI would require x2go, which I don't have at home and you probably don't either. A web interface would need to be on osiris, I could figure out some way to try it out, I guess. There are also numerous security concerns. The applications should be easy for someone like a store employee or on-line customer to use. The most common operations, such as adding a customer or renting a tool, should be implemented. These Part 1 instructions now include how to use postgresql in C as well as Java and python. The important issue of transactions and commit status is addressed here. Some previous notes about web applications, and the PHP language [Previously I wrote] Now write one more program, that updates…arrow_forwardc# app formarrow_forward
- (a) Giving C and k constants, determine the big-theta for the function: f(x) = 9x4 − x³ +5x+2+(-13 + 9x³ - 7x)log(8x6 + 5x³ + 9x² + 11x + 3) -arrow_forwardFollow the instructionsarrow_forwardPlease help me adjust the X-axis on my graphs in Excel spreadsheet. Range numbers are from 200 to 500 but is graphed 0 to 300. Link:https://mnscu-my.sharepoint.com/:x:/g/personal/vi2163ss_go_minnstate_edu/EdVWDTGQ2hNJuGHiHPukjuIB9DBRlyoUC8Fuqlxj2E_CQg Thank you!arrow_forward
- EBK JAVA PROGRAMMINGComputer ScienceISBN:9781337671385Author:FARRELLPublisher:CENGAGE LEARNING - CONSIGNMENTMicrosoft Visual C#Computer ScienceISBN:9781337102100Author:Joyce, Farrell.Publisher:Cengage Learning,