![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
|
|
#1 |
|
Newbie
Join Date: Aug 2005
Posts: 3
Rep Power: 0
![]() |
Time
I have two help requests.
I just started doing some java, and I stumbled apon these two errors that i cannot seem to fix: 1. public class TimeDemo { public static void main (String[] arg) { Time t1 = new Time(); //creates a new time object Time t2 = new Time(); //creates a new time object int a=17, b=8, c=20; t1.set(a, b, c); //put the time 17:08:20 in t1 t2.set(23, 59, 59); //put the time 23:59:59 in t2 t1.tick(); //increment t1 by one second t2.tick(); //increment t2 by one second t2.tick(); //increment t2 by one second String s1 = t1.toString(); //s1 gets the value "17:8:21" String s2 = t2.toString(); //s2 gets the value "0:0:1" System.out.println(s1); //print s1 System.out.println(s2); //print s2 } } This one is written exact in the book, and it does not recognize the "Time" variable. 2. public class Time { //instance variables: private int h, m, s; private boolean showSec = true; //methods: public void set (int hour, int min, int sec) { //check that the time is OK: if (hour>=0 && hour<24 && min>=0 && min<60 && sec>=0 && sec<60){ h=hour; m=min; s=second; } else System.out.println("Illegal Time"); } public void setShowSec(boolean show) { showSec = show; } public int getHour() { return h; } public int getMin() { return m; } public int getSec() { return s; } public void tick() { // moves the time forwars one second: s = s+1; if (s == 60) { m = 0; h = h+1; } if (m == 60) { m = 0; h = h+1; } if (h == 24) { h = 0; } public String toString () { //returns "hh:mm:ss" String t = h + ":" + m; // or "hh:mm" if (showSec) t = t + ":" + s; return t; } This one gives me the error: "Illegal start of operation" Line 41 and "';' is expected" Line 46 |
|
|
|
|
|
#2 |
|
Hobbyist Programmer
Join Date: Jun 2005
Location: MA, US
Posts: 204
Rep Power: 4
![]() |
1. Use CODE tags when posting source code here.
2. Time is not a variable, it is a class. You are trying to make an instance of that class (or create an object) with new Time() 3. You are missing the closing brackets for the tick() method and the Time class 4. In the set() method, change s=second to s=sec
__________________
"A stupid man's report of what a clever man says can never be accurate, because he unconciously translates what he hears into something he can understand." - B. Russell http://web.bryant.edu/~srk2 Last edited by skuinders; Aug 11th, 2005 at 2:17 PM. |
|
|
|
|
|
#3 |
|
Newbie
Join Date: Aug 2005
Posts: 3
Rep Power: 0
![]() |
Ah thanks. Didnt see the bracket I was missing. Sorry i didnt use the CODE tags.
![]() |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|