THINK FIRST·CODE LATER

← All labs

Which shapes can be colored? An interface as an ability

Problem

Write:

  • an interface Colorable with one method, String howToColor();
  • an abstract class Shape with the abstract methods getArea() and getName();
  • Square (extends Shape, implements Colorable: "color all four sides"), Disc (extends Shape only) and Flag (width, height, number of stripes; implements Colorable: "color each of the 3 stripes separately").

ColorableApp reads the number of shapes, then lines square side, disc radius or flag width height stripes, and stores them in a Shape[]. For each shape it prints the name and the area, then — only if the object is Colorable — how to color it. For the input

5
square 2
disc 1
flag 3 2 3
square 4.5
disc 2.5

the program prints

Square of side 2.0, area 4.00: color all four sides
Disc of radius 1.0, area 3.14: cannot be colored
Flag 3.0 by 2.0, area 6.00: color each of the 3 stripes separately
Square of side 4.5, area 20.25: color all four sides
Disc of radius 2.5, area 19.63: cannot be colored
3 of 5 shapes are Colorable

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.

l15-6-data-1.txt

5
square 2
disc 1
flag 3 2 3
square 4.5
disc 2.5
What your program must print for l15-6-data-1.txt
Square of side 2.0, area 4.00: color all four sides
Disc of radius 1.0, area 3.14: cannot be colored
Flag 3.0 by 2.0, area 6.00: color each of the 3 stripes separately
Square of side 4.5, area 20.25: color all four sides
Disc of radius 2.5, area 19.63: cannot be colored
3 of 5 shapes are Colorable

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 < l15-6-data-1.txt

Windows PowerShell does not understand <:

Get-Content l15-6-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.