Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Oct 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Help with code output please!!

    PROBLEM: In the sport of golf, scoring is based upon what a normal score should be for a particular hole on a course. That normal score called “par” is based upon the length of that hole measured from the tee, the start point, to the green, the end point. For the vast majority of golf courses and for this problem par values will be either 3, 4 or 5. Golf scores can be reported in three ways. The first way is to report cumulative score. That is, the player scored a 68 after playing the standard 18 holes. The second way is to report the cumulative score in relation to par. If par for the course ( the sum of the par values of all the holes) is 72, then the score reported as 68 would be 4 under par. A score of 75 would be 3 over par. A score of 72 is reported as par. The third way is by holes won. A player wins a hole when his score is lower than his opponent’s score.

    INPUT: There will be 9 input lines. Each line will contain 3 positive integers. The first integer will give the par value of each of the nine holes. The second integer will give the score for player A on the same nine holes. The third integer will give the score for player B on the those nine holes.

    OUTPUT: There will be five outputs. Print the following:

    1. The cumulative score for each player with the better score (the smaller number) first.
    2. The score in relation to par for the better score.
    3. The score in relation to par for the other player.
    4. The number of holes won by the player with the better score.
    5. The sum of the scores on a the hole that is the highest for all the holes played.
    ______________________________________________________________
    INPUT:
    1. 3,2,3
    2. 4,5,5
    3. 5,6,6
    4. 4,3,4
    5. 4,3,4
    6. 4,4,5
    7. 5,5,6
    8. 3,3,3
    9. 4,4,5

    OUTPUT:
    1. 35, 41
    2. 1 under par
    3. 5 over par
    4. 6
    5. 12
    These are my two classes right now. And I am trying to figure out how to get the 5 outputs. Know as you will se in the second class i am changing this code to work for this problem because i used it for a similar problem earlier. Thanks!
    client:

    Code:
    /*
     * Bradley Sanchez
     * Client that obtains data for use by the golfer
     * 9/26/11
     * PD:4B CS2
     */
    
    import java.util.Scanner;
    public class ACSLGolfClient {
    
    	
    	public static void main(String[] args) 
    	{
    		//declare variables
    		GolfScore myGolfScore;
    		int parValue = 0, parTotal = 0, playerA = 0, playerB = 0, totalA = 0, totalB = 0, counter = 0, highScoreA, highScoreB, highScore, aHolesWon = 0, bHolesWon = 0;
    		myGolfScore = new GolfScore();
    		Scanner kbd = new Scanner(System.in).useDelimiter( ",|\n" );
    		//process the data
    		for(int holes = 1; holes <= 9; holes++)
    		{
    			counter++;
    			parTotal += parValue;
    			totalA += playerA;
    			totalB += playerB;
    			
    			//obtained data
    			System.out.println("Please enter par, scoreA, scoreB --> ");
    			parValue = kbd.nextInt();
    			playerA = kbd.nextInt();
    			playerB = kbd.nextInt();
    			
    			
    			//access the class with the data
    			myGolfScore.tallyPar(aHolesWon);
    			myGolfScore.tallyScores(bHolesWon);
    			
    			
    			
    			//output results
    			System.out.println(myGolfScore.outcome(aHolesWon, bHolesWon));
    			System.out.println(totalA);
    			
    		}
    		totalA = myGolfScore.getScore() - myGolfScore.getpar();
    		if(playerA < playerB)
    		{
    			aHolesWon ++;
    		}
    		else
    		{
    			bHolesWon ++;
    		}
    	
    		if(totalA < totalB)
    		{
    			System.out.println(totalA + " , " + totalB);
    		}
    		else
    		{
    			System.out.println(totalB + " , " + totalA);
    		}
    		
    		highScoreA = parTotal - totalA;
    		highScoreB = parTotal - totalB;
    		
    		if (highScoreA >= 0)
    		{
    			System.out.println(Math.abs(highScoreA) + " under par");
    		}
    		else
    		{
    			System.out.println(Math.abs(highScoreA) + " over par");
    		}
    		
    		if (highScoreB < 0)
    		{
    			System.out.println(Math.abs(highScoreB) + " over par");
    		}
    		else
    		{
    			System.out.println(Math.abs(highScoreB) + " under par");
    		}
    		
    		if(aHolesWon > bHolesWon)
    		{
    			System.out.println(aHolesWon);
    		}
    		else
    		{
    			System.out.println(bHolesWon);
    		}
    }
    		
    	}
    This is the second class:
    Code:
    /*
     * Bradley Sanchez
     * This class maintains par, score, and total
     * 9/26/11
     * PD:4B CS2
     */
    
    public class GolfScore 
    {
    	//private data
    	private int par, score;
    	
    	//default constructor
    	public GolfScore()
    	{
    		par = 0;
    		score = 0;
    	}
    	
    	public void tallyPar(int p)
    	{
    		par += p;
    	}
    	
    	public void tallyScores(int s)
    	{
    		score += s;
    	}
    	
    	public int getpar()
    	{
    		return par;
    	}
    	
    	public int getScore()
    	{
    		return score;
    	}
    	
    	public String outcome(int p, int s)
    	{
    		if(p == s)
    			return "a score equal to par";
    		else if(p - s == 1)
    			return "birdie";
    		else if(p - s == 2)
    			return "eagle';";
    		else if(p + 1 == s)
    			return "bogey";
    		else 
    			return "double bogey";
    				
    	}
    
    }
    So i need to change this to get my five outputs and i am not really sure of how to do that. Thanks!

  • #2
    Senior Coder alykins's Avatar
    Join Date
    Apr 2011
    Posts
    1,722
    Thanks
    41
    Thanked 191 Times in 190 Posts
    i don't know Java, but I do know C# and they are similar... don't you need a main() somewhere?
    Also isn't this line pointless?
    Code:
    public void tallyScores(int s)
    	{
    		score += s;
    	}
    also I would look to change the final area of the GolfScore class to a switch statement (it would be easier to read)
    Code:
    public String outcome(int p, int s)
    {
    switch(p-s)
    {
    case 0:         		
      return "a score equal to par";
    case 1:  
      return "birdie";
    case 2:
      return "eagle";
    case -1:
      return "bogey";
    default:
      return "double bogey";
    }
    				
    	}

    I code C hash-tag .Net
    Reference: W3C W3CWiki .Net Lib
    Validate: html CSS
    Debug: Chrome FireFox IE


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •