Question

In: Computer Science

NEED IN JAVA ECLIPSE For this lab, you will create a program that uses method overloading...

NEED IN JAVA ECLIPSE

For this lab, you will create a program that uses method overloading and a switch statement.

Requirements:

The Patapsco River Valley Running Festival needs a program that will allow them to calculate

the cost of the entry fee for each group of entrants. Once the cost is calculated, it is then

displayed to the user.

The program also totals up the total amount of money that will be collected and displays the total

at the very end when the user states that they have no more entries.

You will use the program you wrote in Lab 3 as the starting point for this lab. All of the data

entry for each program is exactly the same. However, the program now displays the cost of the

race entered and the total cost of all of the races.

CODE FROM LAB 3:

/**
*
*/
package volunteers;

/**
*
*
* This program will create a counter which will count the number of Howard County residents attending the event versues the number of
* non- Howard County residents. The program will also tally the number of runners in each of the race lengths. Finally, the program
* will collect the age of the runner.
*/
import java.util.Scanner;
public class Volunteers {
  
   // Instantiate scanner
static Scanner input=new Scanner(System.in);
public static void main(String[] args)
{
  
// declaring and initializing variables
int total_runner=0;
int Howard_County_Residents=0;
int non_Howard_County_Residents=0;
int total_children=0;
int total_adults=0;
int total_senior=0;
int total_5k=0;
int total_10k=0;
int total_HalfMarathon=0;
int total_FullMarathon=0;
int total_fun_run=0;
int age,runChoice;
String userChoice;
while(true)
{
userChoice=takeUserInput("\n\nWould you like to add a runner? Enter 'yes' or 'no'.");
if(userChoice.equals("no"))
break;
total_runner++;
  
userChoice=takeUserInput("\n\nIs the Runner a Howard County resident? Enter 'Yes' or 'No':");
if(userChoice.equals("yes"))
Howard_County_Residents++;
else
non_Howard_County_Residents++;
  
while(true)
{
System.out.println("\n\nWhat is the runner's age?");
age=input.nextInt();
if(age>0)
break;
else
System.out.println("\n\nInvalid Entry. Age must be greater than 0. Please reenter:");
}
  
if(age<10 || age>=70)
{
if(age<10)
total_children++;
else
total_senior++;
while(true)
{
System.out.println("\nSelect the event the runner will be running in:\r\n" +
"1 - 5K\r\n" +   
"2 - 10K\r\n" +   
"3 - Half Marathon\r\n" +   
"4 - Full Marathon\n5 - 1 Mile Fun Run");
runChoice=input.nextInt();
if(runChoice>=1 && runChoice<=5)
break;
else
System.out.println("\nInvalid Entry. Please select a value from the menu. Please reenter:");
}
}
else
{
total_adults++;
while(true)
{
System.out.println("\nSelect the event the runner will be running in:\r\n" +
"1 - 5K\r\n" +
"2 - 10K\r\n" +
"3 - Half Marathon\r\n" +
"4 - Full Marathon");
runChoice=input.nextInt();
if(runChoice>=1 && runChoice<=4)
break;
else
System.out.println("\nInvalid Entry. Please select a value from the menu. Please reenter:");
}
}
if(runChoice==1)
total_5k++;
else if(runChoice==2)
total_10k++;
else if(runChoice==3)
total_HalfMarathon++;
else if(runChoice==4)
total_FullMarathon++;
else
total_fun_run++;
  
} // printing the stats
System.out.println("\nTotal Runners: "+total_runner);
System.out.println("Total Howard County Residents: "+Howard_County_Residents);
System.out.println("Total Non-Howard County Residents: "+non_Howard_County_Residents);

System.out.println("\nTotal number of children: "+total_children);
System.out.println("Total number of adults: "+total_adults);
System.out.println("Total number of seniors: "+total_senior);

System.out.println("\nTotal 5K Runners: "+total_5k);
System.out.println("Total 10K Runners: "+total_10k);
System.out.println("Total Half Marathon Runners: "+total_HalfMarathon);
System.out.println("Total Half Marathon Runners: "+total_FullMarathon);
System.out.println("Total Fun Run Participants: "+total_fun_run);
  
}
public static String takeUserInput(String s)
{
String choice;
while(true)
{
System.out.println(s);
choice=input.next();
if(choice.equals("yes") || choice.equals("no"))
break;
else
{
System.out.print("\nInvalid Entry. Only 'yes' or 'no' is acceptable. Please reenter:");
}
}
return choice;

} // End of main method
} // End of class Volunteer

This enhanced program will need to do the following calculations:

Race Event- 5K

Howard County Res Cost- $25

Non-Howard County Res Cost- $40

Race Event- 10K

Howard County Res Cost- $35

Non-Howard County Res Cost- $50

Race Event- Half Marathon

Howard County Res Cost- $65

Non-Howard County Res Cost- $85

Race Event- Full Marathon

Howard County Res Cost- $85

Non-Howard County Res Cost- $115

Race Event- Fun Run

Howard County Res Cost- $20

Non-Howard County Res Cost- $25

Non-Howard County residents who are 18 years of age or younger or 60 years of age or older get

a $5 discount on their entry fee. There are no discounts for county residents.

You should use a switch statement to implement the table, and you should not use the reserved

word "return" within the switch statement.

NOTE: There should be only ONE named method (an overloaded method is acceptable) to

determine and return the cost of the event for Howard County residents and non-Howard County

residents. Use the method parameters to distinguish between cases.

You should declare constant values for all of the costs listed in the table.

This code should all be contained within one class file.

OUTPUT SHOULD LOOK LIKE THIS:

Would you like to add a runner? Enter 'yes' or 'no'.

test

Invalid Entry. Only 'yes' or 'no' is acceptable. Please reenter:

Would you like to add a runner? Enter 'yes' or 'no'.

yes

Is the Runner a Howard County resident? Enter 'Yes' or 'No':

yes

What is the runner's age?

-2

Invalid Entry. Age must be greater than zero. Please reenter:

What is the runner's age?

32

Select the event the runner will be runnning in:

1 - 5K

2 - 10K

3 - Half Marathon

4 - Full Marathon

3

You have entered the Half Marathon race. The cost is $65.00

Would you like to add a runner? Enter 'yes' or 'no'.

yes

Is the Runner a Howard County resident? Enter 'Yes' or 'No':

no

What is the runner's age?

43

Select the event the runner will be runnning in:

1 - 5K

2 - 10K

3 - Half Marathon

4 - Full Marathon

3

You have entered the Half Marathon race. The cost is $85.00

Would you like to add a runner? Enter 'yes' or 'no'.

yes

Is the Runner a Howard County resident? Enter 'Yes' or 'No':

no

What is the runner's age?

62

Select the event the runner will be runnning in:

1 - 5K

2 - 10K

3 - Half Marathon

4 - Full Marathon

3

You have entered the Half Marathon race. The cost is $80.00

Would you like to add a runner? Enter 'yes' or 'no'.

yes

Is the Runner a Howard County resident? Enter 'Yes' or 'No':

no

What is the runner's age?

9

Select the event the runner will be runnning in:

1 - 5K

2 - 10K

3 - Half Marathon

4 - Full Marathon

5 - 1 Mile Fun Run

5

You have entered the Fun Run race. The cost is $20.00

Would you like to add a runner? Enter 'yes' or 'no'.

yes

Is the Runner a Howard County resident? Enter 'Yes' or 'No':

no

What is the runner's age?

75

Select the event the runner will be runnning in:

1 - 5K

2 - 10K

3 - Half Marathon

4 - Full Marathon

5 - 1 Mile Fun Run

2

You have entered the 10K race. The cost is $45.00

Would you like to add a runner? Enter 'yes' or 'no'.

yes

Is the Runner a Howard County resident? Enter 'Yes' or 'No':

no

What is the runner's age?

23

Select the event the runner will be runnning in:

1 - 5K

2 - 10K

3 - Half Marathon

4 - Full Marathon

4

You have entered the Full Marathon race. The cost is $115.00

Would you like to add a runner? Enter 'yes' or 'no'.

no

Total Runners: 6

Total Howard County Residents: 1

Total Non-Howard County Residents: 5

Total number of children: 1

Total number of adults: 4

Total number of seniors: 1

