KobiF KobiF - 2 months ago 10
Java Question

Java loop needs setter

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?

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;
}
}

Answer

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
Comments