2
\$\begingroup\$

What is the general design pattern for declaring variables in a class?

I have a homework assignment to create a simple Java program which asks the user for a range of two variables. The program then creates a random variable from within this range and prompts the user to guess the number.

I'm having trouble understanding how to declare my variables. I'm not sure if I should declare variables as public or private, and static or non-static. When is it correct to use a public/private variable?

Also, how much code should I include in my main() method? What is generally placed into the main() method?

import java.util.Scanner;
import java.util.Random;

public class HiLo {

  static int guess, guessCount, randomNumber, startRange, endRange;
  static Scanner scan = new Scanner(System.in);
  static Random random = new Random();

  public static void main(String[] args) {
      getRange();
      playGame();
  }

  public static void getRange() {
      System.out.println("Welcome! Enter integer for start of range (must be > 0)");
      startRange = scan.nextInt();

      System.out.println("Enter integer for end of range (must be >0)");
      endRange = scan.nextInt();
      randomNumber = random.nextInt(endRange - startRange + 1) + startRange;
      System.out.println(randomNumber);
  }

  public static void playGame() {
      guessCount = 0;
      System.out.println("Enter guess or 0 to quit: ");
      guess = scan.nextInt();

      while (randomNumber != guess) {
          if (guess == 0) {
              break;
          }
          else if (randomNumber > guess) {
              guessCount++;
              System.out.println("Too Low");
              System.out.println("Enter guess or 0 to quit: ");
              guess = scan.nextInt();
          }
          else if (randomNumber < guess) {
              guessCount++;
              System.out.println("Too High");
              System.out.println("Enter guess or 0 to quit: ");
              guess = scan.nextInt();
          }
      }
      if (randomNumber == guess) {
          guessCount++;
          System.out.println("Correct! That took you " + guessCount + " guesses.");
      }
  } 
}
\$\endgroup\$

1 Answer 1

3
\$\begingroup\$

You can read this to understand scope of variables or method.

In short:

  1. public - Any member of a class declared as public can be accessed outside of the class. So declare public when you want to give client code access to methods/variables of a class.
  2. private - A private method or field is invisible and inaccessible to other classes, and can be used only within the class in which the field or method is declared.
  3. protected - Protected variables and methods allow the class itself to access them, classes inside of the same package to access them, and subclasses of that class to access them.
  4. static - In general when a number of objects created from the same class, they all have their distinct copies of instance variables stored in different location. Sometimes we want to have variables that are common to all objects. To accomplish this use static. Read this.

Think of the main method as a place where you can write code to test your class. No design / logic should be placed in main.

For example:

public class Hilo {
 public Hilo() {}
 //other methods...

public static void main(String[] args) {
 Hilo hilo = new Hilo();
 hilo.getRange();
 hilo.playGame();
 }
}

I would prefer to remove all the static variables used in your class and make those private. The two methods getRange and playGame can be public.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.