Conditional
Logic and flow control (if ... else, switch)
The goal for this lab is
Go menu select Connect to Server.... Type afp://fileserver1 or choose it from the
list of selections and select the Personal volume
when prompted.
drive() method as we discussed in class to use
and if {} else {} statement.
If the car has enough fuel, it drives the distance specified by drive() method's parameter. Otherwise, it travels as far as it
can on the fuel it has. if ( )
condition before the fuelLevel and fuelEfficiency assignments already in
the method since you want those commands to execute if the condition you
write in the parentheses is true.
Add the else clause after that and
assign fuelLevel and fuelEfficiency the appropriate values. Don't put a semi-colon at the end
of the if ( ) line, and remember to
use { } to enclose multiple
lines that should be executed together. Test your method interactively to make sure that the
car doesn't drive past its fuel limit and that the mileage updates
correctly.
if ... else logic from the
previous step by surrounding it with /* before and */ after. Instead, use Math.max() and
Math.min() so that there is a single assignment for the mileage and fuel
level as before, i.e.fuelLevel = Math. ( , );mileage = mileage + Math. ( , );
static final) to your class for at
least 3 different models, e.g. PRIUS = "Prius", CIVIC =
"Civic", TAURUS = "Taurus". Also add another constant for each model's
default fuel efficiency, e.g. CIVIC_EFFICIENCY = 39.0, etc. It's good style to keep all
instance variables together, usually all the public ones followed by the
private ones.
setFuelEfficiency() method that takes a
single double parameter and returns void. It should set the fuelEfficiency variable/field to the
specified parameter. Next,
add a second method public setFuelEfficiency() method that takes a
single String parameter specifying the model of the car. e.g. Civic,
Taurus, Prius, etc. Be sure
to use a different name for the method parameter than your used for the
fuel level instance variable.
This String parameter version should determine the correct
fuelEfficiency setting based on the object's model variable. Use a sequence of nested "if
() ... else if () ... else" tests to decide the correct setting and
store it in a variable declared earlier in the method. Test the String parameter against
the constants for each model and assign a default value if the given
parameter doesn't match any defined model. Don't forget to use the .equals() method to compare strings. The last line of the method should
call the version that takes a double with the value you determined as an
argument.setFuelEfficiency(String
theModel){ double
efficiency = 0; if
(theModel.equals(Automobile.CIVIC)) efficiency =
Automobile.CIVIC_EFFICIENCY; else
if (theModel.equals( )) efficiency = ; //call
the other method to actually assign the value setFuelEfficiency(efficiency);}
Automobile(String modelName,
double amount).
setFuelEfficiency(modelName) in
the constructor to set the fuel efficiency based on the vehicle
model. When you have multiple
constructors it can be tedious to modify them all. There is a special use of the
keyword this in
constructors (see Advanced
topic 3.1 online for syntax) that can lessen this problem. Typically, your most generic
constructor, the one that takes the most parameters, is where you write
all your code and you have all the other constructors call that very
general one using this(param1,param2,
etc.). However,
the call to the other constructor must be the first line. In our case this is minor
inconvenience, since in the above constructor we need to decide the efficiency
value before calling the other
constructor. For now just
repeat the necessary instance variable assignments in the constructors you
want to use for the rest of the lab.
If you have time at the end of the lab try to consolidate your
various constructors using this().
setYear()
method taking an int parameter to modify it from outside the class. In practice we would modify all
versions of the constructors to initialize and set year. However, for now, just use setYear() after you create a new
automobile instance to give that instance a year. Also add a public method that
resets the fuel efficiency called computeFuelEfficiency(). Based on the automobile's model,
year, and mileage it will determine the expected fuel efficiency and
assign it to the car. Compute
the expected fuel efficiency as follows:
true.
The symbol for AND is &&, and NOT is !, e.g. (A &&
!A) is always false
since A can't be both true and false.Math.floor() method
to find the number of times 50,000 divides the mileage. For example, Math.floor(mileage/50,0000)
will give you the integer result of the division as a double. (Be sure not to use commas in the
numbers in your class since commas are used to separate parameters in
Java.)
Start by using setFuelEfficiency(this.model) to set the car's efficiency
to the value for that model. Then,
modify that value as needed for each of the above cases.
Does the order of the cases matter?
What fuel efficiency does a vehicle older than 5 years and with mileage
between 60,000 and 100,000 have?
Try drawing a grid on paper with mileage on one axis and the car's age
on the other to help.
switch statement which can be
used for some nested if else tests.
Which if any tests we have implemented in Automobile can be
expressed using switch?