Question

In: Computer Science

JAVA Program The program below the text reads in the pet type …dog, cat or other...

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:

  • Add a loop that reads data until the user wishes to stop
  • Count and print the number of dogs, cats and other pets
  • Calculate and print the total paid for dogs, cats and other pets
  • Calculate and print the total payment of all pets seen
  • Print which pet is seen the most, dogs, cats, or other

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!

Solutions

Expert Solution

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.");
        }
    }
}

======================================================================


Related Solutions

Pet [0] = "dog"  Pet[1] = "cat"    Pet[2] = "bird"   Pet[3] = "snake"   Pet[4] = "duck"   Pet[5]...
Pet [0] = "dog"  Pet[1] = "cat"    Pet[2] = "bird"   Pet[3] = "snake"   Pet[4] = "duck"   Pet[5] = "fish"   Pet[6} = "rabbit'   Pet[7] = "mouse"   Pet[8] = "pony" Pet[9] = "frog"                       SA 18.) Write a program segment to sort the given array in alphabetical order using the bubble sort method. Raptor Format (Prelude to Programing 6th ed.)
Write Java program Lab52.java which reads in a line of text from the user. The text...
Write Java program Lab52.java which reads in a line of text from the user. The text should be passed into the method: public static String[] divideText(String input) The "divideText" method returns an array of 2 Strings, one with the even number characters in the original string and one with the odd number characters from the original string. The program should then print out the returned strings.
Developing an Equation from Average Costs The America Dog and Cat Hotel is a pet hotel...
Developing an Equation from Average Costs The America Dog and Cat Hotel is a pet hotel located in Las Vegas. Assume that in March, when dog-days (occupancy) were at an annual low of 500, the average cost per dog-day was $14. In July, when dog-days were at a capacity level of 4,000, the average cost per dog-day was $7. (a) Develop an equation for monthly operating costs. (Let X = dog-days per month) Total cost = Answer + Answer *...
Out of 82 people surveyed about what kind of pet they have ( dog, cat or...
Out of 82 people surveyed about what kind of pet they have ( dog, cat or parrot ), 47 people people had a dog, 40 people had a cat and 11 people had a parrot, these are totals and are not mutually exclusive.. The 47 people with a dog may have also had other animals.    There were 5 people that had all 3 pets. There were 15 people who had a dog and a cat, not mutually exclusive. There were...
Write a JAVA program that reads a text file into RAM efficiently, takes a regular expression...
Write a JAVA program that reads a text file into RAM efficiently, takes a regular expression from the user, and then prints every line that matches the RE.
In java P4.6 Following Section 4.9 develop a program that reads text and displays the average...
In java P4.6 Following Section 4.9 develop a program that reads text and displays the average number of words in each sentence. Assume words are separated by spaces, and a sentence ends when a word ends in a period. Start small and just print the first word. Then print the first two words. Then print all words in the first sentence. Then print the number of words in the first sentence. Then print the number of words in the first...
Write a program in Java that reads an input text file (named: input.txt) that has several...
Write a program in Java that reads an input text file (named: input.txt) that has several lines of text and put those line in a data structure, make all lowercase letters to uppercase and all uppercase letters to lowercase and writes the new lines to a file (named: output.txt).
In java Q2. Write a program that reads grades of type double of eight students that...
In java Q2. Write a program that reads grades of type double of eight students that the user provides. The grades lie between 0 and 10. These grades should be written to a binary file and read from it. The program outputs the highest and lowest grades achieved by students on the screen. The file contains nothing but numbers of type double written to the file with writeDouble.
Using Java Write a program that reads a file of numbers of type int and outputs...
Using Java Write a program that reads a file of numbers of type int and outputs all of those numbers to another file, but without any duplicate numbers. You should assume that the input file is sorted from smallest to largest with one number on each line. After the program is run, the output file should contain all numbers that are in the original file, but no number should appear more than once. The numbers in the output file should...
Using Java Write a program that reads a file of numbers of type int and outputs...
Using Java Write a program that reads a file of numbers of type int and outputs all of those numbers to another file, but without any duplicate numbers. You should assume that the input file is sorted from smallest to largest with one number on each line. After the program is run, the output file should contain all numbers that are in the original file, but no number should appear more than once. The numbers in the output file should...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT