NTNUJAVA Virtual Physics Laboratory
Enjoy the fun of physics with simulations!
Backup site http://enjoy.phy.ntnu.edu.tw/ntnujava/
December 12, 2017, 03:38:26 pm *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
 
   Home   Help Search Login Register  
Wisdom is to teach our students how to teach themselves. ...Wisdom
Google Bookmarks Yahoo My Web MSN Live Netscape Del.icio.us FURL Stumble Upon Delirious Ask FaceBook

Pages: [1]   Go Down
  Print  
Author Topic: Traffic Light Simulation (Java)  (Read 34917 times)
0 Members and 1 Guest are viewing this topic. Click to toggle author information(expand message area).
null404
Newbie
*
Offline Offline

Posts: 4

: 1 users think this message is good
1 Traffic Light Simulation (Java)
«
Embed this message
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

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
Logged
Fu-Kwun Hwang
Administrator
Hero Member
*****
Offline Offline

Posts: 3080



WWW
«
Embed this message
Reply #1 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!  Wink ).
Logged
null404
Newbie
*
Offline Offline

Posts: 4

«
Embed this message
Reply #2 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. Grin

Orlando Ribeiro

Logged
Fu-Kwun Hwang
Administrator
Hero Member
*****
Offline Offline

Posts: 3080



WWW
«
Embed this message
Reply #3 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.
Logged
null404
Newbie
*
Offline Offline

Posts: 4

«
Embed this message
Reply #4 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 Smiley.
Logged
null404
Newbie
*
Offline Offline

Posts: 4

«
Embed this message
Reply #5 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++;
}



}

}
Logged
heightgrowthshoes.com
Newbie
*
Offline Offline

Posts: 1

«
Embed this message
Reply #6 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

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
Logged
Fu-Kwun Hwang
Administrator
Hero Member
*****
Offline Offline

Posts: 3080



WWW
«
Embed this message
Reply #7 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.

 
Logged
balsam
Newbie
*
Offline Offline

Posts: 1

«
Embed this message
Reply #8 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..
Logged
Fu-Kwun Hwang
Administrator
Hero Member
*****
Offline Offline

Posts: 3080



WWW
«
Embed this message
Reply #9 on: April 22, 2008, 08:59:13 am »

Please write download your algorithm if you want others to help you!
Logged
Pages: [1]   Go Up
  Print  
Wisdom is to teach our students how to teach themselves. ...Wisdom
 
Jump to:  


Related Topics
Subject Started by Replies Views Last post
Traffic Light System « 1 2 ... 6 7 »
kinematics
Fu-Kwun Hwang 201 582675 Last post September 30, 2016, 12:34:30 pm
by Charli05
Time duration for the yellow traffic light
Kinematics
Fu-Kwun Hwang 4 38961 Last post August 16, 2009, 07:38:51 am
by Fu-Kwun Hwang
traffic light whole system by using matlab
Request for physics Simulations
bennycyg 11 21821 Last post September 30, 2016, 12:46:41 pm
by Charli05
Mesoscopic traffic light simulation
Simulations from other web sites
Ifeanyi 2 13414 Last post December 09, 2011, 07:14:35 am
by Ifeanyi
solar power traffic light replace the signals on wires
kinematics
louse 0 2059 Last post June 04, 2013, 12:50:21 pm
by louse
Powered by MySQL Powered by PHP Powered by SMF 1.1.13 | SMF © 2006-2011, Simple Machines LLC Valid XHTML 1.0! Valid CSS!
Page created in 0.16 seconds with 22 queries.since 2011/06/15