-1

I'm trying to compile a code that's using composition. My goal is to implement data validation code to make sure that when an object is instantiated, the scores that I'm using are greater than zero and less than 300. I've been attempting to throw an IllegalArgumentException, but whenever I compile the whole thing with a negative integer or an integer greater than 300, I don't receive any error message for the value and the code continues to run as normal. I just wanna know if anyone can lead me towards the right direction and give me some suggestions. Here's the code I came up with:

public class ScoresClass 
{
    private int score0;
    private int score1;
    private int score2;
    private int score3;

    public ScoresClass(int a, int b, int c, int d)
    {
        if (score0 < 0 || score0 >= 300)
            throw new IllegalArgumentException("Score must be between 0 - 300");
        if (score1 < 0 || score1 >= 300)
            throw new IllegalArgumentException("Score must be between 0 - 300");
        if (score2 < 0 || score2 >= 300)
            throw new IllegalArgumentException("Score must be between 0 - 300");
        if (score3 < 0 || score3 >= 300)
            throw new IllegalArgumentException("Score must be between 0 - 300");

        this.score0 = a;
        this.score1 = b;
        this.score2 = c;
        this.score3 = d;
    }

    public String toString()
    {
         return String.format("Score 0: %d%n Score 1: %d%n Score 2: %d%n "
            + "Score 3: %d%n", score0, score1, score2, score3);
    }

}

Here's my code that compiles the username:

public class BowlerClass 
{
    private String fName;
    private String lName;
    private ScoresClass value;

    public BowlerClass(String first, String last, ScoresClass amount)
    {
        this.fName = first;
        this.lName = last;
        this.value = amount;
    }

    public String toString()
    {
        return String.format("%s %s%n %s", fName, lName, value);
    }
}

And finally the test code:

public class BowlerTest {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        ScoresClass a = new ScoresClass(5000, 150, 200, 250);
        BowlerClass b = new BowlerClass("Paul", "McCartney", a);

        System.out.println(b);

    }

}
pinklitmus
  • 11
  • 2
  • I think in the `ScoresClass` you want to validate that `a`, `b`, `c`, `d` are correct before assigning them to the instance variables. Right now you are checking the values of the unassigned (and therefore initialized to 0) values of `score0`,rather than `a`. – KevinO Apr 27 '18 at 06:38
  • Learn about proper wording: your usage of *compiling* is not at all what compiling is about. And: learn about proper naming. What do you think that names a, b, c, d, e, mean? Exactly: they mean nothing. You dont write code to make the compiler happy. You write code that communicates intent to human readers. – GhostCat Apr 27 '18 at 06:38
  • @GhostCat Will do, thanks for the heads up – pinklitmus Apr 27 '18 at 06:40
  • There is a little keyword that will help you: `final`. Make all fields `final` unless you actually need to change them later (you don't in this code). The compiler would then have stopped you using these fields before initializing them. – Andy Turner Apr 27 '18 at 08:26

2 Answers2

2

Your fields score0 to score3 are initialized as 0. You have to set them BEFORE checking if the value is valid.

Alternatively, what would be more appropriate here, is to check if a,b,c and d are valid, and not check against the fields.

Link64
  • 718
  • 4
  • 20
0

Using an IllegalArgumentException is a really good idea, but you should check the arguments and not the instance variables. Don't modify instance variables until you know that the parameters are ok, otherwise you still leave the object in an incorrect state.

Additionally, you can enhance the message by adding the name of the parameter and the value of the parameter, this will help in finding the cause of the error and solving it.

public ScoresClass(int a, int b, int c, int d)
 {
    if (a < 0 || a >= 300)
        throw new IllegalArgumentException("Score 'a' must be between 0 - 300 :" + a);
    if (b < 0 || b >= 300)
       throw new IllegalArgumentException("Score 'b' must be between 0 - 300: " + b);
    if (c < 0 || c >= 300)
       throw new IllegalArgumentException("Score 'c' must be between 0 - 300: " + c);
    if (d < 0 || d >= 300)
       throw new IllegalArgumentException("Score 'd' must be between 0 - 300: " + d);
M. le Rutte
  • 3,525
  • 3
  • 18
  • 31