Question

Question: I do no understand this week's JAVA programming project, which is a program that mimics...

Question:

I do no understand this week's JAVA programming project, which is a program that mimics ticket purchases for train objects. It is a train class that includes:

  • 5 static constants. The five values can be seen in the Method descriptions below. There were no static constants provided, but my best guess is:
    • kidTicketCost (cost of kids ticket is $7.50)
    • adultTicketCost (adult ticket cost is $14.50)
    • ageOfKid (age of the kid)
    • ageOfAdult,(age of the adult)
    • trainSeatStatus. (checks to see if train is full) (I have no ideal if this is correct, it may also be the senior discount)
  • Instance constants:
    • name (stores the trains name)
    • capacity (stores the trains number of chairs)
  • Two parameter constructor:
    • receives the trains name and capacity.
  • ​​​​​​​Instance variables:
    • boughtSeats (stores how many seats on the train have been bought)
    • ticketEarnings (Stores earnings from the trains ticket sales)
  • ​​​​​​​Methods:
    • sellKidTicket that will pass the age and kid ticket cost to a helper method. The kid ticket cost is $7.50 and will enable method-call chaining.
    • sellAdultTicket that will pass the age and adult ticket cost to a helper method. The adult ticket cost is $14.50 will enable method-call chaining.
    • addPerson, a helper method that accepts the age of the person and the ticket cost. Check to see if the train is full, and if it is, prints "Sorry, the tram is full. Unable to purchase ticket." See if the person is three or younger, in which case the child shares a seat and rides for free. Check to see if the person is 65 or older. If they are they earn a senior discount of $2 (ticket is $2 less than an adult ticket).
    • displaySeatsAndEarnings, a method that displays the seats purchased and ticket earnings for the train calling object.

TrainDriver class:

public static void main(String[] args)
{
Train train1 = new Train("Polar Express", 30);
Train train2 = new Train("Disney Safari", 5);

// Sell tickets for train #1
System.out.println("Train #1");
tram1.sellAdultTicket(49).sellAdultTicket(30).sellAdultTicket(65);
tram1.sellKidTicket(12).sellKidTicket(3);
System.out.println();
tram1.displaySeatsAndEarnings();

// Sell some tickets for train #2
System.out.println("\nTrain #2");
for (int i=0; i<7; i++)
{
   tram2.sellAdultTicket(30);
}
System.out.println();
tram2.displaySeatsAndEarnings();
} // end main

Output:

Tram #1
An adult wants to buy a ticket.
A ticket has been sold for $14.50.
An adult wants to buy a ticket.
A ticket has been sold for $14.50.
An adult wants to buy a ticket.
The adult is old enough to receive the senior discount, so they pay only $12.50.
A kid wants to buy a ticket.
A ticket has been sold for $7.50.
A kid wants to buy a ticket.
The child is young enough to share a seat, so they ride for free.

Train "Polar Express" has sold 4 seats for a grand total of $XX.XX.

Train #2
An adult wants to buy a ticket.
A ticket has been sold for $14.50.
An adult wants to buy a ticket.
A ticket has been sold for $14.50.
An adult wants to buy a ticket.
A ticket has been sold for $14.50.
An adult wants to buy a ticket.
A ticket has been sold for $14.50.
An adult wants to buy a ticket.
A ticket has been sold for $14.50.
An adult wants to buy a ticket.
Sorry, the train is full. Unable to purchase ticket.
An adult wants to buy a ticket.
Sorry, the train is full. Unable to purchase ticket.

Train "Disney Safari" has sold 5 seats for a grand total of $XX.XX
0 0
Add a comment Improve this question Transcribed image text
Answer #1

/*
* The java program demonstrates the Train class by calling corresponding methods
* and print the results to console output.
* */
//TestTicket.java

