Question: /* * *This program repeatedly asks the user for an animal and a trip *and for each, prints the attributes of the animal and the

/* * *This program repeatedly asks the user for an animal and a trip *and for each, prints the attributes of the animal and the length *of time needed for the trip */ package animal.kingdom;

/** * * @author */ import java.util.Scanner; //For using input

abstract class Animal{ String kind; //name of the animal double airSpeed; double landSpeed; boolean flies; abstract double FastTime(double distance);//returns the amount of time in hours the animal will take to cover the distance void print(){//prints the four protected data items System.out.println("Kind:\t"+kind); System.out.println("Air Speed:\t"+airSpeed); System.out.println("Land Speed:\t"+landSpeed); if(flies) System.out.println("Can Fly:\tYes"); else System.out.println("Can Fly:\tNo"); } }

class Mammal extends Animal{

Mammal(){//notes that the animal cannot fly flies = false; } @Override double FastTime(double distance) {//give the time that the animal will take to make the trip on the ground return distance/landSpeed; } }

class Cow extends Mammal{ Cow(){ kind = "\tCow"; airSpeed = 0; landSpeed = 5; } }

class Bat extends Mammal{ Bat(){ kind = "\tBat"; airSpeed = 22; landSpeed = 1; flies = true; } @Override double FastTime(double distance) { return distance/airSpeed; } }

class Bird extends Animal{

Bird(){ flies = true; } @Override double FastTime(double distance) { return distance/airSpeed; } }

class Hawk extends Bird{ Hawk(){ kind = "\tHawk"; airSpeed = 40; landSpeed = 2; } }

class Penguin extends Bird{ Penguin(){ kind = "\tPenguin"; airSpeed = 0; landSpeed = 1.5; flies = false; } @Override double FastTime(double distance){ return distance/landSpeed; } } public class AnimalKingdom {

/** * @param args the command line arguments */ public static void main(String[] args) { Scanner sc = new Scanner(System.in); String animalName; Double distance; Animal animal1;//declaring the Animal class as a vairable do{ System.out.println("Enter an animal (or quit) : "); animalName = sc.nextLine(); if(!animalName.equals("quit")){ System.out.println("Enter a distance to travel : "); distance = sc.nextDouble(); } else { break; } switch (animalName) { case "cow": animal1 = new Cow(); break; case "bat": animal1 = new Bat(); break; case "hawk": animal1 = new Hawk(); break; case "penguin": animal1 = new Penguin(); break; default: animal1 = new Cow(); //we have to initialize with something , lets say Cow() break; } animal1.print(); double roundOffTime = Math.round(animal1.FastTime(distance) * 100.0) / 100.0; //for rounding off upto 2 decimal places System.out.println("Time for trip:\t" + roundOffTime + " hours"); }while(!animalName.equals("quit")); } }

This is my code and I get an error message when I try to enter in another animal.

This is the error message:

Exception in thread "main" java.util.InputMismatchException

at java.util.Scanner.throwFor(Scanner.java:864)

at java.util.Scanner.next(Scanner.java:1485)

at java.util.Scanner.nextDouble(Scanner.java:2413)

at animal.kingdom.AnimalKingdom.main(AnimalKingdom.java:115)

Help?

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!