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
  • Please help JAVA Part II: Programming Module (80 marks): Write a banking program that simulates the operation of your l...

    Please help JAVA Part II: Programming Module (80 marks): Write a banking program that simulates the operation of your local bank. You should declare the following collection of classes: 1) An abstract class Customer: each customer has: firstName(String), lastName (String), age (integer), customerNumber (integer) - The Class customer has a class variable lastCustomerNumber that is initialized to 9999. It is used to initialize the customerNumber. Hence, the first customer number is set to 9999 and each time a new customer...

  • Amusement Park Programming Project Project Outcomes Use the Java selection constructs (if and if else). Use...

    Amusement Park Programming Project Project Outcomes Use the Java selection constructs (if and if else). Use the Java iteration constructs (while, do, for). Use Boolean variables and expressions to control iterations. Use arrays or ArrayList for storing objects. Proper design techniques. Project Requirements Your job is to implement a simple amusement park information system that keeps track of admission tickets and merchandise in the gift shop. The information system consists of three classes including a class to model tickets, a...

  • Write in Java Movie Theater Ticket App Write a program that allows a user to purchase...

    Write in Java Movie Theater Ticket App Write a program that allows a user to purchase movie tickets for a showing of one of three movies. Your program will work with two files - a Movie.java and Theater.java file - which will interact with each other. You should begin by creating three Movie objects which will store information about three movies of your choice Once the information has been stored, the program should welcome the user and ask the user...

  • Hi, Kindly assist with my project management assignment below using the attached case study Question 1 Update the project charter for the remainder of the project in response to Adams’ memo (lines 241...

    Hi, Kindly assist with my project management assignment below using the attached case study Question 1 Update the project charter for the remainder of the project in response to Adams’ memo (lines 241 through 246). Question 2 Prepare a plan for the remainder of the project in response to Adams’ memo (lines 241 through 246). Your answers to the above will be assessed in terms of the level of communication displayed, the insights and inferences drawn, and your ability to...

  • Case 34 Emirates Airline Emirates Airline was one of the three Middle East carriers that were sin...

    Case 34 Emirates Airline Emirates Airline was one of the three Middle East carriers that were singled out by the largest US airlines in the report that was released on March 5, 2015. The report charged that that the flagship airline of Dubai, along with Etihad Airways and Qatar Airways, had received over $42 billion in government subsidies and tax breaks since 2004. Claiming that this gave an unfair advantage to these state-owned airlines, the US airlines demanded that the...

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