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

  Technical community blog

Write a blog article for a coding and technical community blog.

  It should have an if statement

The following are hints given: It should have an if statement. This add method determines what number greater than or less than the other number, than it adds the positive or negative. I think this should be a private method.

  Java''s type int has limit onhow large aninteger it can store

Java's type int has a limit on how large an integer it can store. This limit can be circumvented by representing an integer as an array of digits. Write an interactive program that adds two integers of up to 50 digits each.

  Write program in java-calculate and display mortgage payment

Write the program in Java (with a graphical user interface) and have it calculate and display the mortgage payment amount from user input of the amount of the mortgage.

  Object-oriented gui drawing editor

A simple object-oriented GUI drawing editor that allows a user to create, move and erase rectangles, squares, circles and lines in an interactive graphics. How can I draw move erase rectangles, squares, circles and lines in GUI/java.

  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 method called writenums

Write a method called writeNums that takes an integer n as a parameter and prints to the console the first n integers starting with 1 in sequential order, separated by commas. For example, consider the

  Create a project in eclipse

Create a new project

  Bilinear and nearest neighbour algorithm in java

How to do bilinear and nearest neighbour algorithm in java. Show how to eliminate change in pixel aspect ratio in java.

  Implement a shopping cart class with user interface

project will be to implement a shopping cart class with user interface (UI) that contains main() in Net Beans. The UI class will be used to perform user input/output and to invoke the appropriate methods of shopping cart class. When your program star..

  Sorted list adt and the binary search tree adt

Explain the differences between our specifications of the Sorted List ADT and the Binary Search Tree ADT.

  Write java program using array list object

Write a java program (using eclipse) using ArrayList object to allow the professor to enter student's name, his or her's four test scores.

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