【问题标题】:Generic ArrayList and a menu which adds items automatically to Array通用 ArrayList 和一个菜单,可自动将项目添加到 Array
【发布时间】:2014-06-30 04:39:02
【问题描述】:

我必须创建包含猫和狗的宠物列表,并且我必须为此使用继承。 Pet 类将包含 name 变量,Dog 类将包含 name 和 weight 变量,Cat 类将包含 name 和 coatColor 变量。

我必须创建一个包含下面菜单的测试类。

  1. 添加狗
  2. 添加猫
  3. 移除狗
  4. 移除猫
  5. 列出狗
  6. 列出猫
  7. 列出所有宠物
  8. 显示狗的最小、最大和平均体重
  9. 退出

我已经写了每一节课,但我无法完成它们我需要你的帮助。

我的课在下面。 (如果我做错了什么,请编辑并告诉我,我认为我做错了什么)

宠物类:

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;
    }
}

狗类:

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;

    }
}

猫类:

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;
    }
}

宠物测试类:

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

【问题讨论】:

  • 您有什么具体问题吗? I couldn't complete them 太模糊了。您的PetDogCat 课程对我来说看起来不错。不过,您在addDog() 方法中创建Cat 似乎有点奇怪……此外,您在Cat 中的getWeight() 方法可能应该是getColor()。 (复制粘贴错误?)
  • 是的,我注意到这是一个复制粘贴错误。我已经编辑了我的课程,但它无法正常工作,你们可以检查一下吗?如果你告诉我我的错误在哪里,我会很高兴。
  • 你还是太含糊了。你能更具体地说明什么不能正常工作吗?你有例外吗?出乎意料的输出?也许此时您应该发布一个新问题,其中包含一些不起作用的具体示例。
  • 例如,当我输入 6 时,它应该列出猫,但它什么也没做?我错过了什么?

标签: java inheritance arraylist


【解决方案1】:

你所做的大部分是正确的。虽然照顾一些事情:

  1. 你的猫类有一个错误(coatColor getter):错误的名字
  2. 你的方法addDog居然加了一只猫,让人迷惑

现在,您需要的有趣的东西(工具):

  • 每个动作都需要一种方法(添加猫、添加狗等)
  • 您需要在 mainList 中添加您创建的宠物:mainList.add(...)
  • 您宁愿使用 switch/case 语法来调用您的方法
  • 要移除猫或狗,您可能需要指明要移除哪一个(或者可能只是添加的最后一个)
  • 要列出狗或猫,您需要遍历您的 mainList 并仅保留具有良好类型的那些:使用 instanceof 运算符
  • 要显示狗的最小/最大体重和平均体重,您还需要遍历您的列表,并记录所有这些。

无论如何,我可以在每一点上更具体,但这对你来说并不有趣,因为你需要自己寻找解决方案。有更好的方法来做到这一点(例如使用像 Guava 这样的框架),但这是你的下一步!

【讨论】:

  • 谢谢亚历山大。这真的很有用:)
  • 如果您有其他具体问题,我很乐意为您提供帮助
  • 嗯...我编辑了我的 PetTest 方法,现在我不知道为什么它不起作用。我的错误在哪里?
  • 您的代码有很多问题(我的意思是在语法上)。你应该一个一个地研究你的方法,并寻找例子。例如,您不能执行for(Pet dog: mainList) { System.out.println(dog); },因为您的列表中可能有 Cats,您将得到一个 classCastException。相反,请执行for(Pet pet: mainList) { if (pet instanceOf Dog) {System.out.println(dog);} }。顺便说一句,使用foreach syntax 时不需要迭代器。祝你好运!
  • 哦,真的很快 :) 谢谢!你帮了我很多
【解决方案2】:

使 CatDog 类继承自 Pet 类,如下所示:

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

如果您希望子类在没有 getter/setter 方法的情况下使用它,可以将 Pet 类中的 name 变量设置为 protected。 (取决于您的要求)

【讨论】:

  • 也是 Pet 类中的 name 变量,如果您希望子类使用它,则需要对其进行保护:这不是真的,这就是 getter/setter 的作用
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-01-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-24
  • 1970-01-01
相关资源
最近更新 更多