INTRO TO JAVA PROGRAMMING: COMPREHENSIV
INTRO TO JAVA PROGRAMMING: COMPREHENSIV
11th Edition
ISBN: 9780135799932
Author: Liang
Publisher: PEARSON C
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:

INTRO TO JAVA PROGRAMMING: COMPREHENSIV, 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
Q1: Write a class named as Bus that contains the attributes which are mentioned below: The name of Bus. The direction of Bus (North (N), South(S), East(E), West (W)) The position of Bus (from imaginary zero point)The class has the following member functions: A constructor to initialize the attributes Turn function to change the direction of bus to one step right side (e.g if the direction isto East, it should be changed to South and so on) Overload the Turn function to change the direction to any side directly. It should take thedirection as an argument. Move function to change the position of the Bus away from imaginary zero point. Itshould accept the distance as an argument.
13.5 (Enable GeometricObject comparable) Modify the GeometricObject class to implement the Comparable interface and define a static max method in the GeometricObject class for finding the larger of two GeometricObject class. Also, draw the UML diagram and implement the new GeometricObject class. Additionally, write a test program that use the max method to find the larger of two circle, the larger of two rectangles.
(Java) Q 1,2 ANSWER BOTH PART 1 AND 2!!! Explain the answer step-by-step and include verbal explanation. Thank you!   PART 1: 1. Given the below Car class, write a child class of Car named Tesla. The Tesla class has an additional private boolean variable named autoPilotEnabled. It also has two constructors - a default constructor and a three argument constructor. Both constructors call the appropriate constructors of the parent class. Tesla also has a getter and setter for the autoPilotEnabled variable. Finally, it has a toString method that calls the toString of Car, as well as concatenating the autoPilotEnabled variable.   public class Car { private String make;private String model;public Car() {make = "Make unknown";model = "Model unknown";}public Car(String make, String model) {this.make = make;this.model = model;} public String getMake() {return make;}public String getModel() {return model;}public void setMake(String make) {this.make = make;}public void setModel(String model)…

Chapter 11 Solutions

INTRO TO JAVA PROGRAMMING: COMPREHENSIV

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
Name the steps in the programming process.

Digital Fundamentals (11th Edition)

Why is it critical that accumulator variables be properly initialized?

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

Star Pattern Write a program that displays the following pattern:

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

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