Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class TripPlanner {
- public static void main(String[] args) {
- Scanner in = new Scanner(System.in);
- // Pass a Scanner object instead of creating a Scanner object in every single method!
- greeting(in);
- travelTimeAndBudget(in);
- timeDifference(in);
- countryArea(in);
- howFar(in);
- }
- public static void greeting(Scanner input) {
- System.out.println("Welcome to Vacation Planner!");
- System.out.print("What is your name? ");
- String name = input.nextLine();
- System.out.print("Nice to meet you " + name + ", where are you travelling to? ");
- String place = input.nextLine();
- System.out.println("Great! " + place + " sounds like a great trip!");
- }
- public static void travelTimeAndBudget(Scanner input) {
- final int HOURS_IN_DAY = 24;
- final int MINUTES_IN_DAY = HOURS_IN_DAY * 60;
- System.out.print("How many days are you going to spend travelling? ");
- int days = Integer.parseInt(input.nextLine());
- while (days <= 0) {
- System.out.println("Input must be positive!");
- System.out.print("How many days are you going to spend travelling? ");
- days = Integer.parseInt(input.nextLine());
- }
- System.out.print("How much money, in USD, are you planning to spend on your trip? ");
- double money = Double.parseDouble(input.nextLine());
- while (money < 0) {
- System.out.println("Input must be positive!");
- System.out.print("How much money, in USD, are you planning to spend on your trip? ");
- money = Double.parseDouble(input.nextLine());
- }
- System.out.print("What is the three letter currency symbol for your travel destination? ");
- String currency = input.nextLine();
- System.out.print("How much " + currency + " are there in 1 USD? ");
- double conversion = Double.parseDouble(input.nextLine());
- while (conversion < 0) {
- System.out.println("Input must be positive!");
- System.out.print("How much " + currency + " are there in 1 USD? ");
- conversion = Double.parseDouble(input.nextLine());
- }
- // NO NEED TO CONVERT ANY FACTOR TO AN INT!
- double budgetPerDayInUSD = (100 * money / days) / 100.0;
- double budgetPerDay = ((100 * conversion * money / days) / 100.0);
- System.out.println(); // prints whitespace for readability!
- System.out.printf("If you are travelling for %,d days, that is the same as %,d hours or " +
- "%,d minutes.\n", days, (days * HOURS_IN_DAY), days * MINUTES_IN_DAY);
- System.out.printf("If you are going to spend $%,.2f USD, that means per day you can " +
- "spend up to $%,.2f USD.\n",money, budgetPerDayInUSD);
- System.out.printf("Your total budget in %s is %,.2f, which per day is %,.2f\n",
- currency, (conversion * money), budgetPerDay);
- }
- public static void timeDifference(Scanner input) {
- System.out.print("What is the difference, in hours, between your home and your destination? ");
- int hourDifference = Integer.parseInt(input.nextLine());
- // WHAT HAPPENS IF THE int entered by the user is negative???
- while (hourDifference < 0) {
- System.out.println("Hours must e positive!");
- System.out.print("What is the difference, in hours, between your home and your destination? ");
- hourDifference = Integer.parseInt(input.nextLine());
- }
- System.out.println(); // prints whitespace for readability!
- System.out.println("That means that when it is midnight at home, it will be "
- + hourDifference + ":00 in your travel destination and when it's " +
- "noon at home, it will be " + (hourDifference + 12) + ":00");
- }
- public static void countryArea(Scanner input) {
- System.out.print("What is the square area of your destination country in km2? ");
- double area = Double.parseDouble(input.nextLine());
- while (area <= 0) {
- System.out.println("Area must be positive and > 0!");
- System.out.print("What is the square area of your destination country in km2? ");
- area = Double.parseDouble(input.nextLine());
- }
- double areaInSqMi = (38.6102 * area) / 100.0;
- System.out.println(); // prints whitespace for readability!
- System.out.printf("In square miles that is %,.2f\n", areaInSqMi);
- }
- public static void howFar(Scanner input) {
- final double R = 6367.51;
- System.out.print("What are the latitude coordinates in degrees of your destination city? " +
- "(positive for north, negative for south) ");
- double latDest = Double.parseDouble(input.nextLine());
- System.out.print("What are the longitude coordinates in degrees of your destination city?" +
- "(positive for east, negative for west) ");
- double longDest = Double.parseDouble(input.nextLine());
- System.out.print("What are the latitude coordinates in degrees of your home city? " +
- "(positive for north, negative for south) ");
- double latHome = Double.parseDouble(input.nextLine());
- System.out.print("What are the longitude coordinates in degrees of your home city? " +
- "(positive for east, negative for west) ");
- double longHome = Double.parseDouble(input.nextLine());
- System.out.println(); // prints whitespace for readability!
- latDest *= Math.PI / 180;
- longDest *= Math.PI / 180;
- latHome *= Math.PI / 180;
- longHome *= Math.PI / 180;
- double havLat = (1 - Math.cos(latDest - latHome)) / 2;
- double havLong = (1 - Math.cos(longDest - longHome)) / 2;
- double haversine = havLat + Math.cos(latHome) * Math.cos(latDest) * havLong;
- double distance = 2 * R * Math.asin(Math.sqrt(haversine));
- double distanceBetCityAndDest = (distance * 100) / 100.0;
- System.out.printf("The distance between your home city and your destination is %,.2f km" +
- ".\n",distanceBetCityAndDest);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment