RSS Forums RSS

Simple Java Help

Please support our Java advertiser: Programming Forums
Thread Solved
Reply
Posts: 4
Reputation: 00matt is an unknown quantity at this point 
Solved Threads: 0
00matt 00matt is offline Offline
Newbie Poster

Simple Java Help

  #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 Images
File Type: pdf lab7.pdf (59.2 KB, 6 views)
Attached Files
File Type: java Time.java (923 Bytes, 7 views)
File Type: java TimeTest.java (1.4 KB, 6 views)
AddThis Social Bookmark Button
Reply With Quote  
Posts: 173
Reputation: Antenka will become famous soon enough Antenka will become famous soon enough 
Solved Threads: 38
Antenka's Avatar
Antenka Antenka is offline Offline
Junior Poster

Re: Simple Java Help

  #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  
Posts: 1,158
Reputation: javaAddict is just really nice javaAddict is just really nice javaAddict is just really nice javaAddict is just really nice 
Solved Threads: 163
javaAddict's Avatar
javaAddict javaAddict is offline Offline
Veteran Poster

Re: Simple Java Help

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

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;
}
There are 3 ways to do things:
The right way, the wrong way,
And The Object Oriented Way.

When you do things your way, make sure you don't forget the Object Oriented Way
Reply With Quote  
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

  #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  
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

  #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  
Posts: 173
Reputation: Antenka will become famous soon enough Antenka will become famous soon enough 
Solved Threads: 38
Antenka's Avatar
Antenka Antenka is offline Offline
Junior Poster

Re: Simple Java Help

  #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 3: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  
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

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

Really appreciated all your help,

Matt
Reply With Quote  
Posts: 3,508
Reputation: jasimp has a spectacular aura about jasimp has a spectacular aura about 
Solved Threads: 45
Featured Poster
jasimp's Avatar
jasimp jasimp is offline Offline
Senior Poster

Re: Simple Java Help

  #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  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.



Views: 395 | Replies: 7 | Currently Viewing: 1 (0 members and 1 guests)

 

Thread Tools Display Modes
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 2:10 pm.
Newsletter Archive - Sitemap - Privacy Statement - Acceptable Use Policy - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC