Java For the problem description below, pick out three classes that you would im
ID: 3575799 • Letter: J
Question
Java
For the problem description below, pick out three classes that you would implement. Do not include a Driver class or a main program, although that would be necessary if you were to write the whole program. Show the class names and data (only) in UML. Include aggregation relationship(s) that exist between classes. Do not implement any code.
You are writing a pet tracking program for an animal shelter. This program will have Shelters, which house animals. Each Shelter will have a name, which cannot be changed, location, and a list of cats and dogs to track. The location and list of cats and dogs may change frequently. Cats and dogs both have ID information, nickname, breed, and weight. Dogs also hava a field for favorite toy.
Explanation / Answer
package DataStructures.StacksQueues;
import java.util.LinkedList;
public class AnimalShelter {
public static void main(String[] args){
// Example
Shelter animalShelter = new Shelter();
// Create Animals
Animal cat1 = new Animal("cat", "Benny");
Animal cat2 = new Animal("cat", "Bucky");
Animal dog1 = new Animal("dog", "Snoop");
// Abandon Animals
animalShelter.abandon(cat1);
animalShelter.abandon(dog1);
animalShelter.abandon(cat2);
// Adopt
Animal adopt1 = animalShelter.getCat();
adopt1.print();
Animal adopt2 = animalShelter.getOldest();
adopt2.print();
Animal adopt3 = animalShelter.getDog();
adopt3.print();
Animal adopt4 = animalShelter.getCat();
adopt4.print();
}
public static class Animal {
// Animal Qualities
private String animalName;
private String animalType;
private int animalAge;
// Default Constructor
Animal(String type, String name){
// Sanitize the type
type = type.toLowerCase().replaceAll(" ", "");
// Initialize Animal
animalType = type;
// Initialize Animal Name
animalName = name;
// Initialize Age
animalAge = 0;
}
// Set Animal Age Function
public void setAnimalAge(int age){
// Set Value
animalAge = age;
}
// Get the Animal Type
public String getAnimalType(){
// Return Animal Type
return animalType;
}
// Get the Animal's Age
public int getAnimalAge(){
// Return the Age
return animalAge;
}
// Print Animal Details
public void print(){
if(!animalName.equals("null") || !animalType.equals("null")) {
// Output
System.out.println("Name: " + animalName);
System.out.println("Type: " + animalType);
System.out.println("Shelter Age: " + Integer.toString(animalAge));
System.out.println(" ");
}
}
}
public static class Shelter {
// Shelter Sections
LinkedList<Animal> catShelter;
LinkedList<Animal> dogShelter;
// Time Counter
int time;
// Default Constructor
Shelter(){
// Initialize Linked Lists
catShelter = new LinkedList<>();
dogShelter = new LinkedList<>();
// Initialize the Time
time = 1;
}
// Queue Animal
public void abandon(Animal abandonedAnimal){
// If the Animal is a Cat, Add it to the Cat Shelter
if(abandonedAnimal.getAnimalType().equals("cat")){
// Set the Time for the Abandoned Animal
abandonedAnimal.setAnimalAge(time);
// Increment Time Away
time++;
// Add to Cat Shelter
catShelter.addLast(abandonedAnimal);
}
// If the Animal is a Dog, Add it to the Dog Shelter
if(abandonedAnimal.getAnimalType().equals("dog")){
// Set the Time for the Abandoned Animal
abandonedAnimal.setAnimalAge(time);
// Increment Time Away
time++;
// Add to Cat Shelter
dogShelter.addLast(abandonedAnimal);
}
}
// Get Cat Method
public Animal getCat(){
// Check the Cat Shelter
if(catShelter.isEmpty()) return new Animal("null", "null");
// Otherwise Get the First Cat and Return In
else{
// First Cat
Animal firstCat = catShelter.getFirst();
// Remove the Cat that was Just Accessed
catShelter.removeFirst();
// Return the Animal
return firstCat;
}
}
// Get Dog Method
public Animal getDog(){
// Check the Dog Shelter
if(dogShelter.isEmpty()) return new Animal("null", "null");
// Otherwise Get the First Dog and Return In
else{
// First Dog
Animal firstDog = dogShelter.getFirst();
// Remove the Dog that was Just Accessed
dogShelter.removeFirst();
// Return the Animal
return firstDog;
}
}
// Get Oldest Animal
public Animal getOldest(){
// Check the Dog and Cat Shelters
if(dogShelter.isEmpty() && catShelter.isEmpty()) return new Animal("null", "null");
// Otherwise Get the First Dog and Return In
else{
// First Dog
Animal firstDog = dogShelter.getFirst();
// First Cat
Animal firstCat = catShelter.getFirst();
// Check Which Animal is Older
if(firstDog.getAnimalAge() < firstCat.getAnimalAge()){
// Remove the First Dog
dogShelter.removeFirst();
// Return the Dog
return firstDog;
}
else{
// Remove the First Cat
catShelter.removeFirst();
// Return the Cat
return firstCat;
}
}
}
}
}
OR
--------------------------------------------------
--------------------------------------------------
AnimalShelter.java
import java.util.LinkedList;
import java.util.NoSuchElementException;
public class AnimalShelter {
public enum AnimalType{
DOG, CAT
}
private int animalId;
LinkedList<Integer> cats;
LinkedList<Integer> dogs;
public AnimalShelter(){
cats = new LinkedList<Integer>();
dogs = new LinkedList<Integer>();
}
public void enqueue(AnimalType type){
switch(type){
case DOG:
dogs.add(animalId);
animalId++;
break;
case CAT:
cats.add(animalId);
animalId++;
break;
}
}
public Integer dequeueCat(){
if(cats.isEmpty()) throw new NoSuchElementException("There are no cats in the animal shelter.");
return cats.pop();
}
public Integer dequeueDog(){
if(dogs.isEmpty()) throw new NoSuchElementException("There are no dogs in the animal shelter");
return dogs.pop();
}
public Integer dequeueAny(){
if(dogs.isEmpty() && cats.isEmpty()) throw new NoSuchElementException("There are no animals in the animal shelter.");
if(dogs.isEmpty()){
return cats.pop();
}
else if(cats.isEmpty()){
return dogs.pop();
} else{
if(cats.peek() < dogs.peek()){
return cats.pop();
}
else{
return dogs.pop();
}
}
}
}
Main.java
package DataStructures.StacksQueues;
import java.util.LinkedList;
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.