【问题标题】:OOP School work (beginner)OOP 学校作业(初级)
【发布时间】:2015-04-25 04:28:54
【问题描述】:

按原样,它确实可以编译。

Bug = 在同一行打印品种和去爪声明。由于某种原因无法摆脱此错误

Bug = 无法获得结果逻辑来编译和打印 控制台(新)

有些例子有点像我发布的,但它们确实没有做任何帮助,因为它们并不是我认为我应该去做的。一个工作完美,但使用和排列,在我的课程中我们甚至还没有达到那个部分。这是老师的指导和他给我的一些提示,然后是我的代码。其中一些已被注释掉,因为我试图在打印结果之前消除错误。此外,为了方便起见,我确实更改了一些名称,直到作业完成。对于任何格式问题,我们深表歉意。有什么建议吗?

按此顺序:

  1. 类文件的分配说明
  2. 老师的提示
  3. 到目前为止的类文件代码
  4. 驱动程序文件的分配
  5. 到目前为止的驱动文件代码

    1. 类文件的分配说明:

创建一个名为 Cat 的新类,其中包含以下功能

新类具有以下属性: 名称 - 类型字符串 年龄 - 类型整数 重量 - 类型双 品种 - 类型字符串 delawed - boolean 类型 - true 表示没有爪子,false 表示有爪子

确保您的类对构造函数、访问器和修改器方法进行了合理的补充。每个成员变量必须至少有一个独立的访问器和一个独立的修改器。

示例: public void setName(String name) mutator 用于设置名称 public void setBreed(String Breed) mutator 用于设置品种 public void set(Boolean delawed) 用于设置爪子与否 ****(您必须重载 set 方法才能设置 deClawed 值)** 这是什么?** public String getName() 用于获取名称的访问器 public String getBreed() 用于获取品种的访问器 public boolean getBoolean() 访问用于获取 delawed 的值

确保在引用当前对象的每个实例变量或实例方法时使用此类中的“this”引用。

  1. 老师的提示: 对于您的 IF 语句,您将只使用 Age 和 Claw 方法,但 然后最后当你打印出你需要的所有东西时 方法。你可以创建一个显示方法来显示所有的打印 每个类别的陈述,例如姓名,年龄等......然后它变成 更容易在 main 方法中显示它 - 这将是您的代码 主要方法:

    System.out.println("输入的猫数据为:\n"); myCat1.display(); myCat2.display(); myCat3.display();

  2. 到目前为止的类文件代码

/**************************************************** ****************************************************** * *猫.java *乔纳森·尼斯 * *查。 6 面向对象 ****************************************************** ***********************************************/

import java.util.Scanner;
public class Cat
{
    private String name;
    private int age;
    private double weight;
    private String breed;
    private boolean declawed;

    public void setName(String name)
    {
        this.name = name;
    }

    public String getName()
    {
    return this.name;
    }

    public void setAge(int age)
    {
    this.age = age;
    }

    public int getAge()
    {
    return this.age;
    }

    public void setWeight(double weight)
    {
    this.weight = weight;
    }

    public double getWeight()
    {
    return this.weight;
    }

    public void setBreed(String breed)
    {
        this.breed = breed;
    }

    public String getBreed()
    {
    return this.breed;
    }

    public void setDeclawed(boolean declawed)
    {
    this.declawed = declawed;
    }

    public boolean isDeclawed()
    {
    if (!this.declawed == false)
    {
        return true;
    }
    else
    {
        return false;
    }
    }

    Scanner input = new Scanner(System.in);

    public void display()
    {
    System.out.print("Enter the name of Cat: ");
    this.name = input.nextLine();
    System.out.print("Enter the age of Cat: ");
    this.age = input.nextInt();
    System.out.print("Enter the weight of Cat: ");
    this.weight = input.nextDouble();
    System.out.print("Enter the breed of Cat: ");
    this.breed = input.nextLine();
        System.out.print("Does the cat have claws? True or False?: ");
    this.declawed = input.nextBoolean();
    }
}
  1. 驱动文件的分配说明

