Question

In: Computer Science

Use JAVA BASICS  classes, aggregation and manipulating arrays of objects. I DO NOT WANT THE SAME SOLUTION...

Use JAVA BASICS  classes, aggregation and manipulating arrays of objects.

I DO NOT WANT THE SAME SOLUTION WHICH ALREADY EXISTS ON CHEG.

DO NO USE ARRAY LIST

Scenario:

A dog shelter would like a simple system to keep track of all the dogs that pass through the facility.

The system must record for each dog:

dogId (int) - must be unique

name (string)

age (double) - cannot be less than 0 or more than 25

breed (string)

sex (char) – m for male, f for female

foundHome (bool)    - true means the dogs has been places in a home false otherwise.

You must create an object oriented solution with a text based menu as summarized on the next page. The system must check that valid data is entered. For example, the menu has four items so only 1 to 5 must be allowed as input from the menu.

Summary of Operations

System Menu:

  1. Add dog
  2. View all dogs
  3. View all available dogs
  4. Update dog home status
  5. exit

Overview:

Add Dog:

When a dog is added to the system, you must check that the dogId is not already used in the system. All new dogs have no home as yet (foundHome = false).

View all Dogs:

This menu option shows all dogs in the system. This includes dogs that have a home and those that do not.

View all available dogs:

Shows all dogs in the system, that have no homes as yet.

View dog:

Asks the user for a dogId and then displays the dog information if found and “There is no dog with that id..” if it is not found.

Update dog home status:

Asks the user for a dogId. If a dog with that id is found, the “foundHome” status is changed to true and the dog information is to be displayed. If the dog is not found the message “There is no dog with that id..” should be displayed.

Solutions

Expert Solution

Dog.java

public class Dog {
   //instance variables
   private int dogId;
   private String name;
   private double age;
   private String breed;
   private char sex;
   private boolean foundHome;

   //default constructor
   public Dog()
   {
       this.dogId = 0;
       this.name = "";
       this.age = 0;
       this.breed = "";
       this.sex = ' ';
       this.foundHome = false;
   }

   //constructor with parameters
   public Dog(int dogId, String name, double age, String breed, char sex, boolean foundHome)
   {
       this.dogId = dogId;
       this.name = name;
       this.age = age;
       this.breed = breed;
       this.sex = sex;
       this.foundHome = foundHome;
   }

   //getters
   //method that gets the dogId
   public int getDogId()
   {
       return this.dogId;
   }

   //method that gets the dog name
   public String getName()
   {
       return this.name;
   }

   //method that gets the dog age
   public double getAge()
   {
       return this.age;
   }

   //method that gets the dog breed
   public String getBreed()
   {
       return this.breed;
   }

   //method that gets the dog sex
   public char getSex()
   {
       return this.sex;
   }

   //method that gets the home of the dog
   public boolean getFoundHome()
   {
       return this.foundHome;
   }

   //setters
   //method that sets the dogId
   public void setDogId(int dogId)
   {
       this.dogId = dogId;
   }

   //method that sets the dog name
   public void setName(String name)
   {
       this.name = name;
   }

   //method that sets the dog age
   public void setAge(double age)
   {
       this.age = age;
   }

   //method that sets the dog breed
   public void setBreed(String breed)
   {
       this.breed = breed;
   }

   //method that sets the dog sex
   public void setSex(char sex)
   {
       this.sex = sex;
   }

   //method that sets the home of the dog
   public void setFoundHome(boolean foundHome)
   {
       this.foundHome = foundHome;
   }

   //display the dog
   public String toString()
   {
       if(getFoundHome())
           return "ID: " + getDogId() + " Name: " + getName() + " Age: " + getAge() + " Breed: " + getBreed()
           + " Sex: " + getSex() + " Found Home: Yes";
       else
           return "ID: " + getDogId() + " Name: " + getName() + " Age: " + getAge() + " Breed: " + getBreed()
           + " Sex: " + getSex() + " Found Home: No";
   }
}

DogShelter.java


public class DogShelter extends Dog {
   //instance variable
   private Dog[] dogs = new Dog[100];
   private static int numberOfDogs = 0;
  
