0

First of all, I have read this question which i am aware is dealing with the same fundamental problem I am having.

Nevertheless, I am unable to apply the solution to my own particular problem.

in the following example i have a few overloaded Clock constructors. The error occurs in the case where I am trying to create a Clock from string input.

Does anyone have any tips for the correct implementation of the constructor call?

Code:

    public class Clock 
    {
        public static void main(String[] args) 
        {

            Clock testClock = new Clock(153, 26);
            System.out.println("Input: Clock(153, 26)");
            System.out.println(testClock.toString());

            Clock testClock2 = new Clock(9000);
            System.out.println("Input: Clock(9000)");
            System.out.println(testClock2.toString());


            Clock testClock3 = new Clock(23:59);
            System.out.println("Input: Clock(23:59)");
            System.out.println(testClock3.toString());

            System.out.println("Input: testClock2.add(20)");
            System.out.println(testClock2.add(20));             

            System.out.println("input: testClock.add(testClock2)");
            System.out.println(testClock.add(testClock2).toString());

        }

        // Constructors

        public Clock(int min, int h)
        {
            this.min += min%60;
            h += min/60;
            this.h += h%24;
        }

        public Clock(int min)
        {
            this.min += min%60;
            this.h += (min/60)%24;
        }



        public Clock (String theTime)
        {
            int minutes = Integer.parseInt(theTime.substring(0,1));
            int hours = Integer.parseInt(theTime.substring(3,4));

            Clock stringClock = new Clock(minutes, hours); 
            return stringClock; //error occurs *********************

        }



        private int h;      
        private int min; 

        public int getMin() {
        return min;
        }
        public int getH() {
        return h;
        }

        public Clock add(int min)
        {
            int newMin = this.min + min;
            int newH = this.h;

            Clock newClock = new Clock(newMin, newH);
            return newClock;
        }

        public Clock add(Clock c)
        {
            int newMin = this.min + c.min;
            int newH = this.h + c.h;

            Clock newClock = new Clock(newMin, newH);
            return newClock;
        }

        public String toString()
        {

            String theTime = "";

            if (this.h < 10)
            {
                theTime += "0" + this.h;
            }
            else
            {
                theTime += this.h;
            }

            theTime += ":";

            if (this.min < 10)
            {
                theTime += "0" + this.min;
            }
            else 
            {
                theTime += this.min;
            }

            return theTime;
        }
    }
Jackie
  • 173
  • 9

3 Answers3

2

You could call this but it should be very first statement such as:

    public Clock (String theTime)
    {
        this(
            Integer.parseInt(theTime.substring(0,1)),
            Integer.parseInt(theTime.substring(3,4))
        );

    }

Alternatively you could use a static factory method:

    public static Clock parseHHMM(String theTime)
    {
        int hh = Integer.parseInt(theTime.substring(0,1));
        int mm = Integer.parseInt(theTime.substring(3,4));
        return new Clock(hh, mm);

    }

I prefer the latter, it is common approach in Java, e.g. here.

kan
  • 28,279
  • 7
  • 71
  • 101
2

There is also a problem with that line:

Clock testClock3 = new Clock(23:59);

If you want the argument to be treated as String, you should surround the value passed as argument with quotes, like this:

Clock testClock3 = new Clock("23:59");

, because when you don't change the look of parameter passed, it won't compile.

Przemysław Moskal
  • 3,551
  • 2
  • 12
  • 21
0

You could consider splitting theTime at the semicolon":" so as to obtain an array of two integers, the first being the hour and the second being the minutes. Take a look

    public Clock (String theTime)
    {
        this(Integer.parseInt(theTime.split(":")[1],
        Integer.parseInt(theTime.split(":")[0]);
    }

hope this helps

Cels
  • 1,212
  • 18
  • 25