this is a code for my project ,the project is
In the beginning of the program, the users will choose either to create the road by them self by entering the speed of the car, the lane in which the car is moving and the position of the car in the lane or to let the program run randomly.
When the users choose the first choice they will edit the car number of position per lane , the car lane , the number of car and the speed of each car.
But when the users choose the second choice the program will be running randomly and they will get the road fill with a number of cars and each car has it position and speed .
Finally, the program will finishes the execution either it was the first choice or the second when all the cars reach the np position, all the cars must np position as fast as possible , in case one of the cars face other car in her way then this car can change her lane if the adjacent position if the lane is empty.

import java.util.Scanner;
public class Road
{
	// declare array and veriables
	private int road[][];
	private int carsSpeed[];
	private int carsLane[];
	private int carPos[];
	private int endPos;
	private int nCars;
	private int nLanes;
	private int nPos;
	
	public Road(int road1[][], int carsSpeed1[], int carsLane1[], int carPos1[])
	{
		road= road1;
		carsSpeed= carsSpeed1;
		carsLane= carsLane1;
		carPos= carPos1;
	}
	// read input
	public void readInput()
	{
		Scanner input = new Scanner(System.in);
		System.out.println(" Please Enter your Cars Speed;");
		int carsSpeed =input.nextInt();
		System.out.println("Please Enter how many lines you want:");
		int carsLane = input.nextInt();
		System.out.println(" Please Enter your Cars Position:");
		int carPos = input.nextInt();
		
	}
	// fill road with cars
	public void fillCars()
	{
		
		for(int cari=0; cari<=nCars; ++cari)
		{
			int cariLane = carsLane[cari];
		    int cariPos = carPos[cari];
			road[cariLane][cariPos]=cari;
		}
		 
	}
	//simulate car movement
	public void simulate()
	{
		boolean done = false;
		while (!done) //while loop
		{
			done= moveCars();
		}
	}
	public boolean moveCars()  //movecars boolean  method
	{
		boolean finishAll = true ;
		for(int cari=1; cari<=nCars; cari++)
		
			finishAll= finishAll && moveCar(nCars);
			return finishAll;
		
		
	}
	public boolean moveCar(int cari)
	{
		if(carPos[cari]>= endPos)
		{
			carPos[cari]++;
			return true;
		}
		else
		{
			moveForward(cari);
			return false;
				
		}
	}
	public boolean moveForward ( int cari)
	{
		if(carPos[cari]<carsSpeed[cari])
		{
			carPos[cari]--;
			return true;
		}
		else
		{
			return false;
		}
		
	}
	// display car and road
	public void displayCars()
	{
		
		
	}
	
}

import java.util.Scanner;
public class SimulateRoad
{
	public static void main(String args[])
	{
		Scanner input = new Scanner (System.in); //create scanner to obtain input from command window
		
		final int NCARS = 50;
		final int NLANES = 10;
		final int NPOS = 10000;
		// create array road and cars
		
		int road[][] = new int [NLANES][NCARS];
		int carsSpeed[] = new int [NCARS];
		int carsLane[] = new int [NCARS];
		int carPos[] = new int [NCARS];
		int choice=0;
		
		Road road1= new Road ( road, carsSpeed, carsLane, carPos);
		MoveRandomly Ram1= new MoveRandomly ( road, carsSpeed, carsLane, carPos);
		
 		//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:
 		        	road1.readInput();
		            road1.fillCars();
		            road1.simulate();
		            road1.displayCars();
 		        		
 		        	break;
 		        	
 		        case 2:
 		        	Ram1.RanMove();
 		        	
 		        		
 		        	break;
 		        	
 		        			
 		        case 3:
 		        		
 		        	break;
 		        		
 		        default:
 		        		
 		        	System.out.print(" Invalid Choice ");
 		        		
 		        	break;	
 		     }
		
	}
}

this is what i got so far i,m not sure about the movefoward method also i dont know how to display the cars if someone could help i will really presuate it i'm a beginner BTW...

Recommended Answers

All 3 Replies

This program probably calls for a redesign where you have a Car class that keeps track of position, speed, and direction of the car. First step, just about always, is to decide what you want to do. From that, decide what you need to store and where you should store it. It's not obvious, to me at least, what data you are storing. Some comments are needed in my opinion, and again, a Car class is needed.

thnx vernon but i already have class that initialize all the data which is first 1 that called road
also second class (which is the main ) i'm calling road class which is the data for the car movement & the fill method

now i need displaycar method also i'm not sure about the movefoward method if there is something wrong

I'd advise creating a Car class, but if you've decided not to do that, OK.

I imagine you are intending to use your class variables here. If so, don't declare new variables.

// read input
	public void readInput()
	{
		Scanner input = new Scanner(System.in);
		System.out.println(" Please Enter your Cars Speed;");
		int carsSpeed =input.nextInt();
		System.out.println("Please Enter how many lines you want:");
		int carsLane = input.nextInt();
		System.out.println(" Please Enter your Cars Position:");
		int carPos = input.nextInt();
		
	}

carPos is an array, not an integer.

private int carPos[];
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.