ACSL Golf Code errors

  • (7 Pages)
  • +
  • « First
  • 5
  • 6
  • 7

102 Replies - 2911 Views - Last Post: 02 October 2011 - 06:36 PM Rate Topic: -----

#91 bhsanchez72  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 51
  • Joined: 01-October 11

Re: ACSL Golf Code errors

Posted 02 October 2011 - 08:34 AM

View PostGregBrannon, on 02 October 2011 - 08:31 AM, said:

You made an effort. You even thought about it some and made changes. That's good.

Here's the method as it should be:
    private String getRelationToPar( int score )
    {
        // calculate the difference between score and par
        int difference = par - score;

        // initialize a string for the result
        String relation = "";
        
        // score and par are the same
        if ( difference == 0 )
        {
            relation += "Same as par";
        }
        // the score is less than par
        else if ( difference > 0 )
        {
            relation += difference + " under par.";
        }
        // the score is greater than par
        else
        {
            relation += Math.abs( difference ) + " over par.";
        }
        
        return relation;

    } // end method getRelationToPar()


Read this method. Ask questions about what you don't understand. Paste it into your GolfScore() method. You tell me what's next.

What is math.abs? And next isn't there one more method and if so can you just give it to me so we can be done and just explain it.Or we have to go call the methods in the client. haha
Was This Post Helpful? 0
  • +
  • -

#92 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2205
  • View blog
  • Posts: 5,239
  • Joined: 10-September 10

Re: ACSL Golf Code errors

Posted 02 October 2011 - 08:52 AM

Math.abs() is the method abs() in Java's Math class that returns an absolute value. You could easily find how to do these kind of things in Java by Googling, in this case Google "java absolute value" and an answer that points you to this solution will be in the top 2 or 3 results.

Yes, I've had enough. I'm not sure that you've learned anything or that dragging you kicking and screaming through writing more code will teach you anything. I suggest you reconsider whether you should be in a programming class at all or if you've given the class the effort required to gain any benefit from it.

Here's a completed class GolfScore (some of the names have changed because I've been using two versions of everything. Just copy and paste this over your existing code). See if it all runs together, let me know if you have any questions or discover errors or problems that I can help you with.
/*
 * Bradley Sanchez
 * This class maintains par, score, and total
 * 9/26/11
 * PD:4B CS2
 */

public class GolfScore
{
    //private data
    private int par, score;
    private int aScore = 0;
    private int bScore = 0;
    private int lowScore = 0;
    private int highScore = 0;
    private int aHolesWon = 0;
    private int bHolesWon = 0;
    private int highHoleScore = 0;
    private boolean aWon = false;
    private boolean isTie = false;

    //default constructor
    public GolfScore()
    {
        par = 0;
        score = 0;
    }

    public void tallyPar(int p)
    {
        par += p;
    }

    public void tallyAScores(int s)
    {
        aScore += s;
    }

    public void tallyBScores(int s)
    {
        bScore += s;
    }

    public int getpar()
    {
        return par;
    }

    public int getScore()
    {
        return score;
    }

    public void tallyHolesWon( int playerA, int playerB )
    {
        if ( playerA < playerB )
        {
            aHolesWon++;
        }
        if ( playerB < playerA )
        {
            bHolesWon++;
        }
    }
    
    public void highestHoleScore( int playerA, int playerB )
    {
        if ( ( playerA + playerB ) > highHoleScore )
        {
            highHoleScore = playerA + playerB;
        }
    }
    
    private void calculateGameResults()
    {
        if ( aScore < bScore )
        {
            lowScore = aScore;
            highScore = bScore;
            aWon = true;
            isTie = false;
        }
        if ( bScore < aScore )
        {
            lowScore = bScore;
            highScore = aScore;
            aWon = false;
            isTie = false;
        }
        if ( aScore == bScore )
        {
            lowScore = aScore;
            highScore = aScore;
            aWon = false;
            isTie = true;
        }

    }
    
    private String getRelationToPar( int score )
    {
        int difference = par - score;
        String relation = "";
        
        if ( difference == 0 )
        {
            relation += "Same as par";
        }
        else if ( difference > 0 )
        {
            relation += difference + " under par.";
        }
        else
        {
            relation += Math.abs( difference ) + " over par.";
        }
        
        return relation;

    } // end method getRelationToPar()
    
    private int getHolesWonByWinner()
    {
        int holesWon = 0;
        
        if ( aWon )
        {
            holesWon = aHolesWon;
        }
        if ( !aWon )
        {
            holesWon = bHolesWon;
        }
        // or there was a tie:
        return holesWon;
    }
    
