Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

I've got the following code for sim

Status
Not open for further replies.

sherilu23

Technical User
Jun 24, 2000
7
CA
I've got the following code for simulation:

class Device
{
char status;//requesting, held, releasing idle
int time, idleTime, waitingTime;
int requestEnds, heldEnds;

//public Device(int myDevice)
//{ this(myDevice, 0); }

public Device(int theTime, char theStatus)
{
time = theTime;
status = theStatus;
//waitingTime = 0;
}
public void next()
{
switch (status)
{

case 'R':
requestEnds = time +1;
if (time==requestEnds)
{
status = 'H';
System.out.println("got the bus");
}break;
case 'H':
heldEnds = (int) (500 * Math.random() + 1);
if (time==heldEnds)
{
status = 'R';
System.out.println("bus realesed");
}break;
case 'L':
status = 'I';
System.out.println("device is idle");
break;
case 'I':
idleTime = (int) (10000 * Math.random() + 1);
System.out.println("Idle time is: " + idleTime);
if (time==idleTime)
{
status = 'R';
System.out.println("device is requesting");
} break;
}
}
public void grant()
{
status = 'H';
}
}

public class Bus
{
char status; //granting, holding
double grantEnds, holdingEnds;
public Bus()
{

status = 'G';
grantEnds = 0;
}
public void next(Device d)
{
switch (status)
{ case 'G': //granting

if(time==grantEnds)
{ System.out.println("Holding device");
status = 'H';
holdingEnds = (int) (500 * Math.random() + 1);
System.out.println("Holding ends at: " + holdingEnds);
}
break;
case 'H':
if(time==holdingEnds)
{ System.out.println("Grant next device");
d.grant();
status = 'G';
grantEnds = time + 1;
System.out.println("Grant ends at: " + grantEnds);
}
break;
}
}



//public static final int devicesNo = 20;
public int time;
//Device[] d;
//Bus bus;
public void main(String[] args)
{
Device[] d = new Device[20];
Bus bus = new Bus();
for(time=0; time<=20; time++)
{
for(int i=0; i<=20; i++)
{
d = new Device(time, 'R');
bus.next(d);
d.next();
}
}
}

}

and this is giving me the following error:
Exception in thread &quot;main&quot; java.lang.NoSuchMethodError: main
I hope someone could tell me why.
Thanks in advance.
 
Try changing this declaration:

Device[] d = new Device[20];

to:

Device d;

Device[] d doesn't make sense and neither does Device[20] (did this get munged by Tek-Tips?). You initialize d correctly in your for loop and you don't need to new d when you declare it.

HTH,



Russ
bobbitts@hotmail.com
 
Russ, The statement [tt]Device[] d = new Device[20];[/tt] creates an array called 'd' consisting of 20 devices.

Sherilu23, There are some errors here in the loop as you refer to 'd'. You should refer to the individual device i.e. [tt]d[/tt]

[tt]
d = new Device(time,'R');
bus.next(d);
d.next();
[/tt]

The problem you reported may be something else as well.... My Home -->
 
Place a try catch block around each of the methods catching for exceptions. Then if you catch the exception print a stack trace. This should give you the exact line number. Like this:

try
{
.....
....
...
}
catch(Exception e)
{
e.printStackTrace();
}

Note catch-all error blocks should not be left in code.

HTH


Cal


 
Thanks for all your replies.
I got it to compile, and run at some point.
Then it would say stack overflow.
Calahans: the e.printSTackTrace(), is it a method I have to define, or is it available in Java already?
Also, I'm thinking of using event timer, what do you think?
Thanks in advance
 
Sherilu23,
[tt]catch(Exception e)[/tt] states that e is an instance of a java.lang.Exception.

[tt]printStackTrace()[/tt] is one of the methods in the Exception class, so when you call [tt]e.printStackTrace()[/tt] you are running the method defined in the Exception class. My Home -->
 
&quot;Also, I'm thinking of using event timer, what do you think?&quot;
Event timers are really cool, i used them in a simple game to control refresh rates. The best one is the javax.swing.timer class here is an example....


later


ackka
tmoses@iname.com
&quot;Do No Harm, Leave No Tracks&quot;

ICMP Summer 2000, 2600 Article
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top