Simple Java Help

Thread Solved
Reply

Join Date: Nov 2008
Posts: 4
Reputation: 00matt is an unknown quantity at this point 
Solved Threads: 0
00matt 00matt is offline Offline
Newbie Poster

Simple Java Help

 
0
  #1
Nov 19th, 2008
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, 6 views)
File Type: java Time.java (923 Bytes, 7 views)
File Type: java TimeTest.java (1.4 KB, 6 views)
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 249
Reputation: Antenka has a spectacular aura about Antenka has a spectacular aura about Antenka has a spectacular aura about 
Solved Threads: 65
Antenka's Avatar
Antenka Antenka is offline Offline
Posting Whiz in Training

Re: Simple Java Help

 
0
  #2
Nov 19th, 2008
Correct me, if I'm wrong:
1. You creating new "clock" and setting it to 0.
  1. Time application = new Time();
  2. application.TimeTest(0, 0, 0);
2. Then while setting the hours creating 2 more clocks.
  1. Time application2 = new Time(); application2.setHours(input.nextInt());
and
  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.
So what if you can see the darkest side of me?
No one would ever change this animal I have become
Help me believe it's not the real me
Somebody help me tame this animal
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 1,525
Reputation: javaAddict is a glorious beacon of light javaAddict is a glorious beacon of light javaAddict is a glorious beacon of light javaAddict is a glorious beacon of light javaAddict is a glorious beacon of light javaAddict is a glorious beacon of light 
Solved Threads: 209
Featured Poster
javaAddict's Avatar
javaAddict javaAddict is offline Offline
Posting Virtuoso

Re: Simple Java Help

 
0
  #3
Nov 19th, 2008
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:

  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:

  1. public boolean setSeconds(int seconds) {
  2. if (seconds<0) {
  3. return false;
  4. }
  5. this.seconds = seconds;
  6. return true;
  7. }
Check out my New Bike at my Public Profile at the "About Me" tab
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 4
Reputation: 00matt is an unknown quantity at this point 
Solved Threads: 0
00matt 00matt is offline Offline
Newbie Poster

Re: Simple Java Help

 
0
  #4
Nov 19th, 2008
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, 2 views)
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 4
Reputation: 00matt is an unknown quantity at this point 
Solved Threads: 0
00matt 00matt is offline Offline
Newbie Poster

Re: Simple Java Help

 
0
  #5
Nov 19th, 2008
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
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 249
Reputation: Antenka has a spectacular aura about Antenka has a spectacular aura about Antenka has a spectacular aura about 
Solved Threads: 65
Antenka's Avatar
Antenka Antenka is offline Offline
Posting Whiz in Training

Re: Simple Java Help

 
0
  #6
Nov 19th, 2008
You can output them:
  1. System.out.println(application2.setHours(input.nextInt()));
or check, if the setting passed correctly
  1. if(application2.setHours(input.nextInt())))
  2. {//all good}
  3. else {//something wrong}
Also you can save that value to variable:
  1. Boolean settingStatus = application2.setHours(input.nextInt()));
Last edited by Antenka; Nov 19th, 2008 at 4:28 pm.
So what if you can see the darkest side of me?
No one would ever change this animal I have become
Help me believe it's not the real me
Somebody help me tame this animal
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 4
Reputation: 00matt is an unknown quantity at this point 
Solved Threads: 0
00matt 00matt is offline Offline
Newbie Poster

Re: Simple Java Help

 
1
  #7
Nov 19th, 2008
Thanks everyone, I have it sorted now

Really appreciated all your help,

Matt
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 3,562
Reputation: jasimp has a spectacular aura about jasimp has a spectacular aura about jasimp has a spectacular aura about 
Solved Threads: 50
Featured Poster
jasimp's Avatar
jasimp jasimp is offline Offline
Senior Poster

Re: Simple Java Help

 
0
  #8
Nov 19th, 2008
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
"Argyou not with the hand you are dealt in cards or life." ---- Wizard and Glass
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC