| | |
Simple Java Help
Thread Solved |
•
•
Join Date: Nov 2008
Posts: 4
Reputation:
Solved Threads: 0
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
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.
2. Then while setting the hours creating 2 more clocks.
and
Here is your problem: you setting hours to the clock-2 and getting it (hours) from clock-3.
1. You creating new "clock" and setting it to 0.
java Syntax (Toggle Plain Text)
Time application = new Time(); application.TimeTest(0, 0, 0);
java Syntax (Toggle Plain Text)
Time application2 = new Time(); application2.setHours(input.nextInt());
java Syntax (Toggle Plain Text)
Time app = new Time(); 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
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
Every time you do:
So create only ONE object (do this once:
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:
Now if your teacher wants the set methods to return boolean, then:
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)
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:
Java Syntax (Toggle Plain Text)
public boolean setSeconds(int seconds) { if (seconds<0) { return false; } this.seconds = seconds; return true; }
Check out my New Bike at my Public Profile at the "About Me" tab
•
•
Join Date: Nov 2008
Posts: 4
Reputation:
Solved Threads: 0
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
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
You can output them:
or check, if the setting passed correctly
Also you can save that value to variable:
java Syntax (Toggle Plain Text)
System.out.println(application2.setHours(input.nextInt()));
java Syntax (Toggle Plain Text)
if(application2.setHours(input.nextInt()))) {//all good} else {//something wrong}
java Syntax (Toggle Plain Text)
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
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
![]() |
Similar Threads
- install and run tomcat3 and implement a simple jsp page (JSP)
- Building a simple Java web crawler (Java)
- Java Paint (Java)
- java price is right game help (Java)
- Error message when running simple Java pgm (Java)
- Help with simple Java IO program running under cygwin (Java)
Other Threads in the Java Forum
- Previous Thread: Is there an API I can use to program a flow diagram?
- Next Thread: Time Calculator
| Thread Tools | Search this Thread |
actuate add android api applet application applications array arrays automation balls bank binary bluetooth business chat class clear client code codesnippet collections component database defaultmethod development dice digit dragging ebook eclipse equation error event formatingtextintooltipjava fractal functiontesting game givemetehcodez graphics gui health hql html hyper ide idea image infinite int integer invokingapacheantprogrammatically j2me java javame javaprojects jni jpanel julia linux list main map method methods mobile myregfun mysql netbeans nonstatic openjavafx parameter pearl php problem program project recursion repositories scanner scrollbar server set sms sort sorting spamblocker sql sqlserver state storm string sun superclass swing swt thread threads tree windows