    public void outputResults()
    {
        /******** Output Results: ******* 
         * 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.
         *  
         *  Example:
         *  OUTPUT:
         *  1. 35, 41
         *  2. 1 under par
         *  3. 5 over par
         *  4. 6
         *  5. 12
         */
        
        calculateGameResults();
        
        System.out.println( "\n\nThe results of the golf game are: " );
        
        System.out.println( "1. " + lowScore + ", " + highScore );

        // the score in relation to par for the better score
        System.out.println( "2. " + getRelationToPar( lowScore ) );
        
        // the score in relation to par for the other player
        System.out.println( "3. " + getRelationToPar( highScore ) );

        // number of holes won by the player with the best score
        if ( isTie )
        {
            System.out.println( "4. It was a tie!" );
        }
        else
        {
            System.out.println( "4. " + getHolesWonByWinner() );
        }

        // the sum of the scores on the hole that is the highest
        // for all the holes played
        System.out.println( "5. " + highHoleScore );

    } // end method outputResults()

}

Was This Post Helpful? 1
  • +
  • -

#93 bhsanchez72  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 51
  • Joined: 01-October 11

Re: ACSL Golf Code errors

Posted 02 October 2011 - 08:56 AM

View PostGregBrannon, on 02 October 2011 - 08:52 AM, said:

Math.abs() is the method abs() in Java's Math class that returns an absolute value. You could easily find how to do these kind of things in Java by Googling, in this case Google "java absolute value" and an answer that points you to this solution will be in the top 2 or 3 results.

Yes, I've had enough. I'm not sure that you've learned anything or that dragging you kicking and screaming through writing more code will teach you anything. I suggest you reconsider whether you should be in a programming class at all or if you've given the class the effort required to gain any benefit from it.

Here's a completed class GolfScore (some of the names have changed because I've been using two versions of everything. Just copy and paste this over your existing code). See if it all runs together, let me know if you have any questions or discover errors or problems that I can help you with.
/*
 * Bradley Sanchez
 * This class maintains par, score, and total
 * 9/26/11
 * PD:4B CS2
 */

public class GolfScore
{
    //private data
    private int par, score;
    private int aScore = 0;
    private int bScore = 0;
    private int lowScore = 0;
    private int highScore = 0;
    private int aHolesWon = 0;
    private int bHolesWon = 0;
    private int highHoleScore = 0;
    private boolean aWon = false;
    private boolean isTie = false;

    //default constructor
    public GolfScore()
    {
        par = 0;
        score = 0;
    }

    public void tallyPar(int p)
    {
        par += p;
    }

    public void tallyAScores(int s)
    {
        aScore += s;
    }

    public void tallyBScores(int s)
    {
        bScore += s;
    }

    public int getpar()
    {
        return par;
    }

    public int getScore()
    {
        return score;
    }

    public void tallyHolesWon( int playerA, int playerB )
    {
        if ( playerA < playerB )
        {
            aHolesWon++;
        }
        if ( playerB < playerA )
        {
            bHolesWon++;
        }
    }
    
    public void highestHoleScore( int playerA, int playerB )
    {
        if ( ( playerA + playerB ) > highHoleScore )
        {
            highHoleScore = playerA + playerB;
        }
    }
    
    private void calculateGameResults()
    {
        if ( aScore < bScore )
        {
            lowScore = aScore;
            highScore = bScore;
            aWon = true;
            isTie = false;
        }
        if ( bScore < aScore )
        {
            lowScore = bScore;
            highScore = aScore;
            aWon = false;
            isTie = false;
        }
        if ( aScore == bScore )
        {
            lowScore = aScore;
            highScore = aScore;
            aWon = false;
            isTie = true;
        }

    }
    
    private String getRelationToPar( int score )
    {
        int difference = par - score;
        String relation = "";
        
        if ( difference == 0 )
        {
            relation += "Same as par";
        }
        else if ( difference > 0 )
        {
            relation += difference + " under par.";
        }
        else
        {
            relation += Math.abs( difference ) + " over par.";
        }
        
        return relation;

    } // end method getRelationToPar()
    
    private int getHolesWonByWinner()
    {
        int holesWon = 0;
        
        if ( aWon )
        {
            holesWon = aHolesWon;
        }
        if ( !aWon )
        {
            holesWon = bHolesWon;
        }
        // or there was a tie:
        return holesWon;
    }
    
    public void outputResults()
    {
        /******** Output Results: ******* 
         * 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.
         *  
         *  Example:
         *  OUTPUT:
         *  1. 35, 41
         *  2. 1 under par
         *  3. 5 over par
         *  4. 6
         *  5. 12
         */
        
        calculateGameResults();
        
        System.out.println( "\n\nThe results of the golf game are: " );
        
        System.out.println( "1. " + lowScore + ", " + highScore );

        // the score in relation to par for the better score
        System.out.println( "2. " + getRelationToPar( lowScore ) );
        
        // the score in relation to par for the other player
        System.out.println( "3. " + getRelationToPar( highScore ) );

        // number of holes won by the player with the best score
        if ( isTie )
        {
            System.out.println( "4. It was a tie!" );
        }
        else
        {
            System.out.println( "4. " + getHolesWonByWinner() );
        }

        // the sum of the scores on the hole that is the highest
        // for all the holes played
        System.out.println( "5. " + highHoleScore );

    } // end method outputResults()

}

Thank you i will read this all over now in my client calculateGameResults and getRelationToPar are underlined beacuse i am calling them from golf score but they are privates not publics should i change it to public.
Was This Post Helpful? 0
  • +
  • -

#94 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2205
  • View blog
  • Posts: 5,239
  • Joined: 10-September 10

Re: ACSL Golf Code errors

Posted 02 October 2011 - 09:01 AM

What I just gave you should BE the class GolfScore. There shouldn't be any squigglies. Please verify that the code I gave you is now your GolfScore. If you changed it, send back the updated version to me so that I can see what you've done to it.
Was This Post Helpful? 0
  • +
  • -

#95 bhsanchez72  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 51
  • Joined: 01-October 11

Re: ACSL Golf Code errors

Posted 02 October 2011 - 09:04 AM

View Postbhsanchez72, on 02 October 2011 - 08:56 AM, said:

View PostGregBrannon, on 02 October 2011 - 08:52 AM, said:

Math.abs() is the method abs() in Java's Math class that returns an absolute value. You could easily find how to do these kind of things in Java by Googling, in this case Google "java absolute value" and an answer that points you to this solution will be in the top 2 or 3 results.

Yes, I've had enough. I'm not sure that you've learned anything or that dragging you kicking and screaming through writing more code will teach you anything. I suggest you reconsider whether you should be in a programming class at all or if you've given the class the effort required to gain any benefit from it.

Here's a completed class GolfScore (some of the names have changed because I've been using two versions of everything. Just copy and paste this over your existing code). See if it all runs together, let me know if you have any questions or discover errors or problems that I can help you with.
/*
 * Bradley Sanchez
 * This class maintains par, score, and total
 * 9/26/11
 * PD:4B CS2
 */

public class GolfScore
{
    //private data
    private int par, score;
    private int aScore = 0;
    private int bScore = 0;
    private int lowScore = 0;
    private int highScore = 0;
    private int aHolesWon = 0;
    private int bHolesWon = 0;
    private int highHoleScore = 0;
    private boolean aWon = false;
    private boolean isTie = false;

    //default constructor
    public GolfScore()
    {
        par = 0;
        score = 0;
    }

    public void tallyPar(int p)
    {
        par += p;
    }

    public void tallyAScores(int s)
    {
        aScore += s;
    }

    public void tallyBScores(int s)
    {
        bScore += s;
    }

    public int getpar()
    {
        return par;
    }

    public int getScore()
    {
        return score;
    }

    public void tallyHolesWon( int playerA, int playerB )
    {
        if ( playerA < playerB )
        {
            aHolesWon++;
        }
        if ( playerB < playerA )
        {
            bHolesWon++;
        }
    }
    
    public void highestHoleScore( int playerA, int playerB )
    {
        if ( ( playerA + playerB ) > highHoleScore )
        {
            highHoleScore = playerA + playerB;
        }
    }
    
    private void calculateGameResults()
    {
        if ( aScore < bScore )
        {
            lowScore = aScore;
            highScore = bScore;
            aWon = true;
            isTie = false;
        }
        if ( bScore < aScore )
        {
            lowScore = bScore;
            highScore = aScore;
            aWon = false;
            isTie = false;
        }
        if ( aScore == bScore )
        {
            lowScore = aScore;
            highScore = aScore;
            aWon = false;
            isTie = true;
        }

    }
    
    private String getRelationToPar( int score )
    {
        int difference = par - score;
        String relation = "";
        
        if ( difference == 0 )
        {
            relation += "Same as par";
        }
        else if ( difference > 0 )
        {
            relation += difference + " under par.";
        }
        else
        {
            relation += Math.abs( difference ) + " over par.";
        }
        
        return relation;

    } // end method getRelationToPar()
    