Total 5K Runners: 0

Total 10K Runners: 1

Total Half Marathon Runners: 3

Total Full Marathon Runners: 1

Total Fun Run Participants: 1

The total cost of all of the races entered is $410.00

Solutions

Expert Solution

Program

import java.util.Scanner;

public class Volunteers {

   // Instantiate scanner
   static Scanner input = new Scanner(System.in);

   public static void main(String[] args) {

// declaring and initializing variables
       int total_runner = 0;
       int Howard_County_Residents = 0;
       int non_Howard_County_Residents = 0;
       int total_children = 0;
       int total_adults = 0;
       int total_senior = 0;
       int total_5k = 0;
       int total_10k = 0;
       int total_HalfMarathon = 0;
       int total_FullMarathon = 0;
       int total_fun_run = 0;
       int age, runChoice;
       String userChoice;

       boolean isHoward = true;
       boolean isDiscount = false;
       double cost, totalCost = 0;

       while (true) {
           userChoice = takeUserInput("\n\nWould you like to add a runner? Enter 'yes' or 'no'.");
           if (userChoice.equals("no"))
               break;
           total_runner++;

           userChoice = takeUserInput("\n\nIs the Runner a Howard County resident? Enter 'Yes' or 'No':");
           if (userChoice.equals("yes"))
               Howard_County_Residents++;
           else {
               isHoward = false;
               non_Howard_County_Residents++;
           }

           while (true) {
               System.out.println("\n\nWhat is the runner's age?");
               age = input.nextInt();
               if (age > 0)
                   break;
               else
                   System.out.println("\n\nInvalid Entry. Age must be greater than 0. Please reenter:");
           }

           if (!isHoward && age <= 18 || age >= 60)
               isDiscount = true;

           if (age < 10 || age >= 70) {
               if (age < 10)
                   total_children++;
               else
                   total_senior++;
               while (true) {
                   System.out.println("\nSelect the event the runner will be running in:\r\n" + "1 - 5K\r\n"
                           + "2 - 10K\r\n" + "3 - Half Marathon\r\n" + "4 - Full Marathon\n5 - 1 Mile Fun Run");
                   runChoice = input.nextInt();
                   if (runChoice >= 1 && runChoice <= 5)
                       break;
                   else
                       System.out.println("\nInvalid Entry. Please select a value from the menu. Please reenter:");
               }
           } else {
               total_adults++;
               while (true) {
                   System.out.println("\nSelect the event the runner will be running in:\r\n" + "1 - 5K\r\n"
                           + "2 - 10K\r\n" + "3 - Half Marathon\r\n" + "4 - Full Marathon\r\n" + "5 - Fun Run");
                   runChoice = input.nextInt();
                   if (runChoice >= 1 && runChoice <= 5)
                       break;
                   else
                       System.out.println("\nInvalid Entry. Please select a value from the menu. Please reenter:");
               }
           }

           cost = determineCost(isHoward, isDiscount, runChoice);

           switch (runChoice) {// using switch case
           case 1:
               total_5k++;
               System.out.println("You have entered the 5k race. The cost is $" + cost);
               break;
           case 2:
               total_10k++;
               System.out.println("You have entered the 10k race. The cost is $" + cost);
               break;
           case 3:
               total_HalfMarathon++;
               System.out.println("You have entered the Half Marathon race. The cost is $" + cost);
               break;
           case 4:
               total_FullMarathon++;
               System.out.println("You have entered the Full Marathon race. The cost is $" + cost);
               break;
           case 5:
               total_fun_run++;
               System.out.println("You have entered the Fun Run race. The cost is $" + cost);
               break;
           }

           totalCost += cost;

           // reseting the value
           isHoward = true;
           isDiscount = false;

       } // printing the stats
       System.out.println("\nTotal Runners: " + total_runner);
       System.out.println("Total Howard County Residents: " + Howard_County_Residents);
       System.out.println("Total Non-Howard County Residents: " + non_Howard_County_Residents);

       System.out.println("\nTotal number of children: " + total_children);
       System.out.println("Total number of adults: " + total_adults);
       System.out.println("Total number of seniors: " + total_senior);

       System.out.println("\nTotal 5K Runners: " + total_5k);
       System.out.println("Total 10K Runners: " + total_10k);
       System.out.println("Total Half Marathon Runners: " + total_HalfMarathon);
       System.out.println("Total Half Marathon Runners: " + total_FullMarathon);
       System.out.println("Total Fun Run Participants: " + total_fun_run);
       System.out.println("The total cost of all of the races entered is $" + totalCost);
   }

   public static String takeUserInput(String s) {
       String choice;
       while (true) {
           System.out.println(s);
           choice = input.next();
           if (choice.equals("yes") || choice.equals("no"))
               break;
           else {
               System.out.print("\nInvalid Entry. Only 'yes' or 'no' is acceptable. Please reenter:");
           }
       }
       return choice;

   }

   // for calculating cost
   public static double determineCost(boolean isHoward, boolean isDiscount, int runChoice) {

       // constants of races price
       final double FIVE_K_HOWARD = 25;
       final double FIVE_K_COUNTY = 40;
       final double TEN_K_HOWARD = 35;
       final double TEN_K_COUNTY = 50;
       final double HALF_K_HOWARD = 65;
       final double HALF_K_COUNTY = 85;
       final double FULL_K_HOWARD = 85;
       final double FULL_K_COUNTY = 115;
       final double FUN_K_HOWARD = 20;
       final double FUN_K_COUNTY = 25;

       double cost = 0;

       switch (runChoice) {

       case 1:
           cost = isHoward ? FIVE_K_HOWARD : FIVE_K_COUNTY;
           break;

       case 2:
           cost = isHoward ? TEN_K_HOWARD : TEN_K_COUNTY;
           break;
       case 3:
           cost = isHoward ? HALF_K_HOWARD : HALF_K_COUNTY;
           break;
       case 4:
           cost = isHoward ? FULL_K_HOWARD : FULL_K_COUNTY;
           break;
       case 5:
           cost = isHoward ? FUN_K_HOWARD : FUN_K_COUNTY;
           break;
       }

       if (isDiscount)// providing discount
           cost = cost - 5;

       return cost;
   }
}

Output

Would you like to add a runner? Enter 'yes' or 'no'. yes Is the Runner a Howard County resident? Enter 'Yes' or 'No': yes What is the runner's age? 15 Select the event the runner will be running in: 1 - 5K 2 - 10K 3 - Half Marathon 4 - Full Marathon 5 - Fun Run You have entered the Half Marathon race. The cost is $61.75 Would you like to add a runner? Enter 'yes' or 'no'. yes Is the Runner a Howard County resident? Enter 'Yes' or 'No': no What is the runner's age? 25

Select the event the runner will be running in: 1 - 5K 2 - 10K 3 - Half Marathon 4 - Full Marathon 5 - Fun Run You have entered the Fun Run race. The cost is $25.0 Would you like to add a runner? Enter 'yes' or 'no'. no Total Runners: 2 Total Howard County Residents: 1 Total Non-Howard County Residents: 1 Total number of children: 0 Total number of adults: 2 Total number of seniors: 0 Total 5K Runners: Total 10K Runners: 0 Total Half Marathon Runners: 1 Total Half Marathon Runners: Total Fun Run Participants: 1 The total cost of all of the races entered is $86.75

Would you like to add a runner? Enter 'yes' or 'no'. yes Is the Runner a Howard County resident? Enter 'Yes' or 'No': yes What is the runner's age? 15 Select the event the runner will be running in: 1 - 5K 2 - 10K 3 - Half Marathon 4 - Full Marathon 5 - Fun Run You have entered the Half Marathon race. The cost is $61.75 Would you like to add a runner? Enter 'yes' or 'no'. yes Is the Runner a Howard County resident? Enter 'Yes' or 'No': no What is the runner's age? 25

Select the event the runner will be running in: 1 - 5K 2 - 10K 3 - Half Marathon 4 - Full Marathon 5 - Fun Run You have entered the Fun Run race. The cost is $25.0 Would you like to add a runner? Enter 'yes' or 'no'. no Total Runners: 2 Total Howard County Residents: 1 Total Non-Howard County Residents: 1 Total number of children: 0 Total number of adults: 2 Total number of seniors: 0 Total 5K Runners: Total 10K Runners: 0 Total Half Marathon Runners: 1 Total Half Marathon Runners: Total Fun Run Participants: 1 The total cost of all of the races entered is $86.75


