views:

134

answers:

8

I have some questions about java. There are two questions in the code (I left them as comments). Also what is the purpose of using setting and getting methods? Could you please explain it very briefly. I am a beginner. Thank you :)

public class Die
{
   private final int MAX = 6;  
   private int faceValue;  

   public Die()
   {
      faceValue = 1;

      //why do you set the faceValue as 1?
   }

   public int roll()
   {
      faceValue = (int)(Math.random() * MAX) + 1;
      //Why do we use MAX instead of just number 6?

      return faceValue;
   }

   public void setFaceValue (int value)
   {
      if (value > 0 && value <= MAX)
         faceValue = value;
   }

   public int getFaceValue()
   {
      return faceValue;
   }

   public String toString()
   {
      String result = Integer.toString(faceValue);
      return result;
   }
}
+1  A: 

Setting faceValue is assigning a reasonable die roll upon initialization, without depending on a call to roll(). Using MAX instead of 6 makes it easier to change the die size - e.g., plenty of other games use different die sizes and maybe you want to port this code to them.

As a side note, the formatting in the question code could use a little work to improve readability.

Carl
+1  A: 
//why do you set the faceValue as 1?

In Java, integers are set to 0 by default, but in dice there is no face has 0 value.

//Why do we use MAX instead of just number 6?

MAX is used, so that whenever you want to update the value of MAX, you update it at one place. And it will get updated where ever you have used it. In this way it becomes easy to maintain and accommodate changes.

JavaGeek
+2  A: 

You use a constant ("MAX") because on some occasion you might want to change the value, for example if you want to roll a twenty-sided dice. In that case you only need to change the code in one line which makes it easier to maintain. It doesn't make THAT much sense in this simple kind of program, but in more complex projects, you want to use constants to quickly change certain (fixed) values without checking every line.

Getter and Setter methods are used in Object-oriented programming to keep variables encapsulated and provide an interface to make those variable accessable by other classes

redfalcon
+2  A: 

First of all you should use the code sample tag properly this is ugly to read like that. Using getter/setter methods will prevent the direct access to an instance variable. This is also called data hiding or encapsulation. As for your questions, the faceValue gets initialized with the value 1, you normaly do init things within the constructor. The second question Math.random will generate a number between 0-1 you are multiplying it with 6 which results into a number between 0 and 5. So you add +1 to have the range of 1-6.

kukudas
+1  A: 

Hi , So this is what we call a Data class this means that this class is used to represent the data that's getting sent around your program in this case its the data for the die. The Getters/Setters if done correctly allow you to access this data and change values , creating data classes like this is useful when creating multiples, so in your case if you just declared these variable within the main class it would be very hard and akward to created multiple dies this way though you can create multiple instances of die. As far as facevalue being 1 i assume this is because the value of a real die is never 0 so therefore setting to one is the default value before the roll. it could be any value really as they all have similar probabilitys. You use max because its always going to be 6 and this way it cannot be changed by any other methods by mistake. Hope that helps +1 Good Question for Beginners Thanks Chris

Chris
+2  A: 

Simply

//why do you set the faceValue as 1?

because default value set would be 0 .it is not desirable in your logic so one.

//Why do we use MAX instead of just number 6?

Its always better practice to use constant values as public static final field

org.life.java
+2  A: 

Q: Why use getters and setters?

A: Using them in conjuction with private instance variables allows you to define exactly how the value of a field is to be changed. It might become tedious because most of the time, getters and setters will just get/set the value without any checks or side effects. But even here in your code, you see how the setter of faceValue checks to see if the value given is between 0 and the MAX. This guarentees when anyone, including yourself, wants to change faceValue, the input will be checked.

This is called encapsulation, where you hide the fields of a class from everything outside that class and define very specific ways on how to change get and manipulate those fields.

Q: Why set faceValue to 1?

A: This just initializes faceValue to 1. So if you call getValue without first rolling the die, it will always return 1. It can be anything you want.

Q: Why use MAX?

A: It is very good programming practice to use constants for any constant in your code.

First of all, it makes the code easier to read. If I were reading your code and I saw value <= 6, I'd be confused about what 6 actually is. If I saw something like DICE_MAX_VALUE, then I know exactly what you were checking.

Secondly, perhaps more importantly, using constants lets you define it once and use it anywhere and be guarenteed to have the same value. If you ever wanted to change this into a 20 faced die, you'd have to go into your code and change all instances of '6' into '20' and that can be annoying, time consuming, and error prone.

shoebox639
+1  A: 

This example is directly out of a textbook I used in university. Are you in Ryerson's ITM program?

Remember that a physical die will roll anything between a 1 and a 6. Consider the following:

public class Monopoly
{
  public static void main(String[] args)
  {
     Die myDie = new Die();
     System.out.println(myDie.getFaceValue());
  }
}

What do you think would happen if your constructor did not initialize facevalue?

Using MAX instead of 6 has more to do with code readability. In a big program, you could put 6 everywhere. It would be fine for you because you wrote it, but the developer that comes to replace you in two years would see these 6s everywhere and may not understand why you used the number 6. By using a constant (MAX) and using that word, you're adding more context to what's going on. Secondly, if the game rules change to use a die that goes from 1-8, it's easier to update MAX = 8 in one area, as opposed to changing 6s to 8s everywhere.

baultista