In: Computer Science
3) Create a Java program that uses NO methods, but use scanner:
Write a program where you will enter the flying distance from one continent to another, you will take the plane in one country, then you will enter miles per gallon and price of gallon and in the end it will calculate how much gas was spend for that distance in miles.
Steps: 1) Prompt user to enter the name of country that you are
2) Declare variable to enter and relate to scanner.
3) Prompt user to enter the name of country where you are willing to go.
4) Declare variable to enter and relate to scanner.
5) Create scanner, also declare variable double for distance, miles Per Gallon and price Per Gallon.
6) Prompt the user to enter data of your choice for each one of them
7) Create variable ticket which calculate distance divided by multiplication of miles Per Gallon and price Per Gallon
8) Create condition that if ticket cost is less then 1000 you are flying close, if greater then 1000 and less then 5000 they are flying far, if greater then 10000 very far and son on
9) In the end display the miles from one country to another and cost of flying and how far you are flying based on conditions.
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter the name of the country that
are travelling from: ");
String from = sc.next();
System.out.print("Enter the name of country where you
are willing to go: ");
String to = sc.next();
double distance,mpg,ppg;
System.out.print("Enter distance: ");
distance=sc.nextDouble();
System.out.print("Enter miles Per Gallon: ");
mpg=sc.nextDouble();
System.out.print("Enter price Per Gallon: ");
ppg=sc.nextDouble();
double ticket = distance/(mpg*ppg);
String str="";
if(ticket<1000)
{
str="you are flying close";
}
else if(ticket>1000&&ticket<5000)
{
str="you are flying far";
}
else
{
str="you are very far";
}
System.out.println("Miles from "+from+" to "+to+" =
"+distance);
System.out.println("Cost of flying = "+ticket);
System.out.println(str);
}
}