    private int getHolesWonByWinner()
    {
        int holesWon = 0;
        
        if ( aWon )
        {
            holesWon = aHolesWon;
        }
        if ( !aWon )
        {
            holesWon = bHolesWon;
        }
        // or there was a tie:
        return holesWon;
    }
    
    public void outputResults()
    {
        /******** Output Results: ******* 
         * 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.
         *  
         *  Example:
         *  OUTPUT:
         *  1. 35, 41
         *  2. 1 under par
         *  3. 5 over par
         *  4. 6
         *  5. 12
         */
        
        calculateGameResults();
        
        System.out.println( "\n\nThe results of the golf game are: " );
        
        System.out.println( "1. " + lowScore + ", " + highScore );

        // the score in relation to par for the better score
        System.out.println( "2. " + getRelationToPar( lowScore ) );
        
        // the score in relation to par for the other player
        System.out.println( "3. " + getRelationToPar( highScore ) );

        // number of holes won by the player with the best score
        if ( isTie )
        {
            System.out.println( "4. It was a tie!" );
        }
        else
        {
            System.out.println( "4. " + getHolesWonByWinner() );
        }

        // the sum of the scores on the hole that is the highest
        // for all the holes played
        System.out.println( "5. " + highHoleScore );

    } // end method outputResults()

}

Thank you i will read this all over now in my client calculateGameResults and getRelationToPar are underlined beacuse i am calling them from golf score but they are privates not publics should i change it to public.

This is the error i get when i go to run it:
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
The method calculateGameResults() from the type GolfScore is not visible
The method getRelationToPar(int) in the type GolfScore is not applicable for the arguments ()
Was This Post Helpful? 0
  • +
  • -

#96 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2205
  • View blog
  • Posts: 5,239
  • Joined: 10-September 10

Re: ACSL Golf Code errors

Posted 02 October 2011 - 09:07 AM

Post both classes.
Was This Post Helpful? 0
  • +
  • -

#97 bhsanchez72  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 51
  • Joined: 01-October 11

Re: ACSL Golf Code errors

Posted 02 October 2011 - 09:09 AM

View PostGregBrannon, on 02 October 2011 - 09:07 AM, said:

Post both classes.

client:
/*
 * 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, playerA = 0, playerB = 0, aScore = 0, bScore = 0, aHolesWon = 0, bHolesWon = 0;
        boolean aWon = false;
        myGolfScore = new GolfScore();
        String pattern = ",|" + System.getProperty( "line.separator" );
        Scanner kbd = new Scanner(System.in).useDelimiter( pattern );
        //process the data
        for(int holes = 1; holes <= 9; holes++)
        {
            //obtained data
            System.out.println("Please enter par, scoreA, scoreB --> ");
            parValue = kbd.nextInt();
            playerA = kbd.nextInt();
            playerB = kbd.nextInt();

            // Tally the course par
            myGolfScore.tallyPar( parValue );

            // Tally score for each player
            myGolfScore.tallyAScores(playerA);
            myGolfScore.tallyBScores(playerB); 
            
            // determines if score is a new high score.
            myGolfScore.highestHoleScore(playerA, playerB);
            
         // calculate the game's results
            myGolfScore.calculateGameResults();
            
            // the score in relation to par for the better score
            myGolfScore.getRelationToPar();

        }

        myGolfScore.outputResults();
    }
}


second:
/*
 * Bradley Sanchez
 * This class maintains par, score, and total
 * 9/26/11
 * PD:4B CS2
 */

public class GolfScore
{
    //private data
    private int par, score;
    private int aScore = 0;
    private int bScore = 0;
    private int lowScore = 0;
    private int highScore = 0;
    private int aHolesWon = 0;
    private int bHolesWon = 0;
    private int highHoleScore = 0;
    private boolean aWon = false;
    private boolean isTie = false;

    //default constructor
    public GolfScore()
    {
        par = 0;
        score = 0;
    }

    public void tallyPar(int p)
    {
        par += p;
    }

    public void tallyAScores(int s)
    {
        aScore += s;
    }

    public void tallyBScores(int s)
    {
        bScore += s;
    }

    public int getpar()
    {
        return par;
    }

    public int getScore()
    {
        return score;
    }

    public void tallyHolesWon(int playerA, int playerB)
    {
        if (playerA < playerB)
        {
            aHolesWon++;
        }
        if (playerB < playerA)
        {
            bHolesWon++;
        }
    }
    
    public void highestHoleScore(int playerA, int playerB)
    {
        if ((playerA + playerB) > highHoleScore)
        {
            highHoleScore = playerA + playerB;
        }
    }
    
