Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Aug 11th, 2005, 2:15 PM   #1
ChronicCode
Newbie
 
Join Date: Aug 2005
Posts: 3
Rep Power: 0 ChronicCode is on a distinguished road
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
ChronicCode is offline   Reply With Quote
Old Aug 11th, 2005, 3:00 PM   #2
skuinders
Hobbyist Programmer
 
skuinders's Avatar
 
Join Date: Jun 2005
Location: MA, US
Posts: 204
Rep Power: 4 skuinders is on a distinguished road
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 3:17 PM.
skuinders is offline   Reply With Quote
Old Aug 11th, 2005, 3:22 PM   #3
ChronicCode
Newbie
 
Join Date: Aug 2005
Posts: 3
Rep Power: 0 ChronicCode is on a distinguished road
Ah thanks. Didnt see the bracket I was missing. Sorry i didnt use the CODE tags.
ChronicCode is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 12:55 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC