我试图编写一个包含两个类的Java程序:Dog和一个驱动程序类Kennel。编写一个包含两个类的java程序
狗包含以下信息:
- 一个整数的年龄。
- 字符串名称。如果给定名称包含非字母字符,则初始化为Wolfy。
- 一个字符串树皮,代表狗在说话时发出的发声。
- 代表头发长度的布尔值;真指示短发。
- 代表狗体重的浮子重量(磅)。
- 代表尾部类型的枚举(LONG,SHORT,NONE)。
狗有以下方法:
- 默认构造函数。
- 将名称作为参数的构造函数。
- 方法private boolean validName(String)返回true/false给定名称是否包含非字母字符。
- 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方法呢? –