Write a class called Triangle that can be used to represent a triangle.  Write a class called TriangleDemo that will interface with the Triangle class.

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question
100%

Write a class called Triangle that can be used to represent a triangle. 

Write a class called TriangleDemo that will interface with the Triangle class.

 

 Triangle

 

  • A Triangle will have 3 sides.  The class will be able to keep track of the number of Triangle objects created.  It will also hold the total of the perimeters of all the Triangle objects created. 
  • It will allow a client to create a Triangle, passing in integer values for the three sides.  If the values for the sides that are passed in do not represent a valid Triangle, then all sides will be set to a value of 1.   The constructor should add 1 to the count of the number of Triangles created and also call a method to calculate the perimeter and then add the perimeter for that object to an accumulator. 

 

In addition to the constructor, the Triangle class must have the following methods that return a boolean value:

 

  • isRight ()
  • isAcute()
  • isObtuse()
  • isScalene() - no two sides are the same length
  • isIsosceles() - exactly two sides are the same length
  • isEquilateral() - all three sides are the same length
  • isValid() - the sum of any two sides of a triangle must be greater than the third in order to represent a valid triangle.  Also, no side may be 0 or negative. 
  • equals (Triangle t) – compares two Triangle objects to determine if they are equal.  We will compare their perimeters to determine equality.

 

Additional methods

 

  • toString() – returns a String that specifies the values for the 3 sides of the triangle
  • calcPerim() – calculate and return the perimeter of the object.
  • addTotalPerim().  This method will call calc_perim() and add the perimeter for that object to an accumulator. 
  • reduceTotalPerim().  This method should subtract the perimeter for that object from the accumulator.
  • Accessor and mutator methods for all properties
  • The Triange object cannot assume that the values of the 3 sides are entered in any particular order.

 

Be sure to specify proper visibility for all methods and properties

 

Note 1

 

When given 3 triangle sides, to determine if the triangle is right, acute or obtuse:

 

1) Square all 3 sides.

2) Sum the squares of the 2 shortest sides.

3) Compare this sum to the square of the 3rd side.

if sum = 3rd side²   Right Triangle

if sum > 3rd side²   Acute Triangle

if sum < 3rd side²   Obtuse Triangle

 

 

TriangleDemo

 

  • Inside a loop in main
    • Allow the user to type 3 positive integers on separate lines for each triangle. 
    • Instantiate 9 Triangles (refer to the sample dialog below for test data)
    • Store each Triangle object in an array of Triangle objects.
    • Inside a separate loop
  • Use if statements to test each triangle for its properties and print appropriate output.
    • Print the values of the sides of each triangle.
    • Print the properties that apply for each triangle.  See the sample output.
    • Print the Perimeter for each triangle.
  • Test your equals method.
  • At the end print the total of the perimeters for all triangles.
  • Output must be labeled, aligned, and reasonably well-spaced

 

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps with 1 images

Blurred answer
Knowledge Booster
Developing computer interface
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
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education