Write a public class named CoffeeMachine. This class is going
to represent a coffee machine that has a powerbutton and water reservoir.
Add six instance variables, a String named model
which will hold the model name of the machine, an int
named capacity to hold the capacity of the water reservoir,
an int named waterLevel which records how full
the reservoir is, an int named totalBrewed which
records the total amount of coffee brewed in the lifetime of the machine,
a boolean named on which indicates whether the
machine is on or off, and a String named error
which is used to record errors that occur during the operation of the
machine such as trying to fill the reservoir with too much water.
Write a constructor that takes two arguments, a String
model name and an int capacity. The constructor should
initialize the corresponding instance variables. The other four instance
variables should always start out
at 0, 0, false,
and null.
Write getters for all six instance variables. The getters for
the Strings and the int should follow the
normal getName pattern while the getters for
the boolean instance variable should use the alternate naming
pattern for booleans, isName.
Don’t write any setters. Unless you did for some reason, this test should be passing already.
Write a method pressPowerButton that takes no arguments and
toggles the on state. I.e. if on
is false it should become true and vice versa.
Write a method addWater that takes an int
argument and adds that value to the waterLevel. However, if
doing so would overflow the capacity of the machine it should
instead set waterLevel to the max capacity and
set the error field to the message, "You have a puddle
on the counter now.".
Write a method brew that takes a single int
argument and brews a cup of coffee of that many millileters if the machine
is on. It should decrease the waterLevel by that amount and
increase totalBrewed by the same amount. However, if there is
not enough water in the machine, brew only as much as you have water
available and set error to the message, "Out of
water." If the machine is not on it should do nothing other than
setting error to "Can't brew when machine is off".
Write another brew method, this one taking no arguments. It
should have exactly the same result as calling the other brew
method with 250 as the argument.
Write a method clearError that takes no arguments and clears
the error field, setting it back to null.
Write a method toString that takes no arguments and returns
a String in the format:
[MODEL_NAME] water level: 500ml; total brewed: 12355ml
where MODEL_NAME is filled in with the actual model name
and the numbers for the water level and total brewed come from the
current state of the machine. And if the error field is
not null append:
; error: ERROR MESSAGE where ERROR_MESSAGE is
filled in with the current error.
These tests test the behavior of the lifecycle methods you just wrote. If you wrote them all correctly they should just pass. If they do not pass, they will tell you the difference between the expected state of the machine and the actual state of your object.
On this test you will write a class to represent a coffee machine according to the specification in the problems.
Note that as a new feature to save on typing, anything in the problem
instructions that is set in this code font you can now click
and copy to your clipboard. Also, if you want you can open a
new spelling aid page (opens in a
new tab) which has a bunch of Java keywords and class and method names that
you can click to copy. During this test you should have only this tab, the
spellings page, and if you really want the AP
CSA Java Quick Reference which you will have access to during the AP
exam.