0

I have an issue with my test class, the output as you can see below, the minutes returns an additional "0" which is I do not want, I do believe the issue comes from getDisplayValue method in my NumberDisplay class. Before I tried witn just if cases, however it was incorrect, it gave me many "0"s and then my teacher advise me to do with for-case, so I did, the results were superior in my test class to NumberDisplay then when I tried it on my 2nd test class to clock, it seems odd If you know what is the issue, I would appreciate the support


public class NumberDisplay {

    private int minLimit;
    private int maxLimit;
    private int value;
    private boolean wrapped;

    public NumberDisplay(int minLimit, int maxLimit) throws IllegalValueException {

        if (maxLimit > minLimit) {
            this.minLimit = minLimit;
            this.maxLimit = maxLimit - 1;
            this.value = minLimit;
            this.wrapped = true;
            // creates new number display with the value minimum limit
            // and the limits between maximum limit. if maximum limit is not greater than
            // minimum limit
            // it should throw exception

        } else {
            throw new IllegalValueException("felaktig");

        }

    }

    public int getValue() {
        return this.value;

        // return the current value on display with integer number
    }

    public void setValue(int newValue) throws IllegalValueException {
        if (newValue < minLimit || newValue >= maxLimit) {

            throw new IllegalValueException("Felaktig prova igen set time är felaktig");
            // set the current display value to newValue. if newValue is less greater than
            // minimum limit
            // or greater than value from display then throw exception
        } else {


                this.value = newValue;


        }

    }

    public String getDisplayValue() {
        String str = String.valueOf(this.maxLimit);
        String str1 = "0";


        if(str.length()==1) {
            return str1+this.value;
        }
        if(str.length()==2) {
            return str1+this.value;
        }
        if(str.length()==3) {
            return str1+str1+this.value;
        }
        if(str.length()==4) {
            return str1+str1+str1+this.value;
        }
        if(str.length()==5) {
            return str1+str1+str1+str1+this.value;
        }
        if(str.length()==6) {
            return str1+str1+str1+str1+str1+this.value;
        }else {
            for(int i =0;i<str.length();i++) {
                str1 = str1 +this.value;
            }
        }
        return ""+this.value;

    }


        // return the current value with 0 infront of the value

    public void increment() {

        if (this.value == maxLimit) {

            this.value = this.minLimit;
            this.wrapped = true;
        } else {

            this.value = +1;
        }

        // increases the display with one value and checks if maximum limit is reached
        // then
        // it should be converted to minimum limit

        // Consider to do the string delightful so all of values sets with as many as
        // numbers
        // the display should have. Values with low numbers should be zeros in front,
        // e.g. clock 9 09:00

    }

    public boolean diWrapAround() {
        if (this.value < maxLimit) {

            return this.wrapped = true;
        } else

            return false;

    }
    // it should return true if the display reached over maximum limit and started
    // from
    // minimum limit , otherwise it should return false,
    // use attribute wrapped to check boolean false or true.

}
public class Clock {

    private NumberDisplay hours;
    private NumberDisplay minutes;
    private String displayString;

    public Clock() throws IllegalValueException {

        this.hours = new NumberDisplay(0, 24);
        this.minutes = new NumberDisplay(0, 60);
        this.updateDisplay();

    }

    public Clock(int hour, int minute) throws IllegalValueException {
        this.hours = new NumberDisplay(0, 24);
        this.minutes = new NumberDisplay(0, 60);
        this.hours.setValue(hour);
        this.minutes.setValue(minute);

        this.updateDisplay();


    }

    public void timeTick() {

        this.minutes.increment();


    }

    public void setTime(int hour, int minute) throws IllegalValueException {
        this.hours.setValue(hour);
        this.minutes.setValue(minute);
        this.updateDisplay();

    }

    public String getTime() {
        return this.displayString;


    }

    private void updateDisplay() {

        this.displayString = this.hours.getDisplayValue()+":"+this.minutes.getDisplayValue();


    }

}
public class Test2 {

    public static void main(String[] args) {

        try {
            Clock klocka = new Clock(4343, 223);
            System.out.println(klocka.getTime());
        } catch (IllegalValueException a) {

            System.out.println(a.getMessage());
        }
        System.out.println("Expected: felmeddelande");
        System.out.println("");
        try {
            Clock klocka = new Clock(0, 24);
            System.out.println(klocka.getTime());
            System.out.println("Expected: 00:24");
            System.out.println("");
            klocka.timeTick();
            System.out.println(klocka.getTime());
            System.out.println("Expected: 00:01");
            System.out.println("");
            System.out.println(klocka.getTime());
            System.out.println("Expected to be 00:00");
            System.out.println("");
            try {
                klocka.setTime(123, 4554);
                System.out.println(klocka.getTime());
            } catch (IllegalValueException a) {

                System.out.println(a.getMessage());
            }
            System.out.println("Expected: Felmeddelande");
            System.out.println("");
            try {
                klocka.setTime(12, 23);
                System.out.println(klocka.getTime());
            } catch (IllegalValueException e) {
                System.out.println(e.getMessage());

            }
            System.out.println("Expected: 12:23");
            System.out.println("");

        } catch (IllegalValueException a) {

            System.out.println(a.getMessage());
        }

    }

}

Output

Felaktig prova igen set time är felaktig
Expected: felmeddelande

00:024
Expected: 00:24

00:024
Expected: 00:01

00:024
Expected to be 00:00

Felaktig prova igen set time är felaktig
Expected: Felmeddelande

012:023
Expected: 12:23

Adam Lord
  • 35
  • 9
  • 3
    Welcome to Stack Overflow. Your sample code is over 200 lines long, which makes it hard to tell exactly what you're looking for. If your question is really "how can I left-pad a string with zeroes to a particular length" you could demonstrate that in about 10 lines. Please try to provide a [mcve] in future - and ideally change this question into a [mcve] itself. I suspect relatively little of the code here is directly relevant to your question. – Jon Skeet Nov 29 '19 at 09:00

1 Answers1

1

The problem is that getDisplayValue doesn't take into account the length of a value itself, making decisions purely on maxLimit. See:

if(str.length()==2) {
    return str1+this.value;
}

For a two-digit display it will pad any value with a zero. So, you get "00" for "0" and "012" for "12".

To fix that you need to take the length of a value into account:

int limit = String.valueOf(maxLimit).length();
int length = String.valueOf(value).length();
StringBuilder displayValue = new StringBuilder();
//add a necessary number of zeros
for(int i=0; i<limit-length; i++) {
     displayValue.append("0");
}
displayValue.append(value);
return displayValue.toString();

There're smarter ways to pad a string with zeros, if you want to avoid a loop. See: Left padding a String with Zeros

P.S. You should probably test NumberDisplay thoroughly in isolation, before testing a Clock.

default locale
  • 11,849
  • 13
  • 52
  • 59