Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

You are working to help GMU build a program to calculate fares for its new bicyc

ID: 3757130 • Letter: Y

Question

You are working to help GMU build a program to calculate fares for its new bicycle taxi service, GMU Bike-N-Go. For each trip, the fare is charged a base rate of $5.14 plus $2.16 per mile. Miles may be specified in decimal values. For example, travelling 0.5 miles would cost $1.08. A bicycle taxi can only travel in one of four directions at a time: north, south, east, or west.

Assume the bicycle taxi starts at position coordinates (0, 0) on a (X, Y) map where X represents east/west movement and Y represents north/south movement. Also assume east is the positive X axis and north is the positive Y axis. Create a program that will continuously prompt the user to enter a direction (e.g. south) and number of miles (e.g. 0.2) until the user has finished entering distances traveled. Once the user has finished entering all distances traveled, display a well-formatted receipt displaying the total distance traveled, fare charged, and final position coordinates.

You must check that each type of user input is valid. When input is invalid, the user must be informed of an error and re-prompted.

You may not ask the user how many distances traveled they will enter.

Your solution must not use arrays.

Remember to parse String data using constructs like Integer.parseInt() and Double.parseDouble() where necessary. Additionally, when using these constructs, don’t forget to include try/catch for good validation.

Drawing out multiple sample bicycle taxi trips on paper (using a grid or graph paper) will help you visualize working with map coordinates.

Explanation / Answer

Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts. If you are satisfied with the solution, please rate the answer. Thanks

// BicycleTaxiFare.java

import java.util.Scanner;

public class BicycleTaxiFare {

      // constants

      private static double BASE_FARE = 5.14;

      private static double COST_PER_MILE = 2.16;

      /**

      * helper method to prompt the user to enter miles traveled in a direction,

      * validate the input and return it

      */

      static double getDistance(Scanner scanner) {

            System.out.println("Enter number of miles: ");

            try {

                  //getting miles

                  double miles = Double.parseDouble(scanner.nextLine());

                  if (miles > 0) {

                        // valid input

                        return miles;

                  }

            } catch (Exception e) {

                  //non numeric input

                  System.out.println("Invalid input, try again!");

            }

            // invalid input, asking again

            return getDistance(scanner);

      }

      public static void main(String[] args) {

            //defining a Scanner

            Scanner scanner = new Scanner(System.in);

            //initializing variables

            double x = 0, y = 0, totalMilesTraveled = 0, miles;

            String choice = ""; //loop controller

            //looping until user wishes to quit

            while (!choice.equalsIgnoreCase("Q")) {

                  //getting direction (N for North, E for East etc)

                  System.out.println("Enter a direction N/E/W/S or Q to finish: ");

                  choice = scanner.nextLine();

                  if (choice.equalsIgnoreCase("N")) {

                        //getting miles traveled in north direction

                        miles = getDistance(scanner);

                        //adding to total miles traveled

                        totalMilesTraveled += miles;

                        y += miles; //adding to y coordinate

                  } else if (choice.equalsIgnoreCase("E")) {

                        miles = getDistance(scanner);

                        totalMilesTraveled += miles;

                        x += miles; //adding to x coordinate

                  } else if (choice.equalsIgnoreCase("S")) {

                        miles = getDistance(scanner);

                        totalMilesTraveled += miles;

                        y -= miles; //subtracting from y coordinate

                  } else if (choice.equalsIgnoreCase("W")) {

                        miles = getDistance(scanner);

                        totalMilesTraveled += miles;

                        x -= miles; //subtracting from x coordinate

                  } else if (!choice.equalsIgnoreCase("Q")) {

                        System.out.println("Invalid choice!");

                  }

            }

            //calculating the total cost

            double cost = BASE_FARE + (totalMilesTraveled * COST_PER_MILE);

            //displaying all stats

            System.out.printf("Total Miles Travelled: %.2f ", totalMilesTraveled);

            System.out.printf("Total Fare: $%.2f ", cost);

            System.out.printf("Ending coordinate (x,y): (%.2f,%.2f) ", x, y);

      }

}

/*OUTPUT*/

Enter a direction N/E/W/S or Q to finish:

N

Enter number of miles:

10

Enter a direction N/E/W/S or Q to finish:

E

Enter number of miles:

11.3

Enter a direction N/E/W/S or Q to finish:

W

Enter number of miles:

2.3

Enter a direction N/E/W/S or Q to finish:

S

Enter number of miles:

8

Enter a direction N/E/W/S or Q to finish:

Q

Total Miles Travelled: 31.60

Total Fare: $73.40

Ending coordinate (x,y): (9.00,2.00)

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote