Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program that creates Pet objects from data read from the keyboard. Store

ID: 3767964 • Letter: W

Question

Write a program that creates Pet objects from data read from the keyboard. Store these objects into an instance of ArrayList. Then sort the Pet objects into alphabetic order by pet name, and finally display the data in the sorted Pet objects on the screen. Your program should check for erroneous input, display an appropriate error message, and repeat the input process until the user enters all data correctly.  Use a do-while loop to allow the user to enter an arbitrary number of Pet records. Use a selection sort algorithm to put the Pet objects in alphabetical order. Submit your Java source code file.

Explanation / Answer

import java.util.ArrayList;
import java.util.Iterator;
import java.util.Scanner;

/**
* @author Srinivas Palli
*
*/
public class Pet {
   private String name;
   private int age; // in years
   private double weight; // in pounds

   /**
   * This main is just a demonstration program.
   */
   public static void main(String[] args) {
       Scanner scanner = new Scanner(System.in);
       ArrayList<Pet> arrayList = new ArrayList<Pet>();

       do {
           Pet myDog = new Pet();
           System.out.print("Enter Pet Name:");
           String petName = scanner.next();

           System.out.print("Enter Pet Age:");
           int petAge = scanner.nextInt();

           System.out.print("Enter Pet Weight:");
           double petWeight = scanner.nextDouble();

           myDog.set(petName, petAge, petWeight);
           arrayList.add(myDog);
           System.out.print("If you want to continue(y/n):");
           String ch = scanner.next();
           if (ch.equalsIgnoreCase("n")) {
               break;
           }

       } while (true);

       System.out.println("Sorted in Ascending Order :");
       arrayList = new Pet().nameByOrder(arrayList);
       Iterator<Pet> it = arrayList.iterator();
       while (it.hasNext()) {
           Pet p = it.next();
           p.writeOutput();

       }

   }

   public void writeOutput() {
       System.out.println("Name: " + name);
       System.out.println("Age: " + age + " years");
       System.out.println("Weight: " + weight + " pounds");
   }

   public void set(String newName) {
       name = newName;
       // age and weight are unchanged.
   }

   public void set(int newAge) {
       if (newAge <= 0) {
           System.out.println("Error: illegal age.");
           System.exit(0);
       } else
           age = newAge;
       // name and weight are unchanged.
   }

   public void set(double newWeight) {
       if (newWeight <= 0) {
           System.out.println("Error: illegal weight.");
           System.exit(0);
       } else
           weight = newWeight;
       // name and age are unchanged.
   }

   public void set(String newName, int newAge, double newWeight) {
       name = newName;
       if ((newAge <= 0) || (newWeight <= 0)) {
           System.out.println("Error: illegal age or weight.");
           System.exit(0);
       } else {
           age = newAge;
           weight = newWeight;
       }
   }

   public String getName() {
       return name;
   }

   public int getAge() {
       return age;
   }

   public double getWeight() {
       return weight;
   }

   public ArrayList<Pet> nameByOrder(ArrayList<Pet> totalListOfItems) {
       int max, i, j;
       Pet temp;
       for (i = 0; i < totalListOfItems.size() - 1; i++) {
           max = i;
           for (j = i + 1; j < totalListOfItems.size(); j++) {
               if (totalListOfItems.get(max).getName()
                       .compareTo(totalListOfItems.get(j).getName()) > 0)
                   max = j;

           }
           temp = totalListOfItems.get(i);
           totalListOfItems.set(i, totalListOfItems.get(max));
           totalListOfItems.set(max, temp);
       }

       return totalListOfItems;
   }

}

OUTPUT:

Enter Pet Name:Zert
Enter Pet Age:3
Enter Pet Weight3.4
If you want to continue(y/n):y
Enter Pet Name:Xert
Enter Pet Age:4
Enter Pet Weight4.5
If you want to continue(y/n):y
Enter Pet Name:Sert
Enter Pet Age:6
Enter Pet Weight6.7
If you want to continue(y/n):n
Sorted in Ascending Order :

Name: Sert
Age: 6 years
Weight: 6.7 pounds

Name: Xert
Age: 4 years
Weight: 4.5 pounds

Name: Zert
Age: 3 years
Weight: 3.4 pounds

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote