need help with the output for Case(12 - 18). Can\'t get the correct outputs. imp
ID: 3548170 • Letter: N
Question
need help with the output for Case(12 - 18). Can't get the correct outputs.
import java.util.Scanner;
public class RoundOfGolf
{
public static void main(String[] args)throws Exception {
Scanner keyboard = new Scanner(System.in);
GolfHole method = new GolfHole();
int totalScore = 0;
String description;
int score = 0;
int number;
char again = 'y';
String line = "";
int menuSelect = 18;
int parTotal = 0;
boolean done = false;
int []distance = new int[]{344,334,116,268,329,469,314,134,396,373,309,455,128,312,315,102,271,427};
int []GolfHole = new int[18];
int[]handicap = new int[]{4,2,18,16,12,8,10,14,6,5,11,7,15,9,1,17,13,3};
int[] par = new int[]{4,4,3,4,4,5,4,3,5,4,4,5,3,4,4,3,4,5};
int aces= 0, dEagle = 0,eagle =0,above = 0,below = 0,birdies =0,tpar =0,bogeys =0,
even = 0, dBogeys =0,tBogeys =0;
for(number = 0; number <= GolfHole.length-1; number++)
//for(number = 0; number <=3; ++number)
{
System.out.println("Please Enter your score for hole number " + (number+1));
score = keyboard.nextInt();
totalScore = totalScore + score;
if(score - par[number]== 0)
{
tpar++;
// par++;
}
else if(score - par[number]== 1)
{
bogeys++;
above++;
}
else if(score - par[number] == 2)
{
dBogeys++;
above++;
}
else if(score - par[number] == 3)
{
tBogeys++;
above++;
}
else if(score - par[number] == -1)
{
birdies++;
below++;
}
else if(score - par[number] == -2)
{
eagle++;
below++;
}
else if(score - par[number] == -3)
{
dEagle++;
below++;
}
else if (score == 1)
{
aces++;
below++;
}
}//end for loop
while(again == 'y')
{
System.out.println();
menuSelect();
menuSelect = keyboard.nextInt();
switch (menuSelect) {
case 1:
System.out.println(" Your total score is " + totalScore);
break;
case 2:
System.out.println("Your total stroke above par is " + above);
break;
case 3:
System.out.println("Your total stroke below par is " + below );
break;
case 4:
System.out.println("Your total number of aces is " + aces);
break;
case 5:
System.out.println("Your total number of double eagle is " + dEagle);
break;
case 6:
System.out.println("Your total number of eagle is " + eagle);
break;
case 7:
System.out.println("Your total number of birdies is " + birdies);
break;
case 8:
System.out.println("Your total number of pars is " + tpar);
break;
case 9:
System.out.println("Your total number of bogeys is " + bogeys);
break;
case 10:
System.out.println("Your total number of double bogeys is " + dBogeys);
break;
case 11:
System.out.println("Your total number of triple bogeys is " + tBogeys);
break;
case 12:
System.out.println("Here is the information on the holes you score aces on ");
System.out.println("The handicap for hole " + GolfHole[number-1] + " is " + handicap[aces] + "");
System.out.println("The distance for hole " + GolfHole[score] + " is " + distance[aces] + "");
System.out.println();
break;
case 13:
System.out.println("You scored double eagle on these holes");
System.out.println("The handicap for hole " + GolfHole[score] + " is " + handicap[eagle] + " ");
System.out.println("The distance for hole " + GolfHole[score] + " is " + distance[eagle] + " ");
break;
case 14:
System.out.println("You scored birdies on these holes");
System.out.println("The handicap for hole " + GolfHole[score] + " is " + handicap[birdies] + " ");
System.out.println("The distance for hole " + GolfHole[score] + " is " + distance[birdies] + " ");
break;
case 15:
System.out.println("You scored pars on these holes");
System.out.println("The handicap for hole " + GolfHole[tpar] + " is " + handicap[tpar] + " ");
System.out.println("The distance for hole " + GolfHole[tpar] + " is " + distance[tpar] + " ");
break;
case 16:
System.out.println("You scored bogeys on these holes");
System.out.println("The handicap for hole " + GolfHole[score] + " is " + handicap[bogeys] + " ");
System.out.println("The distance for hole " + GolfHole[score] + " is " + distance[bogeys] + " ");
break;
case 17:
System.out.println("You scored double bogeys on these holes");
System.out.println("The handicap for hole " + GolfHole[score] + " is " + handicap[dBogeys] + " ");
System.out.println("The distance for hole " + GolfHole[score] + " is " + distance[dBogeys] + " ");
break;
case 18:
System.out.println("You scored triple bogeys on these holes");
System.out.println("The handicap for hole " + GolfHole[score] + " is " + handicap[tBogeys] + " ");
System.out.println("The distance for hole " + GolfHole[score] + " is " + distance[tBogeys] + " ");
break;
case 19:
System.out.println("Eixt the program");
System.exit(0);
default:
System.out.println("Invalid selection");
}
}
}//end main
public static void menuSelect()
{
System.out.println();
System.out.println(" YOUR OPTIONS");
System.out.println("1.Calculate and print total score");
System.out.println("2.Calculate and print number of strokes above par for the round");
System.out.println("3.Calculate and print number of strokes below par for the round");
System.out.println("4.Calculate and print the number of aces (hole in one)");
System.out.println("5.Calculate and print the number of double eagles (3 below par)");
System.out.println("6.Calculate and print the number of eagles (2 below par)");
System.out.println("7.Calculate and print the number of birdies (one below par)");
System.out.println("8.Calculate and print the number of pars");
System.out.println("9.Calculate and print the number of bogeys (one above par)");
System.out.println("10.Calculate and print the number of double bogeys (two above par)");
System.out.println("11.Calculate and print the number of triple bogeys (three above par)");
System.out.println("12.Print the hole information for the holes on which they scored aces");
System.out.println("13.Print the hole information for the holes on which they scored double eagles");
System.out.println("14.Print the hole information for the holes on which they scored birdies");
System.out.println("15.Print the hole information for the holes on which they scored pars");
System.out.println("16.Print the hole information for the holes on which they scored bogeys");
System.out.println("17.Print the hole information for the holes on which they scored double bogeys");
System.out.println("18.Print the hole information for the holes on which they scored triple bogeys");
System.out.println("19.Exit the program");
}//end menu
}//end class
public class GolfHole
{
//instance variables
private int number;
private int par;
private int handicap;
private int score;
private int distance;
private String description;
//default constructor
public GolfHole()
{
number = 0;
par = 0;
handicap = 0;
score = 0;
distance = 0;
description = "Not yet assigned";
}//end default constructor
//non-default constructor
GolfHole(int numberPassed, int parPassed, int handicapPassed,
int scorePassed, int distancePassed, String descriptionPassed)
{
number = numberPassed;
par = parPassed;
handicap = handicapPassed;
score = scorePassed;
distance = distancePassed;
description = descriptionPassed;
}//end non-default constructor
//getters
public int getNumber()
{
return number;
}//end getter
public int getPar()
{
return par;
}//end getter
public int getHandicap()
{
return handicap;
}//end getter
public int getScore()
{
return score;
}//end getter
public int getDistance()
{
return distance;
}//end getter
public String getDescription()
{
return description;
}//end getter
//setters
public void setNumber(int numberPassed)
{
if(numberPassed < 1 || numberPassed > 18)
{
System.out.println("Invalid input hole number must be 1 - 18 inclusive.");
System.out.println("Hole number will be set to 0");
numberPassed = 0;
}//end if
number = numberPassed;
}//end setter
public void setPar(int parPassed)
{
if(parPassed < 1 || parPassed > 5)
{
System.out.println("Invalid input par must be 1 - 5 inclusive.");
System.out.println("Par will be set to 0");
parPassed = 0;
}//end if
par = parPassed;
}//end getter
public void setHandicap(int handicapPassed)
{
if(handicapPassed < 1 || handicapPassed > 40)
{
System.out.println("Invalid handicap must be 1 - 40 inclusive.");
System.out.println("Handicap will be set to 0");
handicapPassed = 0;
}//end if
handicap = handicapPassed;
}//end setter
public void setScore(int scorePassed)
{
if(scorePassed < 1 || scorePassed > 8)
{
System.out.println("Invalid score must be 1 - 8 inclusive.");
System.out.println("Score will be set to 8");
scorePassed = 8;
}//end if
score = scorePassed;
}//end getter
public void setDistance(int distancePassed)
{
if(distancePassed < 1)
{
System.out.println("Invalid distance must be positive.");
System.out.println("Distance will be set to 0");
distancePassed = 8;
}//end if
distance = distancePassed;
}//end setter
public void setDescription(String descriptionPassed)
{
description = descriptionPassed;
}//end setter
public String toString()
{
return "Hole information "
+ " Number : " + number
+ " Par : " + par
+ " Handicap : " + handicap
+ " Score : " + score
+ " Distance : " + distance
+ " Description : " + description + " ";
}
}//end class
Explanation / Answer
Hi, I analyzed your program. What I could find is you are using a GolfHole class and a GolfHole array. If you want to use the parameters like handicap, score,distance from Golf class, so that it can do the check while setting the parameters of the above given parameters. The Golfhole class is not used in your program anywhere. I assume you have a standard par, handicap and distance values, intalized as an array. Once you have done the calculation for the current score in the if condition, you call the menu to output the values. Since the array is intialized, it is going to take the standard value for par, handicap and distance. Case 12 to 18 - It is not involving the actual Golf class. Hence the if condition while setting the parameters will never work. I assume because of the above problem, the desired output not seen for you. I have modified the program involving the GolfHole class so the additional check will be performed on Handicap, Distance and par , though par,handicap,distance cannot be modified as per your program as the values are initialized in the array.. I have given the modified program below as per my above analysis. Please find below the program. Hope it helps. import java.util.Scanner; public class RoundOfGolf { public static void main(String[] args) throws Exception { Scanner keyboard = new Scanner(System.in); GolfHole method = new GolfHole(); int totalScore = 0; String description = ""; int score = 0; int number; char again = 'y'; String line = ""; int menuSelect = 18; int parTotal = 0; boolean done = false; int[] distance = new int[] { 344, 334, 116, 268, 329, 469, 314, 134, 396, 373, 309, 455, 128, 312, 315, 102, 271, 427 }; int[] GolfHole = new int[18]; int[] handicap = new int[] { 4, 2, 18, 16, 12, 8, 10, 14, 6, 5, 11, 7, 15, 9, 1, 17, 13, 3 }; int[] par = new int[] { 4, 4, 3, 4, 4, 5, 4, 3, 5, 4, 4, 5, 3, 4, 4, 3, 4, 5 }; int aces = 0, dEagle = 0, eagle = 0, above = 0, below = 0, birdies = 0, tpar = 0, bogeys = 0, even = 0, dBogeys = 0, tBogeys = 0; method.setNumber(GolfHole.length); method.setDescription(description); for (number = 0; number 5) { System.out.println("Invalid input par must be 1 - 5 inclusive."); System.out.println("Par will be set to 0"); parPassed = 0; }// end if par = parPassed; }// end getter public void setHandicap(int handicapPassed) { if (handicapPassed < 1 || handicapPassed > 40) { System.out.println("Invalid handicap must be 1 - 40 inclusive."); System.out.println("Handicap will be set to 0"); handicapPassed = 0; }// end if handicap = handicapPassed; }// end setter public void setScore(int scorePassed) { if (scorePassed < 1 || scorePassed > 8) { System.out.println("Invalid score must be 1 - 8 inclusive."); System.out.println("Score will be set to 8"); scorePassed = 8; }// end if score = scorePassed; }// end getter public void setDistance(int distancePassed) { if (distancePassed < 1) { System.out.println("Invalid distance must be positive."); System.out.println("Distance will be set to 0"); distancePassed = 8; }// end if distance = distancePassed; }// end setter public void setDescription(String descriptionPassed) { description = descriptionPassed; }// end setter public String toString() { return "Hole information " + " Number : " + number + " Par : " + par + " Handicap : " + handicap + " Score : " + score + " Distance : " + distance + " Description : " + description + " "; } }// end classRelated Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.