Related Solutions

For this coding exercise, you need to create a new Java project in Eclipse and finish...
For this coding exercise, you need to create a new Java project in Eclipse and finish all the coding in Eclipse. Run and debug your Eclipse project to make sure it works. Then you can just copy and paste the java source code of each file from Eclipse into the answer area of the corresponding box below. The boxes will expand once you paste your code into them, so don’t worry about how it looks J All data members are...
Create a program in java eclipse named “Forecast” and a method called “String getWeather(boolean raining, int...
Create a program in java eclipse named “Forecast” and a method called “String getWeather(boolean raining, int temperature)”. Depending on the parameters, return one of four outcomes: If it’s not raining and under 30 degrees: return “The weather is chilly” If it’s not raining and at/over 30 degrees: return “The weather is sunny” If it’s raining and under 30 degrees: return “The weather is snowy” If it’s raining and at/over 30 degrees: “The weather is rainy” You must use nested if...
I need to write a java program (in eclipse) that will read my text file and...
I need to write a java program (in eclipse) that will read my text file and replace specific placeholders with information provided in a second text file. For this assignment I am given a text file and I must replace <N>, <A>, <G>, with the information in the second file. For example the information can be John 22 male, and the template will then be modified and saved into a new file or files (because there will be multiple entries...
Create a Java Program to calculate luggage costs. USING ECLIPSE IDE The Business Rules are: A....
Create a Java Program to calculate luggage costs. USING ECLIPSE IDE The Business Rules are: A. Two bags per person are free. B. The Excess Bag Charge is $75 per bag. The program needs to do the following: 1. In your main method(),    Create integers to store the number of Passengers and also the total number of bags    Prompt for the number of passengers on a ticket.    Prompt for the total number of bags for all passengers...
Create a Java Program to show a savings account balance. using eclipse IDE This can be...
Create a Java Program to show a savings account balance. using eclipse IDE This can be done in the main() method. Create an int variable named currentBalance and assign it the value of 0. Create an int variable named amountToSaveEachMonth. Prompt "Enter amount to save each month:" and assign the result to the int variable in step 2. Create an int variable name numberOfMonthsToSave. Prompt "Enter the number of months to save:" and store the input value into the variable...
WRITE A JAVA PROGRAM: For this lab, you will create a 3 x 7 two dimensional...
WRITE A JAVA PROGRAM: For this lab, you will create a 3 x 7 two dimensional array to store how many pounds of food three monkeys eats each day in a week. The 2D array is then passed to functions to find total, average and least amount of food consumption, etc. The program then need to display: the average amount of food the three monkeys ate per day the least amount of food eaten all week by any one monkey....
in java we need to order a list , if we create a program in java...
in java we need to order a list , if we create a program in java what  are the possible ways of telling your program how to move the numbers in the list to make it sorted, where each way provides the required result. list the name of sorting with short explanation
Create a program in java with the following information: Design a program that uses an array...
Create a program in java with the following information: Design a program that uses an array with specified values to display the following: The lowest number in the array The highest number in the array The total of the numbers in the array The average of the numbers in the array Initialize an array with these specific 20 numbers: 26 45 56 12 78 74 39 22 5 90 87 32 28 11 93 62 79 53 22 51 example...
Language: Java Create a TryIt.java program with a main method. You are going to use this...
Language: Java Create a TryIt.java program with a main method. You are going to use this program to demonstrate some things about how Java works. Make your methods here private, because they are not intended to be called from outside the program. For each test below, make sure that you print to the console: 1) What is being tested 2) The desired output 3) The actual output Question to be answered: Should you use == or the String method equals...
You need to create a Java class library to support a program to simulate a Point-...
You need to create a Java class library to support a program to simulate a Point- of-Sale (POS) system. General Requirements: You should create your programs with good programming style and form using proper blank spaces, indentation and braces to make your code easy to read and understand; You should create identifiers with sensible names; You should make comments to describe your code segments where they are necessary for readers to understand what your code intends to achieve. Logical structures...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT