Task - Control Flow Statements - Decision Making Statements #87
Replies: 22 comments
-
Version 1import java.util.Scanner;
public class Version1 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter the age:");
int age=Integer.parseInt(sc.nextLine());
int basePrice=30;
int charge=0;
if(age>=65){
charge=(int)(basePrice-basePrice*0.5); }
else
charge=basePrice;
System.out.printf("The charge is %d $",charge);
sc.close();
}
}Version 2import java.util.Scanner;
public class Project1 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter the age:");
int age=Integer.parseInt(sc.nextLine());
int basePrice=40;
System.out.println("Enter the county name:");
String county=sc.nextLine();
int charge=0;
//to check the age for positive value.
if(age>0){
//Senior citizen from warren
if(age>=65 && county.equalsIgnoreCase("Warren"))
{
charge=(int)(basePrice-(30*0.5));
}
else if(age>=65){
//to check senior citizen
if((county.equalsIgnoreCase("Campbell"))){
//Whether the senior citizen is from campbell or not.
charge=(int)(basePrice-(basePrice*0.5)-basePrice*7.5/100);
}
else{
//charge for senior citizen from other county.
charge=(int)(basePrice-basePrice*0.5);
}
}
else if(age<5){
//Check whether the visitor is child or not.
charge=0;
}
else if(county.equalsIgnoreCase("Warren")){
//Charge for non senior citizen from Warren.
charge=30;
}
else if(age<14 && county.equalsIgnoreCase("Clermont")){
//Checks the Clermont visitor's age.
charge=basePrice-(basePrice*18)/100;
}
else {
charge=basePrice;
}
System.out.printf("The charge is %d$",charge);
}
else{
System.out.println("Error Invalid Age :(");
}
sc.close();
}
}
|
Beta Was this translation helpful? Give feedback.
-
Version 1import java.util.Scanner;
class Price
{
int age;
final int price = 30;
int fprice;
int calcTicketPrice(int age)
{
if(age>=65)
{
fprice = (int) price - (price * 0.5);
}
else
{
fprice = price;
}
return fprice;
}
}
public class TicketPrice {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Price p = new Price();
System.out.println("Enter age");
p.age = Integer.parseInt(sc.nextLine());
int fp = p.calcTicketPrice(p.age);
System.out.println(String.format("Price of ticket is: $%d",fp));
sc.close();
}
}Version 2package edu;
import java.util.Scanner;
class Price
{
int age;
final double price = 30.00;
final double newprice = 40.00;
double fprice;
String country;
double calcTicketPrice(int age, String country)
{
// Senior citizen
if(age>=65)
{
// Senior citizen (age 65 and above) from Campbell
if(country.equalsIgnoreCase("Campbell"))
{
fprice = newprice - (newprice * 0.5);
fprice = fprice - (fprice * 0.075);
}
// Senior citizen (age 65 and above) from country other than Campbell
else
{
fprice= newprice - (newprice * 0.5);
}
}
// Children age from 1 to 4
else if(age<5)
{
fprice = 0;
}
// Citizen from country other than Warren
else if(age>=5 && age<=65 && country.equalsIgnoreCase("Warren"))
{
fprice = price;
}
// Children age less than 14 and country from Clermont
else if(age<14 && country.equalsIgnoreCase("Clermont"))
{
fprice = newprice - (newprice * 0.18);
}
else if(age>14 && age<65)
{
// Citizen from Warren
if(country.equalsIgnoreCase("Warren"))
{
fprice=price;
}
// Citizen from country other than Warren
else
{
fprice=newprice;
}
}
return fprice;
}
}
public class TicketPrice {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Price p = new Price();
System.out.println("Enter age");
p.age = Integer.parseInt(sc.nextLine());
if(p.age<=0)
{
System.out.println("Invalid age");
System.exit(0);
}
System.out.println("Enter country");
p.country=sc.next();
double fp = p.calcTicketPrice(p.age,p.country);
System.out.println(String.format("Price of ticket is: $%.2f",fp));
sc.close();
}
} |
Beta Was this translation helpful? Give feedback.
-
Task-decision making statement1.Version1.javapackage ControlFlowStatements;
import java.util.Scanner;
public class Version1 {
public static void checkPrice(int age,int price)
{
int new_price=price-((50*price)/100);
System.out.println((age>=65)?"Price of the ticket : $" + new_price:"Price of the ticket : $" +price);
}
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int price=30;
System.out.println("Enter the age :");
int age=Integer.parseInt(sc.nextLine());
checkPrice(age,price);
}
}Output2.Version2.javapackage ControlFlowStatements;
import java.util.*;
public class Version2 {
public static void checkPrice(int age,String country,int price)
{
if(age<5) //ticket for children under age 5 is free
{
System.out.println("Ticket is free!!!");
}
// 18% discount for children under age of 14 from clermont
else if(age<14 && country.equalsIgnoreCase("clermont"))
{
int new_price=(int) (price-(price*0.18));
System.out.println("Price of a ticket : $"+new_price);
}
//7.5% discount for senior citizen from campbell
else if(age>=65 && country.equalsIgnoreCase("campbell"))
{
int new_price=(int) (price-(price*0.5)); //already 50% discount
new_price=(int) (new_price-(new_price*0.075));// 7.5% discount
System.out.println("Price of a ticket : $"+new_price);
}
//50% discount for senior citizen
else if(age>=65)
{
int new_price=(int) (price-(price*0.5));
System.out.println("Price of a ticket : $"+new_price);
}
// price of a ticket for other age people
else
{
System.out.println("Price of a ticket : $"+ price);
}
}
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter a age: ");
int age=sc.nextInt();
System.out.println("Enter the country: ");
String country=sc.next();
//checking for valid age
if(age<=0)
{
System.out.println("Invalid Age... ");
System.exit(0);
}
//setting base price for a ticket
int price=(country.equalsIgnoreCase("warren"))?30:40;
//method calling
checkPrice(age,country,price);
}
}Output |
Beta Was this translation helpful? Give feedback.
-
1,2import java.util.*;
public class ContionalAssignment {
public static void main(String[] args) {
Scanner obj = new Scanner(System.in);
System.out.println("Enter the age:");
int age = obj.nextInt();
System.out.println("Enter the county:");
String county = obj.next();
double ticketPrice = 0;
int basePrice = 0;
double percentagePrice = 0;
if (!county.equalsIgnoreCase("Warren")) {
basePrice = 40;
} else {
basePrice = 30;
}
if (age > 0) {
if (age < 14 && county.equalsIgnoreCase("Clermont")) {
percentagePrice = basePrice * 0.18;
ticketPrice = basePrice - percentagePrice;
} else if (age <= 5) {
ticketPrice = 0;
} else if (age >= 65) {
if (county.equalsIgnoreCase("Campbell")) {
percentagePrice = (basePrice * 0.075) + (basePrice * 0.5);
ticketPrice = basePrice - percentagePrice;
} else {
percentagePrice = basePrice * 0.5;
ticketPrice = basePrice - percentagePrice;
}
} else {
ticketPrice = basePrice;
}
System.out.println("ticketPrice:" + ticketPrice);
} else {
System.out.println("invalid age");
}
obj.close();
}
} |
Beta Was this translation helpful? Give feedback.
-
Version1package methods.menudriven;
import java.util.Scanner;
public class Version1{
public static void print(int age,int price){
if(age>=65){
int chargeprice=price-((price*50)/100);
System.out.println("charge price is $"+ chargeprice);}
else{
System.out.println("charge price is $"+price);
}
}
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter the age:");
int age=sc.nextInt();
int price=30;
Version1.print(age,price);
}
}output:Version2package methods.menudriven;
import java.util.Scanner;
public class Version2 {
public static void display(int age,String county,int price){
//childern under 5 (tickets are free)
if(age<5){
price=0;
System.out.println("ticket price is "+price);
}
//children under 14 from clermont are given with 18% discount
else if((age<14)&&county.equalsIgnoreCase("clermont")){
price=(int) (price-(price*0.18));
System.out.println("ticket price is $"+price);
}
//senoir citizen(>=65) from campbell county are given with additional 7.5% discount
else if((age>=65)&&county.equalsIgnoreCase("campbell")){
int d_price=(int) (price-(price*50)/100);
d_price=(int) (d_price-(d_price*0.075));
System.out.println("ticket price is $"+d_price);
}
//senior citizen from any county is given with 50% dicount
else if(age>=65){
price=(price-(price*50)/100);
System.out.println("ticket price is $"+price);
}
else
System.out.println("ticket price is $"+price);
}
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter the age ");
int age=sc.nextInt();
System.out.println("Enter the county");
String county=sc.next();
//Price are 40 for person from Warren county and 30 for person from other county
if(age>0) // valid age
{
int base_price=(county.equalsIgnoreCase("Warren")?30:40);
Version2.display(age, county,base_price);}
else
System.out.println("invalid age...");
}
}output |
Beta Was this translation helpful? Give feedback.
-
Version 1island.javaimport java.util.Scanner;
public class Island {
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int age,bprice=30,price;
System.out.print("Enter the age of the person: ");
age=Integer.parseInt(sc.nextLine());
if(age>=65)
{
System.out.println("50 % Discount applied");
price=bprice-((bprice*5)/10);
}
else
{
price=bprice;
}
System.out.println(String.format("Price = $%d ",price));
sc.close();
}
}OutputVersion 2islandversion2.javaimport java.util.Scanner;
public class islandversion2 {
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int age;
double price=40,bprice=40;
String county;
System.out.print("Enter the age:");
age=Integer.parseInt(sc.nextLine());
if(age>0)
{
System.out.print("Enter the county:");
county=sc.nextLine();
county=county.toLowerCase();
if(age<5)
{
price=0;
System.out.println("Its free for kids under the age 1 to 5");
}
else if(county.equals("warren"))
{
price=30;
if(age>=65)
{
System.out.println("50 % Discount applied");
price=bprice-((bprice*5)/10);
}
}
else if(age>=65)
{
System.out.println("50 % Discount applied");
price=bprice-((bprice*5)/10);
if(county.equals("campbell"))
{
System.out.println("Additional discount of 7.5% applied");
price=(price*0.75);
}
}
else if((age>=5 && age<14) && county.equals("clermont"))
{
price=bprice-((bprice*18)/100);
}
if(price!=0)
System.out.println(String.format("Price = $%.2f",price));
}
else
{
System.out.println("Enter a valid age");
}
sc.close();
}
}Output |
Beta Was this translation helpful? Give feedback.
-
Version 1import java.util.Scanner;
class Version1{
public static void main(String[] args) {
Scanner sc= new Scanner(System.in);
System.out.println("Enter the age ");
int age = sc.nextInt();
int ticketprice= 30;int discount=50;
if(age>=65){// Check the age of senior citizen and apply the discount
ticketprice=(ticketprice*discount)/100;
System.out.printf("\nThe ticket price is %d $:",ticketprice);
}
else {// Other than senior citizen no discount on ticket price
System.out.printf("%d $",ticketprice);
}
sc.close();
}
}Version 2import java.util.Scanner;
public class Version2 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the age of the person");
int age = sc.nextInt();
System.out.println("Enter the county of the person");
String county =sc.next();
int ticketprice;
int discount=0;
if(age<=0) { // To check the age of the person is valid or not
System.out.println("Invalid age: ");
System.exit(0);
}
else if(age<5) // To check the age less than 5 then ticketprice is 0
{
ticketprice=0;
System.out.println("The ticketprice is :"+ticketprice);
}
// To check senior citizen age and county and print accordingly
else if (age>=65 && county.equalsIgnoreCase("warren")){
ticketprice=30;
discount=(int) (ticketprice*50)/100;
ticketprice=ticketprice-discount;
System.out.println("price" +ticketprice);
}
else if (age>=65){
if(county.equalsIgnoreCase("Campbell")){
ticketprice=40;
discount=(int) ((ticketprice*0.5)+ticketprice*7.5/100);
ticketprice=ticketprice-discount;
System.out.println("price" +ticketprice);
}
else{
ticketprice=40;
ticketprice=ticketprice-(ticketprice*50)/100;
}
}
// To check age less than 14 and county clermont then discount 18 percent of ticket price else no discount.
else if(age<14 && county.equalsIgnoreCase("clermont")){
ticketprice=40;
discount=(int) (ticketprice*18)/100;
int new_price=ticketprice-discount;
System.out.println("The ticketprice is :"+ new_price);}
else{
ticketprice=40;
System.out.println("The ticketprice is "+ticketprice);
}
sc.close();
}
} |
Beta Was this translation helpful? Give feedback.
-
Version 1import java.util.Scanner;
public class Version1 {
public static void main(String[] args) {
int ticketPrice=30;
Scanner sc=new Scanner(System.in);
System.out.println("Enter the Age:");
int age=sc.nextInt();
if(age>=65){
ticketPrice-=ticketPrice*0.5;
}
System.out.println("Price:: $"+ticketPrice);
sc.close();
}
}Version 2import java.util.Scanner;
public class Version2 {
public static void main(String[] args) {
Scanner sc =new Scanner(System.in);
System.out.print("Enter the Age::");
int age=sc.nextInt();
if (age<=0){
System.out.println("ERROR:: Invalid Age.");
}
else{
System.out.print("Enter the Coutry::");
String country=sc.next();
int ticketPrice=country.equalsIgnoreCase("Warren")?30:40;
if(age>=65){
ticketPrice-=ticketPrice*0.5;
if(country.equalsIgnoreCase("Campbell")){
// Ticket Price for the person with age greater than 65 and living in campbell will have Extra Dicount
ticketPrice-=ticketPrice*0.075;
}
}
else if(age<=14 && country.equalsIgnoreCase("Clermont")){
ticketPrice -= (ticketPrice*18)/100;
}
else if(age<5){
ticketPrice=0;
}
System.out.println(" Price : $"+ticketPrice);
}
sc.close();
}
} |
Beta Was this translation helpful? Give feedback.
-
1 Version 1import java.util.*;
public class Version1{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
System.out.println("enter the age : ");
int age = sc.nextInt();
int ticket = 30, discount = 50;
if(age >= 65){
ticket = (ticket*discount)/100;
System.out.printf("The price of the ticket is : $%d ",ticket);
}
else{
System.out.printf("The price of the ticket is : $%d",ticket);
}
}
}Output:2 Version 2import java.util.*;
public class Version2{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
System.out.println("enter the age : ");
int age = sc.nextInt();
double ticket;
double discount=0;
if(age<=0){
System.out.println("Invalid age...");
}
else{
System.out.println("Enter the country : ");
String country = sc.next();
if(age<5){
ticket = 0;
System.out.println("The price of the ticket is : $"+ticket);
}
else if(age>=65 && country.equals("Warren")){
ticket = 30;
discount = (ticket*50)/100;
ticket -= discount;
System.out.println("The price of the ticket is : $"+ticket);
}
else if(age>=65){
if(country.equals("Campbell")){
ticket = 40;
discount = ((ticket*0.5)+(ticket*7.5/100));
ticket -= discount;
System.out.println("The price of the ticket is : $"+ticket);
}
else{
ticket = 40;
ticket -= ticket*0.5;
System.out.println("The price of the ticket is : $"+ticket);
}
}
else if(age<=14 && country.equals("Clermont")){
ticket = 40;
discount = (ticket*18)/100;
ticket -= discount;
System.out.println("The price of the ticket is :"+ticket);
}
else{
ticket = 40;
System.out.println("The price of the ticket is :"+ticket);
}
}
}
}Output: |
Beta Was this translation helpful? Give feedback.
-
version 1import java.util.Scanner;
public class AdmissionBooth1 {
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
Scanner sc=new Scanner(System.in);
System.out.println("Enter Your age:");
int age=scanner.nextInt();
int basePriceTicket=30;
int charge=0;
if (age>=65){
charge=(int) (basePriceTicket-(basePriceTicket*0.5));
System.out.println("The Charge is:"+charge);
}else{
charge=basePriceTicket;
System.out.println("The Charge is:"+charge);
}
}
}version 2import java.util.Scanner;
public class AdmissionBooth2 {
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
Scanner sc=new Scanner(System.in);
System.out.println("Enter your age:");
int age = scanner.nextInt();
System.out.println("Enter your counrty");
String country=sc.nextLine();
int charge=0;
int basePriceTicket=0;
if(country.equalsIgnoreCase("Warren")){
charge=30;
charge=basePriceTicket;
}
else{
charge=40;
charge=basePriceTicket;
}
if(age>0){
if(age<5){
System.out.println("The Ticket is free for childrens:");
charge=0;
}
else if(age<14 && country.equalsIgnoreCase("Clermont")){
charge=(int) (basePriceTicket-(basePriceTicket*0.18));
}
else if(age>=65 ){
if(country.equalsIgnoreCase("Campbell")){
double discount=((basePriceTicket*0.5)+(basePriceTicket*0.075));
charge=(int) (basePriceTicket-discount);
}
else{
charge=(int) (basePriceTicket-(basePriceTicket*0.5));
}
}
System.out.println("The TicketPrice is " + charge);
}else{
System.out.println("Invalid Age!");
}
}
} |
Beta Was this translation helpful? Give feedback.
-
Version 1package Project.AdmissionBooth;
import java.util.Scanner;
public class AdmissionBoothVersion1 {
public static void main(String[] args) {
int basePrice=30;
int charge=0;
Scanner sc=new Scanner(System.in);
System.out.print("Enter your age: ");
int ageOfPerson=sc.nextInt();
if(ageOfPerson>=65){
charge=(int) (basePrice-(basePrice*0.5));
}
else{
charge=basePrice;
}
System.out.print("The total price is: $"+charge);
sc.close();
}
}Version 2package Project.AdmissionBooth;
import java.util.Scanner;
public class AdmissionBoothVersion2 {
static void checkPrice(int age,String county,int price)
{
//ticket for children under age 5 is free
if(age<5){
System.out.print("Free Ticket");
}
//18% discount for children under age of 14 from Clermont
else if(age<14 && county.equalsIgnoreCase("clermont")){
int newPrice=(int) (price-(price*0.18));
System.out.print("Price of a ticket : $"+newPrice);
}
//7.5% discount for senior citizen from Campbell with existing discount
else if(age>=65 && county.equalsIgnoreCase("campbell")){
int newPrice=(int) (price-(price*0.5));
newPrice=(int) (newPrice-(newPrice*0.075));
System.out.print("Price of a ticket : $"+newPrice);
}
//50% discount for senior citizen
else if(age>=65){
int newPrice=(int) (price-(price*0.5));
System.out.print("Price of a ticket : $"+newPrice);
}
// price of a ticket for others
else{
System.out.print("Price of a ticket : $"+ price);
}
}
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter a age: ");
int age=sc.nextInt();
System.out.println("Enter the county: ");
String county=sc.next();
//checking for valid age
if(age<=0){
System.out.println("Entry's Denied");
System.exit(0);
}
//setting base price for a ticket
int price=(county.equalsIgnoreCase("warren"))?30:40;
checkPrice(age,county,price);
sc.close();
}
} |
Beta Was this translation helpful? Give feedback.
-
VERSION 1,2import java.util.Scanner;
public class MainClass {
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
int basePrice = 0;
double totalprice = 0;
System.out.print("Enter person age : ");
int age = Integer.parseInt(sc.nextLine());
System.out.print("Enter person country : ");
String country = sc.nextLine();
if(!country.equals("warren")){
basePrice = 40;
}
else{
basePrice = 30;
}
if(age > 0)
{
if(age<5){
basePrice = 0;
}
else if(age < 14 && country.equals("clermont")){
totalprice = basePrice - basePrice*18/100;
}
else if(age >= 65 && country.equals("campbell")){
totalprice = basePrice - basePrice*50/100 ;
totalprice = totalprice - totalprice*7.5/100;
}
else if(age >= 65 && !country.equals("campbell")){
totalprice = basePrice - basePrice*50/100;
}
else{
totalprice = basePrice;
}
System.out.print("Ticket Price "+totalprice);
}
else{
System.out.print("Invalid Age ");
}
}
} |
Beta Was this translation helpful? Give feedback.
-
1 Task 1 version 1public class TaskVersion1 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter your age:");
int age = scanner.nextInt();
double basePrice=30.00;
double ticketPrice=0;
if(age<=0)
{
System.out.println("Invalid Age...");
}
else if(age>=65)
{
ticketPrice=(basePrice*50)/100;
System.out.println("Ticket price for citizens age more than 65 years is $"+(int)ticketPrice);
}
else
{
System.out.println("Ticket price for citizens age less than 65 years is $"+(int)basePrice);
}
scanner.close();
}
}Task 1 version 2public class TaskVersion2 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter your age:");
int age = Integer.parseInt(scanner.nextLine());
System.out.println("Enter you country");
String country = scanner.nextLine();
double basePrice = 40.00;
double ticketPrice = 0;
if (age > 0) {
if (age <= 14 && country.equalsIgnoreCase("Clermont")) {
ticketPrice = basePrice - (basePrice * 18) / 100;
} else if (age <= 14 && age > 5) {
ticketPrice = basePrice;
} else if (age <= 5) {
basePrice = 0;
ticketPrice = basePrice;
} else if (age >= 65) {
if (country.equalsIgnoreCase("Campbell") && (age >= 65)) {
ticketPrice = basePrice - (basePrice * 50) / 100 - (basePrice * 7.5) / 100;
} else if (country.equalsIgnoreCase("warren") && (age >= 65)) {
basePrice = 30.00;
ticketPrice = basePrice - (basePrice * 50) / 100;
} else {
ticketPrice = basePrice - (basePrice * 50) / 100;
}
} else if (age < 65) {
if (country.equalsIgnoreCase("warren")) {
basePrice = 30.00;
ticketPrice = basePrice;
} else {
ticketPrice = basePrice;
}
}
System.out.printf("Ticket price is $%.2f", ticketPrice);
} else {
System.out.println("Invalid age...");
}
scanner.close();
}
} |
Beta Was this translation helpful? Give feedback.
-
Version 1import java.util.Scanner;
public class AdmissionBooth {
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
System.out.println("Enter Your age:");
int age=sc.nextInt();
sc.close();
int basePrice=30;
int charge=0;
if (age>=65){
charge=(int) (basePrice-(basePrice*0.5));
System.out.println("The charge of the ticket is : $%d "+charge);
}else{
charge=basePrice;
System.out.println("The charge of the ticket is : $%d "+charge);
}
}
}Version 2import java.util.Scanner;
public class AdmissionBooth {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter your age:");
int age = sc.nextInt();
System.out.println("Enter your country");
String country = sc.nextLine();
double basePrice = 40.00;
double charge = 0;
if (age > 0) {
if (age <= 14 && country.equalsIgnoreCase("Clermont")) {
charge = basePrice - (basePrice * 18) / 100;
} else if (age <= 14 && age > 5) {
charge = basePrice;
} else if (age <= 5) {
basePrice = 0;
charge = basePrice;
} else if (age >= 65) {
if (country.equalsIgnoreCase("Campbell") && (age >= 65)) {
charge = basePrice - (basePrice * 50) / 100 - (basePrice * 7.5) / 100;
} else if (country.equalsIgnoreCase("warren") && (age >= 65)) {
basePrice = 30.00;
charge = basePrice - (basePrice * 50) / 100;
} else {
charge = basePrice - (basePrice * 50) / 100;
}
} else if (age < 65) {
if (country.equalsIgnoreCase("warren")) {
basePrice = 30.00;
charge = basePrice;
} else {
charge = basePrice;
}
}
System.out.printf("Ticket price is $%.2f", charge);
} else {
System.out.println("Invalid age...");
}
sc.close();
}
} |
Beta Was this translation helpful? Give feedback.
-
1,2public class Citizens {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter age ::");
int age = sc.nextInt();
int basePrice = 0;
int chargePrice = 0;
System.out.println("Enter the Country :: ");
String country = sc.next();
if (country.equalsIgnoreCase("Warren")) basePrice = 30;
else basePrice = 40;
if(age<=0){
System.out.println("You have provide an invalid age!!!");
System.exit(0);
} else if (age < 5) {
chargePrice = 0;
} else if(age<14 && country.equalsIgnoreCase("clermont")) {
chargePrice=(int) (basePrice - (basePrice*0.18));
} else if(age>=65 && country.equalsIgnoreCase("campbell")) {
int discount = (int) (basePrice - (basePrice*0.5));
discount = (int) (discount - (discount*0.075));
chargePrice = discount;
} else if(age>=65) {
chargePrice = (int) (basePrice - (basePrice*0.5));
} else {
chargePrice = basePrice;
}
System.out.println("Charged price :: $"+chargePrice);
}
} |
Beta Was this translation helpful? Give feedback.
-
import java.util.*;
public class task87 {
public static void main(String[] args) {
version1();
version2();
}
public static void version1() {
Scanner scn = new Scanner(System.in);
int baseprice = 30;
int price;
System.out.println("Enter age");
int age = scn.nextInt();
if (age >= 65) {
price = (int) (baseprice - (baseprice * 0.5));
System.out.println("Your ticket price is $" + price);
} else {
System.out.println("Your ticket price is $" + baseprice);
}
}
public static void version2() {
Scanner scn = new Scanner(System.in);
int baseprice = 30;
int price;
System.out.println("Enter age");
int age = scn.nextInt();
if (age > 0) {
System.out.println("Enter your county");
String county = scn.next();
if (age < 5) {
price = 0;
System.out.println("Your ticket price is " + price);
}
if (county.equalsIgnoreCase("Warren")) {
price = 30;
System.out.println("Your ticket price is " + price);
} else if (county != "Warren") {
if (age > 5 && age < 65) {
price = 40;
System.out.println("Your ticket price is " + price);
}
}
if (county.equalsIgnoreCase("Clermont")) {
if (age < 14) {
int bp = 40;
price = (int) (bp - (bp * 0.18));
System.out.println("Your ticket price is $" + price);
}
}
if (age >= 65) {
if (county.equalsIgnoreCase("Campbell")) {
price = (int) (baseprice - (baseprice * 0.575));
System.out.println("Your ticket price is $" + price);
} else {
System.out.println("Your ticket price is $" + baseprice);
}
}
} else {
System.out.println("Enter valid age");
}
}
} |
Beta Was this translation helpful? Give feedback.
-
import java.util.*;
public class D87 {
public static void main(String[] args) {
Version1();
Version2();
}
public static void Version1() {
Scanner sc = new Scanner(System.in);
System.out.println("Enter Your age:");
int age = sc.nextInt();
double basePrice = 30;
double charge = 0;
if (age >= 65) {
charge = basePrice - (basePrice * 0.5);
System.out.printf("The charge of the ticket is : $%f \n ", charge);
} else {
charge = basePrice;
System.out.printf("The charge of the ticket is : $%f \n ", charge);
}
}
public static void Version2() {
Scanner sc = new Scanner(System.in);
System.out.println("enter the age : ");
int age = sc.nextInt();
double ticket;
double discount = 0;
if (age <= 0) {
System.out.println("Invalid age...");
} else {
System.out.println("Enter the country : ");
String country = sc.next();
if (age < 5) {
ticket = 0;
System.out.println("The price of the ticket is : $" + ticket);
} else if (age >= 65 && country.equals("Warren")) {
ticket = 30;
discount = (ticket * 50) / 100;
ticket -= discount;
System.out.println("The price of the ticket is : $" + ticket);
} else if (age >= 65) {
if (country.equals("Campbell")) {
ticket = 40;
discount = ((ticket * 0.5) + (ticket * 7.5 / 100));
ticket -= discount;
System.out.println("The price of the ticket is : $" + ticket);
} else {
ticket = 40;
ticket -= ticket * 0.5;
System.out.println("The price of the ticket is : $" + ticket);
}
} else if (age <= 14 && country.equals("Clermont")) {
ticket = 40;
discount = (ticket * 18) / 100;
ticket -= discount;
System.out.println("The price of the ticket is :" + ticket);
} else {
ticket = 40;
System.out.println("The price of the ticket is :" + ticket);
}
}
}
} |
Beta Was this translation helpful? Give feedback.
-
Codepackage java_programs;
import java.util.*;
import javax.lang.model.util.ElementScanner14;
public class ControlFlowStatement {
public static void main(String[] args) {
fnCalculateTicketPrice();
fnCalculateTicketPrice();
fnCalculateTicketPrice();
fnCalculateTicketPriceAdvanced();
fnCalculateTicketPriceAdvanced();
fnCalculateTicketPriceAdvanced();
}
// In the first version of the program, there is one ticket price of $30.00.
// Senior citizens (age ≥ 65) are given a
// 50% discount. Write this program as follows. Import your Scanner. Declare the
// needed variables (the person’s
// age, the base price of a ticket ($30) and the price you will charge). Input
// the user’s age, compute the price of
// the ticket and output the result in a formatted way (that is, using a $). You
// do not need to use DecimalFormat
// for this part of the program but you will as you enhance it so you might want
// to set this up now. Save, compile
// and run your program a few times, asking the user for different ages such as
// 10, 50, 65, 80 and 0.
public static void fnCalculateTicketPrice() {
Scanner sc = new Scanner(System.in);
System.out.println("enter the age of person: ");
int age = sc.nextInt();
int basePrice = 30;
int finalPrice;
if (age >= 65) {
finalPrice = basePrice - basePrice / 2;
} else {
finalPrice = basePrice;
}
System.out.println("Price of the ticket is: $" + finalPrice);
}
public static void fnCalculateTicketPriceAdvanced() {
Scanner sc = new Scanner(System.in);
System.out.println("enter the age of person: ");
int age = sc.nextInt();
System.out.println("enter the country of person: ");
String country = sc.next();
int basePriceNormal = 40;
int specialBasePrice = 30;
double finalPrice;
if (age > 0) {
if (age >= 65) {
if (country.equalsIgnoreCase("Warren")) {
finalPrice = specialBasePrice - specialBasePrice / 2;
} else {
finalPrice = basePriceNormal - basePriceNormal / 2;
if (country.equalsIgnoreCase("Campbell")) {
finalPrice = finalPrice - (finalPrice * 7.5 / 100);
}
}
}
else if (age < 5) {
finalPrice = 0;
} else if (age < 14) {
if (country.equalsIgnoreCase("Clermont")) {
finalPrice = basePriceNormal - (basePriceNormal * 18) / 100;
} else {
finalPrice = basePriceNormal - basePriceNormal / 2;
}
} else {
if (country.equalsIgnoreCase("Warren")) {
finalPrice = specialBasePrice;
} else {
finalPrice = basePriceNormal;
}
}
System.out.println("Price of the ticket is: $" + finalPrice);
}
else {
System.out.println("Age can't be negative or zero");
}
}
}output |
Beta Was this translation helpful? Give feedback.
-
import java.util.*;
public class d87 {
public static void main(String[] args) {
q1();
q2();
}
public static void q1() {
Scanner obj = new Scanner(System.in);
int baseprice = 30;
int price;
System.out.println("Enter age");
int age = obj.nextInt();
if (age >= 65) {
price = (int) (baseprice - (baseprice * 0.5));
System.out.println("Your ticket price is $" + price);
} else {
System.out.println("Your ticket price is $" + baseprice);
}
}
public static void q2() {
Scanner obj = new Scanner(System.in);
int baseprice = 30;
int price;
System.out.println("Enter age");
int age = obj.nextInt();
if (age > 0) {
System.out.println("Enter your county");
String county = obj.next();
if (age < 5) {
price = 0;
System.out.println("Your ticket price is " + price);
}
if (county.equalsIgnoreCase("Warren")) {
price = 30;
System.out.println("Your ticket price is " + price);
} else if (county != "Warren") {
if (age > 5 && age < 65) {
price = 40;
System.out.println("Your ticket price is " + price);
}
}
if (county.equalsIgnoreCase("Clermont")) {
if (age < 14) {
int bp = 40;
price = (int) (bp - (bp * 0.18));
System.out.println("Your ticket price is $" + price);
}
}
if (age >= 65) {
if (county.equalsIgnoreCase("Campbell")) {
price = (int) (baseprice - (baseprice * 0.575));
System.out.println("Your ticket price is $" + price);
} else {
System.out.println("Your ticket price is $" + baseprice);
}
}
} else {
System.out.println("Enter valid age");
}
}
}screenshot |
Beta Was this translation helpful? Give feedback.
-
import java.lang.Runtime.Version;
import java.util.Scanner;
public class discussion87 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the age:");
int age = Integer.parseInt(sc.nextLine());
versionOne(age);
System.out.println("Enter the county name:");
String county = sc.nextLine();
versionTwo(age, county);
}
private static void versionTwo(int age, String county) {
int basePrice = 40;
int charge = 0;
if (age > 0) {
if (age >= 65 && county.equalsIgnoreCase("Warren")) {
charge = (int) (basePrice - (30 * 0.5));
} else if (age >= 65) {
if ((county.equalsIgnoreCase("Campbell"))) {
charge = (int) (basePrice - (basePrice * 0.5) - basePrice * 7.5 / 100);
} else {
charge = (int) (basePrice - basePrice * 0.5);
}
} else if (age < 5) {
charge = 0;
}
else if (county.equalsIgnoreCase("Warren")) {
charge = 30;
} else if (age < 14 && county.equalsIgnoreCase("Clermont")) {
charge = basePrice - (basePrice * 18) / 100;
} else {
charge = basePrice;
}
System.out.printf("The charge is %d$", charge);
} else {
System.out.println("Error Invalid Age :(");
}
}
private static void versionOne(int age) {
int basePrice = 30;
int charge = 0;
if (age >= 65) {
charge = (int) (basePrice - basePrice * 0.5);
} else
charge = basePrice;
System.out.printf("The charge is %d $", charge);
}
} |
Beta Was this translation helpful? Give feedback.
-
version 1import java.util.Scanner;
public class Switch {
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
int baseprice = 30;
int price;
System.out.println("Enter age");
int age = scn.nextInt();
if (age >= 65) {
price = (int) (baseprice - (baseprice * 0.5));
System.out.println("Your ticket price is $" + price);
} else {
System.out.println("Your ticket price is $" + baseprice);
}
}
}version 2import java.util.Scanner;
public class Switch {
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
int baseprice = 30;
int price;
System.out.println("Enter age");
int age = scn.nextInt();
if (age > 0) {
System.out.println("Enter your county");
String county = scn.next();
if (age < 5) {
price = 0;
System.out.println("Your ticket price is " + price);
}
if (county.equalsIgnoreCase("Warren")) {
price = 30;
System.out.println("Your ticket price is " + price);
}
else if (county != "Warren" && (age > 5 && age < 65)) {
price = 40;
System.out.println("Your ticket price is " + price);
}
if (county.equalsIgnoreCase("Clermont") && (age < 14)) {
int bp = 40;
price = (int) (bp - (bp * 0.18));
System.out.println("Your ticket price is $" + price);
}
if (age >= 65) {
if (county.equalsIgnoreCase("Campbell")) {
price = (int) (baseprice - (baseprice * 0.575));
System.out.println("Your ticket price is $" + price);
}
else {
System.out.println("Your ticket price is $" + baseprice);
}
}
}
else {
System.out.println("Enter valid age");
}
}
} |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.


































