| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1108 人关注过本帖
标题:(求助)一道难题
只看楼主 加入收藏
lyf3368
Rank: 1
等 级:新手上路
帖 子:26
专家分:0
注 册:2009-10-6
结帖率:25%
收藏
已结贴  问题点数:10 回复次数:10 
(求助)一道难题
有点长


Add the following instance methods to the Person class:

buyPass(TicketBooth booth) - This method simulates the person buying a ride pass from the given booth object. It
should modify the person and the booth in the appropriate way,
and then return a boolean indicating whether or not the
transaction was successful. The transaction is successful
ONLY if the booth has a pass available AND the person has
enough money for the pass.

buyTickets(int number, TicketBooth booth) - This method simulates the person attempting to buy the specified number of
tickets from the given booth object. It should modify the person
and the booth in the appropriate way. If the booth does not
have the specified number of tickets available, then the
number of tickets to be sold should be reduced to the number
of remaining tickets available. Then, if the person has enough money to buy the resulting number of tickets (i.e., either the requested amount or the reduced amount) then the purchase is made. If the person does not have enough money to buy the tickets, then the number of tickets sold should be further reduced to the number of tickets that the person can afford. The method must return
an integer representing the number of tickets actually sold. Make sure that the booth maintains a proper total of all money paid to it.

allowedToRide(Ride aRide) - This method determines whether or not the person is allowed to ride on the given ride. A person can get on a ride if he/she meets the ride's minimum height requirements AND the person either has a pass OR the required number of tickets for that ride. The method MUST return a boolean and MUST NOT have any System.out.println code within it.
Also, it MUST be written efficiently ... you will loose marks for unnecessary code ... make use of any methods you already have.

getOn(Ride aRide) - This method simulates the person getting on the given ride. A person can get on a ride ONLY if he/she is allowed to (i.e., make sure you use the method you just wrote).
Make sure that the Person AND the Ride object are updated accordingly (no tickets are used if the person has a pass). The method MUST NOT have any System.out.println code within it and it should return true if the person was able to get on the ride and false otherwise.

Make sure to now test your code with the following test case in a class called FairTester:

class FairTester {
    public static void main(String args[]) {
       Ride coaster, ferris, merryGo, tosser;
       Person billy, donna, fredy, harry, larry;
       TicketBooth booth;

       // Make some rides on which the people can ride, specify name, #tickets &height req
       coaster = new Ride("Roller Coaster", 6, 4.25f);
       ferris = new Ride("Ferris Wheel", 5, 3.1f);
       merryGo = new Ride("Merry-Go-Round", 2, 0);
       tosser = new Ride("Tummy Tosser", 7, 4.9f);

       // Make a booth, specifiying the number of available passes and tickets
      
booth = new TicketBooth(4, 100);

       // Make some people by specifying their height and their money amounts
      
billy = new Person(4.9f, 10.00f);
       donna = new Person(3.0f, 5.00f);
       fredy = new Person(6.0f, 0.00f);
       harry = new Person(4.8f, 78.50f);
       larry = new Person(4.0f, 50.00f);

System.out.println(booth);

System.out.println("\nBilly is a " + billy);
System.out.println("Billy just bought " + billy.buyTickets(20, booth)+ " tickets.");
System.out.println("Billy attempting to go on the " + coaster);
System.out.println("Billy got on: " + billy.getOn(coaster));
System.out.println("Billy attempting to go on the " + tosser);
System.out.println("Billy got on: " + billy.getOn(tosser));
System.out.println("Billy is now a " + billy + "\n");

System.out.println("Donna is a " + donna);
System.out.println("Donna is trying to buy a pass...was she successful: "
+ donna.buyPass(booth));

System.out.println("Donna just bought " + donna.buyTickets(6, booth)+ " tickets.");
System.out.println("Donna is attempting to go on the " + ferris);
System.out.println("Donna got on: " + donna.getOn(ferris));
System.out.println("Donna is attempting to go on the " + merryGo);
System.out.println("Donna got on: " + donna.getOn(merryGo));
System.out.println("Donna is now a " + donna + "\n");

System.out.println("Fredy is a " + fredy);
System.out.println("Fredy just bought " + fredy.buyTickets(5, booth)+ " tickets.");
System.out.println("Fredy is attempting to go on the " + merryGo);
System.out.println("Fredy got on: " + fredy.getOn(merryGo));
System.out.println("Fredy is now a " + fredy + "\n");

System.out.println("Harry is a " + harry);
System.out.println("Harry just bought " + harry.buyTickets(10, booth)+ " tickets.");
System.out.println("Harry is trying to buy a pass...was he successful: "
+ harry.buyPass(booth));

System.out.println("Harry is attempting to go on the " + coaster);
System.out.println("Harry got on: " + harry.getOn(coaster));
System.out.println("Harry is attempting to go on the " + tosser);
System.out.println("Harry got on: " + harry.getOn(tosser));
System.out.println("Harry is attempting to go on the " + coaster);
System.out.println("Harry got on: " + harry.getOn(coaster));
System.out.println("Harry is now a " + harry + "\n");

System.out.println("Larry is a " + larry);
System.out.println("Larry just bought " + larry.buyTickets(15, booth)+ " tickets.");
System.out.println("Larry is attempting to go on the " + tosser);
System.out.println("Larry got on: " + larry.getOn(tosser));
System.out.println("Larry is attempting to go on the " + coaster);
System.out.println("Larry got on: " + larry.getOn(coaster));
System.out.println("Larry is attempting to go on the " + merryGo);
System.out.println("Larry got on: " + larry.getOn(merryGo));
System.out.println("Larry is now a " + larry + "\n");

System.out.println("Ticket Booth made $" + booth.moneyMade);
System.out.println(booth);

System.out.println(coaster + " and had " + coaster.numberOfRiders + " riders.");
System.out.println(ferris + " and had " + ferris.numberOfRiders + " riders.");
System.out.println(merryGo + " and had " + merryGo.numberOfRiders + " riders.");
System.out.println(tosser + " and had " + tosser.numberOfRiders + " riders.");
   }
}


Here is the output to expect (pay attention to the bold values to make sure that your code is correct):

Ticket booth with 4 passes and 100 tickets

Billy is a 4.9' person with $10.00 and 0 tickets
Billy just bought 20 tickets.
Billy attempting to go on the Roller Coaster requiring 6 tickets with a height restriction of 4.25'
Billy got on: true
Billy attempting to go on the Tummy Tosser requiring 7 tickets with a height restriction of 4.9'
Billy got on: true
Billy is now a 4.9' person with $0.00 and 7 tickets

Donna is a 3.0' person with $5.00 and 0 tickets
Donna is trying to buy a pass...was she successful: false
Donna just bought 6 tickets.
Donna is attempting to go on the Ferris Wheel requiring 5 tickets with a height restriction of 3.1'
Donna got on: false
Donna is attempting to go on the Merry-Go-Round requiring 2 tickets with a height restriction of 0.0'
Donna got on: true
Donna is now a 3.0' person with $2.00 and 4 tickets

Fredy is a 6.0' person with $0.00 and 0 tickets
Fredy just bought 0 tickets.
Fredy is attempting to go on the Merry-Go-Round requiring 2 tickets with a height restriction of 0.0'
Fredy got on: false
Fredy is now a 6.0' person with $0.00 and 0 tickets

Harry is a 4.8' person with $78.50 and 0 tickets
Harry just bought 10 tickets.
Harry is trying to buy a pass...was he successful: true
Harry is attempting to go on the Roller Coaster requiring 6 tickets with a height restriction of 4.25'
Harry got on: true
Harry is attempting to go on the Tummy Tosser requiring 7 tickets with a height restriction of 4.9'
Harry got on: false
Harry is attempting to go on the Roller Coaster requiring 6 tickets with a height restriction of 4.25'
Harry got on: true
Harry is now a 4.8' person with $57.00 and a pass

Larry is a 4.0' person with $50.00 and 0 tickets
Larry just bought 15 tickets.
Larry is attempting to go on the Tummy Tosser requiring 7 tickets with a height restriction of 4.9'
Larry got on: false
Larry is attempting to go on the Roller Coaster requiring 6 tickets with a height restriction of 4.25'
Larry got on: false
Larry is attempting to go on the Merry-Go-Round requiring 2 tickets with a height restriction of 0.0'
Larry got on: true
Larry is now a 4.0' person with $42.50 and 13 tickets

Ticket Booth made $42.0
Ticket booth with 3 passes and 49 tickets
Roller Coaster requiring 6 tickets with a height restriction of 4.25' and had 3 riders.
Ferris Wheel requiring 5 tickets with a height restriction of 3.1' and had 0 riders.
Merry-Go-Round requiring 2 tickets with a height restriction of 0.0' and had 2 riders.
Tummy Tosser requiring 7 tickets with a height restriction of 4.9' and had 1 riders.
搜索更多相关主题的帖子: 难题 
2009-10-21 14:28
dadongzicool
Rank: 6Rank: 6
等 级:贵宾
威 望:11
帖 子:209
专家分:474
注 册:2009-3-20
收藏
得分:2 
lz是不是发的ACM的题目  看见英文就懵

