NTNUJAVA Virtual Physics Laboratory
Enjoy the fun of physics with simulations!
Backup site http://enjoy.phy.ntnu.edu.tw/ntnujava/

Information about this web site => Request for physics Simulations => Topic started by: null404 on January 03, 2008, 09:10:15 pm



Title: Traffic Light Simulation (Java)
Post by: null404 on January 03, 2008, 09:10:15 pm

Hi, Everyone, I'm new in this business, in Java Programming.
I have to do a small program about traffic light simulation, whit this characteristics, check the web link:

http://www.google.com/translate?u=http%3A%2F%2Fwww.di.fc.ul.pt%2F%7Etl%2Fcgi-bin%2Fshow-gensec.clisp.cgi%3FDOCNAME%3DLABP%26GENSEC%3D-1-I-2-5%26USER%3Dguest%26PASSWORD%3Dguest&langpair=pt%7Cen&hl=en&ie=UTF8 (http://www.google.com/translate?u=http%3A%2F%2Fwww.di.fc.ul.pt%2F%7Etl%2Fcgi-bin%2Fshow-gensec.clisp.cgi%3FDOCNAME%3DLABP%26GENSEC%3D-1-I-2-5%26USER%3Dguest%26PASSWORD%3Dguest&langpair=pt%7Cen&hl=en&ie=UTF8)

I don't have a clue in how to do this program, if someone could help I would be very appreciated.
 Tank you very much.

Orlando Ribeiro


Title: Re: Traffic Light Simulation (Java)
Post by: Fu-Kwun Hwang on January 04, 2008, 09:47:20 am
I think you need to read the web page you have provided more carefully.
If you have something in double and if it is related to physics, write it done in detail and I will try to help.
You need to sort out what you need to do and make a plan. You need to help yourself first.

For example: I do not know what kind of help you need? You did not even explain it in detail. (you just ask us to read the web page!  ;) ).


Title: Re: Traffic Light Simulation (Java)
Post by: null404 on January 04, 2008, 08:37:43 pm

Hi, Fu-Kwun Hwang I'm sorry for not giving in details my problems regarding this program, i'm new in java programming.

This is a small program that I have to do for College, and my problems are I do not know how to show the cars as alphabet characters, during the simulation.
And how to show the traffic lights whit the "-"(the green light) and "|"(the red light), symbols. And making the cars stops when the light are red, and make them fill the empty spaces until the red light.


Tank You for your help. ;D

Orlando Ribeiro



Title: Re: Traffic Light Simulation (Java)
Post by: Fu-Kwun Hwang on January 04, 2008, 09:30:30 pm
You can use "String" to show the symbol on the screen:define Strings(stored alphabet characters) to represent your car. --> Call java native function to draw string at x,y position.

You also need to define variables for velocity. And change x,y coordinate according to velocity.
If you want to stop a car, apply an acceleration to change the velocity( which will change the position, i.e. make car stop). This is the physics part (High school physics).

You can read java tutorial (from sun's web site) to learn how to create simulation if you want to write code by yourself.
You can also download EJS from our server which will make job easier. (You can modified my traffic-light ejs code to get what you want.


Title: Re: Traffic Light Simulation (Java)
Post by: null404 on January 04, 2008, 09:53:02 pm
Ok, tanks for the help, I will try to create the program and after i done I will post it here :).


Title: Re: Traffic Light Simulation (Java)
Post by: null404 on January 23, 2008, 09:20:49 pm
As promised here is the code that I manage to create, not bad for a beginner.
Code:
package semaforo;

import java.lang.String;
import java.util.Random;
import java.util.Scanner;


public class Semaforo {

/**
* @param args
*/

public static void main(String[] args) {
int m = 0; // Duration of the simulation in seconds (m)
int n = 0; // Lights in the period of the second (n)
int d = 0; //With the period in which the state of the road is shown on the screen (d)
int car = 0; // car variable
char traficlight = '-'; // traffic light variable  state = open

int road = 0;  // road  variable
int simulation;    // simulation variable
int alfacar = 65;  // car showed in alphabetic letters ( código inserido posteriormente)
String history;     // string history    ( código inserido posteriormente)



Random gencar = new Random();
Scanner scan = new Scanner(System.in);

System.out.println("Introduza valor para m:"); // include the value of m
simulation  = scan.nextInt();
System.out.println("Introduza valor para n:"); // include the value of n
n  = scan.nextInt();
System.out.println("Introduza valor para d"); // include the value of d
d  = scan.nextInt();
history = new String("....");



while(m < simulation){

if(road == n){
road = 0;
traficlight = traficlight == '-' ? '|' : '-';
}
road++;


char[] vetchistory = new char[history.length()]; 
vetchistory = history.toCharArray();
for(int i = history.length()-2; i >= 0;i--){
if(traficlight == '-' || (traficlight == '|' && vetchistory[i+1] == '.')){
vetchistory[i+1] = vetchistory[i];
vetchistory[i] = '.';
}
}
System.out.println(car + ":" + m + ": " + history.toString() + traficlight);  // this part runs the cars and show them as Alpha Characters
if(car == 0 && vetchistory[0] == '.'){
vetchistory[0] = (char)alfacar;
history = String.copyValueOf(vetchistory);
if(alfacar == 90) alfacar = 65;
else alfacar++;
}else if(car == 0){
history = String.copyValueOf(vetchistory);
history = (char)alfacar + history.toString();
if(alfacar == 90) alfacar = 65;
else alfacar++;
}else{
history = String.copyValueOf(vetchistory);
history = "." + history.toString();
}
if(car == 0 || car == -1){
car = (int) Math.floor(Math.random()*d) + 1;
}
car--;
m++;
}



}

}


Title: Re: Traffic Light Simulation (Java)
Post by: heightgrowthshoes.com on January 31, 2008, 06:01:29 pm
Hi, Everyone, I'm new in this business, in Java Programming.
I have to do a small program about traffic light simulation, whit this characteristics, check the web link:

http://www.google.com/translate?u=http%3A%2F%2Fwww.di.fc.ul.pt%2F%7Etl%2Fcgi-bin%2Fshow-gensec.clisp.cgi%3FDOCNAME%3DLABP%26GENSEC%3D-1-I-2-5%26USER%3Dguest%26PASSWORD%3Dguest&langpair=pt%7Cen&hl=en&ie=UTF8 (http://www.google.com/translate?u=http%3A%2F%2Fwww.di.fc.ul.pt%2F%7Etl%2Fcgi-bin%2Fshow-gensec.clisp.cgi%3FDOCNAME%3DLABP%26GENSEC%3D-1-I-2-5%26USER%3Dguest%26PASSWORD%3Dguest&langpair=pt%7Cen&hl=en&ie=UTF8)

I don't have a clue in how to do this program, if someone could help I would be very appreciated.
 Tank you very much.

Orlando Ribeiro
great day to you all


Title: Re: Traffic Light Simulation (Java)
Post by: Fu-Kwun Hwang on January 31, 2008, 09:29:49 pm
The above web page already provide enough information about the requirement for the simulation.
You said: You do not have a clue in how to do this program.

If you do not provide more information, I do not know how to help you. And I can not do it for you.
You need to be more specific: e.g. Do you know how to print string? Do you know how to generate random number? ...
If you indicated which part you do not know how to do. May be I can try to help you.

 


Title: Re: Traffic Light Simulation (Java)
Post by: balsam on April 08, 2008, 06:05:28 pm
hi every one ..
I'm new user and i want a help .i need to write a traffic light program either in c++ or visual basic i have an algorithm but i couldn't write a program please help me..


Title: Re: Traffic Light Simulation (Java)
Post by: Fu-Kwun Hwang on April 22, 2008, 08:59:13 am
Please write download your algorithm if you want others to help you!