In: Computer Science
JAVA Program
The program below the text reads in the pet type …dog, cat or other (only the words dog, cat, other) and the appointment amount. It is supplied as a beginning code to the assignment:
_______________________________________________________________ pet_lab package; import javax.swing.JOptionPane; public class pet_lab { public static void main (String [] args) { String pet, temp; double payment; pet = JOptionPane.showInputDialog (null, "Enter the pet type", "", JOptionPane.QUESTION_MESSAGE); temp = JOptionPane.showInputDialog (null, "Enter the payment for the appointment", "", JOptionPane.QUESTION_MESSAGE); payment = Double.parseDouble (temp); System.exit (0); } } _______________________________________________________________
You are to make the following changes:
Please paste both the java code, a screenshot of the same code, and a screenshot/pasting of the final answer. Using this to check my Java data. Thanks!
Thanks for the question. Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks! =========================================================================== package pet_lab ;
import javax.swing.JOptionPane; public class pet_lab { public static void main(String[] args) { String pet, temp, enterAgain; double payment; int dogsCount = 0, catCount = 0, otherCount = 0; double dogTotal = 0, catTotal = 0, otherTotal = 0; //Add a loop that reads data until the user wishes to stop do { pet = JOptionPane.showInputDialog(null, "Enter the pet type", "", JOptionPane.QUESTION_MESSAGE); temp = JOptionPane.showInputDialog(null, "Enter the payment for the appointment", "", JOptionPane.QUESTION_MESSAGE); payment = Double.parseDouble(temp); if (pet.equalsIgnoreCase("dog")) { dogsCount += 1; dogTotal += payment; } else if (pet.equalsIgnoreCase("cat")) { catCount += 1; catTotal += payment; } else { otherCount += 1; otherTotal += payment; } enterAgain = JOptionPane.showInputDialog(null, "Continue (yes or no): ", "", JOptionPane.QUESTION_MESSAGE); } while (enterAgain.equalsIgnoreCase("yes")); //Count and print the number of dogs, cats and other pets System.out.println("Dogs Count: " + dogsCount); System.out.println("Cats Count: " + catCount); System.out.println("Other Pet Count: " + otherCount); //Calculate and print the total paid for dogs, cats and other pets System.out.printf("Total Paid for Dogs : $%10.2f\n", dogTotal); System.out.printf("Total Paid for Cats : $%10.2f\n", catTotal); System.out.printf("Total Paid for Others: $%10.2f\n", otherTotal); //Calculate and print the total payment of all pets seen double total = dogTotal+catTotal+otherTotal; System.out.printf("Total Payment : $%10.2f\n", total); //Print which pet is seen the most, dogs, cats, or other if(dogsCount>catCount && dogsCount>otherCount){ System.out.println("Dogs were seen the most."); } else if(catCount>dogsCount && catCount>otherCount){ System.out.println("Cats were seen the most."); } else if(otherCount>dogsCount && otherCount>catCount){ System.out.println("Other were seen the most."); } else{ System.out.println("All pets were seen equally."); } } }
======================================================================