1. Engineering
  2. Computer Science
  3. need help making a timetest class that tests all of...

Question: need help making a timetest class that tests all of...

Question details

Need help making a TimeTest class that tests all of my public methods in Time.java using JUnit, I have provided Time.java below.

*** Here is my Time.java class BELOW ***

class Time implements Cloneable, Comparable {
    private int hour, minute;
    private boolean pm;
    public Time(int hour, int minute, boolean pM) {
        setHour(hour);
        setMinute(minute);
        this.pm = pM;
    }
    public int getHour() {
        return hour;
    }
    public void setHour(int hour) {
        if(hour < 1 || hour > 12) {
            throw new IllegalArgumentException();
        }
        this.hour = hour;
    }
    public int getMinute() {
        return minute;
    }
    public void setMinute(int minute) {
        if(minute < 0 || minute > 59) {
            throw new IllegalArgumentException();
        }
        this.minute = minute;
    }
    public boolean isPM() {
        return pm;
    }
    public void setPM(boolean pM) {
        this.pm = pM;
    }


    public static Time fromString(String string) {
        if(string.length() != 8) {
            throw new IllegalArgumentException("Invalid String");
        }
        if(!string.contains(" ") || !string.contains(":")) {
            throw new IllegalArgumentException("Invalid String");
        }

        String timePart = string.split(" ")[0];
        String pm = string.split(" ")[1];

        if(!pm.equals("AM") && !pm.equals("PM")) {
            throw new IllegalArgumentException("Invalid String");
        }

        int h = Integer.parseInt(timePart.split(":")[0]);
        int m = Integer.parseInt(timePart.split(":")[1]);
        return new Time(h, m, pm.equals("PM"));
    }

    public void shift(int i) {
        minute += i;

        if(minute < 0) {
            while(minute < 0) {
                minute += 60;
                hour -= 1;

                if(hour == 0) {
                    hour = 12;
                    pm = !pm;
                }
            }
        } else if(minute > 59) {

            while(minute > 59) {
                minute = minute - 60;
                hour += 1;
                if(hour == 12) {
                    pm = !pm;
                } else if(hour == 13) {
                    hour = 1;
                }
            }
        }

    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + hour;
        result = prime * result + minute;
        result = prime * result + (pm ? 1231 : 1237);
        return result;
    }
    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Time other = (Time) obj;
        if (hour != other.hour)
            return false;
        if (minute != other.minute)
            return false;
        if (pm != other.pm)
            return false;
        return true;
    }


    public Time clone() {
        return new Time(hour, minute, pm);
    }
    @Override
    public int compareTo(Time o) {
        if(!o.isPM() && isPM()) {
            return 1;
        } else if(!isPM() && o.isPM()) {
            return -1;
        }

        int h1 = hour;
        if(h1 == 12) {
            h1 = 0;
        }

        int h2 = o.hour;
        if(h2 == 12) {
            h2 = 0;
        }

        if(h1 < h2) {
            return -1;
        } else if(h1 > h2) {
            return 1;
        } else {
            if(minute < o.minute) {
                return -1;
            } else if(minute > o.minute) {
                return 1;
            } else {
                return 0;
            }
        }
    }
    @Override
    public String toString() {
        return String.format("%02d:%02d ", hour, minute) + (isPM() ? "PM" : "AM");
    }

}
Solution by an expert tutor
Blurred Solution
This question has been solved
Subscribe to see this solution