   //method that adds a dog to the system
   public void addDog(int dogId, String name, double age, String breed, char sex)
   {
       boolean dogExists = false;
       //check if the dog id exists
       for(int i = 0; i < numberOfDogs; i++)
       {
           if(dogs[i].getDogId() == dogId)
               dogExists = true;
       }
       //add the dog if id does not exist
       if(!dogExists)
       {
           dogs[numberOfDogs] = new Dog(dogId, name, age, breed, sex, false);
           System.out.println("Dog added successfully");
           numberOfDogs++;
       }
       //else display error message
       else
           System.out.println("DogId already exists");
   }
  
   //method that displays all the dogs
   public void viewAllDogs()
   {
       for(int i = 0; i < numberOfDogs; i++)
       {
           System.out.println(dogs[i].toString());
       }
   }
  
   //method that displays the dogs that don't have home yet
   public void viewAllAvailableDogs()
   {
       //run a loop and display all the dogs
       for(int i = 0; i < numberOfDogs; i++)
       {
           if(dogs[i].getFoundHome() == false)
               System.out.println(dogs[i].toString());
       }
   }
  
   //method that shows a particular dog
   public void viewDog(int dogId)
   {
       int index = -1;
       //check if dog exists with given id
       for(int i = 0; i < numberOfDogs; i++)
       {
           //if exists store the index
           if(dogs[i].getDogId() == dogId)
               index = i;
       }
       //if index is -1 display error message
       if(index == -1)
           System.out.println("There is no dog with that id..");
       //else display the dog
       else
           System.out.println(dogs[index].toString());
   }
  
   //method that update the dog home status
   public void updateDogHomeStatus(int dogId)
   {
       int index = -1;
       //check if dog exists with given id
       for(int i = 0; i < numberOfDogs; i++)
       {
           //if exists store the index
           if(dogs[i].getDogId() == dogId)
               index = i;
       }
       //if index is -1 display error message
       if(index == -1)
       {
           System.out.println("There is no dog with that id..");
       }
       //else update and display the dog
       else
       {
           dogs[index].setFoundHome(true);
           System.out.println("\nUpdated dog info:");
           viewDog(dogs[index].getDogId());
       }
   }
}

DogShelterDemo.java

import java.util.Scanner;

public class DogShelterDemo {

   public static void main(String[] args) {
       Scanner input = new Scanner(System.in);
       DogShelter dogs = new DogShelter();
       //variables
       int dogId;
       String name;
       double age;
       String breed;
       char sex;
       int userChoice;
       //run a loop until the user wishes to exit
       do
       {
           //display the menu
           System.out.println("1. Add dog");
           System.out.println("2. View all dogs");
           System.out.println("3. View all available dogs");
           System.out.println("4. Update dog home status");
           System.out.println("5. Exit");
           //prompt and read the user choice
           System.out.print("Enter your choice: ");
           userChoice = input.nextInt();
           //run a switch case based on user choice
           switch(userChoice)
           {
           case 1:
               //read the dog details from the user
               System.out.print("Enter the dog id: ");
               dogId = input.nextInt();
               System.out.print("Enter the name: ");
               input.nextLine();
               name = input.nextLine();
               System.out.print("Enter the age: ");
               age = input.nextInt();
               while(age < 1 || age > 25)
               {
                   System.out.println("Age cannot be less than 1 and greater than 25. Try again: ");
                   age = input.nextInt();
               }
               System.out.print("Enter the breed: ");
               input.nextLine();
               breed = input.nextLine();
               System.out.print("Enter the sex: ");
               sex = input.nextLine().charAt(0);
               dogs.addDog(dogId, name, age, breed, sex); //add the dog
               break;
           case 2:
               //display all the dogs
               System.out.println("\nInfo of all dogs:");
               dogs.viewAllDogs();
               break;
           case 3:
               //display all available dogs
               System.out.println("\nInfo of all dogs available:");
               dogs.viewAllAvailableDogs();
               break;
           case 4:
               //prompt and read the dogId
               System.out.print("Enter the dog id: ");
               dogId = input.nextInt();
               //update the home status
               dogs.updateDogHomeStatus(dogId);
               break;
           case 5:
               System.out.println("Thank you for using our system.");
               break;
           default:
               System.out.println("Invalid input.");
           }
           System.out.println();
       }while(userChoice != 5);
       input.close();
   }
}

Output:

Code Screenshots:

Let me know if you have any concerns with the above solution


Related Solutions

This is Python programming Focus 1. Classes and Objects 2. Creating objects 3. Manipulating objects This...
This is Python programming Focus 1. Classes and Objects 2. Creating objects 3. Manipulating objects This lab maps to learning the following objectives: Write a working program that creates a Class, Instances of Objects from that Class, and Functions that use Objects as parameters. For this portion of the lab, you will create a new program for your Professor. Create a class named Student that holds the following data about a student: 1. Name 2. Student ID number 3. GPA...
How do objects enhance Java? How do objects relate to classes and methods?
How do objects enhance Java? How do objects relate to classes and methods?
Using JAVA: This assignment is about aggregation and class collaboration. You will create several Classes that...
Using JAVA: This assignment is about aggregation and class collaboration. You will create several Classes that will be part of an overall class named InstrumentDisplay. The classes are FuelGage, Odometer, MilesSinceLastGas, and CruisingRange. The FuelGage should assume a 15 gallon tank of gasoline and an average consumption of 1 gallon every 28 miles. It should increment in 1 gallon steps when you "add gas to the tank". It should decrement by 1 every 28 miles. It should display its current...
For this question we will be using arrays and classes in Java to compute the min,...
For this question we will be using arrays and classes in Java to compute the min, max, and average value of items for a given array of integers. Complete the following using the base template provided below: -Create methods for min, max, and average and call them from main to print out their values. -Add a method to determine the median (http://www.mathsisfun.com/median.html) and print that value. This method is currently not in the template, so you will need to add...
A. What are Objects? B. How do Objects differ from Classes? C. Where are Objects stored...
A. What are Objects? B. How do Objects differ from Classes? C. Where are Objects stored in Memory? D. Why do you not need to declare when you are finished using an Object in Java? E. Can you edits the contents of a String? View keyboard shortcuts EditViewInsertFormatToolsTable 12pt Paragraph
Part 1 – Classes and objects Create a new Java project called usernamePart1 in NetBeans. In...
Part 1 – Classes and objects Create a new Java project called usernamePart1 in NetBeans. In my case the project would be called rghanbarPart1. Select the option to create a main method. Create a new class called Vehicle. In the Vehicle class write the code for: • Instance variables that store the vehicle’s make, model, colour, and fuel type • A default constructor, and a second constructor that initialises all the instance variables • Accessor (getters) and mutator (setters) methods...
HELLO CAN YOU PLEASE DO THIS JAVA PROGRAM WITH THE DIFFERNT CLASSES LISTED BELOW. I WILL...
HELLO CAN YOU PLEASE DO THIS JAVA PROGRAM WITH THE DIFFERNT CLASSES LISTED BELOW. I WILL LEAVE AWESOME RATING. THANK YOU IN ADVANCE. The code needed for this assignment has to somehow implement a stack interface (including a vector stack, array stack, and a linked stack). The classes needed are the Game class, the disk class, the driver, and the stack interface (including arraystack, linkedstack, and vectorstack) QUESTION Suppose you are designing a game called King of the Stacks. The...
Write a Java Program using the concept of objects and classes. Make sure you have two...
Write a Java Program using the concept of objects and classes. Make sure you have two files Employee.java and EmployeeRunner.java. Use the template below for ease. (Please provide detailed explanation) Class Employee Class Variables: Name Work hour per week Hourly payment Class Methods: - Get/Sets - generateAnnualSalary(int:Duration of employment)               annual salary = Hourly payment * Work hours per week * 50 If the employee is working for more than 5 years, 10% added bonus             -void displayAnnualSalary ( )...
Show some examples of a few Parameters and Objects problems in java language and the solution...
Show some examples of a few Parameters and Objects problems in java language and the solution code. Need more practice as a new learner
Objectives: use Scite Use recursion to solve a problem Create classes to model objects Problem :...
Objectives: use Scite Use recursion to solve a problem Create classes to model objects Problem : The Rectangle class (Filename: TestRectangle.java) Design a class named Rectangle to represent a rectangle. The class contains: Two double data fields named width and height that specify the width and height of the rectangle. The default values are 1 for both width and height. A no-arg constructor that creates a default rectangle. A constructor that creates a rectangle with the specified width and height....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT