├── README.md ├── .idea ├── .gitignore ├── vcs.xml ├── libraries │ └── Prog1Tools.xml ├── encodings.xml ├── misc.xml ├── modules.xml └── uiDesigner.xml ├── out └── production │ └── Prog1WINF-Examples │ ├── META-INF │ └── Prog1WINF-Examples.kotlin_module │ └── berlin │ └── tu │ └── mcc │ └── prog1 │ └── ws2526 │ └── Pseudocode.txt ├── src ├── de │ └── tuberlin │ │ └── mcc │ │ └── prog1winf │ │ ├── birdshow │ │ ├── CanFly.java │ │ ├── Bird.java │ │ └── Flugshow.java │ │ ├── generics │ │ ├── NutzungCollections.java │ │ └── NumberUtils.java │ │ ├── outlook │ │ ├── OtherClass.java │ │ └── Example.java │ │ ├── datastructures │ │ ├── BTreeNode.java │ │ ├── ListEntry.java │ │ ├── QueueWrapper.java │ │ ├── NumberUtils.java │ │ ├── BTreeTestclass.java │ │ ├── BTree.java │ │ ├── Factorial.java │ │ ├── BinTree.java │ │ ├── ListRecursiveIterative.java │ │ ├── BinSearch.java │ │ └── SinglyLinkedList.java │ │ ├── sockets │ │ └── SimpleServer.java │ │ ├── concurrency │ │ ├── SyncBlock.java │ │ ├── CounterThread.java │ │ ├── CounterRunnable.java │ │ ├── JoinedThread.java │ │ ├── NoWaitNotify.java │ │ └── WaitNotify.java │ │ └── threads │ │ ├── MyThreadSafeQueue.java │ │ ├── QueueFullException.java │ │ └── ThreadManagement.java └── berlin │ └── tu │ └── mcc │ └── prog1 │ ├── ws2324 │ ├── PremiumUser.java │ ├── Order.java │ ├── Shirt.java │ ├── Clothing.java │ ├── graph │ │ ├── TrainNetwork.java │ │ ├── Graph.java │ │ └── GraphNode.java │ ├── Product.java │ ├── Main.java │ ├── Jeans.java │ ├── ConditionalBranching.java │ ├── FirstClass.java │ └── User.java │ ├── ws2526 │ ├── Addierer.java │ ├── Pseudocode.txt │ ├── DumbArrayQueue.java │ ├── Prog1Queue.java │ ├── SlightySmarterArrayQueue.java │ ├── FirstClass.java │ └── ChessFieldOps.java │ ├── slides │ ├── exceptions │ │ ├── Exceptional.java │ │ ├── FinallySafe.java │ │ └── ExceptionallySafe.java │ └── oop │ │ ├── TestClassLoader.java │ │ ├── TestClassLoader2.java │ │ ├── SubClass.java │ │ ├── ApartmentHouse.java │ │ ├── Building.java │ │ ├── House.java │ │ └── innerclasses │ │ ├── Train.java │ │ └── Skyscraper.java │ ├── ws2425 │ ├── CoinChange.java │ ├── Pair.java │ ├── SecondClass.java │ ├── UsingGenerics.java │ └── FirstClass.java │ └── ws2223 │ └── FirstClass.java ├── .gitignore └── Prog1WINF-Examples.iml /README.md: -------------------------------------------------------------------------------- 1 | # prog1winf-examples -------------------------------------------------------------------------------- /.idea/.gitignore: -------------------------------------------------------------------------------- 1 | # Default ignored files 2 | /workspace.xml -------------------------------------------------------------------------------- /out/production/Prog1WINF-Examples/META-INF/Prog1WINF-Examples.kotlin_module: -------------------------------------------------------------------------------- 1 |  -------------------------------------------------------------------------------- /src/de/tuberlin/mcc/prog1winf/birdshow/CanFly.java: -------------------------------------------------------------------------------- 1 | package de.tuberlin.mcc.prog1winf.birdshow; 2 | 3 | public interface CanFly { 4 | 5 | public void fly(); 6 | 7 | } 8 | -------------------------------------------------------------------------------- /src/de/tuberlin/mcc/prog1winf/generics/NutzungCollections.java: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dbermbach/prog1winf-examples/HEAD/src/de/tuberlin/mcc/prog1winf/generics/NutzungCollections.java -------------------------------------------------------------------------------- /.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /.idea/libraries/Prog1Tools.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /.idea/encodings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Compiled class file 2 | *.class 3 | 4 | # Log file 5 | *.log 6 | 7 | # BlueJ files 8 | *.ctxt 9 | 10 | # Mobile Tools for Java (J2ME) 11 | .mtj.tmp/ 12 | 13 | # Package Files # 14 | *.jar 15 | *.war 16 | *.nar 17 | *.ear 18 | *.zip 19 | *.tar.gz 20 | *.rar 21 | 22 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml 23 | hs_err_pid* 24 | -------------------------------------------------------------------------------- /src/de/tuberlin/mcc/prog1winf/outlook/OtherClass.java: -------------------------------------------------------------------------------- 1 | package de.tuberlin.mcc.prog1winf.outlook; 2 | 3 | public class OtherClass { 4 | 5 | 6 | public class InnerClass{ 7 | 8 | void foo(){ 9 | 10 | } 11 | 12 | } 13 | 14 | public static class StaticInnerClass{ 15 | 16 | } 17 | 18 | } 19 | 20 | 21 | abstract class AbstractClass{ 22 | abstract void foobar(); 23 | } -------------------------------------------------------------------------------- /src/berlin/tu/mcc/prog1/ws2324/PremiumUser.java: -------------------------------------------------------------------------------- 1 | package berlin.tu.mcc.prog1.ws2324; 2 | 3 | public class PremiumUser extends User { 4 | 5 | protected PremiumUser(String username, String email, String password) { 6 | super(username, email, password); 7 | } 8 | 9 | @Override 10 | public void sendtext(String message) { 11 | System.out.println("Liebe/r " + getUsername() + ", " + message); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /src/de/tuberlin/mcc/prog1winf/datastructures/BTreeNode.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | */ 4 | package de.tuberlin.mcc.prog1winf.datastructures; 5 | 6 | /** 7 | * @author Dave 8 | * 9 | */ 10 | public class BTreeNode { 11 | 12 | public int value; 13 | 14 | public BTreeNode left; 15 | 16 | public BTreeNode right; 17 | 18 | /** 19 | * @param value 20 | */ 21 | public BTreeNode(int value) { 22 | super(); 23 | this.value = value; 24 | } 25 | 26 | } 27 | -------------------------------------------------------------------------------- /src/berlin/tu/mcc/prog1/ws2526/Addierer.java: -------------------------------------------------------------------------------- 1 | package berlin.tu.mcc.prog1.ws2526; 2 | 3 | import Prog1Tools.IOTools; 4 | 5 | public class Addierer { 6 | 7 | public static void main(String[] args) { 8 | int zahl1 = IOTools.readInt("bitte Zahl 1 eingeben:"); 9 | int zahl2 = IOTools.readInt("bitte Zahl 2 eingeben:"); 10 | System.out.println("Die Summe von " + zahl1 + " und " + zahl2 + " ist " + (zahl1 + zahl2)); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /src/berlin/tu/mcc/prog1/ws2324/Order.java: -------------------------------------------------------------------------------- 1 | package berlin.tu.mcc.prog1.ws2324; 2 | 3 | public class Order { 4 | 5 | private User user; 6 | private Product[] products; 7 | 8 | public Order(User user, Product... products) { 9 | this.user = user; 10 | this.products = products; 11 | } 12 | 13 | public User getUser() { 14 | return user; 15 | } 16 | 17 | public Product[] getProducts() { 18 | return products; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /src/berlin/tu/mcc/prog1/ws2324/Shirt.java: -------------------------------------------------------------------------------- 1 | package berlin.tu.mcc.prog1.ws2324; 2 | 3 | public class Shirt extends Clothing{ 4 | private String size; 5 | 6 | public Shirt(String description, int price) { 7 | super(description, price); 8 | } 9 | 10 | public String getSize() { 11 | return size; 12 | } 13 | 14 | public void setSize(String size) { 15 | if (size != null) { 16 | this.size = size; 17 | } 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /src/de/tuberlin/mcc/prog1winf/datastructures/ListEntry.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | */ 4 | package de.tuberlin.mcc.prog1winf.datastructures; 5 | 6 | /** 7 | * @author David Bermbach 8 | * 9 | * created on 24.10.2014 10 | */ 11 | public class ListEntry { 12 | 13 | /**value of this list entry*/ 14 | public Object value; 15 | 16 | /**reference to the successor of this list entry*/ 17 | public ListEntry next; 18 | // add "ListEntry prev;" for doubly linked list 19 | 20 | 21 | } 22 | -------------------------------------------------------------------------------- /src/berlin/tu/mcc/prog1/ws2324/Clothing.java: -------------------------------------------------------------------------------- 1 | package berlin.tu.mcc.prog1.ws2324; 2 | 3 | public class Clothing extends Product{ 4 | 5 | private String color; 6 | 7 | public Clothing(String description, int price) { 8 | super(description, price); 9 | } 10 | 11 | public String getColor() { 12 | return color; 13 | } 14 | 15 | public void setColor(String color) { 16 | if (color != null) { 17 | this.color = color; 18 | } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /src/de/tuberlin/mcc/prog1winf/datastructures/QueueWrapper.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | */ 4 | package de.tuberlin.mcc.prog1winf.datastructures; 5 | 6 | /** 7 | * @author Dave 8 | * 9 | */ 10 | public class QueueWrapper { 11 | 12 | private SinglyLinkedList list = new SinglyLinkedList(); 13 | 14 | public void enqueue(String s) { 15 | list.append(s); 16 | } 17 | 18 | public String dequeue() { 19 | Object o = list.getValue(0); 20 | if (o == null) 21 | return null; 22 | list.deleteValue(o); 23 | return (String) o; 24 | } 25 | 26 | } 27 | -------------------------------------------------------------------------------- /Prog1WINF-Examples.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /src/berlin/tu/mcc/prog1/ws2526/Pseudocode.txt: -------------------------------------------------------------------------------- 1 | 1) Input ist eine ungerade Anzahl an Zahlen. Finde den Wert, für den gilt, dass 2 | die Hälfte der Zahlen größer und die andere Hälfte kleiner als er ist (Median). 3 | 2) Input ist eine Menge an Zahlen. Finde den häufigsten Wert oder melde 'unentschieden'. 4 | 3) Ziehe ein Spannbettlaken auf ein 1,40m-Bett. 5 | 4) Input ist eine Menge an drei Tönen. Identifiziere, ob und um welchen Dur-, Moll- oder 6 | verminderten Akkord es sich handelt, oder ob es sich um einen übermäßigen Akkord handelt. 7 | Gib sonst 'unknown' zurück. -------------------------------------------------------------------------------- /src/berlin/tu/mcc/prog1/slides/exceptions/Exceptional.java: -------------------------------------------------------------------------------- 1 | package berlin.tu.mcc.prog1.slides.exceptions; 2 | 3 | public class Exceptional { 4 | 5 | public static void main(String[] args) { 6 | imDevastated(); 7 | } 8 | 9 | static void imDevastated() { 10 | howCouldThisHappen(); 11 | } 12 | 13 | static void howCouldThisHappen() { 14 | ohNoSomethingWentWrong(); 15 | } 16 | 17 | static void ohNoSomethingWentWrong() { 18 | Exception e = null; 19 | e.printStackTrace(); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /src/berlin/tu/mcc/prog1/slides/oop/TestClassLoader.java: -------------------------------------------------------------------------------- 1 | package berlin.tu.mcc.prog1.slides.oop; 2 | 3 | public class TestClassLoader { 4 | static Helper helper1 = new Helper("helper1"); 5 | 6 | static { 7 | System.out.println("static block"); 8 | } 9 | 10 | static Helper helper2 = new Helper("helper2"); 11 | 12 | public static void main(String[] args) { 13 | System.out.println("main"); 14 | } 15 | 16 | } 17 | 18 | 19 | class Helper { 20 | Helper(String s) { 21 | System.out.println(s); 22 | } 23 | } -------------------------------------------------------------------------------- /out/production/Prog1WINF-Examples/berlin/tu/mcc/prog1/ws2526/Pseudocode.txt: -------------------------------------------------------------------------------- 1 | 1) Input ist eine ungerade Anzahl an Zahlen. Finde den Wert, für den gilt, dass 2 | die Hälfte der Zahlen größer und die andere Hälfte kleiner als er ist (Median). 3 | 2) Input ist eine Menge an Zahlen. Finde den häufigsten Wert oder melde 'unentschieden'. 4 | 3) Ziehe ein Spannbettlaken auf ein 1,40m-Bett. 5 | 4) Input ist eine Menge an drei Tönen. Identifiziere, ob und um welchen Dur-, Moll- oder 6 | verminderten Akkord es sich handelt, oder ob es sich um einen übermäßigen Akkord handelt. 7 | Gib sonst 'unknown' zurück. -------------------------------------------------------------------------------- /src/de/tuberlin/mcc/prog1winf/sockets/SimpleServer.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | */ 4 | package de.tuberlin.mcc.prog1winf.sockets; 5 | 6 | import java.net.ServerSocket; 7 | import java.net.Socket; 8 | 9 | /** 10 | * @author Dave 11 | * 12 | */ 13 | public class SimpleServer { 14 | 15 | public static void main(String[] args) throws Exception{ 16 | ServerSocket server = new ServerSocket(8082); 17 | Socket client = server.accept(); 18 | 19 | int i; 20 | while ((i = client.getInputStream().read()) != -1) { 21 | System.out.write(i); 22 | } 23 | server.close(); 24 | 25 | } 26 | 27 | } 28 | -------------------------------------------------------------------------------- /src/de/tuberlin/mcc/prog1winf/concurrency/SyncBlock.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | */ 4 | package de.tuberlin.mcc.prog1winf.concurrency; 5 | 6 | import java.util.ArrayList; 7 | import java.util.List; 8 | 9 | /** 10 | * @author David Bermbach 11 | * 12 | * created on 23.12.2014 13 | */ 14 | public class SyncBlock { 15 | 16 | List stringList = new ArrayList<>(); 17 | 18 | void add(String s) { 19 | synchronized (stringList) { 20 | stringList.add(s); 21 | } 22 | } 23 | 24 | void print() { 25 | synchronized (stringList) { 26 | for (String s : stringList) 27 | System.out.println(s); 28 | } 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /src/de/tuberlin/mcc/prog1winf/generics/NumberUtils.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | */ 4 | package de.tuberlin.mcc.prog1winf.generics; 5 | 6 | import java.util.ArrayList; 7 | import java.util.List; 8 | 9 | /** 10 | * @author Dave 11 | * 12 | */ 13 | public class NumberUtils { 14 | 15 | public double sum(List numberList) { 16 | double sum = 0; 17 | for (Number n : numberList) 18 | sum += n.doubleValue(); 19 | return sum; 20 | } 21 | 22 | public static void main(String[] args) { 23 | NumberUtils n = new NumberUtils(); 24 | List l = new ArrayList<>(); 25 | for (int i = 0; i < 10; i++) { 26 | l.add(i*1.0); 27 | } 28 | n.sum(l); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /src/berlin/tu/mcc/prog1/slides/exceptions/FinallySafe.java: -------------------------------------------------------------------------------- 1 | package berlin.tu.mcc.prog1.slides.exceptions; 2 | 3 | public class FinallySafe { 4 | 5 | public static void main(String[] args) { 6 | handleExceptions(); 7 | } 8 | 9 | public static void handleExceptions() { 10 | try { 11 | Exceptional.imDevastated(); 12 | System.out.println("This statement is never reached."); 13 | } catch (Exception e) { 14 | System.out.println("Gotcha, exception!"); 15 | } finally{ 16 | System.out.println("Finally, cleaned up this mess."); 17 | } 18 | System.out.println("It didn't crash, heureka!"); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /src/de/tuberlin/mcc/prog1winf/datastructures/NumberUtils.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | */ 4 | package de.tuberlin.mcc.prog1winf.datastructures; 5 | 6 | import java.util.ArrayList; 7 | import java.util.List; 8 | 9 | /** 10 | * @author Dave 11 | * 12 | */ 13 | public class NumberUtils { 14 | 15 | public double sum(List numberList) { 16 | double sum = 0; 17 | for (Number n : numberList) 18 | sum += n.doubleValue(); 19 | return sum; 20 | } 21 | 22 | public static void main(String[] args) { 23 | NumberUtils n = new NumberUtils(); 24 | List l = new ArrayList(); 25 | for (int i = 0; i < 10; i++) { 26 | l.add(i*1.0); 27 | } 28 | n.sum(l); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /src/berlin/tu/mcc/prog1/slides/exceptions/ExceptionallySafe.java: -------------------------------------------------------------------------------- 1 | package berlin.tu.mcc.prog1.slides.exceptions; 2 | 3 | public class ExceptionallySafe { 4 | 5 | public static void main(String[] args) { 6 | handleExceptions(); 7 | } 8 | 9 | public static void handleExceptions() { 10 | try { 11 | Exceptional.imDevastated(); 12 | System.out.println("This statement is never reached."); 13 | } catch (Exception e) { 14 | System.out.println("I got your back, you're safe now. " 15 | + "But stop calling methods on null references."); 16 | } 17 | System.out.println("It didn't crash, heureka!"); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /src/berlin/tu/mcc/prog1/slides/oop/TestClassLoader2.java: -------------------------------------------------------------------------------- 1 | package berlin.tu.mcc.prog1.slides.oop; 2 | 3 | public class TestClassLoader2 extends SuperClass { 4 | static Helper helper1 = new Helper("helper"); 5 | 6 | static { 7 | System.out.println("static"); 8 | } 9 | 10 | public static void main(String[] args) { 11 | System.out.println("main"); 12 | Child c = new Child(); 13 | } 14 | 15 | } 16 | 17 | class SuperClass { 18 | static { 19 | System.out.println("super static"); 20 | } 21 | 22 | static Helper superHelper = new Helper("super"); 23 | } 24 | 25 | class Child extends SuperClass { 26 | static Helper childHelper = new Helper("child"); 27 | } 28 | 29 | 30 | -------------------------------------------------------------------------------- /src/berlin/tu/mcc/prog1/ws2425/CoinChange.java: -------------------------------------------------------------------------------- 1 | package berlin.tu.mcc.prog1.ws2425; 2 | 3 | import Prog1Tools.IOTools; 4 | 5 | public class CoinChange { 6 | 7 | 8 | 9 | public static void main(String[] args) { 10 | double[] coins = {0.01, 0.02, 0.05, 0.1, 0.2, 0.5, 1, 2}; 11 | double amount = IOTools.readDouble("For which amount shall we calculate the change:"); 12 | double rest = amount; 13 | for(int i = coins.length-1;i>=0;i--){ 14 | int noOfCoins =0; 15 | while(rest>=coins[i]){ 16 | rest -=coins[i]; 17 | noOfCoins++; 18 | } 19 | System.out.println(noOfCoins+"x"+coins[i]+ "EUR"); 20 | } 21 | } 22 | 23 | } 24 | -------------------------------------------------------------------------------- /src/de/tuberlin/mcc/prog1winf/threads/MyThreadSafeQueue.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | */ 4 | package de.tuberlin.mcc.prog1winf.threads; 5 | 6 | import java.util.ArrayList; 7 | import java.util.List; 8 | 9 | /** 10 | * @author Dave 11 | * 12 | */ 13 | public class MyThreadSafeQueue { 14 | 15 | private List list = new ArrayList<>(10); 16 | 17 | 18 | public void enqueue(T t) throws QueueFullException { 19 | synchronized (list) { 20 | if (list.size() >= 10) 21 | throw new QueueFullException("voll!"); 22 | list.add(t); 23 | } 24 | } 25 | 26 | public T dequeue() { 27 | synchronized (list) { 28 | if (list.size() > 0) 29 | return list.remove(0); 30 | else 31 | return null; 32 | } 33 | } 34 | 35 | } 36 | -------------------------------------------------------------------------------- /src/de/tuberlin/mcc/prog1winf/datastructures/BTreeTestclass.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | */ 4 | package de.tuberlin.mcc.prog1winf.datastructures; 5 | 6 | import java.util.ArrayList; 7 | import java.util.List; 8 | 9 | /** 10 | * @author Dave 11 | * 12 | */ 13 | public class BTreeTestclass { 14 | 15 | /** 16 | * @param args 17 | */ 18 | public static void main(String[] args) { 19 | BTree tree = new BTree(); 20 | tree.insert(3); 21 | tree.insert(37); 22 | tree.insert(4); 23 | 24 | 25 | //generics examples (no connection to BTree) 26 | List list = new ArrayList(); 27 | BTree[] array = list.toArray(new BTree[list.size()]); 28 | Object[] array2 = list.toArray(); 29 | 30 | List abc = new ArrayList(); 31 | 32 | 33 | } 34 | 35 | } 36 | -------------------------------------------------------------------------------- /src/berlin/tu/mcc/prog1/ws2324/graph/TrainNetwork.java: -------------------------------------------------------------------------------- 1 | package berlin.tu.mcc.prog1.ws2324.graph; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | public class TrainNetwork { 7 | 8 | public static void main(String[] args) { 9 | Graph network = new Graph<>(); 10 | network.createAndConnectNode("Zoo", asList("Bahnhof Zoo"), new ArrayList<>()); 11 | network.createAndConnectNode("Tiergarten", asList("Neben der TU"), asList("Zoo")); 12 | network.createAndConnectNode("Savignyplatz", asList("Restaurants"), asList("Zoo")); 13 | 14 | System.out.println(network); 15 | 16 | } 17 | 18 | 19 | 20 | 21 | private static List asList(String str) { 22 | List res = new ArrayList(); 23 | res.add(str); 24 | return res; 25 | } 26 | 27 | } 28 | -------------------------------------------------------------------------------- /src/berlin/tu/mcc/prog1/slides/oop/SubClass.java: -------------------------------------------------------------------------------- 1 | package berlin.tu.mcc.prog1.slides.oop; 2 | 3 | public class SubClass extends ParentClass { 4 | Helper hs = new Helper("instance sub"); 5 | 6 | SubClass(){ 7 | this("this call sub"); 8 | System.out.println("back in sub"); 9 | } 10 | 11 | SubClass(String s){ 12 | System.out.println(s); 13 | } 14 | } 15 | 16 | class ParentClass extends GrandParentClass{ 17 | Helper hp = new Helper("instance parent"); 18 | Helper hp2 = new Helper("instance parent2"); 19 | 20 | ParentClass(){ 21 | System.out.println("parent"); 22 | } 23 | 24 | } 25 | 26 | class GrandParentClass{ 27 | GrandParentClass(){ 28 | System.out.println("grandparent"); 29 | } 30 | } 31 | 32 | class Main { 33 | public static void main(String[] args) { 34 | SubClass sc = new SubClass(); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /src/de/tuberlin/mcc/prog1winf/concurrency/CounterThread.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | */ 4 | package de.tuberlin.mcc.prog1winf.concurrency; 5 | 6 | /** 7 | * @author David Bermbach 8 | * 9 | * created on 22.12.2014 10 | */ 11 | public class CounterThread extends Thread { 12 | 13 | 14 | 15 | /* (non-Javadoc) 16 | * @see java.lang.Runnable#run() 17 | */ 18 | @Override 19 | public void run() { 20 | for (int i = 1; i < 11; i++) { 21 | System.out.println("I am " + Thread.currentThread().getName() 22 | + " and my number is " + i); 23 | try { 24 | Thread.sleep((long) (Math.random() * 100)); 25 | } catch (InterruptedException e) { 26 | this.interrupt(); 27 | } 28 | } 29 | } 30 | 31 | /** 32 | * @param args 33 | */ 34 | public static void main(String[] args) { 35 | new CounterThread().start(); 36 | new CounterThread().start(); 37 | } 38 | 39 | 40 | 41 | } 42 | -------------------------------------------------------------------------------- /src/de/tuberlin/mcc/prog1winf/concurrency/CounterRunnable.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | */ 4 | package de.tuberlin.mcc.prog1winf.concurrency; 5 | 6 | /** 7 | * @author David Bermbach 8 | * 9 | * created on 22.12.2014 10 | */ 11 | public class CounterRunnable implements Runnable { 12 | 13 | /* 14 | * (non-Javadoc) 15 | * 16 | * @see java.lang.Thread#run() 17 | */ 18 | @Override 19 | public void run() { 20 | for (int i = 1; i < 11; i++) { 21 | System.out.println("I am " + Thread.currentThread().getName() 22 | + " and my number is " + i); 23 | try { 24 | Thread.sleep((long) (Math.random() * 100)); 25 | } catch (InterruptedException e) { 26 | Thread.currentThread().interrupt(); 27 | } 28 | } 29 | } 30 | 31 | /** 32 | * @param args 33 | */ 34 | public static void main(String[] args) { 35 | new Thread(new CounterRunnable()).start(); 36 | new Thread(new CounterRunnable()).start(); 37 | } 38 | 39 | } 40 | -------------------------------------------------------------------------------- /src/berlin/tu/mcc/prog1/ws2324/Product.java: -------------------------------------------------------------------------------- 1 | package berlin.tu.mcc.prog1.ws2324; 2 | 3 | public class Product { 4 | protected String description; 5 | protected int price; 6 | 7 | public Product(String description, int price) { 8 | this.description = description; 9 | this.price = price; 10 | } 11 | 12 | public String getDescription() { 13 | return description; 14 | } 15 | 16 | public void setDescription(String description) { 17 | if (description != null) { 18 | this.description = description; 19 | } 20 | } 21 | 22 | public int getPrice() { 23 | return price; 24 | } 25 | 26 | public void setPrice(int price) { 27 | this.price = price; 28 | } 29 | 30 | @Override 31 | public String toString() { 32 | return "description='" + description + '\'' + 33 | ", price=" + price + 34 | '}'; 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /src/berlin/tu/mcc/prog1/ws2324/Main.java: -------------------------------------------------------------------------------- 1 | package berlin.tu.mcc.prog1.ws2324; 2 | 3 | 4 | public class Main { 5 | 6 | public static void main(String[] args) { 7 | 8 | User u1 = new User("Martin", "grambow@tu-berlin.de", "geheim"); 9 | User u2 = new User("Martin", "grambow@tu-berlin.de", "geheim"); 10 | User u3 = new User("Martin", "grambow@tu-berlin.de", "geheim"); 11 | 12 | if (u1.equals(u2) && u2.equals(u3)) { 13 | System.out.println("sind gleich"); 14 | } else { 15 | System.out.println("nicht gleich"); 16 | } 17 | 18 | 19 | Shirt s1 = new Shirt("Ein Shirt", 5000); 20 | s1.setSize("XL"); 21 | 22 | Jeans j1 = new Jeans("Eine Jeans", 8000, 34); 23 | j1.setLength(34); 24 | j1.setColor("black"); 25 | j1.setColor("orange"); 26 | 27 | Order o = new Order(u1, s1, j1); 28 | System.out.println(o.getProducts()[1]); 29 | 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /src/de/tuberlin/mcc/prog1winf/datastructures/BTree.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | */ 4 | package de.tuberlin.mcc.prog1winf.datastructures; 5 | 6 | /** 7 | * @author Dave 8 | * 9 | */ 10 | public class BTree { 11 | 12 | private BTreeNode root; 13 | 14 | public void insert(int value){ 15 | if(root==null){ 16 | this.root = new BTreeNode(value); 17 | return; 18 | } 19 | insert(root,value); 20 | } 21 | 22 | private void insert(BTreeNode node, int value) { 23 | if (node.value > value) { 24 | // links 25 | if (node.left == null) { 26 | // hier einfuegen 27 | node.left = new BTreeNode(value); 28 | return; 29 | } else { 30 | // an Kindknoten delegieren 31 | insert(node.left, value); 32 | } 33 | 34 | } else if (node.value < value) { 35 | // rechts 36 | if (node.right == null) { 37 | node.right = new BTreeNode(value); 38 | return; 39 | } else { 40 | insert(node.right, value); 41 | } 42 | } else { 43 | return; 44 | } 45 | } 46 | 47 | } 48 | -------------------------------------------------------------------------------- /src/berlin/tu/mcc/prog1/ws2324/Jeans.java: -------------------------------------------------------------------------------- 1 | package berlin.tu.mcc.prog1.ws2324; 2 | 3 | public class Jeans extends Clothing{ 4 | private int length; 5 | private String fit; 6 | 7 | public Jeans(String description, int price, int length) { 8 | super(description, price); 9 | this.length = length; 10 | } 11 | 12 | public Jeans(String description) { 13 | super(description, 2300); 14 | } 15 | 16 | public int getLength() { 17 | return length; 18 | } 19 | 20 | public void setLength(int length) { 21 | this.length = length; 22 | } 23 | 24 | public String getFit() { 25 | return fit; 26 | } 27 | 28 | public void setFit(String fit) { 29 | if (fit != null) { 30 | this.fit = fit; 31 | } 32 | } 33 | 34 | @Override 35 | public void setColor(String color) { 36 | if ("blue".equals(color) || "black".equals(color)) { 37 | super.setColor(color); 38 | } 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /src/berlin/tu/mcc/prog1/slides/oop/ApartmentHouse.java: -------------------------------------------------------------------------------- 1 | package berlin.tu.mcc.prog1.slides.oop; 2 | 3 | public class ApartmentHouse { 4 | Heating heating = new GasBoiler(); 5 | 6 | void heat(){ 7 | heating.heat(); 8 | } 9 | 10 | public static void main(String[] args) { 11 | ApartmentHouse apt = new ApartmentHouse(); 12 | apt.heat(); 13 | apt.heating = new HeatPump(); 14 | apt.heat(); 15 | apt.heating = new Heating() { 16 | @Override 17 | void heat() { 18 | System.out.println("I'm an anonymous mystery heater!"); 19 | } 20 | }; 21 | 22 | } 23 | } 24 | 25 | abstract class Heating { 26 | abstract void heat(); 27 | } 28 | 29 | class HeatPump extends Heating{ 30 | @Override 31 | void heat() { 32 | System.out.println("I'm clean."); 33 | } 34 | } 35 | 36 | class GasBoiler extends Heating{ 37 | @Override 38 | void heat() { 39 | System.out.println("I'm so dirty."); 40 | } 41 | } -------------------------------------------------------------------------------- /src/berlin/tu/mcc/prog1/ws2324/graph/Graph.java: -------------------------------------------------------------------------------- 1 | package berlin.tu.mcc.prog1.ws2324.graph; 2 | 3 | 4 | import java.util.HashMap; 5 | import java.util.List; 6 | import java.util.Map; 7 | 8 | public class Graph { 9 | 10 | private final Map> nodes = new HashMap<>(); 11 | 12 | public GraphNode getNodeForIdentifier(T o) { 13 | return nodes.get(o); 14 | } 15 | 16 | public void createAndConnectNode(T identifier, List values, List neighbors) { 17 | GraphNode node = new GraphNode<>(identifier, values); 18 | for (T id : neighbors) { 19 | GraphNode other = getNodeForIdentifier(id); 20 | if (other != null) node.connectTo(other); 21 | else System.out.println("Node for identifier " + id + " is unknown."); 22 | } 23 | nodes.put(identifier,node); 24 | } 25 | 26 | @Override 27 | public String toString() { 28 | return "Graph{" + 29 | "nodes=" + nodes + 30 | '}'; 31 | } 32 | } 33 | 34 | -------------------------------------------------------------------------------- /src/berlin/tu/mcc/prog1/slides/oop/Building.java: -------------------------------------------------------------------------------- 1 | package berlin.tu.mcc.prog1.slides.oop; 2 | 3 | public abstract class Building { 4 | 5 | void repair(){ 6 | replaceRoof(); 7 | paint(); 8 | } 9 | 10 | abstract void replaceRoof(); 11 | abstract void paint(); 12 | } 13 | 14 | class Tower extends Building { 15 | @Override 16 | void replaceRoof() { 17 | System.out.println("Need a helicopter."); 18 | } 19 | 20 | @Override 21 | void paint() { 22 | System.out.println("Have fun climbing!"); 23 | } 24 | } 25 | 26 | class Kennel extends Building { 27 | @Override 28 | void replaceRoof() { 29 | System.out.println("Need some wood."); 30 | } 31 | 32 | @Override 33 | void paint() { 34 | System.out.println("Don't get bitten!"); 35 | } 36 | } 37 | 38 | 39 | class MainClass{ 40 | public static void main(String[] args) { 41 | Tower t = new Tower(); 42 | Building b = new Kennel(); 43 | t.repair(); 44 | b.repair(); 45 | t.paint(); 46 | b.paint(); 47 | } 48 | } -------------------------------------------------------------------------------- /src/berlin/tu/mcc/prog1/ws2425/Pair.java: -------------------------------------------------------------------------------- 1 | package berlin.tu.mcc.prog1.ws2425; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | public class Pair { 7 | 8 | T first; 9 | U second; 10 | 11 | 12 | public Pair(T first, U second) { 13 | this.first = first; 14 | this.second = second; 15 | 16 | } 17 | } 18 | 19 | 20 | 21 | 22 | 23 | class IdenticalPair extends Pair{ 24 | 25 | public IdenticalPair(T first, T second){ 26 | super(first,second); 27 | } 28 | } 29 | 30 | class MyMainClass{ 31 | public static void main(String[] args) { 32 | Pair p = new Pair<>("hello world", 123); 33 | Pair p2 = new Pair<>(123.0,123.0); 34 | System.out.println("p: "+p.first.getClass().getSimpleName() + "-" + p.second.getClass().getSimpleName()); 35 | System.out.println("p2: " + p2.first.getClass().getSimpleName() + "-" + p2.second.getClass().getSimpleName()); 36 | List> pairlist = new ArrayList<>(); 37 | //pairlist.add(p2); //geht nicht 38 | pairlist.add(p); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /src/berlin/tu/mcc/prog1/ws2425/SecondClass.java: -------------------------------------------------------------------------------- 1 | package berlin.tu.mcc.prog1.ws2425; 2 | 3 | public class SecondClass { 4 | 5 | public static void main(String[] args) { 6 | System.out.println(add(3, 4)); 7 | int something = add(add(3, 4), add(3 * 18 / 12, 4)); 8 | printNumber(123); 9 | addNumbers(1); 10 | addNumbers(1,2,3,4,5,6,7,8,1,add(1,2)); 11 | addNumbers(new int[]{1,2,3}); 12 | addNumbers(); 13 | } 14 | 15 | public static int add(int a, int b) { 16 | return a + b; 17 | } 18 | // double double 19 | // int long 20 | // long int 21 | // int int 22 | 23 | 24 | public static int add(int a, int b, int c){ 25 | return addNumbers(a,b,c); 26 | } 27 | 28 | public static double add(double a, double b){ 29 | return a+b; 30 | } 31 | 32 | public static int addNumbers(int...a) { 33 | int res = 0; 34 | for (int i : a) res += i; 35 | return res; 36 | } 37 | 38 | public static void printNumber(int a) { 39 | if (a == 42) return; 40 | System.out.println("I love " + a); 41 | } 42 | 43 | } 44 | -------------------------------------------------------------------------------- /src/de/tuberlin/mcc/prog1winf/concurrency/JoinedThread.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | */ 4 | package de.tuberlin.mcc.prog1winf.concurrency; 5 | 6 | /** 7 | * @author David Bermbach 8 | * 9 | * created on 22.12.2014 10 | */ 11 | public class JoinedThread extends Thread { 12 | 13 | 14 | /* 15 | * (non-Javadoc) 16 | * 17 | * @see java.lang.Thread#run() 18 | */ 19 | @Override 20 | public void run() { 21 | System.out.println(getName() + ": Sleeping now..."); 22 | try { 23 | Thread.sleep(1000); 24 | } catch (InterruptedException e) { 25 | this.interrupt(); 26 | } 27 | System.out.println(getName() + ": I'm done."); 28 | } 29 | 30 | /** 31 | * @param args 32 | * @throws InterruptedException 33 | */ 34 | public static void main(String[] args) throws InterruptedException { 35 | JoinedThread jt = new JoinedThread(); 36 | System.out.println(Thread.currentThread().getName() 37 | + ": Ready to start."); 38 | jt.start(); 39 | System.out.println(Thread.currentThread().getName() 40 | + ": Ready to join."); 41 | jt.join(); 42 | System.out.println(Thread.currentThread().getName() + ": He's done!"); 43 | 44 | } 45 | 46 | } 47 | -------------------------------------------------------------------------------- /src/berlin/tu/mcc/prog1/ws2425/UsingGenerics.java: -------------------------------------------------------------------------------- 1 | package berlin.tu.mcc.prog1.ws2425; 2 | 3 | import java.util.ArrayList; 4 | import java.util.HashMap; 5 | import java.util.List; 6 | import java.util.Map; 7 | 8 | public class UsingGenerics { 9 | public static void main(String[] args) { 10 | Map students = new HashMap<>(); 11 | Student jane = new Student("Jane Doe", 42); 12 | Student john = new Student("john doe", 123); 13 | students.put(jane.id,jane); 14 | students.put(john.id, john); 15 | System.out.println("List of students"); 16 | System.out.println(students); 17 | System.out.println("Pretty list of students:"); 18 | for(int i: students.keySet()){ 19 | System.out.println("ID " + i + ": " + students.get(i).name); 20 | } 21 | List allIDs = new ArrayList<>(); 22 | allIDs.addAll(students.keySet()); 23 | System.out.println("All student IDs: " + allIDs); 24 | 25 | } 26 | } 27 | 28 | class Student{ 29 | String name; 30 | int id; 31 | 32 | public Student(String name, int id) { 33 | this.name = name; 34 | this.id = id; 35 | } 36 | } -------------------------------------------------------------------------------- /src/berlin/tu/mcc/prog1/ws2223/FirstClass.java: -------------------------------------------------------------------------------- 1 | package berlin.tu.mcc.prog1.ws2223; 2 | 3 | public class FirstClass { 4 | 5 | public static void main(String[] args) { 6 | System.out.println("Hello Wo\nrld!"); 7 | 8 | int i = 5; 9 | int j; 10 | j = (5 + i) * (8 - 5); 11 | i = j / 20; 12 | j = j % 20; 13 | //System.out.println("i = "+i); 14 | System.out.println("j = " + j + " und das ist total toll oO"); 15 | String s = "i = " + i; 16 | System.out.println(s); 17 | System.out.println(10.0 / 4); 18 | System.out.println(i / j + " vs. nicht-ganzzahlig: " + (i / (double) j)); 19 | 20 | i = 1; 21 | j = 1; 22 | int a = i++; 23 | int b = ++j; 24 | System.out.println("a=" + a + ", b=" + b); 25 | --i; 26 | i--; 27 | System.out.println(i == a); 28 | /* 29 | ungleich 3!=a 30 | groesser i>a 31 | kleiner i=j 33 | kleiner gleich j<=a 34 | !true 35 | 36 | boolean1 boolean2 37 | boolean1 | boolean2 38 | */ 39 | 40 | 41 | } 42 | 43 | 44 | 45 | 46 | } 47 | -------------------------------------------------------------------------------- /src/berlin/tu/mcc/prog1/slides/oop/House.java: -------------------------------------------------------------------------------- 1 | package berlin.tu.mcc.prog1.slides.oop; 2 | 3 | public abstract class House { 4 | abstract void moveIn(); 5 | } 6 | 7 | interface CanSwim { 8 | void swim(); 9 | } 10 | 11 | class HouseBoat extends House implements CanSwim { 12 | @Override 13 | public void swim() { 14 | System.out.println("I am sailing!"); 15 | } 16 | 17 | @Override 18 | void moveIn() { 19 | System.out.println("Home sweet home!"); 20 | } 21 | 22 | public static void main(String[] args) { 23 | HouseBoat hb = new HouseBoat(); 24 | hb.moveIn(); 25 | hb.swim(); 26 | } 27 | } 28 | 29 | class Duck implements CanSwim { 30 | @Override 31 | public void swim() { 32 | System.out.println("quack!"); 33 | } 34 | } 35 | 36 | class Fish implements CanSwim { 37 | @Override 38 | public void swim() { 39 | System.out.println("blub."); 40 | } 41 | } 42 | 43 | class Lake { 44 | 45 | void letThemSwim(CanSwim... swimmers) { 46 | for (CanSwim cs : swimmers) cs.swim(); 47 | } 48 | 49 | public static void main(String[] args) { 50 | Lake lake = new Lake(); 51 | lake.letThemSwim(new HouseBoat(), new Duck(), new Duck()); 52 | } 53 | } 54 | 55 | -------------------------------------------------------------------------------- /src/de/tuberlin/mcc/prog1winf/threads/QueueFullException.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | */ 4 | package de.tuberlin.mcc.prog1winf.threads; 5 | 6 | /** 7 | * @author Dave 8 | * 9 | */ 10 | public class QueueFullException extends Exception { 11 | 12 | /** 13 | * 14 | */ 15 | public QueueFullException() { 16 | super(); 17 | // TODO Auto-generated constructor stub 18 | } 19 | 20 | /** 21 | * @param message 22 | * @param cause 23 | * @param enableSuppression 24 | * @param writableStackTrace 25 | */ 26 | public QueueFullException(String message, Throwable cause, 27 | boolean enableSuppression, boolean writableStackTrace) { 28 | super(message, cause, enableSuppression, writableStackTrace); 29 | // TODO Auto-generated constructor stub 30 | } 31 | 32 | /** 33 | * @param message 34 | * @param cause 35 | */ 36 | public QueueFullException(String message, Throwable cause) { 37 | super(message, cause); 38 | // TODO Auto-generated constructor stub 39 | } 40 | 41 | /** 42 | * @param message 43 | */ 44 | public QueueFullException(String message) { 45 | super(message); 46 | // TODO Auto-generated constructor stub 47 | } 48 | 49 | /** 50 | * @param cause 51 | */ 52 | public QueueFullException(Throwable cause) { 53 | super(cause); 54 | // TODO Auto-generated constructor stub 55 | } 56 | 57 | } 58 | -------------------------------------------------------------------------------- /src/de/tuberlin/mcc/prog1winf/datastructures/Factorial.java: -------------------------------------------------------------------------------- 1 | package de.tuberlin.mcc.prog1winf.datastructures; 2 | 3 | public class Factorial { 4 | 5 | public static int factorialIterative(int n){ 6 | int res = 1; 7 | while(n>1) res *= n--; 8 | return res; 9 | } 10 | 11 | public static int factorialRecursive(int n){ 12 | if (n==1) return 1; 13 | return n*factorialRecursive(n-1); 14 | } 15 | 16 | public static void main(String[] args) { 17 | int n = 5; 18 | // System.out.println("Iterative: "+ factorialIterative(n)); 19 | // System.out.println("Recursive: "+ factorialRecursive(n)); 20 | benchmark(); 21 | } 22 | 23 | private static void benchmark() { 24 | long start, afterIterative, end; 25 | int max = 500, iterations = 10000; 26 | System.out.println("n;iterative_in_ns;recursive_in_ns"); 27 | for(int n = 1; n<=max;n++){ 28 | start = System.nanoTime(); 29 | for(int i=0;i { 10 | 11 | final T identifier; 12 | 13 | final List values = new ArrayList<>(); 14 | 15 | final Map> edges = new HashMap<>(); 16 | 17 | public GraphNode(T identifier, List values) { 18 | this.identifier = identifier; 19 | this.values.addAll(values); 20 | } 21 | 22 | public GraphNode(T identifier, U value){ 23 | this.identifier = identifier; 24 | this.values.add(value); 25 | } 26 | 27 | public void connectTo(GraphNode other){ 28 | this.edges.put(other.identifier,other); 29 | other.edges.put(this.identifier,this); 30 | } 31 | 32 | public boolean isConnectedTo(T other){ 33 | return edges.get(other) != null; 34 | } 35 | 36 | public int getNumberOfNeighbors(){ 37 | return edges.size(); 38 | } 39 | 40 | @Override 41 | public String toString() { 42 | return "GraphNode{" + 43 | "identifier=" + identifier + 44 | ", values=" + values + 45 | ", edges=" + edges.keySet() + 46 | '}'; 47 | } 48 | } 49 | 50 | -------------------------------------------------------------------------------- /src/de/tuberlin/mcc/prog1winf/threads/ThreadManagement.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | */ 4 | package de.tuberlin.mcc.prog1winf.threads; 5 | 6 | import java.util.Date; 7 | 8 | /** 9 | * @author Dave 10 | * 11 | */ 12 | public class ThreadManagement { 13 | 14 | /** 15 | * @param args 16 | * @throws InterruptedException 17 | */ 18 | public static void main(String[] args) throws InterruptedException { 19 | MeinThread mt = new MeinThread(); 20 | mt.start(); 21 | System.out.println(Thread.currentThread().getName()+": "+"Thread started."); 22 | Thread.sleep(5000); 23 | mt.interrupt(); 24 | System.out.println(Thread.currentThread().getName()+": "+"interrupted."); 25 | mt.join(); 26 | System.out.println(Thread.currentThread().getName()+": "+"done."); 27 | 28 | 29 | 30 | } 31 | 32 | } 33 | 34 | class MeinThread extends Thread { 35 | 36 | /* 37 | * (non-Javadoc) 38 | * 39 | * @see java.lang.Thread#run() 40 | */ 41 | @Override 42 | public void run() { 43 | int counter = 5; 44 | while (!isInterrupted()&&counter-->0) { 45 | System.out.println(Thread.currentThread().getName()+": "+"Still alive "+ new Date()); 46 | try { 47 | Thread.sleep(500); 48 | } catch (InterruptedException e) { 49 | System.out.println(Thread.currentThread().getName()+": "+"was interrupted."); 50 | this.interrupt(); 51 | } 52 | } 53 | } 54 | 55 | } 56 | -------------------------------------------------------------------------------- /src/berlin/tu/mcc/prog1/ws2324/ConditionalBranching.java: -------------------------------------------------------------------------------- 1 | package berlin.tu.mcc.prog1.ws2324; 2 | 3 | public class ConditionalBranching { 4 | 5 | public static void main(String[] args) { 6 | int i = 42 * 42; 7 | if (i > 100) { 8 | System.out.println("was true"); 9 | if (i > 200) { 10 | System.out.println("was also greater than 200"); 11 | } else if (i == 200) { 12 | System.out.println("was exactly 200"); 13 | } else { 14 | System.out.println("but was less than 200"); 15 | } 16 | } else { 17 | System.out.println("was false"); 18 | } 19 | 20 | i = 200; 21 | if (i > 200) { 22 | System.out.println("was also greater than 200"); 23 | } else if (i == 200) { 24 | System.out.println("was exactly 200"); 25 | } else { 26 | System.out.println("but was less than 200"); 27 | } 28 | 29 | //switch case 30 | i = 0; 31 | switch (i){ 32 | case 0: 33 | System.out.println("was zero"); 34 | break; 35 | case 1: 36 | System.out.println("was one"); break; 37 | case 42: 38 | System.out.println("was 42"); break; 39 | default: 40 | System.out.println("was something else: " +i); 41 | 42 | } 43 | 44 | 45 | 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /src/berlin/tu/mcc/prog1/slides/oop/innerclasses/Train.java: -------------------------------------------------------------------------------- 1 | package berlin.tu.mcc.prog1.slides.oop.innerclasses; 2 | 3 | public class Train implements CanMove { 4 | 5 | private Wheel[] wheels = {new Wheel(), new Wheel(), new Wheel(), new Wheel()}; 6 | 7 | @Override 8 | public void move() { 9 | for (Wheel w : wheels) w.rotate(); 10 | } 11 | 12 | public static class Wheel implements Rotatable { 13 | @Override 14 | public void rotate() { 15 | } 16 | } 17 | } 18 | 19 | class Bike implements CanMove { 20 | 21 | private Wheel[] wheels = {new Wheel(), new Wheel()}; 22 | 23 | @Override 24 | public void move() { 25 | for (Wheel w : wheels) w.rotate(); 26 | } 27 | 28 | public static class Wheel implements Rotatable { 29 | @Override 30 | public void rotate() { 31 | } 32 | } 33 | 34 | void replaceWheels(Wheel front, Wheel back) { 35 | wheels[0] = front; 36 | wheels[1] = back; 37 | } 38 | } 39 | 40 | interface Rotatable { 41 | void rotate(); 42 | } 43 | 44 | interface CanMove { 45 | void move(); 46 | } 47 | 48 | class Traveler { 49 | public static void main(String[] args) { 50 | Train t = new Train(); 51 | Bike b = new Bike(); 52 | t.move(); 53 | b.move(); 54 | Bike.Wheel front = new Bike.Wheel(); 55 | Bike.Wheel back = new Bike.Wheel(); 56 | b.replaceWheels(front, back); 57 | 58 | } 59 | } -------------------------------------------------------------------------------- /src/de/tuberlin/mcc/prog1winf/birdshow/Bird.java: -------------------------------------------------------------------------------- 1 | package de.tuberlin.mcc.prog1winf.birdshow; 2 | 3 | public abstract class Bird { 4 | 5 | private String name; 6 | 7 | public Bird(String name){ 8 | this.name = name; 9 | } 10 | 11 | public abstract void sayName(); 12 | 13 | 14 | public String getName(){ 15 | return this.name; 16 | } 17 | } 18 | 19 | 20 | class Amsel extends Bird implements CanFly{ 21 | 22 | public Amsel(String name) { 23 | super(name); 24 | } 25 | 26 | @Override 27 | public void sayName() { 28 | System.out.print("Amsel " + this.getName()); 29 | } 30 | 31 | @Override 32 | public void fly() { 33 | System.out.println(this.getClass().getSimpleName() + ": I'm flying."); 34 | } 35 | } 36 | 37 | class DummeAmsel extends Amsel { 38 | 39 | 40 | public DummeAmsel(String name) { 41 | super(name); 42 | } 43 | 44 | @Override 45 | public void fly() { 46 | System.out.println(this.getClass().getSimpleName() + ": voll gegen die Wand."); 47 | } 48 | 49 | } 50 | 51 | 52 | class Strauss extends Bird{ 53 | 54 | public Strauss(String name) { 55 | super(name); 56 | } 57 | 58 | @Override 59 | public void sayName() { 60 | System.out.print("Strauss " + this.getName()); 61 | } 62 | } 63 | 64 | class Raumschiff implements CanFly{ 65 | 66 | @Override 67 | public void fly() { 68 | System.out.println("Wroaaaam"); 69 | } 70 | } -------------------------------------------------------------------------------- /src/de/tuberlin/mcc/prog1winf/birdshow/Flugshow.java: -------------------------------------------------------------------------------- 1 | package de.tuberlin.mcc.prog1winf.birdshow; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | public class Flugshow { 7 | 8 | public void letFly(List flyers){ 9 | System.out.println("Starting flight show"); 10 | for(CanFly c : flyers) { 11 | c.fly(); 12 | } 13 | } 14 | 15 | public void foobar(List list){ 16 | list.remove((int)(Math.random()*list.size())); 17 | } 18 | 19 | public void letAllBirdsFly(List birds){ 20 | System.out.println("Starting bird flight show"); 21 | for(Bird b: birds){ 22 | if(b instanceof CanFly) ((CanFly) b).fly(); 23 | else{b.sayName(); 24 | System.out.println(" cannot fly.");} 25 | } 26 | } 27 | 28 | 29 | public static void main(String[] args) { 30 | List flyers = new ArrayList<>(); 31 | Amsel horst = new Amsel("Horst"); 32 | DummeAmsel honk = new DummeAmsel("Honk"); 33 | Raumschiff raumschiff = new Raumschiff(); 34 | flyers.add(horst); 35 | flyers.add(honk); 36 | flyers.add(raumschiff); 37 | flyers.add((Amsel)honk); 38 | Flugshow f = new Flugshow(); 39 | f.letFly(flyers); 40 | List amseln = new ArrayList<>(); 41 | amseln.add(honk); 42 | amseln.add(horst); 43 | f.letFly(amseln); 44 | //List list = new ArrayList(); -> geht nicht 45 | List birds = new ArrayList<>(); 46 | birds.addAll(amseln); 47 | birds.add(new Strauss("Dude")); 48 | f.letAllBirdsFly(birds); 49 | 50 | } 51 | 52 | } 53 | -------------------------------------------------------------------------------- /src/berlin/tu/mcc/prog1/ws2526/DumbArrayQueue.java: -------------------------------------------------------------------------------- 1 | package berlin.tu.mcc.prog1.ws2526; 2 | 3 | public class DumbArrayQueue implements Prog1Queue { 4 | 5 | private int[] values = new int[0]; 6 | 7 | @Override 8 | public void enqueue(int val) { 9 | int [] valNew = new int[values.length+1]; 10 | for(int i = 0;i l = new ArrayList<>(); 24 | for (int i = 1; i < 10; i++) { 25 | l.add(i); 26 | } 27 | Collections.shuffle(l); 28 | BinTree b = new BinTree(); 29 | System.out.println("In:"); 30 | for (int i : l) { 31 | System.out.print(i); 32 | b.insert(i); 33 | } 34 | System.out.println("\nOut:"); 35 | b.printInOrder(); 36 | 37 | } 38 | 39 | } 40 | 41 | 42 | class Node { 43 | 44 | public int value; 45 | 46 | public Node left; 47 | 48 | public Node right; 49 | 50 | 51 | Node(int value) { 52 | super(); 53 | this.value = value; 54 | } 55 | 56 | void insert(int val) { 57 | if (val < value) { 58 | if (left == null) left = new Node(val); 59 | else left.insert(val); 60 | } else if (val > value) { 61 | if (right == null) right = new Node(val); 62 | else right.insert(val); 63 | } 64 | } 65 | 66 | String inOrder() { 67 | String res = ""; 68 | if (left != null) res += left.inOrder(); 69 | res += value; 70 | if (right != null) res += right.inOrder(); 71 | return res; 72 | } 73 | 74 | } 75 | -------------------------------------------------------------------------------- /src/berlin/tu/mcc/prog1/ws2324/FirstClass.java: -------------------------------------------------------------------------------- 1 | package berlin.tu.mcc.prog1.ws2324; 2 | 3 | import Prog1Tools.IOTools; 4 | 5 | public class FirstClass { 6 | 7 | public static void main(String[] args) { 8 | System.out.println("hello world!"); 9 | String name; 10 | name = "Jane Doe"; 11 | String name2 = "John Doe"; 12 | /*System.out.println(name); 13 | System.out.print("Hello "); 14 | System.out.println(name2);*/ 15 | name2 = "Hello " + name; 16 | System.out.println(name2 + " and all the others"); 17 | // comment 18 | long i = 42; 19 | int j = (int) i; 20 | System.out.println(i+(3*8/12)-515*i); 21 | i = i*43; 22 | i*=43; 23 | long meineLieblingsLongVariable = 31; 24 | System.out.println("1 durch 2 = "+ ((double)1/2)); 25 | System.out.println("1 durch 2 = "+ (1/2)); 26 | System.out.println("1 durch 2 = "+ (1/2.0)); 27 | System.out.println("1 mod 2 = " + (1%2)); 28 | double d = Math.sqrt(49); 29 | boolean b = meineLieblingsLongVariable <= >= == != 30 | boolean b2 = false; 31 | b2 = b == b2; 32 | b2 = !b; // not 33 | b2 = b && b2; // and 34 | b2 = b || b2; // or 35 | 36 | int c = (int) (i = 0); 37 | c++; 38 | ++c; 39 | c--; 40 | --c; 41 | i = 0; 42 | i = c++; // äquivalent zu: i=c;c=c+1; 43 | System.out.println("i="+i +"c="+c); 44 | i = 0; c =0; 45 | i = ++c; // äquivalent zu; c=c+1;i=c; 46 | System.out.println("i="+i+"c="+c); 47 | meineLieblingsLongVariable++; 48 | 49 | i = IOTools.readInt(); 50 | System.out.println(i++); 51 | i = IOTools.readInt("bitte Zahl eingeben: "); 52 | System.out.println("Echo: "+i); 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /src/berlin/tu/mcc/prog1/ws2526/Prog1Queue.java: -------------------------------------------------------------------------------- 1 | package berlin.tu.mcc.prog1.ws2526; 2 | 3 | public interface Prog1Queue { 4 | 5 | public void enqueue(int i); 6 | 7 | public Integer dequeue(); 8 | 9 | public int size(); 10 | 11 | public boolean isEmpty(); 12 | 13 | } 14 | 15 | 16 | class Prog1QueueBenchmark { 17 | 18 | public static void main(String[] args) { 19 | Prog1Queue dumb = new DumbArrayQueue(); 20 | Prog1Queue smarter = new SlightySmarterArrayQueue(); 21 | int noOfEnqueue = 1_000_000; 22 | int noOfDequeue = 1_000_000; 23 | long timeDumb = benchmark(dumb, noOfEnqueue, noOfDequeue); 24 | long timeSmarter = benchmark(smarter, noOfEnqueue, noOfDequeue); 25 | System.out.println("Results for " + noOfEnqueue +" enqueue() and " + noOfDequeue + " dequeue() calls:" 26 | +"\nDumbArrayQueue: " + timeDumb +" ms" 27 | +"\nSlightlySmarterArrayQueue: " + timeSmarter + "ms"); 28 | 29 | 30 | } 31 | 32 | private static long benchmark(Prog1Queue q, int noOfEnqueue, int noOfDequeue) { 33 | int enqCounter = 0, deqCounter = 0; 34 | long start = System.currentTimeMillis(); 35 | while (enqCounter < noOfEnqueue || deqCounter < noOfDequeue) { 36 | //System.out.println(dumb); 37 | if (enqCounter == noOfEnqueue) { 38 | // System.out.println("dumb: enq max reached"); 39 | q.dequeue(); 40 | deqCounter++; 41 | } else if (deqCounter == noOfDequeue) { 42 | // System.out.println("dumb: deq max reached"); 43 | q.enqueue(1); 44 | enqCounter++; 45 | } else { 46 | if (Math.random() < 0.5) { 47 | q.dequeue(); 48 | deqCounter++; 49 | } else { 50 | q.enqueue(1); 51 | enqCounter++; 52 | } 53 | } 54 | } 55 | return System.currentTimeMillis() - start; 56 | } 57 | 58 | 59 | } 60 | -------------------------------------------------------------------------------- /src/de/tuberlin/mcc/prog1winf/datastructures/ListRecursiveIterative.java: -------------------------------------------------------------------------------- 1 | package de.tuberlin.mcc.prog1winf.datastructures; 2 | 3 | public class ListRecursiveIterative { 4 | 5 | private Entry head; 6 | 7 | void appendIterative(Object obj) { 8 | if (head == null) head = new Entry(obj); 9 | else head.appendIterative(new Entry(obj)); 10 | } 11 | 12 | void appendRecursive(Object obj) { 13 | if (head == null) head = new Entry(obj); 14 | else head.appendRecursive(new Entry(obj)); 15 | } 16 | 17 | public static void main(String[] args) { 18 | ListRecursiveIterative list1 = new ListRecursiveIterative(), list2 = new ListRecursiveIterative(); 19 | for (int i = 1; i < 10; i++) { 20 | list1.appendIterative(i); 21 | list2.appendRecursive(i); 22 | } 23 | 24 | System.out.println("Iterative:" + list1.toStringIterative()); 25 | System.out.println("Recursive:" + list2.toStringRecursive()); 26 | } 27 | 28 | String toStringIterative() { 29 | String res = "["; 30 | if (head != null) res += head.toStringIterative(); 31 | return res + "]"; 32 | } 33 | 34 | String toStringRecursive() { 35 | String res = "["; 36 | if (head != null) res += head.toStringRecursive(); 37 | return res + "]"; 38 | } 39 | 40 | 41 | } 42 | 43 | 44 | class Entry { 45 | Object value; 46 | Entry next; 47 | 48 | Entry(Object o) { 49 | value = o; 50 | } 51 | 52 | void appendRecursive(Entry entry) { 53 | if (next == null) next = entry; 54 | else next.appendRecursive(entry); 55 | 56 | } 57 | 58 | void appendIterative(Entry entry) { 59 | Entry tmp = this; 60 | while (tmp.next != null) tmp = tmp.next; 61 | tmp.next = entry; 62 | } 63 | 64 | String toStringIterative() { 65 | String res = "" + value; 66 | Entry tmp = this.next; 67 | while (tmp != null) { 68 | res += "," + tmp.value; 69 | tmp = tmp.next; 70 | } 71 | return res; 72 | } 73 | 74 | String toStringRecursive() { 75 | if (next == null) return "" + value; 76 | return value + "," + next.toStringRecursive(); 77 | } 78 | 79 | 80 | } -------------------------------------------------------------------------------- /src/berlin/tu/mcc/prog1/slides/oop/innerclasses/Skyscraper.java: -------------------------------------------------------------------------------- 1 | package berlin.tu.mcc.prog1.slides.oop.innerclasses; 2 | 3 | public class Skyscraper { 4 | 5 | private final Elevator elevator; 6 | 7 | private final Floor[] floors; 8 | 9 | public Skyscraper(int numberOfFloors) { 10 | floors = new Floor[numberOfFloors]; 11 | for (int i = 0; i < numberOfFloors; i++) floors[i] = new Floor(i); 12 | elevator = new Elevator(); 13 | elevator.current = floors[0]; 14 | } 15 | 16 | public void takeElevator(Floor start, Floor end) { 17 | if (start == null || end == null) { 18 | System.out.println("invalid trip"); 19 | return; 20 | } 21 | while (elevator.isAbove(start)) elevator.down(); 22 | while (elevator.isBelow(start)) elevator.up(); 23 | elevator.stop(); 24 | while (elevator.isAbove(end)) elevator.down(); 25 | while (elevator.isBelow(end)) elevator.up(); 26 | elevator.stop(); 27 | } 28 | 29 | public Floor getFloorForNumber(int num) { 30 | if (num < 0 || num > floors.length) return null; 31 | return floors[num]; 32 | } 33 | 34 | private class Elevator { 35 | 36 | private Floor current; 37 | 38 | boolean isBelow(Floor f) { 39 | return current.number < f.number; 40 | } 41 | 42 | boolean isAbove(Floor f) { 43 | return current.number > f.number; 44 | } 45 | 46 | void up() { 47 | if (current.number < floors.length - 1) current = floors[current.number + 1]; 48 | System.out.println("on floor " + current.number); 49 | } 50 | 51 | void down() { 52 | if (current.number > 0) current = floors[current.number - 1]; 53 | System.out.println("on floor " + current.number); 54 | } 55 | 56 | void stop() { 57 | System.out.println("letting people leave and enter"); 58 | } 59 | 60 | } 61 | 62 | 63 | public class Floor { 64 | public final int number; 65 | 66 | Floor(int number) { 67 | this.number = number; 68 | } 69 | } 70 | } 71 | 72 | class Inhabitant { 73 | public static void main(String[] args) { 74 | Skyscraper s = new Skyscraper(10); 75 | s.takeElevator(s.getFloorForNumber(3), s.getFloorForNumber(8)); 76 | } 77 | } 78 | -------------------------------------------------------------------------------- /src/de/tuberlin/mcc/prog1winf/datastructures/BinSearch.java: -------------------------------------------------------------------------------- 1 | package de.tuberlin.mcc.prog1winf.datastructures; 2 | 3 | import java.util.Arrays; 4 | 5 | public class BinSearch { 6 | 7 | public static int binSearchIterative(int[] arr, int val) { 8 | System.out.println("Searching for " + val + " in " + Arrays.toString(arr)); 9 | int left = 0, right = arr.length - 1, middle = (right - left) / 2 + left; 10 | while (true) { 11 | System.out.println("Checking index position " + middle); 12 | if (arr[middle] == val) return middle; 13 | if (arr[middle] > val) { 14 | System.out.print(arr[middle] + " is greater than " + val); 15 | right = middle - 1; 16 | middle = (right - left) / 2 + left; 17 | System.out.println(", now checking with left=" + left + ", middle=" + middle + ", right=" + right); 18 | } else { 19 | System.out.print(arr[middle] + " is smaller than " + val); 20 | left = middle + 1; 21 | middle = (right - left) / 2 + left; 22 | System.out.println(", now checking with left=" + left + ", middle=" + middle + ", right=" + right); 23 | } 24 | } 25 | } 26 | 27 | public static int binSearchRecursive(int[] arr, int val) { 28 | return binSearchRecursive(arr, val, 0,arr.length-1); 29 | } 30 | 31 | private static int binSearchRecursive(int[] arr, int val, int left, int right) { 32 | int middle = (right - left) / 2 + left; 33 | System.out.println("Checking with left=" + left + ", middle=" + middle + ", right=" + right); 34 | if (arr[middle] == val) return middle; 35 | if (arr[middle] > val) { 36 | System.out.println(arr[middle] + " is greater than " + val); 37 | return binSearchRecursive(arr, val, left, middle - 1); 38 | } else { 39 | System.out.println(arr[middle] + " is smaller than " + val); 40 | return binSearchRecursive(arr, val, middle + 1, right); 41 | } 42 | } 43 | 44 | public static void main(String[] args) { 45 | int[] arr = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; 46 | int val = 6; 47 | System.out.println("Found " + val + " at position " + binSearchIterative(arr, val)); 48 | System.out.println("Found " + val + " at position " + binSearchRecursive(arr, val)); 49 | } 50 | 51 | } 52 | -------------------------------------------------------------------------------- /src/berlin/tu/mcc/prog1/ws2324/User.java: -------------------------------------------------------------------------------- 1 | package berlin.tu.mcc.prog1.ws2324; 2 | 3 | public class User { 4 | 5 | private String username = ""; 6 | private String email = ""; 7 | private String password; 8 | private int discount ; 9 | private static int sale = 10; 10 | 11 | 12 | protected User(String username, String email, String password) { 13 | this.username = username; 14 | this.password = password; 15 | this.email = email; 16 | } 17 | 18 | public static User createUserFrom(String username, String email, String password) { 19 | if (username != null && email != null && password != null && email.contains("@")) { 20 | return new User(username, email, password); 21 | } 22 | return null; 23 | } 24 | 25 | public boolean authenticate(String pw) { 26 | return this.password.equals(pw); 27 | } 28 | 29 | public String getUsername() { 30 | return username; 31 | } 32 | 33 | public void setUsername(String username) { 34 | if (username != null) { 35 | this.username = username; 36 | } 37 | } 38 | 39 | public String getEmail() { 40 | return email; 41 | } 42 | 43 | public void setEmail(String email) { 44 | if (email != null && email.contains("@")) { 45 | this.email = email; 46 | } else { 47 | System.out.println("Das hat nicht geklappt"); 48 | } 49 | } 50 | 51 | public void setPassword(String oldPw, String password) { 52 | if (oldPw != null && password != null && authenticate(oldPw)) { 53 | this.password = password; 54 | } 55 | } 56 | 57 | public int getDiscount() { 58 | return discount; 59 | } 60 | 61 | public void setDiscount(int discount) { 62 | this.discount = discount; 63 | } 64 | 65 | public static int getSale() { 66 | return sale; 67 | } 68 | 69 | public static void setSale(int sale) { 70 | User.sale = sale; 71 | } 72 | 73 | public void sendtext(String message) { 74 | System.out.println("Hallo " + username + ", " + message); 75 | } 76 | 77 | @Override 78 | public boolean equals(Object o) { 79 | if (this == o) return true; 80 | if (!(o instanceof User)) return false; 81 | 82 | User user = (User) o; 83 | 84 | if (!getEmail().equals(user.getEmail())) return false; 85 | return password.equals(user.password); 86 | } 87 | 88 | @Override 89 | public int hashCode() { 90 | int result = getEmail().hashCode(); 91 | result = 31 * result + password.hashCode(); 92 | return result; 93 | } 94 | } 95 | -------------------------------------------------------------------------------- /src/de/tuberlin/mcc/prog1winf/concurrency/NoWaitNotify.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | */ 4 | package de.tuberlin.mcc.prog1winf.concurrency; 5 | 6 | /** 7 | * @author David Bermbach 8 | * 9 | * created on 23.12.2014 10 | */ 11 | public class NoWaitNotify { 12 | 13 | volatile int value = 0; 14 | volatile boolean produced = false; 15 | Long failedTries = 0L; 16 | 17 | void produce() { 18 | value++; 19 | produced = true; 20 | } 21 | 22 | void consume() { 23 | produced = false; 24 | } 25 | 26 | void failedTry() { 27 | synchronized (failedTries) { 28 | failedTries++; 29 | } 30 | } 31 | 32 | long getFailedTries(){ 33 | synchronized (failedTries) { 34 | return failedTries; 35 | } 36 | } 37 | 38 | /** 39 | * @param args 40 | * @throws InterruptedException 41 | */ 42 | public static void main(String[] args) throws InterruptedException { 43 | NoWaitNotify nwn = new NoWaitNotify(); 44 | ProducerNoWaitNotify p = new ProducerNoWaitNotify(nwn); 45 | ConsumerNoWaitNotify c = new ConsumerNoWaitNotify(nwn); 46 | p.start(); 47 | c.start(); 48 | Thread.sleep(5000); 49 | p.interrupt(); 50 | c.interrupt(); 51 | p.join(); 52 | c.join(); 53 | System.out.println("failed tries: " + nwn.getFailedTries()); 54 | } 55 | 56 | } 57 | 58 | class ProducerNoWaitNotify extends Thread { 59 | 60 | private NoWaitNotify nwn; 61 | 62 | public ProducerNoWaitNotify(NoWaitNotify nwn) { 63 | super(); 64 | this.nwn = nwn; 65 | } 66 | 67 | /* 68 | * (non-Javadoc) 69 | * 70 | * @see java.lang.Thread#run() 71 | */ 72 | @Override 73 | public void run() { 74 | while (!isInterrupted()) { 75 | synchronized (nwn) { 76 | if (nwn.produced) { 77 | // System.out.println("Cannot produce :(..."); 78 | nwn.failedTry(); 79 | continue; 80 | } 81 | System.out.println("Produced " + (nwn.value + 1)); 82 | nwn.produce(); 83 | } 84 | 85 | } 86 | System.out.println("Producer is terminated."); 87 | } 88 | 89 | } 90 | 91 | class ConsumerNoWaitNotify extends Thread { 92 | 93 | private NoWaitNotify nwn; 94 | 95 | public ConsumerNoWaitNotify(NoWaitNotify nwn) { 96 | super(); 97 | this.nwn = nwn; 98 | } 99 | 100 | /* 101 | * (non-Javadoc) 102 | * 103 | * @see java.lang.Thread#run() 104 | */ 105 | @Override 106 | public void run() { 107 | while (!isInterrupted()) { 108 | synchronized (nwn) { 109 | if (!nwn.produced) { 110 | // System.out.println("Cannot consume :(..."); 111 | nwn.failedTry(); 112 | continue; 113 | } 114 | System.out.println("Consumed " + nwn.value); 115 | nwn.consume(); 116 | } 117 | 118 | } 119 | System.out.println("Consumer is terminated."); 120 | } 121 | } 122 | -------------------------------------------------------------------------------- /src/de/tuberlin/mcc/prog1winf/concurrency/WaitNotify.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | */ 4 | package de.tuberlin.mcc.prog1winf.concurrency; 5 | 6 | /** 7 | * @author David Bermbach 8 | * 9 | * created on 23.12.2014 10 | */ 11 | public class WaitNotify { 12 | 13 | volatile int value = 0; 14 | volatile boolean produced = false; 15 | Long failedTries = 0L; 16 | 17 | void produce() { 18 | value++; 19 | produced = true; 20 | } 21 | 22 | void consume() { 23 | produced = false; 24 | } 25 | 26 | void failedTry() { 27 | synchronized (failedTries) { 28 | failedTries++; 29 | } 30 | } 31 | 32 | long getFailedTries() { 33 | synchronized (failedTries) { 34 | return failedTries; 35 | } 36 | } 37 | 38 | /** 39 | * @param args 40 | * @throws InterruptedException 41 | */ 42 | public static void main(String[] args) throws InterruptedException { 43 | WaitNotify wn = new WaitNotify(); 44 | ProducerWaitNotify p = new ProducerWaitNotify(wn); 45 | ConsumerWaitNotify c = new ConsumerWaitNotify(wn); 46 | p.start(); 47 | c.start(); 48 | Thread.sleep(5000); 49 | p.interrupt(); 50 | c.interrupt(); 51 | 52 | p.join(); 53 | c.join(); 54 | System.out.println("failed tries: " + wn.getFailedTries()); 55 | } 56 | 57 | } 58 | 59 | class ProducerWaitNotify extends Thread { 60 | 61 | private WaitNotify wn; 62 | 63 | /** 64 | * @param wn 65 | */ 66 | public ProducerWaitNotify(WaitNotify wn) { 67 | this.wn = wn; 68 | } 69 | 70 | /* 71 | * (non-Javadoc) 72 | * 73 | * @see java.lang.Thread#run() 74 | */ 75 | @Override 76 | public void run() { 77 | while (!isInterrupted()) { 78 | synchronized (wn) { 79 | while (wn.produced) { 80 | // System.out.println("Cannot produce :(..."); 81 | wn.failedTry(); 82 | try { 83 | wn.wait(10); 84 | } catch (InterruptedException e) { 85 | this.interrupt(); 86 | } 87 | } 88 | System.out.println("Produced " + (wn.value + 1)); 89 | wn.produce(); 90 | wn.notify(); 91 | } 92 | 93 | } 94 | synchronized (wn) { 95 | wn.notifyAll(); 96 | } 97 | System.out.println("Producer is terminated."); 98 | 99 | } 100 | 101 | } 102 | 103 | class ConsumerWaitNotify extends Thread { 104 | 105 | private WaitNotify wn; 106 | 107 | /** 108 | * @param wn 109 | */ 110 | public ConsumerWaitNotify(WaitNotify wn) { 111 | this.wn = wn; 112 | } 113 | 114 | /* 115 | * (non-Javadoc) 116 | * 117 | * @see java.lang.Thread#run() 118 | */ 119 | @Override 120 | public void run() { 121 | while (!isInterrupted()) { 122 | synchronized (wn) { 123 | while (!wn.produced) { 124 | // System.out.println("Cannot consume :(..."); 125 | wn.failedTry(); 126 | try { 127 | wn.wait(10); 128 | } catch (InterruptedException e) { 129 | this.interrupt(); 130 | } 131 | } 132 | System.out.println("Consumed " + wn.value); 133 | wn.consume(); 134 | wn.notify(); 135 | } 136 | 137 | } 138 | synchronized (wn) { 139 | wn.notifyAll(); 140 | } 141 | System.out.println("Consumer is terminated."); 142 | } 143 | } 144 | -------------------------------------------------------------------------------- /src/berlin/tu/mcc/prog1/ws2526/SlightySmarterArrayQueue.java: -------------------------------------------------------------------------------- 1 | package berlin.tu.mcc.prog1.ws2526; 2 | 3 | public class SlightySmarterArrayQueue implements Prog1Queue { 4 | 5 | private Integer[] values = new Integer[1]; 6 | 7 | private int headPos = 0, tailPos = -1; 8 | 9 | @Override 10 | public void enqueue(int val) { 11 | // System.out.println("pre-insert ("+val+")="+getStatusString()); 12 | if(tailPos new InterfaceName() 90 | is not allowed because the interface cannot be instantiated, since there is no 91 | implementation. What can be used is the following: 92 | => new InterfaceName(){add class content here} 93 | This creates a class that doesn't have a real classname and instantiates it for 94 | direct use in the following. This can be used with interfaces and abstract classes 95 | in the exact same way. 96 | */ 97 | 98 | 99 | } 100 | 101 | 102 | 103 | 104 | } 105 | -------------------------------------------------------------------------------- /src/berlin/tu/mcc/prog1/ws2425/FirstClass.java: -------------------------------------------------------------------------------- 1 | package berlin.tu.mcc.prog1.ws2425; 2 | 3 | 4 | import Prog1Tools.IOTools; 5 | 6 | public class FirstClass { 7 | 8 | public static void main(String[] args) { 9 | System.out.println("hello world"); 10 | /*System.out.print(12345);*/ 11 | System.out.print(23456); // test 12 | // das ist ein kommentar 13 | /* das hier auch 14 | und geht bis hier 15 | */ 16 | int i; 17 | i = 42; 18 | //byte,short,int,long=> ganzzahlen 19 | //float, double => gleitkommazahlen 20 | //boolean 21 | //char 22 | //String => eigentlich kein primitiver Datentyp 23 | boolean b = true;//false 24 | double d = 1.23e14;//1.23 .23 1235467 12345.2346783568374 -0.00000012345 25 | float f = 1.23f; 26 | long l = 123l; 27 | String s = "hello\n\t \\"; 28 | char c = 'a'; 29 | String s2 = null; 30 | s2 = s; 31 | b = false; 32 | c = '\n'; 33 | System.out.println(b); 34 | int i2 = 2; 35 | long l2 = i2; 36 | i2 = (int) l2; 37 | int eingelesen = IOTools.readInt(); 38 | System.out.println(eingelesen); 39 | System.out.println(IOTools.readLine("bitte ein string:")); 40 | // 2. vorlesung 41 | i2 = i2 + i2; // +-*/% 42 | i2 = (i2 * 3) + 12; 43 | i2 = 2 / 3; // ergebnis 0 44 | i2 = 2 % 3; // ergebnis 2 45 | d = 2.0 / 3.0; // ergebnis 0.6666... 46 | d = 2 / 3.0; 47 | d = (double) 2 / 3; 48 | d = (double) (2 / 3); 49 | d = (1.0 * 2) / 3; 50 | 51 | i = 0; 52 | i++; 53 | ++i; 54 | i--; 55 | --i; 56 | i2 = i++; // i2 ist 0, i ist 1 57 | i = 0; 58 | i2 = ++i; // i2 und i sind 1 59 | int i3, i4 = 13, i5 = 42; 60 | i = i2 = i3 = i4 = i5; 61 | boolean comp; 62 | comp = i < i2; // > <= >= == != 63 | b = b && comp; // b&comp 64 | b = b || comp; // b|comp 65 | b = !comp; 66 | b = (b && b || (!b && b) || b) & b && i < i2; 67 | i = i + 3; 68 | i += 3;// *= /= -= ... 69 | d = Math.sqrt(Math.PI * 42 * Math.random()); 70 | String helloWorld = "hello " + "world" + d + d; 71 | 72 | if (i < i2) { 73 | System.out.println("i=i2"); 76 | } 77 | 78 | if (i < i2) { 79 | System.out.println("<"); 80 | } else if (i > i2) { 81 | System.out.println(">"); 82 | } else { 83 | System.out.println("=="); 84 | } 85 | 86 | switch (i) { 87 | case 0: 88 | case -1: 89 | case -42: 90 | case 1: 91 | System.out.println(1); 92 | System.out.println("i ist doof"); 93 | break; 94 | case 2: 95 | System.out.println(2); 96 | break; 97 | case 3: 98 | System.out.println(3); 99 | break; 100 | default: 101 | System.out.println("was Anderes"); 102 | } 103 | 104 | //arrays 105 | int[] arr;//deklarieren 106 | arr = new int[3]; //initialisieren 107 | arr[0] = 42; 108 | arr[1] = 44; 109 | arr[2] = 1; 110 | System.out.println(arr[2]); 111 | //tiefe kopie 112 | int[] arr2 = new int[arr.length]; 113 | arr2[0] = arr[0]; 114 | arr2[1] = arr[1]; 115 | arr2[2] = arr[2]; 116 | arr = new int[]{2, 233, 32345, 45, 51}; 117 | //2d array 118 | int[][] array2d = new int[2][3]; 119 | array2d[0] = new int[42]; 120 | 121 | for (int j = 0; j < 42; j++) { 122 | System.out.println("iteration " + j + ": Hello world!"); 123 | } 124 | //tiefe kopie mit schleife 125 | int[] langesArray = {1, 2, 3, 4, 5, 6, 76, 7, 98}; 126 | int[] target = new int[langesArray.length]; 127 | for (int j = 0; j < langesArray.length; j++) { 128 | target[j] = langesArray[j]; 129 | } 130 | //rueckwaerts 131 | for (int j = target.length - 1; j >= 0; j--) { 132 | System.out.println(target[j]); 133 | } 134 | //vorwaerts 135 | for (int j = 0; j < target.length; j++) { 136 | System.out.println(target[j]); 137 | } 138 | //und als for each 139 | for (int value : target) { 140 | System.out.println(value); 141 | } 142 | 143 | //solange bedingung erfüllt ist, tue folgendes 144 | double rand; 145 | while (/*bedingung*/ (rand = Math.random()) > 0.2) { 146 | //statements 147 | System.out.println(rand); 148 | } 149 | //tue folgendes, solange bedingung erfüllt 150 | do { 151 | //statements 152 | System.out.println(rand); 153 | } while ((rand = Math.random()) > 0.2); 154 | 155 | for(int j=0;j<10;j++){ 156 | if(j==2||j==5) { 157 | System.out.println("continue!"); 158 | continue;//break; 159 | } 160 | System.out.println(j); 161 | } 162 | 163 | 164 | 165 | } 166 | 167 | } 168 | -------------------------------------------------------------------------------- /src/berlin/tu/mcc/prog1/ws2526/FirstClass.java: -------------------------------------------------------------------------------- 1 | package berlin.tu.mcc.prog1.ws2526; 2 | 3 | import Prog1Tools.IOTools; 4 | 5 | public class FirstClass { 6 | 7 | public static void main(String[] args) { 8 | System.out.println("hello world!"); 9 | System.out.print("foo"); 10 | System.out.print("foo"); 11 | // einzeiliger Kommentar 12 | /* 13 | mehrzeiliger Kommentar 14 | */ 15 | int i; 16 | i = 5; 17 | int j = 6; 18 | //byte, short, int, long => Ganzzahlen 19 | // float, double => Gleitkommazahlen 20 | // boolean 21 | // char 22 | // String 23 | String nameOfEmployee = "Jane Doe\n\t\"\\"; 24 | char c = 'a'; 25 | i = 0x42af; // hexadezimal 26 | i = 0b1011; // binär 27 | i = 123_456_789; 28 | long l = 123l; 29 | double d = 1.23; // .23 123e-56 => entspricht 123 mal zehn hoch minus 56 30 | boolean b = true; // oder false 31 | String s = null; 32 | System.out.println(nameOfEmployee); 33 | b = IOTools.readBoolean("bitte boolean eingeben"); 34 | System.out.println(b); 35 | long meinLong = i; 36 | i = (int) meinLong; 37 | System.out.println("d=" + d); 38 | System.out.println("d als int=" + ((int) d)); 39 | 40 | meinLong = i + 5; // - * / % 41 | System.out.println(3 / 5); 42 | System.out.println(3.0 / 5.0); 43 | meinLong = i * i + (1 + 34) * i - i + 12; 44 | System.out.println(123 + 43); 45 | System.out.println("als string:" + 123 + 43); 46 | System.out.println("als string:" + (123 / 43)); 47 | int m, n = 4, k = 1; 48 | int nameDerErstenVariable = 0, nameDerZweitenVariable = 1; 49 | nameDerErstenVariable = nameDerErstenVariable + nameDerZweitenVariable; 50 | nameDerErstenVariable += nameDerZweitenVariable; //macht das gleiche 51 | i = 1; 52 | m = i++; 53 | m = ++i; 54 | m = i--; 55 | m = --i; 56 | 57 | boolean b2 = i == m; // < > <= >= != 58 | 59 | b = b2 && b; // || ! 60 | b = b2 || b; 61 | b = !(b2 && b); 62 | 63 | m = i = 1; 64 | 65 | double dRand = Math.ceil(Math.random()); 66 | //Google: java api klassenname 67 | 68 | if (dRand == 0) { 69 | System.out.println("oh, it was zero!1!111!"); 70 | } else { 71 | System.out.println("as expected it was 1 :-)"); 72 | } 73 | 74 | if (nameDerZweitenVariable == 0) 75 | System.out.println("oh, it was zero!1!111!"); 76 | else 77 | System.out.println("as expected it was 1 :-)"); 78 | 79 | if (b) { 80 | int jjj = 42 + i; 81 | if (b2) { 82 | System.out.println("b und b2 sind true"); 83 | System.out.println(jjj); 84 | } else { 85 | System.out.println("b ist true, b2 nicht"); 86 | } 87 | } 88 | int jjj = 43; 89 | if (b) { 90 | //do sth 91 | } else if (b2) { 92 | //do sth else 93 | } else if (Math.random() < 0.5) { 94 | //do sth. else again 95 | } else { 96 | System.out.println("b was false, b2 was false, rnd nmbr was >= 0.5"); 97 | } 98 | 99 | //switch case 100 | switch (i) { 101 | case -2: 102 | case -1: 103 | System.out.println("was -1 or -2"); 104 | break; 105 | case 0: 106 | System.out.println("0"); 107 | System.out.println("immer noch 0"); 108 | break; 109 | case 1: 110 | System.out.println("1"); 111 | break; 112 | case 42: 113 | System.out.println("42"); 114 | break; 115 | 116 | case 185: 117 | System.out.println("185"); 118 | break; 119 | default: 120 | System.out.println("i was " + i); 121 | } 122 | 123 | 124 | int[] arr; // leere Kiste hier 125 | arr = new int[3]; //jetzt Storagebox angemietet 126 | arr[0] = 42; // Indizes von 0 bis 2 127 | arr[1] = 43; 128 | arr[2] = 45; 129 | int[] arr2 = new int[84838]; 130 | arr2 = arr; 131 | arr2[0] = 1; 132 | System.out.println(arr[0]); 133 | 134 | //tiefe Kopie 135 | int[] deepcopy = new int[arr.length]; 136 | deepcopy[0] = arr[0]; 137 | deepcopy[1] = arr[1]; 138 | deepcopy[2] = arr[2]; 139 | //System.out.println(arr[-34]); 140 | 141 | int[][] arr2d = new int[2][3]; 142 | arr2d[0][0] = 42; 143 | System.out.println(arr2d.length); 144 | System.out.println(arr2d[1].length); 145 | arr2d[0] = new int[4]; 146 | 147 | arr = new int[]{1, 2, 3, 4, 5}; 148 | int[] arr3 = {1, 2, 3}; 149 | System.out.println(arr[0]); 150 | int[][] arr2dInOneLine = {{1, 2, 3}, {42, 43, 46}, {1, 2, 3, 4, 5}}; 151 | 152 | deepcopy = new int[arr.length]; 153 | for (int ii = 0; ii < arr.length; ii++) { 154 | deepcopy[ii] = arr[ii]; 155 | } 156 | 157 | 158 | for (int[] mein1dArray : arr2dInOneLine) { 159 | for (int meinInt : mein1dArray) { 160 | System.out.print(meinInt + " "); 161 | if (Math.random() > 0.5) break; 162 | } 163 | System.out.println(); 164 | } 165 | 166 | while (IOTools.readBoolean("nochmal?")) { 167 | //Anweisungen 168 | System.out.println("yeah, nochmal!"); 169 | } 170 | 171 | do { 172 | //Anweisungen 173 | System.out.println("yeehah"); 174 | } while (IOTools.readBoolean("wir hatten schon einmal - nochmal?")); 175 | 176 | 177 | int counter = 0; 178 | while (counter++ < 42) { 179 | if (counter == 17 || counter == 34) 180 | continue; 181 | //counter garantiert nicht gleich 17 oder 34 182 | System.out.println(counter); 183 | if (Math.random() > 0.5) break; 184 | } 185 | 186 | FirstClass.printIntArray(arr); 187 | printIntArray(arr2); 188 | printIntArray(arr3); 189 | printIntArray(arr2dInOneLine[0]); 190 | 191 | int meinRand = meinFancyRandom(-34, 42); 192 | 193 | } 194 | 195 | public static void foo(int bar){ 196 | 197 | } 198 | 199 | 200 | public static void foo(double bar){ 201 | 202 | } 203 | 204 | // int, int, int 205 | // int, double, int 206 | // int, int, double 207 | // int, float, float 208 | 209 | 210 | 211 | public static void printIntArray(int[] foo) { 212 | foo(123); 213 | if (foo == null) { 214 | return; 215 | } 216 | String meinString = "foo"; 217 | for (int i : foo) System.out.println(i); 218 | sum(1,2,3,4,5,6,6,7,7); 219 | sum(1,2,3); 220 | sum(new int[]{1,2,3}); 221 | sum(null); 222 | 223 | } 224 | 225 | public static int sum(int... arr) { 226 | int res = 0; 227 | for (int i : arr) res += i; 228 | return res; 229 | } 230 | 231 | 232 | public static int meinFancyRandom(int min, int max) { 233 | if (max <= min) { 234 | System.out.println("du doof"); 235 | return -1; 236 | } 237 | //hier ist in Ordnung 238 | return (int) (Math.random() * (max - min) + min); 239 | } 240 | } 241 | -------------------------------------------------------------------------------- /src/berlin/tu/mcc/prog1/ws2526/ChessFieldOps.java: -------------------------------------------------------------------------------- 1 | package berlin.tu.mcc.prog1.ws2526; 2 | 3 | /** 4 | * This class provides methods for analyzing the chessfield positions 5 | * that can be reached in n steps starting from a specified start position 6 | * @author David 7 | * @version 0.1 8 | * @see FirstClass 9 | * 10 | */ 11 | public class ChessFieldOps { 12 | 13 | 14 | public static void main(String[] args) { 15 | // add to first array dimension to go down, add to second dimension to go right 16 | char startLR = 'D', startUD = '5'; 17 | int[][] chessField = buildChessField(); 18 | markStart(startLR, startUD, chessField); 19 | printChessField(chessField); 20 | //markPositionsReachableForPawn(startLR,startUD, chessField, 2, false); 21 | markPositionsReachableForKnight(startLR,startUD, chessField, 3); 22 | markStart(startLR, startUD, chessField); 23 | System.out.println("result:"); 24 | printChessField(chessField); 25 | 26 | } 27 | 28 | public static void markStart(char startLR, char startUD, int[][] chessField) { 29 | if (isOnField(startLR, startUD, chessField)) chessField[toUDInt(startUD)][toLRInt(startLR)] = -1; 30 | } 31 | 32 | public static void markPositionsReachableForKnight(char startLR, char startUD, int[][] chessField, int noOfSteps) { 33 | 34 | if (noOfSteps <= 0) { 35 | // System.out.println("no more steps"); 36 | return; 37 | } 38 | int lr = toLRInt(startLR), ud = toUDInt(startUD); 39 | 40 | //first N, then E 41 | int udNE = ud - 2, lrNE = lr + 1; 42 | if (isOnField(udNE, lrNE, chessField)) { 43 | chessField[udNE][lrNE] = 1; 44 | System.out.println("going NE from " + toCoordinateString(lr, ud) + ": marking " + toCoordinateString(lrNE, udNE)); 45 | markPositionsReachableForKnight(toLRChar(lrNE), toUDChar(udNE), chessField, noOfSteps - 1); 46 | } 47 | 48 | //first N, then W 49 | int udNW = ud - 2, lrNW = lr - 1; 50 | if (isOnField(udNW, lrNW, chessField)) { 51 | chessField[udNW][lrNW] = 1; 52 | System.out.println("going NW from " + toCoordinateString(lr, ud) + ": marking " + toCoordinateString(lrNW, udNW)); 53 | markPositionsReachableForKnight(toLRChar(lrNW), toUDChar(udNW), chessField, noOfSteps - 1); 54 | } 55 | 56 | //first S, then E 57 | int udSE = ud + 2, lrSE = lr + 1; 58 | if (isOnField(udSE, lrSE, chessField)) { 59 | chessField[udSE][lrSE] = 1; 60 | System.out.println("going SE from " + toCoordinateString(lr, ud) + ": marking " + toCoordinateString(lrSE, udSE)); 61 | markPositionsReachableForKnight(toLRChar(lrSE), toUDChar(udSE), chessField, noOfSteps - 1); 62 | } 63 | //first S, then W 64 | int udSW = ud + 2, lrSW = lr - 1; 65 | if (isOnField(udSW, lrSW, chessField)) { 66 | chessField[udSW][lrSW] = 1; 67 | System.out.println("going SW from " + toCoordinateString(lr, ud) + ": marking " + toCoordinateString(lrSW, udSW)); 68 | markPositionsReachableForKnight(toLRChar(lrSW), toUDChar(udSW), chessField, noOfSteps - 1); 69 | } 70 | //first E, then N 71 | int udEN = ud - 1, lrEN = lr + 2; 72 | if (isOnField(udEN, lrEN, chessField)) { 73 | chessField[udEN][lrEN] = 1; 74 | System.out.println("going EN from " + toCoordinateString(lr, ud) + ": marking " + toCoordinateString(lrEN, udEN)); 75 | markPositionsReachableForKnight(toLRChar(lrEN), toUDChar(udEN), chessField, noOfSteps - 1); 76 | } 77 | //first E, then S 78 | int udES = ud + 1, lrES = lr + 2; 79 | if (isOnField(udES, lrES, chessField)) { 80 | chessField[udES][lrES] = 1; 81 | System.out.println("going ES from " + toCoordinateString(lr, ud) + ": marking " + toCoordinateString(lrES, udES)); 82 | markPositionsReachableForKnight(toLRChar(lrES), toUDChar(udES), chessField, noOfSteps - 1); 83 | } 84 | //first W, then N 85 | int udWN = ud - 1, lrWN = lr - 2; 86 | if (isOnField(udWN, lrWN, chessField)) { 87 | chessField[udWN][lrWN] = 1; 88 | System.out.println("going WN from " + toCoordinateString(lr, ud) + ": marking " + toCoordinateString(lrWN, udWN)); 89 | markPositionsReachableForKnight(toLRChar(lrWN), toUDChar(udWN), chessField, noOfSteps - 1); 90 | } 91 | //first W, then S 92 | int udWS = ud + 1, lrWS = lr - 2; 93 | if (isOnField(udWS, lrWS, chessField)) { 94 | chessField[udWS][lrWS] = 1; 95 | System.out.println("going WS from " + toCoordinateString(lr, ud) + ": marking " + toCoordinateString(lrWS, udWS)); 96 | markPositionsReachableForKnight(toLRChar(lrWS), toUDChar(udWS), chessField, noOfSteps - 1); 97 | 98 | } 99 | 100 | 101 | } 102 | 103 | /** 104 | * checks whether a specified position is on the chess field 105 | * @param lrArrayIndex left-right position as array index number (i.e., [0;array.length-1]) 106 | * @param udArrayIndex up-down position as array index number (i.e., [0;array.length-1]) 107 | * @param chessField the chess field which shall be checked 108 | * @return true if the position is on the chess field, false otherwise 109 | */ 110 | public static boolean isOnField(int lrArrayIndex, int udArrayIndex, int[][] chessField) { 111 | if (lrArrayIndex >= chessField.length || lrArrayIndex < 0 || udArrayIndex >= chessField[0].length || udArrayIndex < 0) { 112 | return false; 113 | } 114 | return true; 115 | } 116 | 117 | public static boolean isOnField(char lr, char ud, int[][] chessField) { 118 | return isOnField(toLRInt(lr), toUDInt(ud), chessField); 119 | } 120 | 121 | 122 | public static void markPositionsReachableForPawn(char startLR, char startUD, int[][] chessField, int noOfSteps, boolean goingDown) { 123 | //pawns can go one step in Up/Down direction, Left/Right stays constant 124 | if (noOfSteps <= 0) { 125 | // System.out.println("no more steps"); 126 | return; 127 | } 128 | int nextLR = toLRInt(startLR), nextUD = toUDInt(startUD); 129 | if (goingDown) nextUD++; 130 | else nextUD--; 131 | 132 | if (!isOnField(nextLR, nextUD, chessField)) { 133 | System.out.println(toCoordinateString(nextLR, nextUD) + " is outside the field"); 134 | return; 135 | } 136 | System.out.println("marking " + toCoordinateString(nextLR, nextUD)); 137 | chessField[nextUD][nextLR] = 1; 138 | markPositionsReachableForPawn(toLRChar(nextLR), toUDChar(nextUD), chessField, noOfSteps - 1, goingDown); 139 | 140 | } 141 | 142 | public static void printChessField(int[][] chessField) { 143 | System.out.print(" "); 144 | for (int i = 0; i < chessField[0].length; i++) { 145 | System.out.print(toLRChar(i) + " "); 146 | } 147 | System.out.println(); 148 | System.out.print(" "); 149 | for (int i = 0; i < chessField[0].length; i++) { 150 | System.out.print("- "); 151 | } 152 | System.out.println(); 153 | for (int i = 0; i < chessField.length; i++) { 154 | System.out.print(toUDChar(i) + "|"); 155 | for (int j = 0; j < chessField[i].length; j++) { 156 | if (chessField[i][j] != -1) System.out.print(chessField[i][j] + " "); 157 | else System.out.print("X "); 158 | } 159 | System.out.println(); 160 | } 161 | } 162 | 163 | public static int[][] buildChessField() { 164 | int[][] res = new int[8][8]; 165 | for (int i = 0; i < res.length; i++) { 166 | for (int j = 0; j < res[i].length; j++) { 167 | res[j][i] = 0; 168 | } 169 | } 170 | 171 | return res; 172 | } 173 | 174 | public static String toCoordinateString(int lr, int ud) { 175 | return "(" + toLRChar(lr) + "|" + toUDChar(ud) + ")"; 176 | } 177 | 178 | /** 179 | * assumes that chars are in the correct range 180 | * 181 | * @param lr 182 | * @return 183 | */ 184 | public static int toLRInt(char lr) { 185 | return lr - 65; 186 | } 187 | 188 | public static char toLRChar(int i) { 189 | return (char) (i + 65); 190 | } 191 | 192 | public static char toUDChar(int i) { 193 | return (char) (i + 49); 194 | } 195 | 196 | public static int toUDInt(char c) { 197 | return c - 49; 198 | } 199 | 200 | } 201 | -------------------------------------------------------------------------------- /.idea/uiDesigner.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | --------------------------------------------------------------------------------