0

I'm really new to programming, and I'm working on a java assignment for school and I know That i got most of the code I've written wrong, but it's the first time I've really attempted anything like this.

I'm trying to trouble shoot, but i get the error

Exception in thread "main" java.lang.StackOverflowError

at a07.Time.(Time.java:27)

at a07.Time.(Time.java:27)

at a07.Time.(Time.java:27)

This continues for quite some time.

I was hoping someone could tell me the cause of the problem so i could start fixing.

    Public class Time {

        private int hour;
        private int minute;
        public static final int MIN_HOUR = 0;
        public static final int MIN_MINS = 0;

        public Time(int hours, int mins) {
            minute = mins;
            hour = hours;
            Time t = new Time(hour, minute);
            t.fixME();
        }

        private void fixME() {
            int minEx = minute / 60;
            int minLeft = minute % 60;
            int newHour = hour + minEx;

            minute = minLeft;
            hour = newHour;

        if (minute < MIN_MINS) {
            int subHour = hour - 1;
            int minNew = minute + 60;
            hour = subHour % 24;
            minute = minNew;

            if (hour < MIN_HOUR) {
                int hourUp = hour + 24;
                hour = hourUp;
            }
        }
    }

    public int getHour() {
        return hour;
    }

    public int getMinute() {
        return minute;
    }

    public Time addMinutes(int mins) {
        int newMinute = minute + mins;
        minute = newMinute;
        Time g = new Time(hour, mins);
        return g;

    }

    public void print() {
        if (hour == 0) {
            System.out.printf("12:" + "%02f", minute + " AM");
        }
        if (hour != 0 && hour < 10) {
            System.out.printf("0" + hour + ":" + "%02f", minute + " AM");
        }
        if (hour >= 10 && hour < 12) {
            System.out.printf(hour + ":" + "%02f", minute + " AM");
        }
        if (hour >= 12) {
            int pmHour = hour - 12;
            if (pmHour == 0) {
                System.out.printf("12:" + "%02f", minute + " PM");
            } else if (pmHour < 12) {
                System.out.printf(pmHour + ":" + "%02f", minute + " PM");
            }

        }

    }
}

Sorry it's not formatted very well. Any ideas? Thanks.

GnarlyTot
  • 21
  • 3

1 Answers1

2

The problem is in your constructor:

public Time(int hours, int mins) {
    minute = mins;
    hour = hours;
    Time t = new Time(hour, minute);
    t.fixME();
}

You're calling the constructor again with the line Time t = new Time(hour, minute), so you're stuck in an infinite recursive loop where you keep calling the function over and over again until your stack overflows. You don't actually need this line; you can just do the following:

public Time(int hours, int mins) {
    minute = mins;
    hour = hours;
    fixME();
}

I don't think you fully understand how objects and constructors work though, you should probably try going through this: https://docs.oracle.com/javase/tutorial/java/javaOO/

Zarwan
  • 5,537
  • 4
  • 30
  • 48