Home » Java » Seconds are not changing in Clock made in Java-Exceptionshub

Seconds are not changing in Clock made in Java-Exceptionshub

Posted by: admin February 25, 2020 Leave a comment

Questions:

I am creating a clock, I am unsure why the time is not moving.

It seems as though my seconds method for changing it, is not doing anything on my time method, when called.

This is my Time class:

public class Time {
private int hours; 
private int minutes; 
private int seconds; 

public Time() {

}
public void time(int newhours, int newminutes, int newseconds) {
    hours=newhours;
    minutes=newminutes;
    seconds=newseconds;

    setHours(newhours);
    setMinutes(newminutes);
    setSeconds(newseconds);


}
public void setTime(int setHours, int setMinutes, int setSeconds) {
/*  hours=setHours;
    minutes=setMinutes;
    seconds=setSeconds;
                        */
    setHours(23);
    setMinutes(59);
    setSeconds(59);
}



public int getHours() {
    return hours;
}
public void setHours(int hours) {
    this.hours = hours;
}

public int getMinutes() {
    return minutes;
}
public void setMinutes(int minutes) {
    this.minutes = minutes;
}

public int getSeconds() {
    return seconds;
}
public void setSeconds(int seconds) {
    this.seconds = seconds;
}



public int nextSecond(int add) {
    add = 1;
    return (seconds + add);
}
public int previousSecond(int sub) {
    sub = 1;
    return (seconds - sub);
}



public String toString() {
    return ("Time is "+hours+ ":"+ minutes+ ":"+ seconds);
}

}

My main client is below. I am suppose to change the time by seconds, increasing it by 10 seconds, then decreasing it by 20, and then making it print out certain times as shown below.

The two that I have to make print, work however when it comes to changing the time, nothing changes.

 public class TimeClient {

 public static void main(String[] args) {
    Time T1 = new Time();

    //set time
    T1.setTime(23,59,59);
    System.out.println(T1);


    //increasing time by 10 seconds
    T1.nextSecond(10);
    System.out.println(T1);


    //decreasing time by 20 seconds
    T1.previousSecond(20);
    System.out.println(T1);


    //wrong hours
    T1.time(25, 00, 00);
    System.out.println(T1);

    //wrong seconds
    T1.time(12,40,86);
    System.out.println(T1);
}

}

How to&Answers: