Java: Create A Program That Instantiates At Least One Pet Object Using Serial Implementation

  1. Home
  2. Homework Library
  3. Computer Science
  4. Java Programming
  5. Java: Create A Program That Instantiates At Least One Pet Object Using Serial Implementation
Subject Computer Science Java Programming

Question

Create a program that instantiates at least one Pet object.
Every time the program is opened, the status of the Pet is assessed and output to the console.
The user should be given options in handling their Pet (i.e. naming it, feeding it, grooming it, etc.).
You will be graded on the following:
- does the pet object and its properties persist on subsequent loads?
- do the actions the user takes change the pet's properties?
- is your program user friendly (clear instruction, error handling, etc.)?
Java allows you to serialize objects fairly easily as long as you plan ahead.

Solution Preview

This material may consist of step-by-step explanations on how to solve a problem or examples of proper writing, including the use of citations, references, bibliographies, and formatting. This material is made available for the sole purpose of studying and learning - misuse is strictly forbidden.

public class Driver {

    public static void main(String[] args) {
       Pet pet = readFile();
       Scanner scanner = new Scanner(System.in);
       if (pet == null) {
            System.out.println("Create your new pet");
            System.out.print("Enter its name: ");
            String name = scanner.nextLine();
            pet = new Pet(name);
       }
       System.out.printf("One day of the virtual world is %.2f seconds in the real world%n",
                (double)Pet.PERIOD / 1000.0);
       String[] task = pet.getGrommingTasks();
       System.out.println("Current status of your "
                + pet.getName());
       System.out.println(pet.toString());
       int index;
       do {
            System.out.println("What do you want to do to your "
                   + pet.getName());
            for (int i = 0; i < task.length; i++) {
                System.out.printf("%d %s%n", i, task[i]);
            }
            System.out.printf("%d %s%n", task.length, "Nothing");
            System.out.print("Enter the index: ");
            try {
                index = Integer.parseInt(scanner.nextLine());
                if (index != task.length) {
                   pet.gromming(index);
                }
            } catch (Exception e) {
                System.err.println("Invalid input");
            }
            
            System.out.println("Current status of your "
                   + pet.getName());
            System.out.println(pet.toString());
            
            System.out.println("Do you want to continue? "
                   + " yes : 1 ; no : other number ");
            try {
                index = Integer.parseInt(scanner.nextLine());
            } catch (Exception e) {
                index = 0;
            }
       } while (index == 1);
       saveFile(pet);
    }...

This is only a preview of the solution. Please use the purchase button to see the entire solution

$30.00

or free if you
register a new account!

Assisting Tutor

Related Homework Solutions

Get help from a qualified tutor
Live Chats