Compare commits
No commits in common. "b9fb2db2a2148d5b1c12292b0694a063bee53efe" and "49b307b6803fd3e47641f8dcc746ce857749fb92" have entirely different histories.
b9fb2db2a2
...
49b307b680
36
src/DiningPhilosophers/Fork.java
Normal file
36
src/DiningPhilosophers/Fork.java
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
package Prog3.src.DiningPhilosophers;
|
||||||
|
|
||||||
|
public class Fork {
|
||||||
|
|
||||||
|
private boolean inUse = false;
|
||||||
|
private final int id;
|
||||||
|
|
||||||
|
public Fork(int id){
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getId(){
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean take(){
|
||||||
|
synchronized (this){
|
||||||
|
if (inUse){
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
inUse = true;
|
||||||
|
try {
|
||||||
|
Thread.sleep(1000);
|
||||||
|
}catch (InterruptedException e){
|
||||||
|
System.out.println("Gabel mit ID: " + id + " hat nicht funktioniert");
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void put(){
|
||||||
|
synchronized(this){
|
||||||
|
inUse = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
52
src/DiningPhilosophers/Philosopher.java
Normal file
52
src/DiningPhilosophers/Philosopher.java
Normal file
@ -0,0 +1,52 @@
|
|||||||
|
package Prog3.src.DiningPhilosophers;
|
||||||
|
|
||||||
|
public class Philosopher implements Runnable{
|
||||||
|
protected int id;
|
||||||
|
protected final Fork left;
|
||||||
|
protected final Fork right;
|
||||||
|
|
||||||
|
public Philosopher(int id, Fork left, Fork right) {
|
||||||
|
this.id = id;
|
||||||
|
this.left = left;
|
||||||
|
this.right = right;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void run(){
|
||||||
|
while(true) {
|
||||||
|
allocateForks();
|
||||||
|
System.out.println("Philosopher " + id + " is eating");
|
||||||
|
try{
|
||||||
|
Thread.sleep(2000);
|
||||||
|
}catch (Exception e){
|
||||||
|
|
||||||
|
}
|
||||||
|
left.put();
|
||||||
|
right.put();
|
||||||
|
System.out.println("Philosopher " + id + " finished eating");
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void allocateForks() {
|
||||||
|
Fork firstFork, secondFork;
|
||||||
|
|
||||||
|
if (left.getId() < right.getId()){
|
||||||
|
firstFork = left;
|
||||||
|
secondFork = right;
|
||||||
|
} else {
|
||||||
|
firstFork = right;
|
||||||
|
secondFork = left;
|
||||||
|
}
|
||||||
|
|
||||||
|
System.out.println("Philosopher " + id + " is trying to take Fork " + firstFork.getId());
|
||||||
|
while (!firstFork.take())
|
||||||
|
Thread.yield();
|
||||||
|
System.out.println("Philosopher " + id + " got Fork " + firstFork.getId());
|
||||||
|
System.out.println("Philosopher " + id + " is trying to take Fork " + secondFork.getId());
|
||||||
|
while (!secondFork.take())
|
||||||
|
Thread.yield();
|
||||||
|
System.out.println("Philosopher " + id + " got Fork " + secondFork.getId());
|
||||||
|
}
|
||||||
|
}
|
23
src/DiningPhilosophers/Table.java
Normal file
23
src/DiningPhilosophers/Table.java
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
package Prog3.src.DiningPhilosophers;
|
||||||
|
|
||||||
|
public class Table {
|
||||||
|
|
||||||
|
public static void main(String[] args){
|
||||||
|
Fork[] forks = { new Fork(0), new Fork(1), new Fork(2),
|
||||||
|
new Fork(3), new Fork(4)};
|
||||||
|
|
||||||
|
Philosopher[] philosophers = {
|
||||||
|
new Philosopher(0, forks[0], forks[1]),
|
||||||
|
new Philosopher(1, forks[1], forks[2]),
|
||||||
|
new Philosopher(2, forks[2], forks[3]),
|
||||||
|
new Philosopher(3, forks[3], forks[4]),
|
||||||
|
new Philosopher(4, forks[4], forks[0])
|
||||||
|
};
|
||||||
|
|
||||||
|
for (Philosopher philosopher : philosophers) {
|
||||||
|
|
||||||
|
Thread t = new Thread(philosopher);
|
||||||
|
t.start();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
49
src/DistributedSorter/SorterClient.java
Normal file
49
src/DistributedSorter/SorterClient.java
Normal file
@ -0,0 +1,49 @@
|
|||||||
|
package DistributedSorter;
|
||||||
|
|
||||||
|
import java.io.*;
|
||||||
|
import java.net.Socket;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.Scanner;
|
||||||
|
|
||||||
|
public class SorterClient implements Runnable{
|
||||||
|
public static void main(String[] args) {
|
||||||
|
SorterClient client = new SorterClient();
|
||||||
|
client.run();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void sendLine(Socket socket, String line) throws IOException {
|
||||||
|
OutputStream out = socket.getOutputStream();
|
||||||
|
PrintWriter writer = new PrintWriter(out);
|
||||||
|
writer.println(line);
|
||||||
|
writer.flush();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String receiveLine(Socket socket) throws IOException {
|
||||||
|
InputStream in = socket.getInputStream();
|
||||||
|
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
|
||||||
|
return reader.readLine();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void run(){
|
||||||
|
Scanner scanner = new Scanner(System.in);
|
||||||
|
while (scanner.hasNextLine()) {
|
||||||
|
String line = scanner.nextLine();
|
||||||
|
if(line.isEmpty())
|
||||||
|
break;
|
||||||
|
line = sort(line);
|
||||||
|
System.out.println(line);
|
||||||
|
}
|
||||||
|
scanner.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
private String sort(String line) {
|
||||||
|
try (Socket socket = new Socket("localhost", 12345)) {
|
||||||
|
|
||||||
|
sendLine(socket, line);
|
||||||
|
return receiveLine(socket);
|
||||||
|
} catch (Exception e) {
|
||||||
|
return "Error connecting to the server.";
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
67
src/DistributedSorter/SorterServer.java
Normal file
67
src/DistributedSorter/SorterServer.java
Normal file
@ -0,0 +1,67 @@
|
|||||||
|
package DistributedSorter;
|
||||||
|
|
||||||
|
import jdk.jshell.spi.ExecutionControl;
|
||||||
|
|
||||||
|
import java.io.*;
|
||||||
|
import java.net.ServerSocket;
|
||||||
|
import java.net.Socket;
|
||||||
|
import java.util.Arrays;
|
||||||
|
|
||||||
|
public class SorterServer implements Runnable {
|
||||||
|
|
||||||
|
Socket client;
|
||||||
|
public SorterServer(Socket client) {
|
||||||
|
this.client = client;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args){
|
||||||
|
final int port = 12345;
|
||||||
|
try (ServerSocket socket = new ServerSocket(port)) {
|
||||||
|
|
||||||
|
while (true){
|
||||||
|
Socket client = socket.accept();
|
||||||
|
Thread thread = new Thread(new SorterServer(client));
|
||||||
|
thread.start();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception e) {
|
||||||
|
System.err.println("Error: " + e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void sendLine(Socket socket, String line) throws IOException {
|
||||||
|
OutputStream out = socket.getOutputStream();
|
||||||
|
PrintWriter writer = new PrintWriter(out);
|
||||||
|
writer.println(line);
|
||||||
|
writer.flush();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String receiveLine(Socket socket) throws IOException {
|
||||||
|
InputStream in = socket.getInputStream();
|
||||||
|
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
|
||||||
|
return reader.readLine();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private String sort(String line){
|
||||||
|
char[] chars = line.toCharArray();
|
||||||
|
Arrays.sort(chars);
|
||||||
|
return new String(chars);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
try{
|
||||||
|
String line = receiveLine(client);
|
||||||
|
String answer = sort(line);
|
||||||
|
sendLine(client, answer);
|
||||||
|
System.out.println(line + "-->"+ answer);
|
||||||
|
} catch (IOException e) {
|
||||||
|
System.err.println("Fehler");
|
||||||
|
}
|
||||||
|
finally{
|
||||||
|
try{client.close();} catch(Exception e)
|
||||||
|
{}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
48
src/Praktikum01/Zahlenfilter.java
Normal file
48
src/Praktikum01/Zahlenfilter.java
Normal file
@ -0,0 +1,48 @@
|
|||||||
|
package Praktikum01;
|
||||||
|
|
||||||
|
|
||||||
|
public class Zahlenfilter {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
int upper_limit = 200;
|
||||||
|
if (args.length > 0){
|
||||||
|
upper_limit = Integer.parseInt(args[0]);
|
||||||
|
}else{
|
||||||
|
System.out.println("Keine Zahl eingegeben. Es wird mit 200 weitergearbeitet.");
|
||||||
|
}
|
||||||
|
int count = 1;
|
||||||
|
Zahlenfilter z = new Zahlenfilter(upper_limit);
|
||||||
|
z.run(count);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
int limit;
|
||||||
|
|
||||||
|
private Zahlenfilter(int limit){
|
||||||
|
|
||||||
|
this.limit = limit;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void run(int c){
|
||||||
|
for(int i = 0; i < this.limit; i++){
|
||||||
|
int preC = c-1;
|
||||||
|
int ergebnis = c + preC;
|
||||||
|
int ergebnis2 = preC + c;
|
||||||
|
|
||||||
|
if (c % 5 == 0){
|
||||||
|
System.out.println(c + " ist durch 5 teilbar");
|
||||||
|
c++;
|
||||||
|
}else if(c % 10 == 9){
|
||||||
|
System.out.println(c + " endet auf 9");
|
||||||
|
c++;
|
||||||
|
}else if(ergebnis % 3 == 0 && ergebnis2 % 3 == 0){
|
||||||
|
System.out.println(c + " und " + preC + " addiert ergeben " + ergebnis + " und " + ergebnis2 + " ist durch 3 teilbar." );
|
||||||
|
c++;
|
||||||
|
}else{
|
||||||
|
System.out.println(c);
|
||||||
|
c++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
59
src/Praktikum03/TriangleChecker.java
Normal file
59
src/Praktikum03/TriangleChecker.java
Normal file
@ -0,0 +1,59 @@
|
|||||||
|
package Praktikum03;
|
||||||
|
|
||||||
|
import java.util.Scanner;
|
||||||
|
|
||||||
|
public class TriangleChecker {
|
||||||
|
|
||||||
|
public enum TriangleType {
|
||||||
|
NONE,
|
||||||
|
NORMAL,
|
||||||
|
ISOSCELES, // Gleichschenklig
|
||||||
|
EQUILATERAL // Gleichseitig
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Scanner s = new Scanner(System.in);
|
||||||
|
System.out.println("Geben Sie die Seitenlängen ein.");
|
||||||
|
float a = enterFloat(s, "a: ");
|
||||||
|
float b = enterFloat(s, "b: ");
|
||||||
|
float c = enterFloat(s, "c: ");
|
||||||
|
s.close();
|
||||||
|
printAnalysis(a, b, c);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Eingabe einer Seitenlänge
|
||||||
|
private static float enterFloat(Scanner s, String prompt) {
|
||||||
|
System.out.print(prompt);
|
||||||
|
return s.nextFloat();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Ausgabe der ermittelten Dreiecksart
|
||||||
|
private static void printAnalysis(float a, float b, float c) {
|
||||||
|
TriangleType type = checkTriangle(a, b, c);
|
||||||
|
switch (type) {
|
||||||
|
case NONE -> System.out.println("Kein Dreieck");
|
||||||
|
case NORMAL -> System.out.println("Dreieck");
|
||||||
|
case ISOSCELES -> System.out.println("Gleichschenkliges Dreieck");
|
||||||
|
case EQUILATERAL -> System.out.println("Gleichseitiges Dreieck");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Analyse der Dreiecksart
|
||||||
|
public static TriangleType checkTriangle(float a, float b, float c) {
|
||||||
|
|
||||||
|
|
||||||
|
if(a == b && b == c)
|
||||||
|
return TriangleType.EQUILATERAL;
|
||||||
|
if(a == b || b == c || a == c)
|
||||||
|
return TriangleType.ISOSCELES;
|
||||||
|
if(a + b < c || a + c < b || b + c < a)
|
||||||
|
return TriangleType.NONE;
|
||||||
|
if(a < 0 || b < 0 || c < 0)
|
||||||
|
return TriangleType.NONE;
|
||||||
|
|
||||||
|
return TriangleType.NORMAL;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
43
src/Praktikum03/Zahlenfilter.java
Normal file
43
src/Praktikum03/Zahlenfilter.java
Normal file
@ -0,0 +1,43 @@
|
|||||||
|
package Praktikum03;
|
||||||
|
|
||||||
|
|
||||||
|
public class Zahlenfilter {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
int upper_limit = 200;
|
||||||
|
if (args.length > 0)
|
||||||
|
upper_limit = Integer.parseInt(args[0]);
|
||||||
|
Zahlenfilter z = new Zahlenfilter(upper_limit);
|
||||||
|
z.printNumber();
|
||||||
|
}
|
||||||
|
|
||||||
|
private int boundary;
|
||||||
|
|
||||||
|
private Zahlenfilter(int limit){
|
||||||
|
boundary = limit;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void printNumber(){
|
||||||
|
for(int i = 1; i <=boundary; i++){
|
||||||
|
if(filter(i))
|
||||||
|
System.out.println(i);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static boolean filter(int i){
|
||||||
|
return !(checkDivisibleBy5(i) || checkEndsWith9(i) || checkSumDivisibleBy3(i, i-1));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static boolean checkDivisibleBy5(int i) {
|
||||||
|
return(i % 5 == 0);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public static boolean checkEndsWith9(int i){
|
||||||
|
return(i % 10 == 9);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static boolean checkSumDivisibleBy3(int i, int j){
|
||||||
|
return((i+j) % 3 == 0);
|
||||||
|
}
|
||||||
|
}
|
9
src/Praktikum03/helloworld/ErrorWriter.java
Normal file
9
src/Praktikum03/helloworld/ErrorWriter.java
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
package Praktikum03.helloworld;
|
||||||
|
|
||||||
|
public class ErrorWriter implements IHelloWorldWriter{
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void writeHelloWorld(){
|
||||||
|
System.err.println("Hello World");
|
||||||
|
}
|
||||||
|
}
|
7
src/Praktikum03/helloworld/Executor.java
Normal file
7
src/Praktikum03/helloworld/Executor.java
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
package Praktikum03.helloworld;
|
||||||
|
|
||||||
|
public class Executor{
|
||||||
|
public static void printHelloWorld(IHelloWorldWriter writer1, IHelloWorldWriter writer2, boolean second){
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
6
src/Praktikum03/helloworld/IHelloWorldWriter.java
Normal file
6
src/Praktikum03/helloworld/IHelloWorldWriter.java
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
package Praktikum03.helloworld;
|
||||||
|
|
||||||
|
public interface IHelloWorldWriter {
|
||||||
|
|
||||||
|
void writeHelloWorld();
|
||||||
|
}
|
9
src/Praktikum03/helloworld/OutWriter.java
Normal file
9
src/Praktikum03/helloworld/OutWriter.java
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
package Praktikum03.helloworld;
|
||||||
|
|
||||||
|
public class OutWriter implements IHelloWorldWriter{
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void writeHelloWorld(){
|
||||||
|
System.out.println("Hello World");
|
||||||
|
}
|
||||||
|
}
|
@ -1,4 +1,4 @@
|
|||||||
package Praktikum05;
|
package Prog3.src.Praktikum05;
|
||||||
|
|
||||||
import java.io.BufferedReader;
|
import java.io.BufferedReader;
|
||||||
import java.io.InputStreamReader;
|
import java.io.InputStreamReader;
|
||||||
|
@ -1,11 +1,11 @@
|
|||||||
package Praktikum05;
|
package Prog3.src.Praktikum05;
|
||||||
|
|
||||||
public class EscapeBot extends Bot{
|
public class EscapeBot extends Bot{
|
||||||
protected EscapeBot(String args[]){
|
protected EscapeBot(String args[]){
|
||||||
super(args);
|
super(args);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected char
|
||||||
|
|
||||||
public static void main(String args[]){
|
public static void main(String args[]){
|
||||||
Bot ebot = new EscapeBot(args);
|
Bot ebot = new EscapeBot(args);
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
package Praktikum05;
|
package Prog3.src.Praktikum05;
|
||||||
|
|
||||||
import java.util.Scanner;
|
import java.util.Scanner;
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user