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;
        }
    }
4
  • Have you tried the solution proposed in the other question? Use this(minutes, hours) instead, a constructor does not return an object. Commented Nov 19, 2017 at 21:42
  • 1
    you don't return from a constructor. Commented Nov 19, 2017 at 21:42
  • Have you gotten an error? If so, where? Commented Nov 19, 2017 at 21:47
  • Your toString function can be return String.format(“%2d:%2d”, h, min) Commented Nov 19, 2017 at 21:52

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.