Initial commit

This commit is contained in:
github-classroom[bot]
2022-03-31 09:49:56 +00:00
commit 5a1de0b8e7
83 changed files with 17003 additions and 0 deletions
+22
View File
@@ -0,0 +1,22 @@
/*
* Gradle build configuration for specific lab module / exercise
* Default declarations can be found in the lab main build configuration (../../gradle.build)
* Declarations in this file extend or override the default values.
*/
// the Java plugin is added by default in the main lab configuration
plugins {
// Apply the application plugin to add support for building a CLI application.
id 'application'
}
description = 'Lab04 TrafficLight'
dependencies {
}
// Configuration for Application plugin
application {
// Define the main class for the application.
mainClass = 'ch.zhaw.prog2.trafficlight.TrafficLightOperation'
}
@@ -0,0 +1,28 @@
package ch.zhaw.prog2.trafficlight;
class Car extends Thread {
private final TrafficLight[] trafficLights;
private int pos;
public Car(String name, TrafficLight[] trafficLights) {
super(name);
this.trafficLights = trafficLights;
pos = 0; // start at first light
start();
}
public synchronized int position() {
return pos;
}
private void gotoNextLight() {
// ToDo: Helper method to move car to next light
}
@Override
public void run() {
while (true) {
// ToDo: drive endlessly through all lights
}
}
}
@@ -0,0 +1,22 @@
package ch.zhaw.prog2.trafficlight;
class TrafficLight {
private boolean red;
public TrafficLight() {
red = true;
}
public synchronized void passby() {
// ToDo: wait as long the light is red
}
public synchronized void switchToRed() {
// ToDo: set light to red
}
public synchronized void switchToGreen() {
// Todo: set light to green
// waiting cars can now pass by
}
}
@@ -0,0 +1,67 @@
package ch.zhaw.prog2.trafficlight;
public class TrafficLightOperation {
private static volatile boolean running = true;
public static void terminate () {
running = false;
}
public static void main(String[] args) {
TrafficLight[] trafficLights = new TrafficLight[7];
Car[] cars = new Car[20];
for (int i = 0; i < trafficLights.length; i++)
trafficLights[i] = new TrafficLight();
for (int i = 0; i < cars.length; i++) {
cars[i] = new Car("Car " + i, trafficLights);
}
// Simulation
while (running) {
for (int greenIndex = 0; greenIndex < trafficLights.length; greenIndex = greenIndex + 2) {
// Display state of simulation
System.out.println("=====================================================");
for (int j = 0; j < trafficLights.length; j++) {
String lightState;
if (j == greenIndex || j == greenIndex + 1)
lightState = "";
else
lightState = "🛑";
System.out.print(lightState + " at Light " + j + ":");
for (int carNumber = 0; carNumber < cars.length; carNumber++) {
if (cars[carNumber].position() == j)
System.out.print(" " + carNumber);
}
System.out.println();
}
System.out.println("=====================================================");
try {
Thread.sleep(3000);
} catch (InterruptedException logOrIgnore) {
System.out.println(logOrIgnore.getMessage());
}
trafficLights[greenIndex].switchToGreen();
if (greenIndex + 1 < trafficLights.length) {
trafficLights[greenIndex + 1].switchToGreen();
}
// green period
try {
Thread.sleep((int) (Math.random() * 500));
} catch (InterruptedException logOrIgnore) {
System.out.println(logOrIgnore.getMessage());
}
trafficLights[greenIndex].switchToRed();
if (greenIndex + 1 < trafficLights.length)
trafficLights[greenIndex + 1].switchToRed();
// red period
try {
Thread.sleep(1000);
} catch (InterruptedException logOrIgnore) {
System.out.println(logOrIgnore.getMessage());
}
}
}
}
}