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


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
lyf3368
Rank: 1
等 级:新手上路
帖 子:26
专家分:0
注 册:2009-10-6
收藏
得分:0 
ACM是什么? 这是我练习上的一道题
2009-10-21 23:44
lyf3368
Rank: 1
等 级:新手上路
帖 子:26
专家分:0
注 册:2009-10-6
收藏
得分:0 
哦,不是啊,这个只是一般的练习题,因为我这里什么都是英文的
2009-10-22 08:38
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
lyf3368
Rank: 1
等 级:新手上路
帖 子:26
专家分:0
注 册:2009-10-6
收藏
得分:0 
class Person {  
    float height;  
    float money;  
    int ticketCount;  
    boolean hasPass;     
 
 
    Person() {  
       this.height = 0.0f;  
       this.money = 0.0f;  
       this.ticketCount = 0;  
       this.hasPass = false;  
    }  
 
    Person(float tempHeight, float tempMoney) {  
        this.height = tempHeight;  
        this.money = tempMoney;  
    }  
 
    void useTickets(int tempTicketCount) {  
        this.ticketCount -= tempTicketCount;  
    }  
 
    public String toString() {  
                       
        if (this.hasPass == true)  
            return height + "'person with $" + money + " and " + "a pass";  
                 
         else
            return height + "'person with $" + money + " and " + ticketCount  
                    + " " +"tickets";  
               
          }
           
      // (4) starts here, took me 4 hours ~~~~~~~~~  
         
    //buy passes method
    boolean buyPass(TicketBooth booth) {
        if(booth.availablePasses >= 1 && this.money >= booth.PASS_PRICE){
            this.money -= booth.PASS_PRICE;
            booth.sellPass();
            this.hasPass = true;
            return true;
           }
         
        return false;
    }
     
     
    //buy tickets method
    int buyTickets(int number, TicketBooth booth) {
        if (booth.availableTickets >= number) {
            if(this.money >= number*booth.TICKET_PRICE) {
                booth.sellTickets(number);
                this.money -= number*booth.TICKET_PRICE;
                this.ticketCount += number;
                return number;
            }
            else {
                int canAfford = (int)(this.money/booth.TICKET_PRICE);
                booth.sellTickets(canAfford);
                this.money -= (canAfford*booth.TICKET_PRICE);
                this.ticketCount =+ canAfford;
                return canAfford;
            }
        }
        else if (this.money >= booth.availableTickets*booth.TICKET_PRICE) {
               booth.sellTickets(booth.availableTickets);   
               this.money -= (booth.availableTickets * booth.TICKET_PRICE);
               this.ticketCount =+ booth.availableTickets;
               return booth.availableTickets;
        }
        return 0;
    }
 
 
    //allowed method
    boolean allowedToRide(Ride aRide) {
        if(this.height >= aRide.heightRequirement && (this.hasPass || this.ticketCount >= aRide.ticketsRequired))
            return true;
        else
            return false;
    }     
     
    //geton method
    boolean getOn(Ride aRide) {
        if (this.allowedToRide(aRide))    {            
                if (!this.hasPass) {
                this.ticketCount -= aRide.ticketsRequired;     
                aRide.numberOfRiders += 1;                 
                return true;
               }
                else if (this.hasPass)  
                    aRide.numberOfRiders += 1;
        return true;                        
            }
                     
        return false;
    }
            
            
 }
 
         
                                    
 
2009-10-24 03:39
快速回复:(求助)一道难题
数据加载中...
 
   



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

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