In: Computer Science
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
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