THINK FIRST·CODE LATER

← All labs

Fan: constants and guarded mutators

Problem

Write a class Fan and a public class FanApp.

Fan has three public constants SLOW = 1, MEDIUM = 2, FAST = 3 and four private fields with these starting values: speed (SLOW), on (false), radius (5) and color ("blue"). Besides the accessors:

method behaviour
boolean setSpeed(int) accepts only 1, 2 or 3; returns whether it changed
boolean setRadius(double) accepts only positive values
void setOn(boolean), void setColor(String) simple mutators
String toString() fan is on: speed 3, color yellow, radius 10.0 — or, when off, fan is off: color blue, radius 5.0

main creates two fans and reads commands until the end of the input, each starting with the fan number: 1 on, 1 off, 2 speed 3, 1 radius 10, 2 color red. A refused value prints a Rejected: … line. At the end both fans are printed. For the input

1 on
1 speed 3
1 radius 10
1 color yellow
2 speed 2
2 radius 5
2 color blue

the program prints

Fan 1: fan is on: speed 3, color yellow, radius 10.0
Fan 2: fan is off: color blue, radius 5.0

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.

l10-7-data-1.txt

1 on
1 speed 3
1 radius 10
1 color yellow
2 speed 2
2 radius 5
2 color blue
What your program must print for l10-7-data-1.txt
Fan 1: fan is on: speed 3, color yellow, radius 10.0
Fan 2: fan is off: color blue, radius 5.0

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

Windows PowerShell does not understand <:

Get-Content l10-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.