In: Computer Science
Write a Java program named, TicketSale, (TicketSale.java) with the following tasks:
cost = ticketCost( adults, children, senior );
Please enter number of adults:1 Please enter number of kids:1 Please enter number of seniors:5 Your total cost for 1 adults, 1 children, and 5 seniors is: $30.00
Please enter number of adults:0 Please enter number of kids:2 Please enter number of seniors:10 Your total cost for 0 adults, 2 children, and 10 seniors is: $70.00
Source Code:
Output:
Code in text format (See above images of code for indentation):
import java.util.*;
/*class definition*/
public class TicketSale
{
/*main method*/
public static void main(String[] args)
{
/*Scanner object to read input from the user*/
Scanner read=new Scanner(System.in);
/*variables*/
int adults,children,senior;
float cost;
/*read number of adults from the user*/
System.out.printf("Enter number of adults: ");
adults=read.nextInt();
/*read children from the user*/
System.out.printf("Enter number of kids: ");
children=read.nextInt();
/*read number of seniors from the user*/
System.out.printf("Enter number of seniors: ");
senior=read.nextInt();
/*method call*/
cost=ticketCost(adults,children,senior);
/*print cost*/
System.out.printf("Your total cost for %d adults, %d children, and %d seniors is: $%.2f",adults,children,senior,cost);
}
/*method definition*/
public static int ticketCost(int adults,int children,int senior)
{
/*variables*/
int cost=0,ch;
/*cost for adults*/
cost+=adults*15.00;
/*check for atleast one adult*/
if(adults>0)
{
/*cost for per senior is 2*/
cost+=senior*2.00;
/*calculate cost for kids*/
if(adults>=children)
cost+=children*5.00;
else
{
/*each adult buy one children ticket for 5*/
cost+=adults*5.00;
ch=children-adults;
/*remaning children tickets cost is 10*/
cost+=ch*10.00;
}
}
/*if no adults calculate cost with below prices for seniors and children*/
else
{
cost+=senior*5.00;
cost+=children*10.00;
}
/*return total cost*/
return cost;
}
}