Dog Years

advertisement
Dog Years
Working out your dog's true age used to be a case of simply multiplying it by seven. But
it's more complicated than that!
Different breeds of dog age at varying speeds. Dogs age at varying speeds at different
stages of their lives.
A more accurate formula would be:
Add 10.5 for each of the first two years then for each additional year.
 for small or medium sized dogs add 4
 for large dogs add 5
 for giant breeds add 6
Using this adjustment for size we can estimate that…
 an 8 year old small or medium dog is 45 in human years
 an 8 year old large dog is 51 in human years
 an 8 year old giant dog is 57 in human years
Complete the Java program which calculates the dog’s age in human years.
import javax.swing.JOptionPane; // import a package for user input
public class DogYears
{
/*****
* Complete the method to calculate dog years
*
* Add 10.5 for each of the first two years
*
then for each additional year:
* for small or medium sized dogs add 4
* for large dogs add 5
* for giant breeds add 6
****/
public static double calculateYears(String size, int yrs)
{
///
<<< complete this method >>>
}
// The main method is complete
public static void main( String args[] )
{
/*** Read a string into variable dogSize ***/
String dogSize = JOptionPane.showInputDialog(null, "Enter the
dog's size: small, medium, large, giant.");
/*** Read an integer into variable dogAge ***/
int dogAge =
Integer.parseInt(JOptionPane.showInputDialog(null, "Enter the dog's
age."));
System.out.println("The dog’s age in human years is " +
calculateYears(dogSize,dogAge));
}
}
Download