Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

How do you fix this program in Java? In the test program, if you enter n to go to rest of program doesn't work The

How do you fix this program in Java? In the test program, if you enter "n" to go to rest of program doesn't work

The program doesn't break to go on with rest of the program

ZooAnimals.java

/** * * Program creates Zoo Animals and calculates weekly food needed in pounds * * @author * */ abstract class ZooAnimals { private String sound; private String name; private int numberOfLegs; private double weight; /** * * Method defines Zoo animal characteristics. */ public ZooAnimals(String sound, String name, int numberOfLegs, double weight) { this.sound = sound; this.name = name; this.numberOfLegs = numberOfLegs; this.weight = weight; } /** * * Method gets sound of Zoo Animal. */ public String getSound() { return sound; } /** * * Method sets the sound of Zoo Animal. */ public void setSound(String sound) { this.sound = sound; } /** * * Method gets name of Zoo Animal. */ public String getName() { return name; } /** * * Method sets the name of Zoo Animal. */ public void setName(String name) { this.name = name; } /** * * Method gets Number of Legs of Zoo Animal. */ public int getNumberOfLegs() { return numberOfLegs; } /** * * Method sets Number of Legs of Zoo Animal */ public void setNumberOfLegs(int numberOfLegs) { this.numberOfLegs = numberOfLegs; } /** * * Method gets Zoo Animals weight. */ public double getWeight() { return weight; } /** * * Method sets Zoo Animals weight. */ public void setWeigth(double weight) { this.weight = weight; } /** * * Method calculates Weekly Food Needed in Pounds. */ public abstract double calculateWeeklyFoodNeededPounds(); @Override /** * Animal Characteristics listed of Zoo Animals. */ public String toString() { return "ZooAnimals [sound= " + sound + ", name=" + name + ",numberOfLegs=" + numberOfLegs + ",weight=" + weight + "]"; } } class Tiger extends ZooAnimals { private String tigerType; /** * * Method describes Tiger characteristics. */ public Tiger(String sound, String name, int numberOfLegs, double weight, String tigerType) { super (sound, name, numberOfLegs, weight); this.tigerType = tigerType; } /** * * Method gets Tiger's type. */ public String getTigerType() { return tigerType; } /** * * Method sets Tiger's type. */ public void setTigerType(String tigerType) { this.tigerType = tigerType; } @Override /** * Method calculates Weekly Food Needed in Pounds. */ public double calculateWeeklyFoodNeededPounds() { return getWeight() * 0.15; } @Override /** * Method describes characteristics of Tiger. */ public String toString() { return "Tiger [tigerType= " + tigerType + ",sound=" + getSound() + ",name=" + getName() + ",numberOfLegs=" + getNumberOfLegs() + ",weight=" + getWeight() + "]"; } } class Gorilla extends ZooAnimals { private String gorillaType; /** * * Method descirbes characteristics of Gorilla. */ public Gorilla (String sound, String name, int numberOfLegs, double weight, String gorillaType) { super(sound, name, numberOfLegs, weight); this.gorillaType = gorillaType; } /** * * Method gets Gorilla Type. */ public String getGorillaType() { return gorillaType; } /** * * Method sets Gorilla Type. */ public void setGorillaType(String gorillaType) { this.gorillaType = gorillaType; } @Override /** * Method calculates Weekly Food Needed in Pounds. */ public double calculateWeeklyFoodNeededPounds() { return 0.5 * super.getWeight(); } @Override /** * Method prints out Gorilla characteristics. */ public String toString() { return "Gorilla [gorillaType=" + gorillaType + ", sound=" + super.getSound() + ", name=" + super.getName() + ", numberOfLegs=" + super.getNumberOfLegs() + ",weight=" + super.getWeight() + "]"; } }

ZooAnimalTestProgram.java

import java.util.ArrayList; import java.util.Scanner; /** * This Program prompts the user in a loop to create Zoo Animals. * * @author * */ public class ZooAnimalTestProgram { public static void main (String[] args) { ArrayList animals = new ArrayList<>(); Scanner scanner = new Scanner(System.in); while (true) { System.out.println("What type of zoo animal, enter 'T' for Tiger or 'G' for Gorilla?"); String type = scanner.nextLine(); if (type.equals("n")) { break; } System.out.println(type + "'s name?"); String name = scanner.nextLine(); System.out.println(type + "'s sound?"); String sound = scanner.nextLine(); System.out.println(type + "'s number of legs?"); int numberOfLegs = scanner.nextInt(); scanner.nextLine(); System.out.println(type + "'s Weight?"); double weight = scanner.nextDouble(); scanner.nextLine(); if (type.equals("T")) { System.out.println("Tiger's type?"); String tigerType = scanner.nextLine(); animals.add(new Tiger(sound, name, numberOfLegs, weight, tigerType)); } else if (type.equals("G")) { System.out.println("Gorilla's type?"); String gorillaType = scanner.nextLine(); animals.add(new Gorilla(sound, name, numberOfLegs, weight, gorillaType)); } System.out.println("Enter another zoo animal, enter 'Y' for yes?"); type = scanner.nextLine(); } scanner.close(); for (ZooAnimals animal : animals) { System.out.println(animal.toString()); System.out.println("Weekly food needed: " + animal.calculateWeeklyFoodNeededPounds() + " pounds"); } System.out.println("Total cost: $" + calculateCost(animals)); } /** * * Method Calculates weekly food needed in pounds. * */ public static double calculateCost(ArrayList animals) { double totalCost = 0; for (ZooAnimals animal : animals) { if (animal instanceof Tiger) { totalCost += animal.calculateWeeklyFoodNeededPounds() * 4; } else if (animal instanceof Gorilla) { totalCost += animal.calculateWeeklyFoodNeededPounds() * 2; } } return totalCost; } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Concepts

Authors: David Kroenke, David Auer, Scott Vandenberg, Robert Yoder

10th Edition

0137916787, 978-0137916788

More Books

Students also viewed these Databases questions

Question

1 Describe the role of todays salesperson.

Answered: 1 week ago

Question

Explain how cultural differences affect business communication.

Answered: 1 week ago

Question

List and explain the goals of business communication.

Answered: 1 week ago