Problem Description: Multi automobile movement simulation in a multi lane road
Consider a road (street) consisting of NL lanes with NC cars moving in one direction. Each car
has to move in a certain lane with possibility of changing lanes. Each lane is divided into NP
positions. Cars movement is simulated using time intervals. Each car (car i) has a specific speed
(si) for moving up to si positions per unit time. Figure 1 shows a simple 3 lanes and 25 positions
road with 7 cars.
Initial road condition
The initial condition may be simulated using one of the following methods. Your program must
prompt the user to which method to be used.
a. Read data from input file using the following format:
First line: nl nc np // 3 integers: number of lanes, number of cars,
// and number of positions per lane
Following nc lines: sp lane lpos // each line has 3 integers for each car:
// speed of the car, the lane in which the car is
// is moving and the position of the car in the lane
b. Generate data randomly. All of the above variables: nl, nc, np, sp, lane, and lpos are
generated using random number generator.
Number of lanes: nl  [2, 6]
Number of cars: nc  [2, nl*5]
Number of positions: np  [20, nc*10]
Car lane: lane  [1, nl]
Car speed: sp  [1, 5]
Car lane position: lpos  [1, np] // must not use same position for different cars

this is what i did so far i did 3 spirited files and i'm going to call them at the main one
//first one :

public class CarMovement 
{
 	private int id;
 	private int speed;
 	private int lane;
 	private int lpos;
 	public CarMovement(int id, int speed , int lane , int lpos)
 	{
 		this.id=id;
 		this.speed=speed;
 		this.lane=lane;
 		this.lpos=lpos;
 	}
 	public void setId(int id)
 	{
 	  this.id=id;
 	}
 	public void setSpeed(int speed)
 	{
 	  this.speed=speed;	
 	}
 	public void setLane(int lane)
 	{
 	  this.lane=lane;
 	}
 	public void setLpos(int lpos)
 	{
 	  this.lpos=lpos;
 	}
 	public int getId()
 	{
 		return id;
 	}
 	public int getSpeed()
 	{
 		return speed;
 	}
 	public int getLane()
 	{
 		return lane;
 	}
 	public int getLpos()
 	{
 		return lpos;
 	}
 	
}
 
//second one for the randomly choice:
public class CarRan
{
	public static void main(String args[])
	{
		Random r1=new Random();
		
		int nl=2+r1.nextInt(5);
		System.out.printf(" The number Of lanes : %d ",nl);
		System.out.println();
		int nc=2+r1.nextInt(nl*5);
		System.out.printf(" The Number Of Cars is : %d ",nc);
		System.out.println();
		int np=20+r1.nextInt(nc*10);
		System.out.printf(" Then Number Of positions : %d ",np);
		System.out.println();
		int lane=1+r1.nextInt(nl);
		System.out.printf(" The Number Of Lane : %d ",lane);
		System.out.println();
		int sp=1+r1.nextInt(5);
		System.out.printf(" The Speed of the cars : %d ",sp);
		System.out.println();
		int lpos=1+r1.nextInt(np);
		System.out.printf(" The Lane in which the car is moving : %d ",lpos);
		
	}
		   
	
}


//third  the main method
import java.util.Scanner; // Program uses scanner
import java.util.Random;  // program uses random
public class CarTest
{
	public static void main(String args[])// main method begins prgram execution
 	{
 		Scanner input = new Scanner (System.in); //create scanner to obtain input from command window
 		// initialize value
 		int choice=0;
 		int nl=10;
 	    int nc=10; 
 	    int nlpos=100;
 	    int sp;
 	    int lane[][]=new int[nl][nlpos];
 	    CarMovement Cars[]=new CarMovement() ;
 	    CarMovement ob1 = new CarMovement(nl,nc,np,sp);
 	    CarRan ob2 = new CarRan();
 		while(choice!=3) // while loop
 		{
 				//prombt user to enter
 		        System.out.println("Choose Your Option From The Menu : ");
 		        System.out.println(" 1- Enter Your Own Movement. ");
 		        System.out.println(" 2- Randomly Movement. ");
 		        System.out.println(" 3- Exit The Program. ");
 		        System.out.println("Enter Your Choice:");
 		        choice=input.nextInt(); // read the input from the user
 		        //pick the ption based on the users choice
 		        switch(choice)
 		        {
 		        	case 1:
 		        		
 		        		ob1.setNl(nl);
 		        		ob1.setNc(nc);
 		        		ob1.setNp(np);
 		        		ob1.setSp(sp);
 		        		
 		        		break;
 		        	
 		        	case 2:
 		        		
 		        		ob2.CarRan();
 		        		
 		        		break;
 		        	
 		        			
 		        	case 3:
 		        		
 		        		break;
 		        		
 		        	default:
 		        		
 		        		System.out.print(" Invalid Choice ");
 		        		
 		        		break;	
 		        }
 		}
 		public int Cars (int nc)
 		{
 			for(int i=0 ; i<nc.length ; i++)
 		}
 	}
}

my question is how can i make the creation of array in the random method and how can i call it also
i'll have 2 use loop but i'm not sure how 2 do it so if someone could help.

Recommended Answers

All 3 Replies

I guess you can use vector.

thnx but i mint what can i do with the random class ?i'm a beginner btw ..

thnx but i mint what can i do with the random class ?i'm a beginner btw ..

You need a program redesign from the ground up, I think. You have multiple main classes. You have a call to a CarRan constructor that does not exist:

CarRan ob2 = new CarRan();

and worse, a call to a CarRan method for a CarRan object.

ob2.CarRan();

A function within class CarRan that is called CarRan should be a constructor. It should be called to create a NEW object, not a call to a function from an object that already exists (obj2). And again, there IS no CarRan constructor. There are no CarRan data members and there are no non-static CarRan methods, so creating a CarRan object seems pointless anyway.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.