943,923 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 664
  • Java RSS
Nov 19th, 2008
0

Simple Java Help

Expand Post »
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
Attached Files
File Type: pdf lab7.pdf (59.2 KB, 16 views)
File Type: java Time.java (923 Bytes, 12 views)
File Type: java TimeTest.java (1.4 KB, 13 views)
Similar Threads
Reputation Points: 19
Solved Threads: 0
Newbie Poster
00matt is offline Offline
4 posts
since Nov 2008
Nov 19th, 2008
0

Re: Simple Java Help

Correct me, if I'm wrong:
1. You creating new "clock" and setting it to 0.
java Syntax (Toggle Plain Text)
  1. Time application = new Time();
  2. application.TimeTest(0, 0, 0);
2. Then while setting the hours creating 2 more clocks.
java Syntax (Toggle Plain Text)
  1. Time application2 = new Time(); application2.setHours(input.nextInt());
and
java Syntax (Toggle Plain Text)
  1. Time app = new Time();
  2. app.getHours();

Here is your problem: you setting hours to the clock-2 and getting it (hours) from clock-3.
Reputation Points: 293
Solved Threads: 82
Posting Whiz
Antenka is offline Offline
361 posts
since Nov 2008
Nov 19th, 2008
0

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:

Java Syntax (Toggle Plain Text)
  1. class Time {
  2. private int seconds = 0;
  3. ....
  4. ....
  5. public Time() {
  6.  
  7. }
  8.  
  9. public Time(int seconds, ......) {
  10. this.seconds = seconds;
  11. .....
  12. .....
  13. }
  14.  
  15. public int getSeconds() {
  16. return seconds;
  17. }
  18.  
  19. public void setSeconds(int seconds) {
  20. this.seconds = seconds;
  21. }
  22. }

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

Java Syntax (Toggle Plain Text)
  1. public boolean setSeconds(int seconds) {
  2. if (seconds<0) {
  3. return false;
  4. }
  5. this.seconds = seconds;
  6. return true;
  7. }
Sponsor
Featured Poster
Reputation Points: 1014
Solved Threads: 446
Nearly a Senior Poster
javaAddict is offline Offline
3,259 posts
since Dec 2007
Nov 19th, 2008
0

Re: Simple Java Help

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
Attached Files
File Type: java Time.java (770 Bytes, 8 views)
Reputation Points: 19
Solved Threads: 0
Newbie Poster
00matt is offline Offline
4 posts
since Nov 2008
Nov 19th, 2008
0

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
Reputation Points: 19
Solved Threads: 0
Newbie Poster
00matt is offline Offline
4 posts
since Nov 2008
Nov 19th, 2008
0

Re: Simple Java Help

You can output them:
java Syntax (Toggle Plain Text)
  1. System.out.println(application2.setHours(input.nextInt()));
or check, if the setting passed correctly
java Syntax (Toggle Plain Text)
  1. if(application2.setHours(input.nextInt())))
  2. {//all good}
  3. else {//something wrong}
Also you can save that value to variable:
java Syntax (Toggle Plain Text)
  1. Boolean settingStatus = application2.setHours(input.nextInt()));
Last edited by Antenka; Nov 19th, 2008 at 4:28 pm.
Reputation Points: 293
Solved Threads: 82
Posting Whiz
Antenka is offline Offline
361 posts
since Nov 2008
Nov 19th, 2008
1

Re: Simple Java Help

Thanks everyone, I have it sorted now

Really appreciated all your help,

Matt
Reputation Points: 19
Solved Threads: 0
Newbie Poster
00matt is offline Offline
4 posts
since Nov 2008
Nov 19th, 2008
0

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
Featured Poster
Reputation Points: 533
Solved Threads: 53
Senior Poster
jasimp is offline Offline
3,593 posts
since Aug 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Is there an API I can use to program a flow diagram?
Next Thread in Java Forum Timeline: Time Calculator





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC