DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   Java (http://www.daniweb.com/forums/forum9.html)
-   -   Simple Java Help (http://www.daniweb.com/forums/thread158357.html)

00matt Nov 19th, 2008 2:43 pm
Simple Java Help
 
3 Attachment(s)
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

Antenka Nov 19th, 2008 3:10 pm
Re: Simple Java Help
 
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.

javaAddict Nov 19th, 2008 3:13 pm
Re: Simple Java Help
 
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;
}

00matt Nov 19th, 2008 3:46 pm
Re: Simple Java Help
 
1 Attachment(s)
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

00matt Nov 19th, 2008 4:11 pm
Re: Simple Java Help
 
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

Antenka Nov 19th, 2008 4:21 pm
Re: Simple Java Help
 
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()));

00matt Nov 19th, 2008 5:22 pm
Re: Simple Java Help
 
Thanks everyone, I have it sorted now :)

Really appreciated all your help,

Matt

jasimp Nov 19th, 2008 5:52 pm
Re: Simple Java Help
 
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 :)


All times are GMT -4. The time now is 6:33 pm.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC