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

  Write a java applet for grade of gas in costco gas station

Write a java applet (not a java application program) for costco gas station. The applet will first ask you whether you are a costco customer, then the grade of gas you want to use.

  Can linear search algorithm be encoded using recursion

Determine the average amount of time people spend waiting for tables and provide examples from the "real world" of unsorted lists, sorted lists, indexed lists, lists that permit duplicate elements, and lists that do not permit duplicate elements

  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..

  Simulate some people catching fish in a lake in java program

In this project, you will simulate some people catching fish in a lake. The purpose of the assignment is to get used to using Arrays as well as getting more experience in having objects interact together.

  Create three classes for a customer

a travel manager and have a job of buying a travel package for a customer. You must create three classes for this programming challenge: Customer, Ticket, and TravelManagerDemo.

  Write java program to store employee id number

Use employee data file called employees.txt should comprise at least 5 employee records. Each record stores employee ID number (six digits) last name, first name, middle inital,gender(m or f).

  Public float usefulload()

public float usefulLoad() // this is the grossWeight - emptyWeight public float usefulLoadWithFuel( in gallons ) // this is the useful load - gallons x 6. The programmer user will pass in the number of gallons on board.

  Create an abstract class named element

Part 1: Create an abstract class named Element that holds properties of elements, including their symbol, atomic number, and atomic weight. Include a constructor that requires values for all three properties and a get method for each value.

  Create a gui interface

Create GUI Interface in java programing with these function: Sort by last name and print all employees info, Sort by job title and print all employees info, Sort by weekly salary and print all employees info, search by job title and print that emp..

  An api for a library that provides for all these operations

Quaternions can be represented with four (4) real numbers (a,b,c,d). They can be added, subtracted, multiplied and divided. You can multiply a quaternion by a scalar (which produces a quaternion as a result)

  Create and implement class called date to store month

Create and Implement the class called Date which has data members to store month (as a number), day, year, and name of month.

  Enterprise java beans (ejb) in software development

Enterprise Java Beans (EJB) in software development, EJB technology, EJB application, Stateless Session Beans (SLSB), Stateful Session Beans (SFSB), Message Driven Bean (MDB), Entity Bean

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