【问题标题】:Writing a java program containing two classes编写一个包含两个类的java程序
【发布时间】:2015-10-18 16:19:34
【问题描述】:

我试图编写一个包含两个类的 Java 程序:Dog 和驱动程序类 Kennel。

一条狗包含以下信息:

  • 整数年龄。
  • 字符串名称。如果给定名称包含非字母字符,则初始化为 Wolfy。
  • 表示狗“说话”时发出的声音。
  • 代表头发长度的布尔值; true 表示短发。
  • 代表狗体重的浮动重量(以磅为单位)。
  • 表示尾部类型(LONG、SHORT、NONE)的枚举。

狗有以下方法:

  • 默认构造函数。
  • 将名称作为参数的构造函数。
  • 一种方法 private boolean validName(String),返回真/假给定名称是否包含非字母字符。
  • humanAge 以“人类年”为单位计算并返回狗的年龄。
  • 言归正传...

我无法弄清楚如何为validName 执行该方法。谁能指出我正确的方向?我也用同样的方式说话吗?代码附在下面。

package lab101;


public class Dog 
{
    public enum TailType 
    {
        LONG, SHORT, NONE
    }    

    private int age;
    private float weight;
    private String name;
    private String bark;
    private boolean hairLength;
    private TailType tail;

    //Default Constructor--> initializes an object (called once)every constructor 
    //must initialize all the classes attributes!!
    public Dog()
    {
        age = 0;
        weight = 0;
        name = "";
        bark = "";
        hairLength = false; 
        tail = TailType.NONE;   
    }

    //Overloaded constructor (must have an argument)
    public Dog(String theName)
    {
        age = 0;
        weight = 0;
        name = theName;
        bark = "";
        hairLength = false; 
        tail = TailType.NONE;   
    }

    //If the name contains non-alphabetic characters
    private boolean validName(String str)
    {
        str = str.toLowerCase();

        if (str.length() <= 0)
        {

        }  


        return false; 
    }


    //Computes the dog's age in human years.
    public int humanAge()
    {
        int theAge = 7 * age;

        return theAge;
    }

    //Returns the value of bark if true
    public String(speak)
    {
        return bark;
    }


    //Method: <privacy: (private, public)> <return-type (data type or void)> <methodName>(<argument>){
    //        <body>
    //        return
    // }

    public String toString()
    {
        String str = "";
        str += "Name: " + name;
        str += "Weight: " + weight;
        str += "Bark: " + bark;
        str += "Hair Length: " + hairLength;
        str += "Tail: " + tail;
        str += "Age: " + age;

        return str;
    }

}

【问题讨论】:

  • validName 方法到底是做什么的?

标签: java class methods


【解决方案1】:

我在尝试弄清楚如何为 validName 执行方法时遇到了麻烦。

假设您的 validName 方法通过检查是否只存在字母来检查是否给出了有效名称:

//Return true if name is valid
private boolean validName(String str)
{
    if(str == null || str.length() == 0)
        return false;
    return str.toLowerCase().matches("[a-z]+"); //Name only has a-z(A-Z)
}

我也是用同样的方法说话还是不用?

好吧,我不知道您所说的“相同方式”是什么意思。但是你的说话方式不对。

根据你的描述,应该是:

public String speak(){
    return bark;
}

您没有提出要求,但可以做得更好的事情:

你正在实现你的类构造函数两次。你可以像这样调用构造函数:

public Dog(){
    this("");  //invoke Dog(String)
}

public Dog(String theName){
    age = 0;
    weight = 0;
    name = theName;
    bark = "";
    hairLength = false; 
    tail = TailType.NONE;   
}

上面的效果和你做的一样,但这是首选。

【讨论】:

    【解决方案2】:

    如果你不懂正则表达式也可以这样做:

    private boolean validName(String str)
    {   char[] letters = str.toUpperCase().toCharArray();
        for(int i = 0 ; i < letters.length; i++){
            int oneLetter = (int) letters[i];
            if( !(oneLetter > 64 && oneLetter < 91)) // letters as ASCII for A-Z
                return false; // if anyone is not A-Z then return false
    
        }
        return  true; // if every letter is valid then return true
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-02-09
      • 2017-03-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-16
      • 2012-03-01
      相关资源
      最近更新 更多