Hi, I am new to java and would really appreciate it if someone could look at the code I have attached.
This is part of a java lab assignment that I have got to do for university. For reference, I have also uploaded the lab file. I am trying to demonstrate the use of get and set methods by creating a clock which can be set by the user. When I call the set method and accept input from the user, the value that I define gets reset to 0 when I call the get method.
If you run the TimeTest.java program, and select 1, then enter any number, the last number that is returned is 0, but it should be the number you just entered.

Any help greatly appreciated…

Matt

Attachments
public class Time{


	private int seconds;

	private int minutes;

	private int hours;

	public Time(){

	}

	public void TimeTest(int seconds, int minutes, int hours){

		this.hours = hours;
		this.minutes = minutes;
		this.seconds = seconds;

		System.out.printf("%02d:%02d:%02d", this.hours, this.minutes, this.seconds);

	}

	public void getSeconds(){

		System.out.println(this.seconds);

	}


	public void getMinutes(){

		System.out.println(this.minutes);

	}

	
	public void getHours(){

		System.out.println(hours);

	}


	public void setSeconds(int seconds){

		this.seconds = seconds;

	}


	public void setMinutes(int minutes){

		this.minutes = minutes;

	}


	public void setHours(int hours){

		this.hours = hours;
		System.out.println(hours);
		System.out.println(this.hours);

	}


	public void addSecond(){

		this.seconds++;

	}

}
import java.util.Scanner;

public class TimeTest{

	public static void main( String args[] ){

		Time application = new Time();
		application.TimeTest(0, 0, 0); //call setHours from Time application

		Scanner input = new Scanner( System.in );

		System.out.println("Please select an option below:"); //Ask user to select option from list
		System.out.println("1. Set Hour");
		System.out.println("2. Set Minute");
		System.out.println("3. Set Second");
		System.out.println("4. Add 1 Second");
		System.out.println("5. Exit");
	
		int option = input.nextInt(); //store user option

		do{ //Start of do...while loop

			switch(option){
	
				case 1:
	
					
					Time application2 = new Time();
					application2.setHours(input.nextInt()); //call setHours from Time application
					Time app = new Time();
					app.getHours();
					break;
				
				case 2:


				case 3:
	

				case 4:
		

				default:
	
					System.out.println("Option not valid - Please try again..."); //Error when selecting invalid option

			}

			System.out.println("Please select an option below:"); //Ask user for option
			System.out.println("1. Set Hour");
			System.out.println("2. Set Minute");
			System.out.println("3. Set Second");
			System.out.println("4. Add 1 Second");
			System.out.println("5. Exit");
	
			option = input.nextInt();

		}while(option != 5);

	
	}


}

Correct me, if I'm wrong:
1. You creating new "clock" and setting it to 0.

Time application = new Time();
application.TimeTest(0, 0, 0);

2. Then while setting the hours creating 2 more clocks.

Time application2 = new Time();					application2.setHours(input.nextInt());

and

Time app = new Time();
app.getHours();

Here is your problem: you setting hours to the clock-2 and getting it (hours) from clock-3.

Every time you do: Time application = new Time(); you create a new object and therefor you reset the values back to zero.
So create only ONE object (do this once: Time application = new Time(); )
And then use the application instance to change its values.


By the way, the Time class is totally wrong. The get, set methods are not suppose to print anything:

class Time {
private int seconds = 0;
....
....
public Time() {

}

public Time(int seconds, ......) {
   this.seconds = seconds;
   .....
   .....
}

public int getSeconds() {
  return seconds;
}

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

Now if your teacher wants the set methods to return boolean, then:

public boolean setSeconds(int seconds) {
  if (seconds<0) {
     return false;
  }
  this.seconds = seconds;
  return true;
}

Hi, Thanks for your quick replies!

I have tried to change the Time.java file, attached, however, when compiling it, it says there are missing return statements?

Also, if i use boolean return type for the set methods, how do I program the TimeTest.java to know whether this is true or false?

Thanks again,
Matt

Attachments
public class Time{

	private int seconds;

	private int minutes;

	private int hours;

	public Time(){

	}

	public void TimeTest(int seconds, int minutes, int hours){

		this.hours = hours;
		this.minutes = minutes;
		this.seconds = seconds;

	}

	public int getSeconds(){

		return seconds;

	}


	public int getMinutes(){

		return minutes;

	}

	
	public int getHours(){

		return hours;

	}


	public boolean setSeconds(int seconds){

		this.seconds = seconds;

	}


	public boolean setMinutes(int minutes){

		this.minutes = minutes;

	}


	public boolean setHours(int hours){

		if (hours>60) {
		return false;
 		}
		this.hours = hours;
		return true;

	}


	public void addSecond(){


	}

}

Figured out why it wouldnt compile...

I just need to know how to program the TimeTest.java to know wether the booleans are true or false?

Thanks

You can output them:

System.out.println(application2.setHours(input.nextInt()));

or check, if the setting passed correctly

if(application2.setHours(input.nextInt())))
{//all good}
else {//something wrong}

Also you can save that value to variable:

Boolean settingStatus = application2.setHours(input.nextInt()));

Thanks everyone, I have it sorted now :)

Really appreciated all your help,

Matt

Comments
Thanks for marking it as solved.

What you need to do is mark the thread as solved, so people don't read the whole thing to realize it has been solved. Just for future (and present) reference. Thanks :)

This question has already been answered. Start a new discussion instead.