Creates pet objects from data read from the keyboard

Assignment Help JAVA Programming
Reference no: EM13165145

The assignment is to 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...

I have written this code to read the Pet objects from the keyboard and store them in a list but I cannot figure out how to display them alphabetically by pet name??

Here is the code so far:

import java.util.ArrayList;

import java.util.InputMismatchException;

import java.util.Scanner;

   public class Pet{

       private String name;

       private int age;

       private double weight;

       public Pet(){

}

       public Pet(String name, int age, double weight){

           setPet(name, age, weight);

       }

       public static void main(String[] args){

           ArrayList<Pet> list = new ArrayList<Pet>();

           Scanner console = new Scanner(System.in);

           System.out.print("Welocme.\nStart Typing the data of the pets.\n\n");

           String loopConditon = "y";

           while(loopConditon.equalsIgnoreCase("y")){

               String name = "";

               int age = 0;

               double weight = 0.0;

               try{

                   System.out.printf("Enter name of the pet : ");

                   name = console.next();

                   System.out.printf("Enter age of the pet : ");

                   age = console.nextInt();

                   System.out.printf("Enter wieght of the pet : ");

                   weight = console.nextDouble();

                   Pet mypet = new Pet(name,age,weight);

                   list.add(mypet);

                   System.out.print("Do you want to add data of another pet ? (Y/N) : ");

                   loopConditon = console.next();

                   System.out.println();

               }

               catch(IllegalArgumentException e){

                   System.out.println(e.getMessage());

                   System.out.println("Discarding the current pet details. Try Again");

               }

               catch(InputMismatchException e){

                   System.out.println("Input is not in correct format");

                   System.out.println("Discarding the current pet details. Try Again");

                   console.next();

               }

           }

           System.out.println("Input Terminated\n");

           System.out.println("The Pet Details are : \n");

           for(int i=0; i < list.size(); i++){

               System.out.println(list.get(i));

           }

       }

       public void setPet(String name, int age, double weight)

           {

           setName(name);

           setAge(age);

           setWeight(weight);

           }

       public void setName(String name)

{

           this.name = name;

   }

       public void setAge(int age){

           if(age <= 0)

               throw new IllegalArgumentException("Error : Illegal Age");

           this.age = age;

}

       public void setWeight(double weight){

           if(weight <= 0)

               throw new IllegalArgumentException("Error : Illegal weight");

           this.weight = weight;

}

       public String toString(){

           return String.format("Name : %s\nAge: %d\nWeight : %.2f\n",

                   name,age,weight);

       }

}

Reference no: EM13165145

Questions Cloud

Create dict letter frequencies that reads the file : createDictLetterFrequencies that reads the file and also takes a parameter n. This method should return a dictionary of dictionaries. The outer dictionary associates the previous n letters of a word with an inner dictionary, which indicates the frequ..
Calculate the overhead cost per account : What is the price if a markup of 35% on total cost is used to determine the price and calculate the overhead cost per account for the Money Market Checking.
Writing a java program that provides simple statistics : You will be writing a Java program that provides simple statistics about the input data. The method stubs are given below. This assignment will give you experience with methods
An infix to postfix method for my java program. : an infixToPostFix method for my java program. The method must work with stacks and it should take a string as parameter. The method should be able to convert to postfix mathematic expressions such as: (12 + 4) - 23(9-6)/12. Treat the parenthesis as l..
Creates pet objects from data read from the keyboard : The assignment is to write a program that creates Pet objects from data read from the keyboard. Store these objects into an instance of ArrayList.
Find the first occurrence, the last occurrence : If numbers in a list aren't unique and therefore the largest number could occur more than once would the algorithm find the first occurrence, the last occurance? Every occurance?
State the acid-catalyzed dehydration of heopentyl : The major product of the acid-catalyzed dehydration of heopentyl(2,2-dimethyl-1-propanol) has a different carbon skeleton than that of the starting alcohol
List advantages of asp : List advantages of ASP (SaaS) hosting services for ERP deployment. What is it about SaaS hosting services that make it very attractive for small to mid-sized corporations?
What amount of the payroll department costs : What is the net advantage or disadvantage of re-working the keyboards and what amount of the payroll department costs will be allocated to the molding department?

Reviews

Write a Review

JAVA Programming Questions & Answers

  Class should have a method that returns the average

Write a class named TestScores. The class constructor should accept an array of test scores as its arguments. The class should have a method that returns the average of the test scores.

  Design a class named large integers

Design a class named largeIntegers such that an object of this class can store an integer of any number of digits.

  Ask the user to enter a positive non-zero integer value.

Write a program which aske the user to Enter a positve non-zero integer value. if the value enterd zero or negative print as error message and end the program; otherwise, use the integer to call a method displayPattern(n) which must display the follo..

  Java program use breadth-first search closest broadcast

Write the java program which will use breadth-first search (which you implement as part of your program) to determine the closest broadcast vertex for each vertex in graph.

  Design and implement an applet called circles

Design and implement an applet called Circles that draws 50 circles of random diameter in random locations. If the diameter of a circle is less than a certain value, the circle is ?lled with the color yellow.

  Ask the user how many times you want to run

c the algorithm. 2. Then choose a word at random from the array of Strings as the target (Use the integer random number generator)

  A method with the signature public static void

A method with the signature public static void printDetails(City[] cities) that will iterate through the cities and printthe details of the city using the displayDetails(...) method.

  Write a program that assigns seats on an airplane

Prepare a program that teaches arithmetic to a young child. The program tests subtraction and addition. Write a program that assigns seats on an airplane.

  Shows the users name and program name

Java program, the program has a page that shows the users name and program name. a second jpanel that shows 4 buttons (circle square rectangle and oval) the problem i am having is that my program is not dropping where i click and the shapes are not s..

  Eclipse or netbeans environments

As recommendation, you can try to use Eclipse or NetBeans environments that are used also for other courses as: Introduction in Java programming and Intermediate programming.

  Write a version of sumpairs

Write a version of sumPairs  that sums each component of the pairs separately, returning a pair consisting of the sum of the first components and the sum of the second components. So basically [(3,1)(10,3)] would return (13,4).

  Design a program that extends the definition of the class

Design a program that extends the definition of the class JFrame to display a window on the screen. Name your class PropertyTax1, title your window "Calculation of Property Taxes," set the window's width to 400 pixels and height to 300 pixels, and te..

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd