Hi.
I'm new to java, and am trying a few easy projects; but I just cannot get this code to work.
I am trying to create an instance of an athlete (Racer), assign him a name through the constructor, then create two instances of Time (which is a seperate class), and assign these instances of time to the instnce of Racer as 'start' and 'finish' times.
The odd thing is, I can do this fine inside main, but when I try to call a method from the instance of Race to do all this assigning (as a good java practice) it says "cannot resolve symbol r1' which is my Race instance.
Why does it let me do it in the main method, but not anywhere else????????
Please, if someone can help, I'd be VERY grateful!!
-----------------------Time.java ------------------------
/**
*@author: Joe Wardell
*@Worksheet: 4
*@Date: 21/10/02
*/
public class Time
{ //declare attributes:
private int timeInSecs;
//constructor; used to initialise the values of Time needed
public Time(int hours, int minutes, int secs)
{
timeInSecs = (hours * 3600) + (minutes * 60) + (secs);
}
//declare the GET methods:
public int getHoursElement()
{
return timeInSecs / 3600;
}
//------------------------------
public int getMinutesElement()
{
return (timeInSecs % 3600) / 60;
}
//-------------------------------------------
public int getSecsElement()
{
return timeInSecs % 60;
}
//--------------------------------------------------------
//method to manipulate the main attribute...
//timeInSecs
public void increment(int hours, int minutes, int secs)
{
timeInSecs = timeInSecs + (hours * 3600) + (minutes * 60) + (secs);
}
public String toString()
{
return "Time is:\n" +
"\t" + getHoursElement() + " hours\n" +
"\t" + getMinutesElement() + " minutes\n" +
"\t" + getSecsElement() + " secs\n";
}
public static void main(String[] args)
{
Time t1 = new Time(10, 20, 30);
t1.increment(1, 10, 50);
System.out.println(t1);
}
}//end of class Time
-----------------------------------------------------------
-------------------------Race.java-------------------------
/**
*@author: Joe Wardell
*@Worksheet: 4
*@Date: 21/10/02
*/
public class Race
{
//declare attributes:
public String name;
public Time start;
public Time finish;
/**constructor for Race
*@param theName takes the new instances name
*/
public Race(String theName)
{
name = theName;
}
//declare the SET and GET method for attribute; name
public void setName (String theName)
{
name = theName;
}
//----------------------------------
public String getName()
{
return name;
}
//----------------------------------
public void setStart (Time theStart)
{
start = theStart;
}
//----------------------------------
public Time getStart()
{
return start;
}
//----------------------------------
public void setFinish (Time theFinish)
{
finish = theFinish;
}
//----------------------------------
public Time getFinish()
{
return finish;
}
//----------------------------------
public void raceDemo()
{
//TimeRace t1 = new Time(10, 30, 21);
//TimeRace t2 = new Time(11, 40, 50);
//r1.setStart(t1);
//r1.setFinish(t2);
//System.out.println(r1);
//All this commented area is where the
//code would be, if calling this method worked!
}
public static void main(String[] args)
{
Race r1 = new Race("Joe Wardell"
r1.raceDemo();
Time t1 = new Time(10, 30, 21);
Time t2 = new Time(11, 40, 50);
r1.setStart(t1);
r1.setFinish(t2);
System.out.println(r1);
}
public String toString()
{
return "Racer Name: " + getName() + "\n"
+ getStart();
}
}//end of class Race
-----------------------------------------------------------
Any and ALL suggestions r very welcome!
Cheers!
JoE we are all of us living in the gutter.
But some of us are looking at the stars.
I'm new to java, and am trying a few easy projects; but I just cannot get this code to work.
I am trying to create an instance of an athlete (Racer), assign him a name through the constructor, then create two instances of Time (which is a seperate class), and assign these instances of time to the instnce of Racer as 'start' and 'finish' times.
The odd thing is, I can do this fine inside main, but when I try to call a method from the instance of Race to do all this assigning (as a good java practice) it says "cannot resolve symbol r1' which is my Race instance.
Why does it let me do it in the main method, but not anywhere else????????
Please, if someone can help, I'd be VERY grateful!!
-----------------------Time.java ------------------------
/**
*@author: Joe Wardell
*@Worksheet: 4
*@Date: 21/10/02
*/
public class Time
{ //declare attributes:
private int timeInSecs;
//constructor; used to initialise the values of Time needed
public Time(int hours, int minutes, int secs)
{
timeInSecs = (hours * 3600) + (minutes * 60) + (secs);
}
//declare the GET methods:
public int getHoursElement()
{
return timeInSecs / 3600;
}
//------------------------------
public int getMinutesElement()
{
return (timeInSecs % 3600) / 60;
}
//-------------------------------------------
public int getSecsElement()
{
return timeInSecs % 60;
}
//--------------------------------------------------------
//method to manipulate the main attribute...
//timeInSecs
public void increment(int hours, int minutes, int secs)
{
timeInSecs = timeInSecs + (hours * 3600) + (minutes * 60) + (secs);
}
public String toString()
{
return "Time is:\n" +
"\t" + getHoursElement() + " hours\n" +
"\t" + getMinutesElement() + " minutes\n" +
"\t" + getSecsElement() + " secs\n";
}
public static void main(String[] args)
{
Time t1 = new Time(10, 20, 30);
t1.increment(1, 10, 50);
System.out.println(t1);
}
}//end of class Time
-----------------------------------------------------------
-------------------------Race.java-------------------------
/**
*@author: Joe Wardell
*@Worksheet: 4
*@Date: 21/10/02
*/
public class Race
{
//declare attributes:
public String name;
public Time start;
public Time finish;
/**constructor for Race
*@param theName takes the new instances name
*/
public Race(String theName)
{
name = theName;
}
//declare the SET and GET method for attribute; name
public void setName (String theName)
{
name = theName;
}
//----------------------------------
public String getName()
{
return name;
}
//----------------------------------
public void setStart (Time theStart)
{
start = theStart;
}
//----------------------------------
public Time getStart()
{
return start;
}
//----------------------------------
public void setFinish (Time theFinish)
{
finish = theFinish;
}
//----------------------------------
public Time getFinish()
{
return finish;
}
//----------------------------------
public void raceDemo()
{
//TimeRace t1 = new Time(10, 30, 21);
//TimeRace t2 = new Time(11, 40, 50);
//r1.setStart(t1);
//r1.setFinish(t2);
//System.out.println(r1);
//All this commented area is where the
//code would be, if calling this method worked!
}
public static void main(String[] args)
{
Race r1 = new Race("Joe Wardell"
r1.raceDemo();
Time t1 = new Time(10, 30, 21);
Time t2 = new Time(11, 40, 50);
r1.setStart(t1);
r1.setFinish(t2);
System.out.println(r1);
}
public String toString()
{
return "Racer Name: " + getName() + "\n"
+ getStart();
}
}//end of class Race
-----------------------------------------------------------
Any and ALL suggestions r very welcome!
Cheers!
JoE we are all of us living in the gutter.
But some of us are looking at the stars.