| | |
Clock.java
![]() |
•
•
Join Date: Dec 2008
Posts: 1
Reputation:
Solved Threads: 0
Hello, first time poster here. So if I come off as asking something so hastily and quickly my bad, but currently I'm creating a clock class on DrJava. This is what I have so far.
I have fullfilled most of the requirements that my profressor has asked except for a few.
1-A way to increment the time, so that if seconds/minutes goes >= 60 they become minute/hour.
2-Things displayed in the main Method:
A:Create an array of objects called timeArray of type Time with 3 elements.
B: Store the Time objects t1, t2, t3 in the array timeArray
C: Display the time of all the objects in the array timeArray.
Just incase of anymore questions, I have attached the file of the requirements of the clock class. So any tips/help would be helpful. Hope it dosen't sound like alot.
Java Syntax (Toggle Plain Text)
clock.java import java.util.*; import java.text.*; public class Clock {private int hours; private int minutes; private int seconds; int minutesleft, secondsleft; //Constructor //item a Clock() { hours=0; minutes=0; seconds=0; } //item b Clock(int h, int m, int s) {hours=h; minutes=m; seconds=s; } //Accessors //item c public void setTime(int h, int m, int s) { hours=h; minutes=m; seconds=s; } //item d public int getHours() {return hours; } //item e public int getMinutes() {return minutes; } //item f public int getSeconds() {return seconds; } //item g public void setHours(int h) {hours=h; } //Modifiers //item h public void setMinutes(int m) {minutes=m; } //item i public void setSeconds(int s) {seconds=s; } //item j public void printTime() { System.out.println(hours+ "H:" + minutes + "M:" + seconds + "S"); } //item k public void inputTime() {Scanner input=new Scanner(System.in); System.out.println("Enter hours: "); hours=input.nextInt(); System.out.println("Enter minutes: "); minutes=input.nextInt(); System.out.println("Enter seconds: "); seconds=input.nextInt(); } //item l public String toString() {String s= hours+ "H:" + minutes + "M:" + seconds + "S"; return s; } //item m public void incrementSeconds(int seconds) {this.seconds=this.seconds+seconds; } //item n public void incrementMinutes(int minutes) {this.minutes=this.minutes+ minutes; } //item o public void incrementHours(int hours) {this.hours=this.hours+hours; } } // this is the end of class Clock, put all new methods before this MAIN METHOD: import java.util.*; import java.text.*; public class lab80 {public static void main(String[] args) { //item a Clock t1=new Clock(); //item b Clock t2=new Clock(); //item c Clock t3=new Clock(10, 30, 15); //item d t1.inputTime(); //item e t2.setTime(20,12, 45); //item f System.out.println("t1 hours:"+ t1.getHours()); //item g System.out.println("t2 minutes:" + t2.getMinutes()); //item h System.out.println("t3 seconds:" + t3.getSeconds()); //item i t1.setHours(5); //item j t2.setMinutes(13); //item k t3.setSeconds(55); //item l t1.printTime(); //item m System.out.println(t2); //item n t1.incrementHours(5); //item o t1.incrementMinutes(10); //item p t1.incrementSeconds(2); //item q t1.printTime(); }//end main }//end class
I have fullfilled most of the requirements that my profressor has asked except for a few.
1-A way to increment the time, so that if seconds/minutes goes >= 60 they become minute/hour.
2-Things displayed in the main Method:
A:Create an array of objects called timeArray of type Time with 3 elements.
B: Store the Time objects t1, t2, t3 in the array timeArray
C: Display the time of all the objects in the array timeArray.
Just incase of anymore questions, I have attached the file of the requirements of the clock class. So any tips/help would be helpful. Hope it dosen't sound like alot.
•
•
Join Date: Jan 2008
Posts: 3,810
Reputation:
Solved Threads: 501
You should probably have your
Regarding going from 0 to 59 and 0 to 23 and not beyond, in your increment functions, use the / and % operators. Add the new amount to the old amount as you do, but then divide by 60 and take the mod of 60. For hours, it would be 24.
Do you have to worry about negative numbers at all? Not sure what the question is regarding the array. Do you not know how to declare an array? Do you not know how to write and read from it?
printTime () function call the toString () function. It's the same code. No sense writing it twice.Regarding going from 0 to 59 and 0 to 23 and not beyond, in your increment functions, use the / and % operators. Add the new amount to the old amount as you do, but then divide by 60 and take the mod of 60. For hours, it would be 24.
Java Syntax (Toggle Plain Text)
public void incrementSeconds(int seconds) { this.seconds=this.seconds+seconds; int minutestoIncrement = this.seconds / 60; if (minutesToIncrement != 0) incrementMinutes (minutesToIncrement); this.seconds = this.seconds % 60; }
Do you have to worry about negative numbers at all? Not sure what the question is regarding the array. Do you not know how to declare an array? Do you not know how to write and read from it?
![]() |
Similar Threads
- java constructor..! (Java)
- Java tutor needed BADLY (Java)
- cpu scheduler in java problem (Java)
- java in mandrake linux 10.0 help (Java)
- Favorite Java textbook (Java)
- Random Java Info (Java)
- How to do constructors (Java)
Other Threads in the Java Forum
- Previous Thread: Recursion help
- Next Thread: Need help with max/min'ing multiply
| Thread Tools | Search this Thread |
911 addball addressbook android api append applet application apps array arrays automation binary bluetooth businessintelligence button card class client code collision component crashcourse css csv database eclipse ee error fractal free ftp game gis givemetehcodez graphics gui html ide image integer integration j2me japplet java javaarraylist javadoc javafx javaprojects jni jpanel julia jvm linked linux list loan machine map method methods migrate mobile netbeans objects oriented output phone physics printf problem program programming project projects radio recursion replaydirector reporting researchinmotion rotatetext scanner se server service set sms software sort sql string swing test textfield threads tree trolltech ubuntu utility windows






