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

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

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

commented: Thanks for marking it as solved. +9

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 :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.