Hi, I\'m having trouble doing this java program. Thank You in advance Tickets fo
ID: 3544030 • Letter: H
Question
Hi, I'm having trouble doing this java program.
Thank You in advance
Tickets for a certain event noramlly cost $15.00 for adults and $7.50 for children. However, each adult who buys a ticket can also purchase one child's ticket at a discounted rate of $5.00. (One child per adult only to get this discount.) Also, if 10 or more tickets(adults and/ or children) are purchased at a time, the total cost is discounted by 5%.
The method name ticketCost will be invoked by main with statement like: cost = ticketCost(adults, children); where the parametrs give the number of adult tickets and the number of children's ticets being purchased. The method shoudl returna double value that tells the total cost for these tickets (using all the information given above.) The program will display the number of adults and children purchasing tickets and thte total cost rounded to te nearest penny.( 2 decimal places) The program will also display correct grammar:
* if there is one adult then the display will not say: 1 adults.
Sample Outputs:
Please enter number of adults: 5
Please enter number of kids: 1
Your total cost for 5 adults and 1 child is: $80.00
Please enter number of adults : 5
Please enter number of kids: 6
Your total cost for 5 adults and 6 children is: $102.13
*** ONlLY one method should be used.
*** There should be no loops in this program
Explanation / Answer
MAIN METHOD:
import java.util.Scanner;
import java.text.*;
public class ticDisc {
public static void main(String[] args) {
double tot ;
int adults, kids;
Scanner obj = new Scanner(System.in);
System.out.println("Enter num of adults");
adults = obj.nextInt();
System.out.println("Enter num of kids");
kids = obj.nextInt();
ticketCost(adults,kids);
}
private static void ticketCost(int adults,int kids){
int NonDiscKids,mem;
double tot =0;
DecimalFormat df = new DecimalFormat("0.00");
String grammer,kidgrammer;
if(adults<=1){
grammer ="adult";
}else{
grammer ="adults";
}
if(kids<=1){
kidgrammer ="kid";
}else{
kidgrammer ="kids";
}
mem = adults+kids;
if(mem<10){
if(adults==0||kids==0){
tot = (adults*15)+(kids*7.50);
tot= ((double)Math.round(tot * 100)/100);
System.out.print("Your Total cost for "+adults+" "+grammer+" and "+kids+" "+kidgrammer+" is $" );
System.out.println(df.format(tot));
return;
}
else if(adults<=kids){
NonDiscKids = kids - adults;
tot = (adults*15)+(adults*5.00)+(NonDiscKids*7.50);
tot= ((double)Math.round(tot * 100)/100);
System.out.print("Your Total cost for "+adults+" "+grammer+" and "+kids+" "+kidgrammer+" is $" );
System.out.println(df.format(tot));
return;
}
else{
tot= ((adults*15)+(kids*5.00));
tot= ((double)Math.round(tot * 100)/100);
System.out.print("Your Total cost for "+adults+" "+grammer+" and "+kids+" "+kidgrammer+" is $");
System.out.println(df.format(tot));
return;
}
}
else{
if(adults==0||kids==0){
tot = (adults*15)+(kids*7.50);
tot = (tot*0.95);
tot= ((double)Math.round(tot * 100)/100);
System.out.print("Your Total cost for "+adults+" "+grammer+" and "+kids+" "+kidgrammer+" is $" );
System.out.println(df.format(tot));
return;
}
else if(adults<=kids){
NonDiscKids = kids - adults;
tot = (adults*15)+(adults*5.00)+(NonDiscKids*7.50);
tot = (tot*0.95);
tot= ((double)Math.round(tot * 100)/100);
System.out.println("Your Total cost for "+adults+" "+grammer+" and "+kids+" "+kidgrammer+" is $");
System.out.println(df.format(tot));
return;
}
else{
tot= (adults*15)+(kids*5.00);
tot = (tot*0.95);
tot= ((double)Math.round(tot * 100)/100);
System.out.println("Your Total cost for "+adults+" "+grammer+" and "+kids+" "+kidgrammer+" is $");
System.out.println(df.format(tot));
return;
}
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.