编写一个驱动程序,读取 3 只 Cat 类型的宠物,并打印出所有有爪子且 3 岁以上的猫的名字和年龄。

应阅读以下信息: 名称(作为字符串) 年龄(整数) 重量(双倍) 品种(作为字符串) DeClawed(作为布尔值)

确保您使用访问器方法检查年龄和爪子。

  1. 到目前为止的驱动文件代码

/**************************************************** ****************************************************** * *CatDriver.java *乔纳森·尼斯 * *查。 6 面向对象驱动程序 ****************************************************** ***********************************************/

import java.util.Scanner;

public class CatDriver
{
    public static void main(String[] args)
    {
    Scanner input = new Scanner(System.in);
    Cat Cat1 = new Cat();
    Cat Cat2 = new Cat();
    Cat Cat3 = new Cat();

    Cat1.display();
    System.out.println();
    Cat2.display();
    System.out.println();
    Cat3.display();
    System.out.println();

    //*for (int i=0; i<3; i++)
    //{
    //  System.out.println("The cats over 3 with claws are:");
    //  if ((!this.age() <= 3) && (this.Declawed() == true))
    //  {
    //      System.out.println("Name: " + this.name);
    //      System.out.println("Age: " + this.age + "Years Old");
        //  }
    //}
    }
}

就像我说的,我已经注释了一些,以便在得出结果之前解决错误。

它几乎可以工作!排序...

【问题讨论】:

  • 请阅读this page
  • 阅读。我是不是做错了什么?
  • 如果没有人愿意帮助我,但我不会仅仅因为它有效而提交其他人的工作。在我窃取别人的代码之前,我将把我的一点点归功于使它编译。我需要帮助和提示以使我的操作。不过我读过。
  • 而不是检查 this.declawed 是否为真或不只是返回值。 public boolean isDeclawed() { return this.declawed; }
  • 感谢@stacksonstacks。你是对的。无论如何,我必须再次检查它。还有什么吗?

标签: java


【解决方案1】:

我能够让它工作。我使用 setter 方法重写了您的显示方法,并且成功了。此外,您的 delawed setter 方法编写得有些混乱,因此您可能需要修改它。 试试这个:

public void display()
{
    System.out.print("Enter the name of Cat: ");
    this.setName(input.nextLine());
    System.out.print("Enter the age of Cat: ");
    this.setAge(Integer.valueOf(input.nextLine()));
    System.out.print("Enter the weight of Cat: ");
    this.setWeight(Double.valueOf(input.nextLine()));
    System.out.print("Enter the breed of Cat: ");
    this.setBreed(input.nextLine());
    System.out.print("Does the cat have claws? True or False?: ");
    this.setDeclawed(!Boolean.valueOf(input.nextLine()));
}

【讨论】:

  • 我们还没有处理 .valueOf,所以我必须检查一下。谢谢@MatthewGasiewski 它确实有效,但我以前从未将它打印在同一行上。万一它再次发生,很高兴知道它为什么会被窃听。有什么想法吗?
  • 不确定它为什么会出现问题。它也发生在我身上。 Java 似乎比您处理它的方式更喜欢转换字符串输入。顺便说一句,我不确定您注释掉的“for”循环是否适合您。您可能希望将对象加载到数组中,以便在这样的循环中循环。
  • 另外,回顾我的代码,您可能只需传入 input.nextDouble 或 input.nextInt 方法的值,而不是转换字符串。
  • 实际上我很确定语法方法是修复同一行错误的方法。我尝试了两种方法,这是唯一有效的方法。
猜你喜欢
  • 2014-11-12
  • 1970-01-01
  • 2010-11-11
  • 2016-12-27
  • 1970-01-01
  • 1970-01-01
  • 2013-11-14
  • 2020-12-24
  • 1970-01-01
相关资源
最近更新 更多