标题:(求助)The Person Class
只看楼主
lyf3368
Rank: 1
等 级:新手上路
帖 子:26
专家分:0
注 册:2009-10-6
结帖率:25%
已结贴  问题点数:10 回复次数:6 
(求助)The Person Class
The Person Class

Define a class called Person which has attributes called height , money , ticketCount and hasPass.

The money and height 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 a boolean 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 The 
2009-10-21 09:04
lampeter123
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:54
帖 子:2508
专家分:6424
注 册:2009-1-30
得分:3 
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");
  }
}

你的优秀和我的人生无关!!!!
    
    我要过的,是属于我自己的生活~~~
2009-10-21 10:21
gameohyes
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:湖南
等 级:版主
威 望:53
帖 子:1275
专家分:3629
注 册:2009-3-5
得分:0 
找到个错误
public void toString -->toString 方法是有返回值的
必须是public String toString

C#超级群 74862681,欢迎大家的到来!
2009-10-21 10:34
gameohyes
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:湖南
等 级:版主
威 望:53
帖 子:1275
专家分:3629
注 册:2009-3-5
得分:7 
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";
        }
    }
}

C#超级群 74862681,欢迎大家的到来!
2009-10-21 10:36
lampeter123
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:54
帖 子:2508
专家分:6424
注 册:2009-1-30
得分:0 
回复 3楼 gameohyes
楼上,说的很对

toString
public String toString()返回该对象的字符串表示。通常,toString 方法会返回一个“以文本方式表示”此对象的字符串。结果应是一个简明但易于读懂的信息表达式。建议所有子类都重写此方法。
Object 类的 toString 方法返回一个字符串,该字符串由类名(对象是该类的一个实例)、at 标记符“@”和此对象哈希码的无符号十六进制表示组成。换句话说,该方法返回一个字符串,它的值等于:

getClass().getName() + '@' + Integer.toHexString(hashCode())
 
返回:
该对象的字符串表示形式。

你的优秀和我的人生无关!!!!
    
    我要过的,是属于我自己的生活~~~
2009-10-21 11:44
lyf3368
Rank: 1
等 级:新手上路
帖 子:26
专家分:0
注 册:2009-10-6
得分:0 
以下是引用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++起什么作用啊,我看不出来,可以解释一下吗
2009-10-21 13:52
gameohyes
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:湖南
等 级:版主
威 望:53
帖 子:1275
专家分:3629
注 册:2009-3-5
得分:0 
注意:这个mary调用了几次,你就懂了.
如:System.out.println(mary);

C#超级群 74862681,欢迎大家的到来!
2009-10-21 18:33



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-289327-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.089220 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved