0

I am looking for some assistance with completing the assignment of Time2 with a test class involving user input. The assignment is: Modify the Time2 class (below) to implement the time as the number of seconds since midnight. The class should have one data field (an int with the number of seconds since midnight) instead of three. This change should not affect the arguments, behavior, or output of the public methods. Create a Driver class with a main method to test your Time2 class. This program should ask the user to input the number of hours, minutes, and seconds past midnight, creating a Time2 object and using the mutator methods. The program should then use the toString() method to print out the time. I believe the only things that need modifying are the the 3 integers - hour, minute, second - to become one integer (totalseconds) and the set/get methods. I believe my calculations are correct but my test class is where I'm stumbling. I'm wondering if there is an issue between the toString method and the toUniversalString. I'm able to have the test class request the user input but it keeps outputting 12:00:00AM.

public class Time2 {
    private int totalseconds;

    public Time2(int hour, int minute, int second)setTime
    {       
        this.totalseconds = (hour * 3600);
        this.totalseconds += (minute * 60);
        this.totalseconds += (second);
    }

    public Time2(Time2 time)setTime
    {
        this(time.getHour(), time.getMinute(), time.getSecond());
    }

    public void setTime(int hour, int minute, int second) 
    {
        if (hour < 0 || hour >= 24)
            throw new IllegalArgumentException("Hour must be 0-23");
        if (minute < 0 || minute >= 59)
            throw new IllegalArgumentException("Minute must be 0-59");
        if (second < 0 || second >= 59)
            throw new IllegalArgumentException("Hour must be 0-59");

         this.totalseconds = (hour * 3600);
         this.totalseconds += (minute * 60);
         this.totalseconds += (second);
    }

    public void setHour(int hour)
    {
        if (hour < 0 || hour >= 24)
            throw new IllegalArgumentException("Hour must be 0-23");
        this.totalseconds = (hour * 3600);
    }

    public void setMinute(int minute)
    {
        if (minute < 0 || minute >= 59)
            throw new IllegalArgumentException("Minute must be 0-59");
        this.totalseconds = (minute * 60);
    }

    public void setSecond(int second)
    {
        if (second < 0 || second >= 24)
            throw new IllegalArgumentException("Second must be 0-59");
        this.totalseconds = (second);
    }

    public int getHour()
    {
        return totalseconds / 3600;
    }

    public int getMinute()
    {
        return (totalseconds - (3600 * getHour())) / 60;
    }

    public int getSecond()
    {
        return totalseconds - (3600 * getHour()) - (60 * getMinute());
    }

    public String toUniversalString()
    {
        return String.format(
        "%02d:%02d:%02d", getHour(), getMinute(), getSecond());
    }

    public String toString()
    {
        return String.format("%d:%02d:%02d %s",((getHour() == 0 || getHour() ==
        12) ? 12 : getHour() % 12), getMinute(), getSecond(), (getHour()
        < 12 ? "AM" : "PM"));
    }
}

Test class

 import java.util.Scanner;

  public class Time2Test {
     public static void main(String[] args) { // instantiate CommissionEmployee object

       Scanner input = new Scanner(System.in);
       System.out.print("Enter hour:");
       String hour = input.next();
       Integer.parseInt(hour);
       System.out.print("Enter minute:");
       String minute = input.next();
       Integer.parseInt(minute);
       System.out.print("Enter second:");
       String second = input.next();
       Integer.parseInt(second);

       Time2 clock = new Time2(0, 0, 0);
       System.out.printf(clock.toString());
     }
  }
Snowbrd1
  • 15
  • 5

1 Answers1

0

You're using the wrong constructor. You have a series of telescoping constructors which don't seem to serve any greater purpose than to confuse yourself. You create a new instance of Clock like this:

Time2 Clock = new Time2();

What you really want is another one of your constructors:

public Time2(int hour, int minute, int second)
{       
    this.totalseconds = (hour * 3600);
    this.totalseconds = (minute * 60);
    this.totalseconds = (second);
}

Before you can call it, however, note that it accepts int input, so you're going to have to parse String input from your scanner using parseInt or valueOf. In either case, you can get a NumberFormatException, which must be caught.

After that, notice that your constructor is missing a call to your setTime() method. You're going to get 12:00:00 because you never set the time using your user input.

And then... notice again, in your constructor, you have a series of assignment operators (=) where really, the operators for minute and second should be +=.

None of this stuff is really technical, but it is sloppy. Sometimes, when you get stuck, you need to just walk away and do something else for a bit. Then go back and review your code with fresh eyes. Stick with it, but take breaks. This stuff is going to be second nature soon enough.

NOTE: don't name instance variables like Clock starting with capital letters. Initial capitalization is reserved for actual class names. Keep the instance names camel case (lower case first letter, capital letter for the second and subsequent words in the variable name likeThis.

MarsAtomic
  • 10,436
  • 5
  • 35
  • 56
  • Thank you for the feedback. I've made some of the changes but I'm confused on some of the others..I've addressed the operators as well as removed some of the excess constructors. And added intParse to my test class. Although, I'm still a little confused on where to add in my setTime method. See my corrections above – Snowbrd1 Dec 15 '19 at 19:31
  • @Snowbrd1 Just add setTime to the end of each constructor. The point of setTime is to set the time, yeah? So after you get the time input in the constructor, you set the time, also in the constructor. That's all. When you see the code magically working, please click the checkmark to the left of this answer to let people know what worked. – MarsAtomic Dec 15 '19 at 20:20
  • Sorry, I'm still learning and don't quite understand your direction. I have 2 constructors and when I place setTime after the parentheses in each I get syntax errors requesting ; and }. If I understand you correctly I edited my 2 constructors above but that doesn't seem right. Thanks again for your help. – Snowbrd1 Dec 15 '19 at 23:32
  • public Time2(int hour, int minute, int second) { this.totalseconds = (hour * 3600); this.totalseconds += (minute * 60); this.totalseconds += (second); setTime(Integer.parseInt(hour), Integer.parseInt(minute), Integer.parseInt(second)); } Make sure to click the check mark next to this answer because we're not even talking about your original question anymore. You should be posting in a new question. – MarsAtomic Dec 15 '19 at 23:42
  • Thank you. I was not understanding the way that you had it worded. It makes more sense now. – Snowbrd1 Dec 16 '19 at 03:12
  • I suspect you inserted setTime after the constructor... Anyhow, I think everything you asked (and more) has been nailed down, so feel free to post new questions separately. – MarsAtomic Dec 16 '19 at 03:21