public class TestTicket
{
   public static void main(String[] args)
   {
       Train train1 = new Train("Polar Express", 30);
       Train train2 = new Train("Disney Safari", 5);

       // Sell tickets for train #1
       System.out.println("Train #1");
       train1.sellAdultTicket(49).sellAdultTicket(30).sellAdultTicket(65);
       train1.sellKidTicket(12).sellKidTicket(3);
       System.out.println();
       train1.displaySeatsAndEarnings();

       // Sell some tickets for train #2
       System.out.println("\nTrain #2");
       for (int i=0; i<7; i++)
       {
           train2.sellAdultTicket(30);
       }
       System.out.println();
       train2.displaySeatsAndEarnings();
   }
}
-------------------------------------------------------------------------------------------------------------------
//Train.java
import java.text.DecimalFormat;
public class Train
{
   private final double kidTicketCost=7.50;//cost of kids ticket is $7.50
   private final double adultTicketCost=14.50;//adult ticket cost is $14.50
   private String name;
   private int capcaity;

   private int boughtSeats ;
   private double ticketEarnings ;


   /*parameter constructor that takes
   * name and capacity as input and
   * set to this class instance variables */

   public Train(String name, int capacity)
   {
       this.name=name;
       this.capcaity=capacity;
       //set 0 to below instance variables
       boughtSeats=0;
       ticketEarnings=0;
   }
   /*Method that takes age for adults*/
   public Train sellAdultTicket(int age)
   {
       System.out.println("An adult wants to buy a ticket.");
       boughtSeats++;
       //call addPerson method
       addPerson(age,adultTicketCost);
       return this;
   }
   /*Method that takes age for kids*/
   public Train sellKidTicket(int age)
   {
       System.out.println("An kid wants to buy a ticket.");  
       boughtSeats++;
       //call addPerson method
       addPerson(age,kidTicketCost);
       return this;
   }
  
   /*Method addPerson that takes age and cost */
   private void addPerson(int age,double cost)
   {  
       if(boughtSeats>capcaity)
           System.out.println("Sorry, the train is full. Unable to purchase ticket.");
       else if(age>=65)
       {
           System.out.println("The adult is old enough to receive the senior discount, so they pay only $12.50.");
           ticketEarnings+=cost-2;
       }
       else if(age<=3)
       {
           System.out.println("The child is young enough to share a seat, so they ride for free.");  
       }
       else if(age>3 && age<64)
       {          
           System.out.println("A ticket has been sold for $14.50.");
           ticketEarnings+=cost;
       }  
       else
       {  
           System.out.println("A ticket has been sold for $7.50.");
           ticketEarnings+=cost;
       }
   }

   /*Display the total tickets bought and
   * total earnings */
   public void displaySeatsAndEarnings()
   {
       DecimalFormat df=new DecimalFormat("0.00");
       System.out.println("Train \""+name+"\" has sold "+ boughtSeats
               +" seats for a grand total of $"
               +df.format(ticketEarnings)+".");
   }


}

-------------------------------------------------------------------------------------------------------------------

Sample Output:

Train #1
An adult wants to buy a ticket.
A ticket has been sold for $14.50.
An adult wants to buy a ticket.
A ticket has been sold for $14.50.
An adult wants to buy a ticket.
The adult is old enough to receive the senior discount, so they pay only $12.50.
An kid wants to buy a ticket.
A ticket has been sold for $14.50.
An kid wants to buy a ticket.
The child is young enough to share a seat, so they ride for free.

Train "Polar Express" has sold 5 seats for a grand total of $49.00.

Train #2
An adult wants to buy a ticket.
A ticket has been sold for $14.50.
An adult wants to buy a ticket.
A ticket has been sold for $14.50.
An adult wants to buy a ticket.
A ticket has been sold for $14.50.
An adult wants to buy a ticket.
A ticket has been sold for $14.50.
An adult wants to buy a ticket.
A ticket has been sold for $14.50.
An adult wants to buy a ticket.
Sorry, the train is full. Unable to purchase ticket.
An adult wants to buy a ticket.
Sorry, the train is full. Unable to purchase ticket.

Train "Disney Safari" has sold 7 seats for a grand total of $72.50.

Add a comment
Know the answer?
Add Answer to:
Question: I do no understand this week's JAVA programming project, which is a program that mimics...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT