Question

I must create list of pets that includes cats and dogs and I must use inheritance for this. Pet class wil include name variable, Dog class will include name and weight variables and Cat class will include name and coatColor variables.

And I must create a Test class which includes that menu below.

  1. Add dog
  2. Add cat
  3. Remove dog
  4. Remove cat
  5. List dogs
  6. List cats
  7. List all pets
  8. Show min, max and average weight of dogs
  9. Quit

I've written every class but I couldn't complete them I need your help.

My classes are below. (If I did something wrong please edit it and tell me, I think I did something wrong)

Pet Class:

public class Pet{
    private static String name;

    protected String getName(){
        return name;
    }

    protected void setName(String newName){
        name = newName;
    }

    public Pet(String petName) {
        name = petName;
    }
}

Dog Class:

public class Dog extends Pet{       
    private Double weight;      
    protected double getWeight(){
        return weight;
    }

    protected void setWeight(double newWeight){
        weight = newWeight;
    }

    public Dog(double dogWeight, String petName){
        super(petName);
        weight = dogWeight;

    }
}

Cat Class:

public class Cat extends Pet{       
    private String coatColor;       
    protected String getWeight(){
        return coatColor;
    }

    protected void setColor(String newColor){
        coatColor = newColor;
    }

    public Cat(String petName, String coatColor){
        super(petName);
        this.coatColor = coatColor;
    }
}

PetTest Class:

import java.util.*;

public class PetTest
{
    //i made it static to make all method to be able to see it.
    static ArrayList<Pet> mainList = new ArrayList<Pet>();
    static Iterator<Pet> mainIter = mainList.iterator();
    static Scanner keyboard = new Scanner(System.in);

    static Pet cat2342 = new Cat("mirnav", "beyaz");
    public static void listDogs()
    {
        while(mainIter.hasNext())
        {
            for(Pet dog: mainList)
            {
                System.out.println(dog);
            }//end of for loop

        }//end of while loop
    }//end of listDogs method

    public static void listCats()
    {
        while(mainIter.hasNext())
        {
            for(Pet cat: mainList)
            {
                System.out.println(cat);
            }//end of for loop

        }//end of while loop
    }//end of listCats method

    public static void addDog(String dogName,Double dogWeight)
    {
        Pet dog = new Dog(dogName, dogWeight);
        mainList.add(dog);
    }//end of addDog method

    public static void addCat(String catName, String furColor)
    {
        Pet cat = new Cat(catName, furColor);
        mainList.add(cat);
    }//end of addCat method

    public static void removeDog(String dogName)
    {
        while (mainIter.hasNext())
        {
            for(Pet pet : mainList)
            {
                if(pet.getName().equals(dogName))
                {
                    mainIter.remove();
                }//end of if statement
            }//end of for loop


        }//end of while loop

    }//end of removeDog method


    public static void removeCat(String catName)
    {
        while (mainIter.hasNext())
        {
            for(Pet pet : mainList)
            {
                if(pet.getName().equals(catName))
                {
                    mainIter.remove();
                }//end of if statement
            }//end of for loop


        }//end of while loop

    }//end of removeCat method


    public static void main(String[] args)
    {

        System.out.println("1. Add dog ");
        System.out.println("2. Add cat");
        System.out.println("3. Remove dog");
        System.out.println("4. Remove cat");
        System.out.println("5. List dogs");
        System.out.println("6. List cats");
        System.out.println("7. List all pets");
        System.out.println("8. Show min, max and average weight of dogs");
        System.out.println("0. Quit");
        int action = keyboard.nextInt();


        Scanner parameter1 = new Scanner(System.in);
        Scanner parameter2 = new Scanner(System.in);

        while(action != 0)
        {
        switch(action)
        {
        case 1: 
            System.out.println("Type in the name of the dog that you want to add.");
            String dogName = parameter1.next();
            System.out.println("Type in the weight of the dog that you want to add.");
            Double dogWeight = parameter2.nextDouble();
            addDog(dogName,dogWeight);
            showMenu();


        case 2:
            System.out.println("Type in the name of the cat that you want to add.");
            String catName = parameter1.next();
            System.out.println("Type in the color of the cat that you want to add.");
            String furColor = parameter2.next();
            addCat(catName,furColor);



        case 3:
            System.out.println("Type in the name of dog that you want to remove.");
            String dogToRemove = parameter1.next();
            removeDog(dogToRemove);



        case 4:
            System.out.println("Type in the name of dog that you want to remove.");
            String catToRemove = parameter1.next();
            removeDog(catToRemove);



        case 5:
            listDogs();

        case 6:
            listCats();




        }//end of switch statement

        }//end of while loop

    }//end of main method



}//end of the class
Was it helpful?

Solution

What you did is mostly right. Though take care of some things :

  1. You have an error in your cat class (for coatColor getter) : wrong name
  2. Your method addDog actually adds a cat, which is confusing

Now, the fun stuff (the tools) you need :

  • You'll need one method for each action (add cat, add dog, etc)
  • You'll need to add the pets you create in the mainList : mainList.add(...)
  • You'd rather use a switch/case syntax to call your methods
  • To remove a cat or a dog, you may need to indicate which one you want to remove (or maybe only the last on added)
  • To list the dogs or the cats, you"ll need to iterate over your mainList and keep only those with a good type : use the instanceof operator
  • To shown min/max and average weight for dog, you'll need to iterate as well over your list, and keep a count of all those.

Anyway, I could be more specific on each point but that would not be fun for you, as you need to look for solutions by yourself. There are better ways to do that (like using a framework like Guava for example), but that's the next step for you !

OTHER TIPS

Make Cat & Dog classes inherit from the Pet class like the following:

public class Cat extends Pet {...}
public class Dog extends Pet {...}

Also the name variable in the Pet class, can be set to be protected if you want the child classes to use it without the getter/setter methods. (Depends on your requirements)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top