-4

Yesterday I asked this question regarding a while loop not ending and was told I need to set "correct" to true inside my main method using a setter. I did some research on setters and getters and I am completely lost. What exactly do they do and how do I use one in this situation?

EDIT: Thank you Ben Wainwright for your answer!

Main Method:

while (lives > 0 && correct == false) {
    startTime = System.currentTimeMillis();
    timeObject.time2();
    levelinfoObject.levelInfo(currentlevel);
    timeObject.time1();
    levelinfoObject.livesInfo(lives);
    timeObject.time1();
    levelinfoObject.skipsInfo(skips);
    timeObject.time2();
    questionsObject.questionOne(lives, correct, choice, skips, currentlevel)        
}

Questions one method in the questions class:

    public void questionOne(int lives, boolean correct, String choice, int skips, int currentlevel) {

    Scanner scanner = new Scanner(System.in);

    System.out.println(" ");
    System.out.println("Question: If there are 6 apples in a tree and you take 4, how many do you have?");

    timeObject.time2();

    System.out.println("A: 3");
    System.out.println("B: 4");
    System.out.println("C: 2");
    System.out.println("D: 6");

    while (correct == false && lives > 0) {

        choice = scanner.nextLine();

        switch(choice) {
        case "a":
            System.out.println("WRONG! Try again.");
            lives = lives - 1;
            break;
        case "b":
            System.out.println("CORRECT! You have the 4 you took obviously.");
            correct = true;
            break;
        case "c":
            System.out.println("WRONG! Try again.");
            lives = lives - 1;
            break;
        case "d":
            System.out.println("WRONG! Try again.");
            lives = lives - 1;
            break;
        case "skip":
            if (skips > 0) {
                System.out.println("You have skipped level " + currentlevel + "!");
                skips = skips - 1;
                correct = true;
            }

            else {
                System.err.println("You do not have any skips left!");
            }
            break;
        default:
            System.err.println("Please type an answer.");
            break;
        }
    }
KobiF
  • 60
  • 1
  • 12
  • Setters and getters are explained in every half-decent tutorial, what's your excuse for not knowing what they are? – Kayaman Oct 02 '16 at 15:25
  • Getters and setters are accessors and mutators to variables on a class. These are very standard to OOP (encapsulation). – Drew Kennedy Oct 02 '16 at 15:26
  • Well @Kayaman my "excuse" is that I don't understand. Like I said above, I tried learning about them and I don't understand. Is too crazy for YOU to understand that someone could possibly have trouble with something. – KobiF Oct 02 '16 at 15:30
  • @KobiF You used a lot of empty words just for saying "I haven't read any tutorials". – Kayaman Oct 02 '16 at 15:33
  • May I offer some [complementary debugging techniques](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) to help you to either debug this on your own, or to narrow down your issue to something appropriate for this site? – Joe C Oct 02 '16 at 16:13

2 Answers2

1

Ok, OOP 101, apologies if you already know this stuff, but it seems like a back to basics approach might be helpful.

In Java, as with most object oriented programming, you are predominantly involved with dealing with 'objects' which are defined by 'classes'. Objects are instances of a particular type of 'thing' within a program which have 'methods' (things they do) and 'properties'.

If we consider the case of a car. A specific car might be red, or yellow; it might also have three or five doors. These are properties. Similarly the car can do things, like drive. Crucially, we don't need to know how the car works to drive it, we just have to press the pedal. The same is true for our Java object; we don't really care about the internal workings, all we have to do is call the drive() method. Below, a 'car' object is created, given a color, and then the 'drive' method is called.

Car c = new Car();
car.colour = Car.Colour.RED;
car.drive();

A 'class' describes what a car is like (its properties) and what it can do (its methods). Thinking about the car analogy; you might consider the class to be like the 'factory blueprint' for the car.

class Car {

   public enum Colour {
      RED,
      GREEN,
      ORANGE,
      RAINBOW_COLOURED
   }

   public Colour colour;
   public int speed;

   public void drive() 
   {
      // ... some code that implements driving
   }
}

So you can see here I've added a number of properties, the values of which can all be changed from the outside. This is all very well and good; perhaps we want to make our car faster so we can just change it by going

car.speed = 10000000000;

Awesome; now we have a super fast car. But what if we want to do something a bit more complicated. How about we represent the engine of our car in code, and the speed of our car is dependent on the power of the engine.

class Car {

   ...

   public int speed() {

      // This is an arbitrary operation for the sake of me not
      // having to learn how engines work. Ahem.

      return engine.power() * 842; 
   }

   public Engine engine; // Assume this class is defined elsewhere

   ...
}

GREAT! that's even more cool now except...

Whoops, the line above which tried to set the speed of the car is now broken.

And that brings us to our main point. If originally, we had only given access to our speed via a method, this problem would not arise. We can do this a couple of ways:

Note that everything seems to have the keyword public before it. That means that 'this property/method can be accessed from outside the object. So first, we change all of our properties to private access, then provide a pair of methods to access them. These methods are called 'setters' and 'getters'. See below

class Car {

   ...

   private int speed;

   public int getSpeed() // The getter
   {
      return speed;
   }

   public void setSpeed(int newSpeed) // The setter
   {
      speed = newSpeed;
   }

   ...
}

Edit

I'm not going to give you the exact code to solve your problem, because you wouldn't learn anything. But roughly what you need to do is:

  • Create a private correct property on your Question class; create at least a getter as shown above
  • In your main class, you can then access this getter on the questionsObject instead of accessing the correct variable in the while loop condition
Ben Wainwright
  • 4,224
  • 1
  • 18
  • 36
  • Thank you for this very detailed explanation! I just have one more question. So after setting up the getter and setter methods above how do I change or use the methods from a different class? @Ben Wainwright – KobiF Oct 02 '16 at 16:14
  • @KobiF I've added a little bit more. There is more than enough in my post now to work it out... :P – Ben Wainwright Oct 02 '16 at 17:20
0

It seems to me like you have a poor understanding of some fundamentals within Java, specifically OO and in this case scope, which is fine, we've all been there.

I recommend going through this well written article by Jon Skeet. This will explain to you the difference of passing by value or reference.

In (very) short; when you pass by value you cannot alter the state of that value outside its own scope.

This explains why your main body does not respond to any variable value changes made in the method, simply because they're not actually being modified, you're just sending them off to a new scope.

As for encapsulation: access modifiers, getters and setters.. I recommend the following tutorial: Java Encapsulation

ambs
  • 487
  • 3
  • 7