    private void calculateGameResults()
    {
        if (aScore < bScore)
        {
            lowScore = aScore;
            highScore = bScore;
            aWon = true;
            isTie = false;
        }
        if (bScore < aScore)
        {
            lowScore = bScore;
            highScore = aScore;
            aWon = false;
            isTie = false;
        }
        if (aScore == bScore)
        {
            lowScore = aScore;
            highScore = aScore;
            aWon = false;
            isTie = true;
        }

    }
    
    private String getRelationToPar(int score)
    {
        int difference = par - score;
        String relation = "";
        
        if (difference == 0)
        {
            relation += "Same as par";
        }
        else if (difference > 0)
        {
            relation += difference + " under par.";
        }
        else
        {
            relation += Math.abs(difference) + " over par.";
        }
        
        return relation;

    } // end method getRelationToPar()
    
    private int getHolesWonByWinner()
    {
        int holesWon = 0;
        
        if  (aWon)
        {
            holesWon = aHolesWon;
        }
        if (!aWon)
        {
            holesWon = bHolesWon;
        }
        
        return holesWon;
    }
    
    public void outputResults()
    {
        calculateGameResults();
       
        System.out.println("\n\nThe results of the golf game are: ");
        
        System.out.println("1. " + lowScore + ", " + highScore);

        // the score in relation to par for the better score
        System.out.println("2. " + getRelationToPar(lowScore));
        
        // the score in relation to par for the other player
        System.out.println("3. " + getRelationToPar(highScore));

        // number of holes won by the player with the best score
        if ( isTie )
        {
            System.out.println("4. It was a tie!");
        }
        else
        {
            System.out.println("4. " + getHolesWonByWinner());
        }

        // the sum of the scores on the hole that is the highest
        // for all the holes played
        System.out.println("5. " + highHoleScore);

    } 

}


Was This Post Helpful? 0
  • +
  • -

#98 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1820
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: ACSL Golf Code errors

Posted 02 October 2011 - 09:17 AM

You set your method private
private void calculateGameResults()
and to be accessed outside that class it should be public.

getRelationToPar(int score) must pass int when called, and you call without passing anything
myGolfScore.getRelationToPar();

This post has been edited by smohd: 02 October 2011 - 09:19 AM

Was This Post Helpful? 0
  • +
  • -

#99 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2205
  • View blog
  • Posts: 5,239
  • Joined: 10-September 10

Re: ACSL Golf Code errors

Posted 02 October 2011 - 09:20 AM

Lines 39 - 44 don't belong in the client. Delete them.

Then try running and let me know what happens.
Was This Post Helpful? 0
  • +
  • -

#100 bhsanchez72  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 51
  • Joined: 01-October 11

Re: ACSL Golf Code errors

Posted 02 October 2011 - 09:27 AM

View PostGregBrannon, on 02 October 2011 - 09:20 AM, said:

Lines 39 - 44 don't belong in the client. Delete them.

Then try running and let me know what happens.

Okay soooo close for the fourth output i get 0 instead of 6?
Was This Post Helpful? 0
  • +
  • -

#101 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2205
  • View blog
  • Posts: 5,239
  • Joined: 10-September 10

Re: ACSL Golf Code errors

Posted 02 October 2011 - 09:44 AM

Somehow, the following lines got left off of the client. Insert the lines at the end of the for loop, starting at line 39.

            // tally the holes won by each player
            myGolfScore.tallyHolesWon( playerA, playerB );

Was This Post Helpful? 0
  • +
  • -

#102 bhsanchez72  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 51
  • Joined: 01-October 11

Re: ACSL Golf Code errors

Posted 02 October 2011 - 09:49 AM

View PostGregBrannon, on 02 October 2011 - 09:44 AM, said:

Somehow, the following lines got left off of the client. Insert the lines at the end of the for loop, starting at line 39.

            // tally the holes won by each player
            myGolfScore.tallyHolesWon( playerA, playerB );

YES it works thank you so much now when i go to do another problem this will help me so much!!!!!!!!!!!!!!!!!
Was This Post Helpful? 0
  • +
  • -

#103 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8343
  • View blog
  • Posts: 31,890
  • Joined: 06-March 08

Re: ACSL Golf Code errors

Posted 02 October 2011 - 06:36 PM

View Postbhsanchez72, on 02 October 2011 - 12:49 PM, said:

YES it works thank you so much now when i go to do another problem this will help me so much!!!!!!!!!!!!!!!!!

Hope it won't take 102 replies next time :)
Was This Post Helpful? 1
  • +
  • -

  • (7 Pages)
  • +
  • « First
  • 5
  • 6
  • 7