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