【发布时间】: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 方法到底是做什么的?