本人出售软件专业毕业设计(包括论文,代码)题目众多,价格从优联系方式:QQ
1635008514(寻找高校代理)
2009-10-21 15:14
lyf3368
Rank: 1
等 级:新手上路
帖 子:26
专家分:0
注 册:2009-10-6
收藏
得分:0 
ACM是什么? 这是我练习上的一道题
2009-10-21 23:44
lampeter123
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:54
帖 子:2508
专家分:6424
注 册:2009-1-30
收藏
得分:2 
ACM是大学生程序设计比赛,主要是有关算法的问题

你的优秀和我的人生无关!!!!
    
    我要过的,是属于我自己的生活~~~
2009-10-22 08:13
lyf3368
Rank: 1
等 级:新手上路
帖 子:26
专家分:0
注 册:2009-10-6
收藏
得分:0 
哦,不是啊,这个只是一般的练习题,因为我这里什么都是英文的
2009-10-22 08:38
火山古人2
Rank: 2
等 级:论坛游民
帖 子:3
专家分:32
注 册:2009-10-22
收藏
得分:2 
2009-10-22 17:11
阿贝
Rank: 2
等 级:论坛游民
威 望:1
帖 子:104
专家分:66
注 册:2009-10-22
收藏
得分:2 
我有个朋友参加了ACM,羡慕啊    牛人啊     楼主这道题搞懂了    离ACM不远矣
2009-10-23 09:52
阿贝
Rank: 2
等 级:论坛游民
威 望:1
帖 子:104
专家分:66
注 册:2009-10-22
收藏
得分:0 
蓝体字应该是程序执行后输出地吧,
看来看去缺三个类啊(Ride 类; Person 类; TicketBooth 类)。
还要添加person的几个方法。
  楼主可根据后面输出的情况编写。
2009-10-23 10:01
lyf3368
Rank: 1
等 级:新手上路
帖 子:26
专家分:0
注 册:2009-10-6
收藏
得分:0 
在朋友的帮助下终于做好了


class Ride {
    String name;
    int ticketsRequired;
    float heightRequirement;
    int numberOfRiders;
         
     
    Ride(String n, int tr, float hr) {
        this.name = n;
        this.ticketsRequired = tr;
        this.heightRequirement = hr;
        this.numberOfRiders = 0;   
    }
     
    Ride (){
        this.name = "UNKNOWN";
        this.ticketsRequired = 0;
        this.heightRequirement = 0;
        this.numberOfRiders = 0;        
    }
    public String toString(){
        return (this.name + " " + "requiring" + " " + this.ticketsRequired + " " +  "tickets with a height restriction of" + " " +  this.heightRequirement);
    }
 
}
        

2009-10-24 03:38
lyf3368
Rank: 1
等 级:新手上路
帖 子:26
专家分:0
注 册:2009-10-6
收藏
得分:0 
class TicketBooth {
   float moneyMade;   
   int availablePasses;
   int availableTickets;
 
   final float TICKET_PRICE = 0.50f;   //constant
   final float PASS_PRICE = 16.50f;    //constant
 
   TicketBooth() {
         this.moneyMade = 0.0f;
         this.availablePasses= 0;
         this.availableTickets = 0;
   }
   
   TicketBooth(int ap) {
         this.moneyMade = 0.0f;
         this.availablePasses = ap;
         this.availableTickets = 0;
   }
   
   TicketBooth(int ap, int at) {
         this.moneyMade = 0.0f;
         this.availablePasses = ap;
         this.availableTickets = at;
   }
   
   void sellPass() {
        if (this.availablePasses >= 1) {
            this.availablePasses -= 1;
            this.moneyMade += PASS_PRICE;
        }
   }
   
   void sellTickets(int num) {
         if (this.availableTickets >= num) {
            this.availableTickets -= num;
            this.moneyMade += (TICKET_PRICE*num);
         }
   }   
     
 
  public String toString() {
      return ("Ticket booth with" + " " + this.availablePasses + " " + "passes and" + " " + this.availableTickets + " " + "tickets");
   
   }
}
2009-10-24 03:39
快速回复:(求助)一道难题
数据加载中...
 
   



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

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