THINK FIRST·CODE LATER

← All labs

Triangle extends GeometricObject

Problem

Write a superclass GeometricObject (private color and filled, a constructor, the two accessors and toString() giving color red, filled), a subclass Triangle, and a public class TriangleApp.

Triangle adds three sides and:

  • a no-argument constructor (a white, unfilled triangle with sides of 1) and a full constructor that passes color and filled to the superclass with super(...);
  • isValid() — the sum of any two sides exceeds the third; getPerimeter(); getArea() (Heron's formula: with s = perimeter / 2, the area is √(s(s − a)(s − b)(s − c)));
  • toString() that extends the inherited one: Triangle: side1 = 3.0 side2 = 4.0 side3 = 5.0, color red, filled.

main reads three sides, a color and true or false. For the input

3 4 5 red true

the program prints

Triangle: side1 = 3.0 side2 = 4.0 side3 = 5.0, color red, filled
Perimeter: 12.00
Area: 6.00
Inherited: color red, filled true
Default: Triangle: side1 = 1.0 side2 = 1.0 side3 = 1.0, color white, not filled

Data for this lab

Your program reads its data from standard input. Download the file, then give it to your program as shown below. Start with data set 1: its expected output is given.

l14-7-data-1.txt

3 4 5 red true
What your program must print for l14-7-data-1.txt
Triangle: side1 = 3.0 side2 = 4.0 side3 = 5.0, color red, filled
Perimeter: 12.00
Area: 6.00
Inherited: color red, filled true
Default: Triangle: side1 = 1.0 side2 = 1.0 side3 = 1.0, color white, not filled

Run your program first, then open this and compare line by line: your output must contain these lines exactly.

How to give the file to my program

Terminal (macOS, Linux, or Windows Command Prompt), after compiling:

java Main < l14-7-data-1.txt

Windows PowerShell does not understand <:

Get-Content l14-7-data-1.txt | java Main

IntelliJ IDEA: Run → Edit Configurations → Redirect input from. Or simply paste the content of the file into the console when the program waits for input.

Write it here or in your IDE, then paste it. Compile and test it yourself before comparing. Your code stays in your browser — it is never sent to or stored on the server.