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
@@ -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 Solution'
dependencies {
}
// Configuration for Application plugin
application {
// Define the main class for the application.
mainClass = 'ch.zhaw.prog2.trafficlight.TrafficLightOperation'
}
@@ -0,0 +1,37 @@
package ch.zhaw.prog2.trafficlight;
import java.util.Random;
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() {
pos = ++pos % trafficLights.length;
}
@Override
public void run() {
Random random = new Random();
while (true) {
try {
sleep(random.nextInt(500));
} catch (InterruptedException e) {
System.out.println(e.getMessage());
}
trafficLights[pos].passby();
gotoNextLight();
}
}
}
@@ -0,0 +1,28 @@
package ch.zhaw.prog2.trafficlight;
class TrafficLight {
private boolean red;
public TrafficLight() {
red = true;
}
public synchronized void passby() {
while (red) {
try {
wait();
} catch (InterruptedException logOrIgnore) {
System.out.println(logOrIgnore.getMessage());
}
}
}
public synchronized void switchToRed() {
red = true;
}
public synchronized void switchToGreen() {
red = false;
notifyAll();
}
}
@@ -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());
}
}
}
}
}