This exercise is reserved for pair programming in the live lab sessions.
Please skip it when doing the exercises individually.
An outbreak of a zombie virus has occurred, but fortunately a very clever biochemist has created an antidote. A dose (in milligrams) of the antidote must be administered according to the following formula:
dose = (days since bitten * 250) / (age of victim)
The dose must be accurate to 3 decimal places or it will not work, so the biochemist writes the program below to calculate the dose to give to new victims. Unfortunately, the biochemist is not a good programmer and after a few days the lab is overrun with zombies.
An example of the program running as intended would be:
: java FloatDiv 23 7 Patient Age: 23 Days since bitten: 7 Dosage of antidote: 76.087mg
What did he do wrong? Change the program so that it calculates the result correctly.
Note
You only have to change one line.
Note
Don’t worry about all the % characters in the last three lines; these are used to get nicely formatted output strings. This Java API documentation explains the format strings used.
public class FloatDiv { public static void main(String args[]) { int age = Integer.parseInt(args[0]); int exposure = Integer.parseInt(args[1]); double dose = ((exposure * 250) / age); System.out.format("Patient Age: %d%n", age); System.out.format("Days since bitten: %d%n", exposure); System.out.format("Dosage of antidote: %.3fmg%n", dose); } }
An automated test has been created for this exercise: FloatDivTest.java.