11 Replies - 1252 Views - Last Post: 25 September 2012 - 01:19 PM Rate Topic: -----

#1 breiman  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 28-February 12

Coding revisions & get methods

Posted 25 September 2012 - 11:01 AM

I need to revise this code according to standard coding guidelines and rewrite these classes. I changed variables from private to public and commented out the get methods. Now an error has been thrown in the class TriviaConsole. I would appreciate some help fixing these errors as well as any additional help rewriting the classes and making appropriate revisions. Any suggestions are appreciated!

public class TriviaGame {

  public String quizName;
  public int maxNumQuestions;
  public int numChances;
  public int remainingChances;
  public int numCorrect = 0;
  public int numIncorrect = 0;
  public TriviaQuestion[] questions;  // the set of questions
  private boolean[] correct;           // true if corresponding question answered
  private int currNumQuestions = 0;    // current number of questions

  public TriviaGame(String quizName, int maxNumQuestions, int numChances) // Precondition:  maxNumQuestions > 0 and numChances > 0
  {
    this.quizName = quizName;
    this.maxNumQuestions = maxNumQuestions;
    this.numChances = numChances;
    remainingChances = numChances;
    questions = new TriviaQuestion[maxNumQuestions];
    correct = new boolean[maxNumQuestions];
  }

  /*public String getQuizName() {
    return quizName;
  }

  public int getNumChances() {
    return numChances;
  }

  public int getRemainingChances() {
    return remainingChances;
  }

  public int getNumCorrect() {
    return numCorrect;
  }

  public int getNumIncorrect() {
    return numIncorrect;
  }

  public int getCurrNumQuestions() {
    return currNumQuestions;
  }*/

  public TriviaQuestion getTriviaQuestion(int questionNumber) // Precondition:   0 < questionNumber <= currNumQuestions
  {
    return questions[questionNumber - 1];
  }

  public boolean isAnswered(int questionNumber) // Precondition:   0 < questionNumber <= currNumQuestions
  {
    return correct[questionNumber - 1];
  }

  public boolean isOver() // Returns true if this game is over, false otherwise.
  {
    return (numCorrect == currNumQuestions)
            || (remainingChances <= 0);
  }

  public void insertQuestion(TriviaQuestion question) // Precondition:  currNumQuestions < maxNumQuestions
  // 
  // Adds question to this TriviaGame.
  {
    questions[currNumQuestions] = question;
    correct[currNumQuestions] = false;
    currNumQuestions = currNumQuestions + 1;
  }

  public void correctAnswer(int questionNumber) // Preconditions: 0 < questionNumber < maxNumQuestions
  //
  // Updates game status to indicate that question number 
  // "questionNumber" was answered correctly.
  {
    correct[questionNumber - 1] = true;
    numCorrect = numCorrect + 1;
    remainingChances = remainingChances - 1;
  }

  public void incorrectAnswer() // Updates game status to indicate that a question 
  // was answered incorrectly
  {
    numIncorrect = numIncorrect + 1;
    remainingChances = remainingChances - 1;
  }
}



public class TriviaConsole {

  public static void main(String[] args) throws IOException {
    Scanner conIn = new Scanner(System.in);

    TriviaGame game;    // the trivia game

    int questNum;       // current question number
    TriviaQuestion tq;  // current question
    String answer;      // answer provided by user

    // Initialze the game
    game = GetTriviaGame.useTextFile("game.txt");

    // Greet the user.
    System.out.println("Welcome to " + game.getQuizName() + ".");
    System.out.println("You will have " + game.getNumChances() + " chances "
            + "to answer " + game.getCurrNumQuestions() + " questions.\n");

    questNum = 1;
    while (!game.isOver()) {
      // Get number of next unanswered question.
      while (game.isAnswered(questNum)) {
        if (questNum == game.getCurrNumQuestions()) {
          questNum = questNum + 1;
        } else {
          questNum = questNum + 1;
        }
      }

      // Ask question and handle user's response.
      tq = game.getTriviaQuestion(questNum);
      System.out.println(tq.getCategory() + ": " + tq.getQuestion());
      answer = conIn.nextLine();
      if (tq.tryAnswer(answer)) {
        System.out.println("Correct!\n");
        game.correctAnswer(questNum);
      } else {
        System.out.println("Incorrect\n");
        game.incorrectAnswer();
        questNum = questNum + 1;
      }
    }

    System.out.println("\nGame Over");
    System.out.println("\nResults:");
    System.out.print("   Chances used: " + (game.getNumChances() - game.getRemainingChances()));
    System.out.println("   Number Correct: " + game.getNumCorrect());
    System.out.println("\nThank you.\n");
  }
}



Is This A Good Question/Topic? 0
  • +

Replies To: Coding revisions & get methods

#2 Mina-no-Hime  Icon User is offline

  • D.I.C Head

Reputation: 98
  • View blog
  • Posts: 176
  • Joined: 23-August 12

Re: Coding revisions & get methods

Posted 25 September 2012 - 11:04 AM

View Postbreiman, on 25 September 2012 - 10:01 AM, said:

I changed variables from private to public and commented out the get methods.

I really hope that isn't part of the coding standards you've changed to, because that's a very bad practice. Always keep your member variables as private, and provide accessor & mutator (getter & setter) functions, so that you can ensure that the values are kept valid.

View Postbreiman, on 25 September 2012 - 10:01 AM, said:

Now an error has been thrown in the class TriviaConsole. I would appreciate some help fixing these errors as well as any additional help rewriting the classes and making appropriate revisions. Any suggestions are appreciated!

What errors are you getting, exactly? We aren't going to run the code for you - you need to specify what errors you're getting so that we can help walk you through solving the issues.
Was This Post Helpful? 4
  • +
  • -

#3 breiman  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 28-February 12

Re: Coding revisions & get methods

Posted 25 September 2012 - 11:08 AM

Thanks for your suggestion on the get and set methods as well as private variables. I was told to get rid of the get and set methods but you say it is good practice to use them so I switched them back and have no errors now. I just need to make revisions according to the standard guidelines.
Was This Post Helpful? 0
  • +
  • -

#4 Mina-no-Hime  Icon User is offline

  • D.I.C Head

Reputation: 98
  • View blog
  • Posts: 176
  • Joined: 23-August 12

Re: Coding revisions & get methods

Posted 25 September 2012 - 11:10 AM

View Postbreiman, on 25 September 2012 - 10:08 AM, said:

I was told to get rid of the get and set methods [...]

Whoever told you to do that needs to read up on coding standards and OOP practices.

Can you link to the "standard guidelines" you're revising your code based on?
Was This Post Helpful? 1
  • +
  • -

#5 breiman  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 28-February 12

Re: Coding revisions & get methods

Posted 25 September 2012 - 11:25 AM

college professor's suggestion..here are some guidelines similar to the assigned ones
Java Programming Style
Oracle conventions
Was This Post Helpful? 0
  • +
  • -

#6 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1424
  • View blog
  • Posts: 3,166
  • Joined: 05-April 11

Re: Coding revisions & get methods

Posted 25 September 2012 - 11:30 AM

Your array 'correct' that is parallel with your 'questions' array is considered bad coding practice.
Should a question have the information if it has been correctly answered or not? Or can you come up with another solution that doesn't involve parallel arrays :) ?
Was This Post Helpful? 1
  • +
  • -

#7 baavgai  Icon User is online

  • Dreaming Coder
  • member icon

Reputation: 5932
  • View blog
  • Posts: 12,857
  • Joined: 16-October 07

Re: Coding revisions & get methods

Posted 25 September 2012 - 11:31 AM

*
POPULAR

So...

from Java Programming Style

Quote

46. Class variables should never be declared public.


From Oracle conventions:

Quote

10.1 Providing Access to Instance and Class Variables
Don't make any instance or class variable public without good reason.


Hint, there is never a "good reason" for a public variable.
Was This Post Helpful? 6
  • +
  • -

#8 breiman  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 28-February 12

Re: Coding revisions & get methods

Posted 25 September 2012 - 11:56 AM

CasiOo, I do not understand your suggestion. I realize you are challenging me to come up with a solution but I do not believe I am able to do so. Thanks to all of you for the help thus far
Was This Post Helpful? 0
  • +
  • -

#9 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 8013
  • View blog
  • Posts: 13,719
  • Joined: 19-March 11

Re: Coding revisions & get methods

Posted 25 September 2012 - 12:32 PM

Public variables are an abomination in the sight of God and lead to unsightly boils, this is true. However, getters and setters are not much better. There is very seldom a good reason for a class to share its fields unless it is simply there to hold that information - for example, a Collection should yield up its data on request. There is even less excuse to allow other classes to set the values at whim.

Classes model stuff. You should be able to ask them good questions and get good answers - a class that just disgorges fields on demand is almost certainly not doing enough modeling. If you're writing a lot of get and set methods, you need to reconsider your design.

Quote

Hint, there is never a "good reason" for a public variable.


There is one. A public constant is legitimate. I have no problem with Integer.MAX_VALUE or array.length. A mutable, on the other hand, has to be kept locked up or it'll go off and get into trouble.

This post has been edited by jon.kiparsky: 25 September 2012 - 12:54 PM

Was This Post Helpful? 0
  • +
  • -

#10 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1424
  • View blog
  • Posts: 3,166
  • Joined: 05-April 11

Re: Coding revisions & get methods

Posted 25 September 2012 - 12:50 PM

Really off topic but now when we are talking about getters/setters I just wanna get my frustrations out!

I'm using a JSON deserializer which takes an object as parameter and then tries to map the properties (c#) to the json data. The annoying part is that the properties need to have a public set method for this to work, but why does the deserializer need a public set method when it is using reflection already to find the properties?! couldn't it just using the private setters...... stupid deserializer :(

Or better - Don't use reflection in the first place!

This post has been edited by CasiOo: 25 September 2012 - 01:02 PM

Was This Post Helpful? 1
  • +
  • -

#11 baavgai  Icon User is online

  • Dreaming Coder
  • member icon

Reputation: 5932
  • View blog
  • Posts: 12,857
  • Joined: 16-October 07

Re: Coding revisions & get methods

Posted 25 September 2012 - 01:09 PM

*
POPULAR

View Postjon.kiparsky, on 25 September 2012 - 03:32 PM, said:

There is one. A public constant is legitimate.


Um, a constant ain't variable. Pretty much by definition. :P
Was This Post Helpful? 5
  • +
  • -

#12 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 8013
  • View blog
  • Posts: 13,719
  • Joined: 19-March 11

Re: Coding revisions & get methods

Posted 25 September 2012 - 01:19 PM

View Postbaavgai, on 25 September 2012 - 03:09 PM, said:

View Postjon.kiparsky, on 25 September 2012 - 03:32 PM, said:

There is one. A public constant is legitimate.


Um, a constant ain't variable. Pretty much by definition. :P



Not in java. JLS 4.12.4. :P yourself.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1