![]() | |
![]() |
| | Thread Tools | Search this Thread | Display Modes |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
Hi there, Im pretty new to Java and J#, however, ive produced 2 classes, found below, that simulate the changing of traffic lights. The first, traffic_light, prints a line in a console every second, each time changing the colour of the traffic light. The second class, strangely_cycling_traffic_light, inherits more or less everything from traffic_light, yet changes the sequence of lights. Here is the code: /** * traffic_light program. */ public class traffic_light implements Runnable { final String name; final String[] sequence; /** * Construct a default traffic light. * @param name The name of the light. */ public traffic_light (String name) { this(name, new String[]{ "Stop", "Prepare to go", "Go", "Prepare to stop", }); } protected traffic_light (String name, String[] sequence) { this.name = name; this.sequence = sequence; } /** * Run Method for changing states */ public void run () { while (true) { for (int index = 0; index < sequence.length; index++) { final String state = sequence[index]; printState(state); timer(1000); } } } // End run. /** * Delays for a period. * @param delay The delay period. */ public static void timer (int delay) { try { Thread.sleep(delay); //timer sleeps for 1 second } catch (InterruptedException exception) { //Does nothing } } // End timer. /** * Prints out a state * @param state The state to print. */ public void printState (String state) { System.out.println( "The " + name + " traffic lights tell the driver to " + state); } /** * Main method to run the program * @param args Command line arguments. */ public static void main (String args[]) { traffic_light trafficLight = new traffic_light("normal"); trafficLight.run(); } // End main. } // End TrafficLight And here is the code for the other class: public class strangely_cycling_traffic_light extends traffic_light { /** * Construct a strangely cycling traffic light. * @param name The name of the light. */ public strangely_cycling_traffic_light (String name) { super(name, new String[]{ "Stop", "Prepare to stop", "Go", "Prepare to go", }); } public static void main (String args[]) { strangely_cycling_traffic_light trafficLight = new strangely_cycling_traffic_light("strangely cycling"); trafficLight.run(); } } Both of these files work perfectly in a Java program such as JCreator, but the project requires a submission in the form of a Visual Studio Project, meaning the files are stored together in the one project, and here is where the problem lies.... Each of the files has a main method, and whichever is set in Visual Studio to be the start-up object will run first, meaing the other cannot be demonstrated. Is there any way of just compiling and running the 2 files individually? Or making some kind of button interface (i have no experience in this) where the user, upon building the project, is presented with a choice of 2 buttons, one for normal traffic light and one for the strange one? Would that even work, bearing in mind the 2 main methods? Would there be any way to adjust the code to remove the need for the 2 main methods? I need to have 2 separate classes, so they cant be condensed into one. Any help would be great. Thanks, Matt |
#3
| |||
| |||
|
|
Well, writing a GUI for this shouldn't be too hard, so going with the two button tehcnique would certainly work. But if you want to keep it simple and keep it in a console window, i'd suggest making a 3rd class, where the user decides which trafficlight they want to see (you could even do it through the arguments even). your main method inside the 3rd class could look somthing like this: public static void main (String args[]) { //prompt the user for input System.out.println ("Please choose a traffic light: (normal, strange)"); java.io.BufferedReader consoleTextIn new java.io.BufferedReader (new java.io.InputStreamReader (System.in)); //read input from the console window String input = onsoleTextIn.readLine (); if (input.equalsIgnoreCase ("normal"))//run normal traffic light { traffic_light trafficLight = new traffic_light("normal"); trafficLight.run(); } else//run strange traffic light { strangely_cycling_traffic_light trafficLight = new strangely_cycling_traffic_light("strangely cycling"); trafficLight.run(); } } so like i said, stuck this into a 3rd class (call it anything you want) and set it to run on startup. This way the user can decide which traffic light they want to see. "MattMcSpirit" wrote: Hi there, Im pretty new to Java and J#, however, ive produced 2 classes, found below, that simulate the changing of traffic lights. The first, traffic_light, prints a line in a console every second, each time changing the colour of the traffic light. The second class, strangely_cycling_traffic_light, inherits more or less everything from traffic_light, yet changes the sequence of lights. Here is the code: /** * traffic_light program. */ public class traffic_light implements Runnable { final String name; final String[] sequence; /** * Construct a default traffic light. * @param name The name of the light. */ public traffic_light (String name) { this(name, new String[]{ "Stop", "Prepare to go", "Go", "Prepare to stop", }); } protected traffic_light (String name, String[] sequence) { this.name = name; this.sequence = sequence; } /** * Run Method for changing states */ public void run () { while (true) { for (int index = 0; index < sequence.length; index++) { final String state = sequence[index]; printState(state); timer(1000); } } } // End run. /** * Delays for a period. * @param delay The delay period. */ public static void timer (int delay) { try { Thread.sleep(delay); //timer sleeps for 1 second } catch (InterruptedException exception) { //Does nothing } } // End timer. /** * Prints out a state * @param state The state to print. */ public void printState (String state) { System.out.println( "The " + name + " traffic lights tell the driver to " + state); } /** * Main method to run the program * @param args Command line arguments. */ public static void main (String args[]) { traffic_light trafficLight = new traffic_light("normal"); trafficLight.run(); } // End main. } // End TrafficLight And here is the code for the other class: public class strangely_cycling_traffic_light extends traffic_light { /** * Construct a strangely cycling traffic light. * @param name The name of the light. */ public strangely_cycling_traffic_light (String name) { super(name, new String[]{ "Stop", "Prepare to stop", "Go", "Prepare to go", }); } public static void main (String args[]) { strangely_cycling_traffic_light trafficLight = new strangely_cycling_traffic_light("strangely cycling"); trafficLight.run(); } } Both of these files work perfectly in a Java program such as JCreator, but the project requires a submission in the form of a Visual Studio Project, meaning the files are stored together in the one project, and here is where the problem lies.... Each of the files has a main method, and whichever is set in Visual Studio to be the start-up object will run first, meaing the other cannot be demonstrated. Is there any way of just compiling and running the 2 files individually? Or making some kind of button interface (i have no experience in this) where the user, upon building the project, is presented with a choice of 2 buttons, one for normal traffic light and one for the strange one? Would that even work, bearing in mind the 2 main methods? Would there be any way to adjust the code to remove the need for the 2 main methods? I need to have 2 separate classes, so they cant be condensed into one. Any help would be great. Thanks, Matt |
#4
| |||
| |||
|
|
Thanks for this, it is really helpful. Il let you know if i run into any problems. Thanks, Matt "Avid J# Programmer" wrote: Well, writing a GUI for this shouldn't be too hard, so going with the two button tehcnique would certainly work. But if you want to keep it simple and keep it in a console window, i'd suggest making a 3rd class, where the user decides which trafficlight they want to see (you could even do it through the arguments even). your main method inside the 3rd class could look somthing like this: public static void main (String args[]) { //prompt the user for input System.out.println ("Please choose a traffic light: (normal, strange)"); java.io.BufferedReader consoleTextIn new java.io.BufferedReader (new java.io.InputStreamReader (System.in)); //read input from the console window String input = onsoleTextIn.readLine (); if (input.equalsIgnoreCase ("normal"))//run normal traffic light { traffic_light trafficLight = new traffic_light("normal"); trafficLight.run(); } else//run strange traffic light { strangely_cycling_traffic_light trafficLight = new strangely_cycling_traffic_light("strangely cycling"); trafficLight.run(); } } so like i said, stuck this into a 3rd class (call it anything you want) and set it to run on startup. This way the user can decide which traffic light they want to see. "MattMcSpirit" wrote: Hi there, Im pretty new to Java and J#, however, ive produced 2 classes, found below, that simulate the changing of traffic lights. The first, traffic_light, prints a line in a console every second, each time changing the colour of the traffic light. The second class, strangely_cycling_traffic_light, inherits more or less everything from traffic_light, yet changes the sequence of lights. Here is the code: /** * traffic_light program. */ public class traffic_light implements Runnable { final String name; final String[] sequence; /** * Construct a default traffic light. * @param name The name of the light. */ public traffic_light (String name) { this(name, new String[]{ "Stop", "Prepare to go", "Go", "Prepare to stop", }); } protected traffic_light (String name, String[] sequence) { this.name = name; this.sequence = sequence; } /** * Run Method for changing states */ public void run () { while (true) { for (int index = 0; index < sequence.length; index++) { final String state = sequence[index]; printState(state); timer(1000); } } } // End run. /** * Delays for a period. * @param delay The delay period. */ public static void timer (int delay) { try { Thread.sleep(delay); //timer sleeps for 1 second } catch (InterruptedException exception) { //Does nothing } } // End timer. /** * Prints out a state * @param state The state to print. */ public void printState (String state) { System.out.println( "The " + name + " traffic lights tell the driver to " + state); } /** * Main method to run the program * @param args Command line arguments. */ public static void main (String args[]) { traffic_light trafficLight = new traffic_light("normal"); trafficLight.run(); } // End main. } // End TrafficLight And here is the code for the other class: public class strangely_cycling_traffic_light extends traffic_light { /** * Construct a strangely cycling traffic light. * @param name The name of the light. */ public strangely_cycling_traffic_light (String name) { super(name, new String[]{ "Stop", "Prepare to stop", "Go", "Prepare to go", }); } public static void main (String args[]) { strangely_cycling_traffic_light trafficLight = new strangely_cycling_traffic_light("strangely cycling"); trafficLight.run(); } } Both of these files work perfectly in a Java program such as JCreator, but the project requires a submission in the form of a Visual Studio Project, meaning the files are stored together in the one project, and here is where the problem lies.... Each of the files has a main method, and whichever is set in Visual Studio to be the start-up object will run first, meaing the other cannot be demonstrated. Is there any way of just compiling and running the 2 files individually? Or making some kind of button interface (i have no experience in this) where the user, upon building the project, is presented with a choice of 2 buttons, one for normal traffic light and one for the strange one? Would that even work, bearing in mind the 2 main methods? Would there be any way to adjust the code to remove the need for the 2 main methods? I need to have 2 separate classes, so they cant be condensed into one. Any help would be great. Thanks, Matt |
#5
| |||
| |||
|
|
Hi again, Im getting an error message : unreported exception java.io.IOException; must be caught or declared to be thrown. My code for the 3rd class is as follows: public class Selector { public static void main (String args[]) { //prompt the user for input System.out.println ("Please choose a traffic light: (normal, strange)"); java.io.BufferedReader consoleTextIn = new java.io.BufferedReader (new java.io.InputStreamReader (System.in)); //read input from the console window String input = consoleTextIn.readLine (); if (input.equalsIgnoreCase ("normal"))//run normal traffic light { traffic_light trafficLight = new traffic_light("normal"); trafficLight.run(); } else//run strange traffic light { strangely_cycling_traffic_light trafficLight = new strangely_cycling_traffic_light("strangely cycling"); trafficLight.run(); } } } // End Selector The error is pointing to this line: String input = consoleTextIn.readLine (); but im not sure what to do, do i use a catch statement? Thanks, Matt "MattMcSpirit" wrote: Thanks for this, it is really helpful. Il let you know if i run into any problems. Thanks, Matt "Avid J# Programmer" wrote: Well, writing a GUI for this shouldn't be too hard, so going with the two button tehcnique would certainly work. But if you want to keep it simple and keep it in a console window, i'd suggest making a 3rd class, where the user decides which trafficlight they want to see (you could even do it through the arguments even). your main method inside the 3rd class could look somthing like this: public static void main (String args[]) { //prompt the user for input System.out.println ("Please choose a traffic light: (normal, strange)"); java.io.BufferedReader consoleTextIn new java.io.BufferedReader (new java.io.InputStreamReader (System.in)); //read input from the console window String input = onsoleTextIn.readLine (); if (input.equalsIgnoreCase ("normal"))//run normal traffic light { traffic_light trafficLight = new traffic_light("normal"); trafficLight.run(); } else//run strange traffic light { strangely_cycling_traffic_light trafficLight = new strangely_cycling_traffic_light("strangely cycling"); trafficLight.run(); } } so like i said, stuck this into a 3rd class (call it anything you want) and set it to run on startup. This way the user can decide which traffic light they want to see. "MattMcSpirit" wrote: Hi there, Im pretty new to Java and J#, however, ive produced 2 classes, found below, that simulate the changing of traffic lights. The first, traffic_light, prints a line in a console every second, each time changing the colour of the traffic light. The second class, strangely_cycling_traffic_light, inherits more or less everything from traffic_light, yet changes the sequence of lights. Here is the code: /** * traffic_light program. */ public class traffic_light implements Runnable { final String name; final String[] sequence; /** * Construct a default traffic light. * @param name The name of the light. */ public traffic_light (String name) { this(name, new String[]{ "Stop", "Prepare to go", "Go", "Prepare to stop", }); } protected traffic_light (String name, String[] sequence) { this.name = name; this.sequence = sequence; } /** * Run Method for changing states */ public void run () { while (true) { for (int index = 0; index < sequence.length; index++) { final String state = sequence[index]; printState(state); timer(1000); } } } // End run. /** * Delays for a period. * @param delay The delay period. */ public static void timer (int delay) { try { Thread.sleep(delay); //timer sleeps for 1 second } catch (InterruptedException exception) { //Does nothing } } // End timer. /** * Prints out a state * @param state The state to print. */ public void printState (String state) { System.out.println( "The " + name + " traffic lights tell the driver to " + state); } /** * Main method to run the program * @param args Command line arguments. */ public static void main (String args[]) { traffic_light trafficLight = new traffic_light("normal"); trafficLight.run(); } // End main. } // End TrafficLight And here is the code for the other class: public class strangely_cycling_traffic_light extends traffic_light { /** * Construct a strangely cycling traffic light. * @param name The name of the light. */ public strangely_cycling_traffic_light (String name) { super(name, new String[]{ "Stop", "Prepare to stop", "Go", "Prepare to go", }); } public static void main (String args[]) { strangely_cycling_traffic_light trafficLight = new strangely_cycling_traffic_light("strangely cycling"); trafficLight.run(); } } Both of these files work perfectly in a Java program such as JCreator, but the project requires a submission in the form of a Visual Studio Project, meaning the files are stored together in the one project, and here is where the problem lies.... Each of the files has a main method, and whichever is set in Visual Studio to be the start-up object will run first, meaing the other cannot be demonstrated. Is there any way of just compiling and running the 2 files individually? Or making some kind of button interface (i have no experience in this) where the user, upon building the project, is presented with a choice of 2 buttons, one for normal traffic light and one for the strange one? Would that even work, bearing in mind the 2 main methods? Would there be any way to adjust the code to remove the need for the 2 main methods? I need to have 2 separate classes, so they cant be condensed into one. Any help would be great. Thanks, Matt |
#6
| |||
| |||
|
|
Each of the files has a main method, and whichever is set in Visual Studio to be the start-up object will run first, meaing the other cannot be demonstrated. Is there any way of just compiling and running the 2 files individually? Or making some kind of button interface (i have no experience in this) where the user, upon building the project, is presented with a choice of 2 buttons, one for normal traffic light and one for the strange one? Would that even work, bearing in mind the 2 main methods? Would there be any way to adjust the code to remove the need for the 2 main methods? I need to have 2 separate classes, so they cant be condensed into one. |
#7
| |||
| |||
|
|
in fact I think VS.net/J# doesn't allow one to have more than one "main" methods in their project, which could be considered a "bug" since most other Java IDEs I know allow you to do so and you select which class has the main method you want to use for running your project (what is the executable file that is) can anyone check and confirm there's no such option? If so you can file it as a bug at: http://msdn.microsoft.com/productfeedback Each of the files has a main method, and whichever is set in Visual Studio to be the start-up object will run first, meaing the other cannot be demonstrated. Is there any way of just compiling and running the 2 files individually? Or making some kind of button interface (i have no experience in this) where the user, upon building the project, is presented with a choice of 2 buttons, one for normal traffic light and one for the strange one? Would that even work, bearing in mind the 2 main methods? Would there be any way to adjust the code to remove the need for the 2 main methods? I need to have 2 separate classes, so they cant be condensed into one. |
![]() |
| Thread Tools | Search this Thread |
| Display Modes | |
| |