Uh oh!
There was an error while loading. Please reload this page.
-
King’s Island needs a program for its admission booths. When visitors to the park come up to the booth to
purchase their tickets, the worker uses this program to figure out how much to charge them. You will write
this program.
Version 1
In the first version of the program, there is one ticket price of $30.00. Senior citizens (age ≥ 65) are given a
50% discount. Write this program as follows. Import your Scanner. Declare the needed variables (the person’s
age, the base price of a ticket ($30) and the price you will charge). Input the user’s age, compute the price of
the ticket and output the result in a formatted way (that is, using a $). You do not need to use DecimalFormat
for this part of the program but you will as you enhance it so you might want to set this up now. Save, compile
and run your program a few times, asking the user for different ages such as 10, 50, 65, 80 and 0.
Version 2
The park wants to add further alterations to ticket costs. Children under 5 (less than 5, not less than or equal
to) are free. Ticket prices are now $40 unless the person is from Warren County in which case the tickets are
$30. Senior citizens still receive a 50% discount regardless of their county of residence. There are two input
parameters now, one for age and one for county. For the county, input this as a String. See below about how
to compare Strings. Calculate and output the ticket price. Ticket prices should be either 0, $15, $20, $30 or
$40 depending on the person’s age and location.
To compare Strings, we cannot use ==. Instead, we use a String message called equals. The format is
string.equals(string2). For instance, if your input variable is called county, then to see if county is Warren,
you would use county.equals(“Warren”). There is also equalsIgnoreCase if you do not want to
force the user to use proper capitalization. Save, compile and run your program using a variety of inputs to
make sure it works correctly.
We have two additional changes to make to the program. The first is that we want to ensure that the user’s age
is valid. A valid age is anything > 0. Use an if statement that will test to see if age <= 0 and if so, output
an error message that it is an invalid age. Otherwise, continue with the remainder of your code. You will have
to place the remainder of your code in a block.
Second, we want to give discounts to people from other counties as follows. Children under 14 from Clermont
County get an 18% discount (over the base price of $40) and senior citizens (>= 65) from Campbell County
get an additional 7.5% discount over their senior citizen discount. There are two ways you can do multiple
tests. The first is to use two nested if statements. For instance, if you want to test to see if someone is male
(‘M’) and age is greater than 20, you could do:
Or, you can combine two tests in one conditional by ANDing the tests together. In Java, AND is indicated
using &&. The above code can be rewritten as if(sex==‘M’&&age>20). Use whichever approach you
prefer for the two new possibilities (Clermont children, Campbell seniors). Revise your program accordingly.
Compile, run and test your program. At this point, make sure you are formatting your output to be of the form
$xx.yy. Note: a 7.5% discount can be computed in two ways. You can do amount=amountamount*.075; (that is, subtract from amount 7.5% of amount) or amount=amount*.925; (make
amount be 92.5% of its current value).
As this is a challenging set of logic, you might want to write down on paper all the conditions, how to determine
each one and how to compute the cost. Also, make sure you have commented your code well.
Beta Was this translation helpful? Give feedback.
All reactions