a person class java_(求助)The Person Class

(求助)The Person Class

The Person ClassDefine a class called Person which has attributes called height , money , ticketCountandhasPass.

The moneyandheight variables are floats representing the amount of money the person currently has and the person's height (in inches).

The ticketCount is an integer indicating the number of tickets the person

currently has and hasPass is aboolean indicating whether or not the

person has a pass to get on the rides (assume a person can have at most 1

pass).

Write a constructor & all necessary methods in the Person class so that the

following code produces the output as shown below:

class PersonTester {

public static void main(String args[]) {

Person mary;

mary = new Person(4.9f, 20.00f);

System.out.println(mary.height);

System.out.println(mary.money);

System.out.println(mary.ticketCount);

System.out.println(mary.hasPass);

System.out.println(mary); // Notice that the money is properly formatted

mary.ticketCount = 3;

System.out.println(mary);

mary.useTickets(2); // You have to write this method    System.out.println(mary);

mary.hasPass = true;

System.out.println(mary);

}

}

Here is the output that your program MUST produce:

4.9

20.0

0

false

4.9' person with $20.00 and 0 tickets

4.9' person with $20.00 and 3 tickets

4.9' person with $20.00 and 1 tickets

4.9' person with $20.00 and a pass

Make sure to test your class with the test code above BEFORE you continue.

----------------解决方案--------------------------------------------------------

class Person

{

float money;

float height;

int ticketCount;

boolean hasPass;

public Person(float height,float money)

{

this.height = height;

this.money = money;

ticketCount = 0;

hasPass = false;

}

public void useTicket(int i)

{

ticketCout -=  i;

}

public void toString()

{

System.out.println(height + "person with $" + money + " and " + ticketCount + " tickets");

}

}

----------------解决方案--------------------------------------------------------

找到个错误

public void toString -->toString 方法是有返回值的

必须是public String toString

----------------解决方案--------------------------------------------------------

package oop;

public class Person {

float height;

float money;

int ticketCount;

boolean hasPass;

PersonTester mary;

int count;

public Person() {

height = 0.0f;

money = 0.0f;

ticketCount = 0;

hasPass = false;

}

public Person(float tempHeight, float tempMoney) {

this.height = tempHeight;

this.money = tempMoney;

}

public void useTickets(int tempTicketCount) {

this.ticketCount -= tempTicketCount;

}

public String toString() {

count++;

if (count == 4 && hasPass == true) {

return height + "'person with $" + money + " and " + "a pass";

} else {

return height + "'person with $" + money + " and " + ticketCount

+ "tickets";

}

}

}

----------------解决方案--------------------------------------------------------

回复 3楼 gameohyes

楼上,说的很对

toString

public String toString()返回该对象的字符串表示。通常,toString 方法会返回一个“以文本方式表示”此对象的字符串。结果应是一个简明但易于读懂的信息表达式。建议所有子类都重写此方法。

Object 类的 toString 方法返回一个字符串,该字符串由类名(对象是该类的一个实例)、at 标记符“@”和此对象哈希码的无符号十六进制表示组成。换句话说,该方法返回一个字符串,它的值等于:

getClass().getName() + '@' + Integer.toHexString(hashCode())

返回:

该对象的字符串表示形式。

----------------解决方案--------------------------------------------------------

以下是引用gameohyes在2009-10-21 10:36:03的发言:

package oop;

public class Person {

float height;

float money;

int ticketCount;

boolean hasPass;

PersonTester mary;

int count;

public Person() {

... 谢谢啊,这个很好,但是我不明白那个count++起什么作用啊,我看不出来,可以解释一下吗

----------------解决方案--------------------------------------------------------

注意:这个mary调用了几次,你就懂了.

如:System.out.println(mary);

----------------解决方案--------------------------------------------------------

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值