So I am working on a Traffic Simulator and I am having a fair bit of trouble with it. I need the menu to display, user chooses the option enters a value for the option and then either it loops with the menu again and they can set other options. Or should I have pop up dialog windows that allow show up immediately starting with menu option 1 and progressing from there. Anyway have a look and see where and what is wrong and what I need to do to fix it, so I can compile it and run it. When compiled it just needs to run from DOS and use ASCII characters for the grid. - is for horizontal lanes, | is for vertical lanes and c is used to represent the cars.

package TrafficSim;

/**
 *
 * @author Simes
 */
public class Main {

    /**
     * This initializes my program
     * @param  args  command line parameters
     */
    public static void main(String[] args) {
        new MainUI().run();
    }
}
package TrafficSim;

import java.util.Scanner;

/**
 *
 * @author Simes
 */
public class MainUI {

    private GridModel project;

    /**
     *
     */
    public MainUI() {
        project = new GridModel();
    }

    /**
     *
     */
    public void showMenu() {
        System.out.println("=== MENU ===\n"
                + "1. Set Number of horizontal lanes (H-street) [min 1, max 3]\n"
                + "2. Set Number of vertical lanes (V-street) [min 1, max 4]\n"
                + "3. Set Probability of a car entering H-street [min 0, max 1]\n"
                + "4. Set Probability of a car entering V-street [min 0, max 1]\n"
                + "5. Run one simulation cycle\n"
                + "6. Set and run number of simulation cycles [min 1, max 10]\n"
                + "7. Exit the program \n"
                + "Enter your choice> ");
    }

    void run() {
        GridView gv = new GridView(project);
        while (true) {
            gv.show();
            boolean exit = true;
            do {
                showMenu();

                Scanner in = new Scanner(System.in);
                int menuChoice = in.nextInt();

                if (menuChoice == 1) {
                    while (true) {
                        System.out.println("Please set the number of horizontal lanes [min 1, max 3]");
                        try {
                            int choice = in.nextInt();
                            if (choice > 0 && choice <= 3) { // reprints the above sout statement if the choice is outside of the range in the if statement
                                project.sethorizontalLaneNum(in.nextInt());
                                break;
                            }
                        } catch (Exception e) { // reprints above sout statement if anything else besides 1, 2 or 3 is entered as the input choice
                        }
                        //in.nextLine();
                    }
                }

                else if (menuChoice == 2) {
                    while (true) {
                        System.out.println("Please set the number of vertical lanes [min 1, max 4]");
                        try {
                            int choice = in.nextInt();
                            if (choice > 0 && choice < 5) { // reprints the above sout statement if the choice is outside of the range in the if statement
                                project.setverticalLaneNum(in.nextInt());
                                break;
                            }
                        } catch (Exception e) { // reprints above sout statement if anything else besides 1, 2, 3 or 4 is entered as the input choice
                        }

                if (menuChoice == 3) {
                    while (true) {
                        System.out.println("Please set the probability of a car entering H-street [min 0, max 1]");
                        try {
                            int choice = in.nextInt();
                            if (choice > 0 && choice < 2) {
                                project.sethorizontalCarProbability(in.nextInt());
                                break;
                            }
                        } catch (Exception e) {
                        }

                if (menuChoice == 4) {
                    while (true) {
                        System.out.println("Please set the probability of a car entering V-street [min 0, max 1]");
                        try {
                            int choice = in.nextInt();
                            if (choice > 0 && choice < 2) {
                                project.setverticalCarProbability(in.nextInt());
                                break;
                            }
                        } catch (Exception e) {
                        }

                if (menuChoice == 5) {
                    while (true) {
                        System.out.println("Run one simulation cycle");
                        try {
                            int choice = in.nextInt();
                            if (choice > 0 && choice < 5) {
                                project.setrunCycle(in.nextInt());
                                break;
                            }
                        } catch (Exception e) {
                        }

                   if (menuChoice == 6) {
                    while (true) {
                        System.out.println("Set and run number of simulation cycles [min 1, max 10]");
                        try {
                            int choice = in.nextInt();
                            if (choice > 0 && choice < 11) {
                                project.setsimulationCycle(in.nextInt());
                                break;
                            }
                        } catch (Exception e) {
                        }

               if (menuChoice == 7) {
                    while (true) {
                        System.out.println("Thank you for using the Traffic Simulator");
                        try {
                            int choice = in.nextInt();
                            if (choice == 7) {
                                project.setExit(true);
                                break;
                            }
                        } catch (Exception e) {
                        }
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            } while (exit != true);
        }
    }
}
package TrafficSim;

import TrafficSim.GridModel;

/**
 *
 * @author Simes
 */
public class Cars {

    private int Car;
    private char[][] grid;
    private final Cars project;

    /**
     *
     * @param project
     */
    public Cars(Cars project) {
        this.project = project;
        int gs = project.getCar();
        grid = new char[gs][gs];
        setCar('c');
        buildhorizontalCars(project.gethorizontalCar());
        buildverticalCars(project.getverticalCar());

    }

    /**
     *
     * @return
     */
    public char[][] getGrid() {
        return grid;
    }

    /**
     *
     * @param grid
     */
    public void setGrid(char[][] grid) {
        this.grid = grid;
    }

    /**
     *
     * @return
     */
    public int getCar() {
        return Car;
    }

    /**
     *
     * @param Car
     */
    public void setCar(int Car) {
        this.Car = Car;
    }

    /**
     *
     * @return
     */
    public int gethorizontalCar() {
        return Car;
    }

    /**
     *
     * @param Car
     */
    public void sethorizontalCar(int Car) {
        this.Car = Car;
    }

    /**
     *
     * @return
     */
    public int getverticalCar() {
        return Car;
    }

    /**
     *
     * @param Car
     */
    public void setverticalCar(int Car) {
        this.Car = Car;
    }

    private void setCar(char c) {

    }

    void show() {
        for (int r = 0; r < grid.length; r++) {
            char[] tempRow = grid[r];
            for (int column = 0; column < tempRow.length; column++) {
                char character = grid[r][column];
                System.out.print(character);
            }
            System.out.println("");
        }
    }

    private void buildhorizontalCar(int horizontalCar) {
        int nr = grid.length;
        int middle = nr / 2 +- 1;
        for (int r = 0; r < nr; r++) {
            char[] row = grid[r];
            if (r == middle) {
                setRow(row, ' ');
            }
        }
    }

    private void setRow(char[] row, char c) {
        for (int i = 0; i < row.length; i++) {
            row[i] = c;
        }
    }

    private void buildverticalCar(int verticalCar) {
        int nc = project.getGridSize();
        int middle = nc / 2 +- 1;
        for (int row = 0; row < grid.length; row++) {
            for (int column = 0; column < grid.length; column++) {
                if (column == middle) {
                    grid[row][column] = ' ';




                }
            }
        }
    }
}
package TrafficSim;

import java.util.Random;
/**
 *
 * @author Simes
 */
public class AssignmentModel {

    private int horizontalLaneNum;
    private int verticalLaneNum;
    private int gridSize;
    private int horizontalCarProbability;
    private int verticalCarProbability;

    /**
     *
     * @return
     */
    public int getGridSize() {
        return gridSize;
    }

    /**
     * 
     * @param gridSize
     */
    public void setGridSize(int gridSize) {
        this.gridSize = gridSize;
    }

    /**
     *
     */
    public AssignmentModel() {
        init();
    }

    private void init() {
        horizontalLaneNum = 1;
        verticalLaneNum = 1;
        gridSize = 50;
        horizontalCarProbability = 1;
        verticalCarProbability = 1;
    }

    /**
     *
     * @return
     */
    public int horizontalLaneNum() {
        return horizontalLaneNum;
    }

    /**
     *
     * @param horizontalLaneNum
     */
    public void sethorizontalLaneNum(int horizontalLaneNum) {
        this.horizontalLaneNum = horizontalLaneNum;
    }

    /**
     *
     * @return
     */
    public int getverticalLaneNum() {
        return verticalLaneNum;
    }

    /**
     *
     * @param verticalLaneNum
     */
    public void verticalLaneNum(int verticalLaneNum) {
        this.verticalLaneNum = verticalLaneNum;
    }

    int gethorizontalLaneNum() {
        return horizontalLaneNum;
    }

    /**
     *
     * @return
     */
    public int getverticalCarProbability() {
        return verticalCarProbability;
    }

    /**
     *
     * @return
     */
    public int gethorizontalCarProbability() {
        return horizontalCarProbability;
    }

    /**
     *
     * @param verticalCarProbability
     */
    public void setverticalCarProbability(int verticalCarProbability) {
        this.verticalCarProbability = verticalCarProbability;
    }

    /**
     *
     * @param horizontalCarProbability
     */
    public void sethorizontalCarProbability(int horizontalCarProbability) {
        this.horizontalCarProbability = horizontalCarProbability;
    }
}
package TrafficSim;

/**
 *
 * @author Simes
 */
public class GridModel {

    private int horizontalLaneNum;
    private int verticalLaneNum;
    private int gridSize;
    private int horizontalCarProbability;
    private int verticalCarProbability;
    private int simulationCycle;
    private int runCycle;
    private boolean Exit;

    /**
     *
     * @return
     */
    public int getGridSize() {
        return gridSize;
    }

    /**
     *
     * @param gridSize
     */
    public void setGridSize(int gridSize) {
        this.gridSize = gridSize;
    }

    /**
     *
     */
    public GridModel() {
        init();
    }

    private void init() {
        horizontalLaneNum = 1;
        verticalLaneNum = 1;
        gridSize = 50;
        horizontalCarProbability = 1;
        verticalCarProbability = 1;
        simulationCycle = 10;
        runCycle = 1;
    }

    /**
     *
     * @return
     */
    public int horizontalLaneNum() {
        return horizontalLaneNum;
    }

    /**
     *
     * @param horizontalLaneNum
     */
    public void sethorizontalLaneNum(int horizontalLaneNum) {
        this.horizontalLaneNum = horizontalLaneNum;
    }

    /**
     *
     * @return
     */
    public int getverticalLaneNum() {
        return verticalLaneNum;
    }

    /**
     *
     * @param verticalLaneNum
     */
    public void verticalLaneNum(int verticalLaneNum) {
        this.verticalLaneNum = verticalLaneNum;
    }

    /**
     *
     * @param verticalLaneNum
     */
    public void setverticalLaneNum(int verticalLaneNum) {
        this.verticalLaneNum = verticalLaneNum;
    }

    int gethorizontalLaneNum() {
        return horizontalLaneNum;
    }

    /**
     *
     * @return
     */
    public int getverticalCarProbability() {
        return verticalCarProbability;
    }

     /**
      *
      * @return
      */
     public int gethorizontalCarProbability() {
        return horizontalCarProbability;
    }

     /**
      *
      * @param verticalCarProbability
      */
     public void setverticalCarProbability(int verticalCarProbability) {
        this.verticalCarProbability = verticalCarProbability;
    }

    /**
     *
     * @param horizontalCarProbability
     */
    public void sethorizontalCarProbability(int horizontalCarProbability) {
        this.horizontalCarProbability = horizontalCarProbability;
    }

    /**
     *
     * @return
     */
    public int getsimulationCycle() {
        return simulationCycle;
    }

    /**
     *
     * @param simulationCycle
     */
    public void setsimulationCycle(int simulationCycle) {
        this.simulationCycle = simulationCycle;
    }

    /**
     *
     * @return
     */
    public int getrunCycle() {
        return runCycle;
    }

    /**
     *
     * @param runCycle
     */
    public void setrunCycle(int runCycle){
        this.runCycle = runCycle;
    }

    /**
     *
     * @return
     */
    public boolean isExit() {
        return Exit;
    }

    /**
     *
     * @param Exit
     */
    public void setExit(boolean Exit) {
        this.Exit = Exit;
    }
}
package TrafficSim;

/**
 *
 * @author Simes
 */
public class GridView {

    private char[][] grid;
    private GridModel project;

    /**
     *
     * @param project
     */
    public GridView(GridModel project) {
        this.project = project;
//        int horizontalnumber = project.gethorizontalLaneNum();
//        int verticalnumber = project.getverticalLaneNum();
//        grid = new char[horizontalnumber][verticalnumber];
        int gs = project.getGridSize();
        grid = new char[gs][gs];
        setAll('?');
        buildHorizontalStreet(project.gethorizontalLaneNum());
        buildVerticalStreet(project.getverticalLaneNum());
//        char character = '?';
    }

    /**
     *
     * @param character
     */
    public void setAll(char character) {
        for (int row = 0; row < grid.length; row++) {
            char[] tempRow = grid[row];
            for (int column = 0; column < tempRow.length; column++) {
                //row[column] = character;
                grid[row][column] = character;
                //System.out.println("" + character);
            }
        }
    }

    /**
     *
     * @param row
     * @param column
     * @param value
     */
    public void set(int row, int column, char value) {
        grid[row][column] = value;
    }

    void show() {
        for (int r = 0; r < grid.length; r++) {
            char[] tempRow = grid[r];
            for (int column = 0; column < tempRow.length; column++) {
                char character = grid[r][column];
                System.out.print(character);
            }
            System.out.println("");
        }
    }

    private void buildHorizontalStreet(int horizontalLaneNum) {
        int nr = grid.length;
        int middle = nr / 2;
        for (int r = 0; r < nr; r++) {
            char[] row = grid[r];
            if (r == middle) {
                setRow(row, ' ');
            }
        }
    }

    private void setRow(char[] row, char c) {
        for (int i = 0; i < row.length; i++) {
            row[i] = c;
        }
    }

    private void buildVerticalStreet(int verticalLaneNum) {
        int nc = project.getGridSize();
        int middle = nc / 2;
        for (int row = 0; row < grid.length; row++) {
            for (int column = 0; column < grid.length; column++) {
                if (column == middle) {
                    grid[row][column] = ' ';
                }
            }
        }
    }
}

Recommended Answers

All 12 Replies

If you are getting errors, Please copy the full text of the message and post it here.

Otherwise explain what your problem is.

The menu loops infinitely regardless of what option I choose and I am way off with the cars class. Trying to implement a horizontal and vertical car, which is more than likely far too complex and I should only have one type of car that I can use in both areas.

The menu loops infinitely

try debugging your code by printing out the values of the variables used to control the looping to see what is happening.
You have lots of nested loops so there are many places for your logic to be wrong.

I have tried debugging and even asking people I know who are more proficient with Java than I am and still no luck, that is why I posted my code here.

Are you looking for help so you can solve the problem or are you looking for some one to solve your problem?

If you want help solving it, add the print outs to your code and narrow done where "The menu loops infinitely". Display the variables that control the looping code and that allows it to exit the loop.
There are a lot of while(true) loops. Each one needs some print outs so you will know which one is looping forever.

In void run(), the second line has a while(true) loop. This loop is never broken out of. I noticed that you call project.setExit(true) when option 7 from the menu is selected. This seems like the value that should be placed in that first while() loop. Do you have a project.getExit() method?

No I don't and I was told to put while(true) on that line so that it loops.

Have you tried debugging your code by adding print outs to see where the code was hanging and why?

I have or attempted to and am completely lost, which is why I was hoping someone here could help me out, otherwise I will submit it as it is and see what happens after that.

To debug your code, you need to understand what you expect every statement in the program you wrote to do or what you intended each statement to do.
By adding printouts through your code, you see what the code actually does allowing you to compare that with what you expect the code to do. When the output is different from what you expected, you need to either change your expectations or the code.

Another technique is to play computer. Manually go thru the code executing each statement in you head and writing down the results as you go thru. This requires that you know what a statement will actually do. By using printouts (as mentioned above) you will see what the code does which can be more accurate than you're trying to figure out what a statement does.

Please help me friends i can't able to understand the "TRAFFIC PROGRAM" and i don't know to do that program also plz post correct traffic program. :) plz

DaniWeb Member Rules include:

"Do provide evidence of having done some work yourself if posting questions from school or work assignments"
"Do not hijack old threads by posting a new question as a reply to an old one"

http://www.daniweb.com/community/rules
Please start your own new thread for your question and show some evidence of the work you have done.

This thread is closed.

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.