├── .gitignore
├── .idea
├── .gitignore
├── encodings.xml
├── libraries
│ └── Prog1Tools.xml
├── misc.xml
├── modules.xml
├── uiDesigner.xml
└── vcs.xml
├── Prog1WINF-Examples.iml
├── README.md
├── out
└── production
│ └── Prog1WINF-Examples
│ └── META-INF
│ └── Prog1WINF-Examples.kotlin_module
└── src
├── berlin
└── tu
│ └── mcc
│ └── prog1
│ ├── slides
│ └── oop
│ │ ├── ApartmentHouse.java
│ │ ├── Building.java
│ │ ├── House.java
│ │ ├── SubClass.java
│ │ ├── TestClassLoader.java
│ │ ├── TestClassLoader2.java
│ │ └── innerclasses
│ │ ├── Skyscraper.java
│ │ └── Train.java
│ ├── ws2223
│ └── FirstClass.java
│ ├── ws2324
│ ├── Clothing.java
│ ├── ConditionalBranching.java
│ ├── FirstClass.java
│ ├── Jeans.java
│ ├── Main.java
│ ├── Order.java
│ ├── PremiumUser.java
│ ├── Product.java
│ ├── Shirt.java
│ ├── User.java
│ └── graph
│ │ ├── Graph.java
│ │ ├── GraphNode.java
│ │ └── TrainNetwork.java
│ └── ws2425
│ ├── CoinChange.java
│ ├── FirstClass.java
│ ├── Pair.java
│ ├── SecondClass.java
│ └── UsingGenerics.java
└── de
└── tuberlin
└── mcc
└── prog1winf
├── birdshow
├── Bird.java
├── CanFly.java
└── Flugshow.java
├── concurrency
├── CounterRunnable.java
├── CounterThread.java
├── JoinedThread.java
├── NoWaitNotify.java
├── SyncBlock.java
└── WaitNotify.java
├── datastructures
├── BTree.java
├── BTreeNode.java
├── BTreeTestclass.java
├── BinSearch.java
├── BinTree.java
├── Factorial.java
├── ListEntry.java
├── ListRecursiveIterative.java
├── NumberUtils.java
├── QueueWrapper.java
└── SinglyLinkedList.java
├── generics
├── NumberUtils.java
└── NutzungCollections.java
├── outlook
├── Example.java
└── OtherClass.java
├── sockets
└── SimpleServer.java
└── threads
├── MyThreadSafeQueue.java
├── QueueFullException.java
└── ThreadManagement.java
/.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 |
--------------------------------------------------------------------------------
/.idea/.gitignore:
--------------------------------------------------------------------------------
1 | # Default ignored files
2 | /workspace.xml
--------------------------------------------------------------------------------
/.idea/encodings.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/.idea/libraries/Prog1Tools.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/.idea/misc.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/.idea/modules.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/.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 |
--------------------------------------------------------------------------------
/.idea/vcs.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/Prog1WINF-Examples.iml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # prog1winf-examples
--------------------------------------------------------------------------------
/out/production/Prog1WINF-Examples/META-INF/Prog1WINF-Examples.kotlin_module:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/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/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/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/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/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 | }
--------------------------------------------------------------------------------
/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/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/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/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/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/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/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/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/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/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/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/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/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/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/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/ws2324/graph/GraphNode.java:
--------------------------------------------------------------------------------
1 | package berlin.tu.mcc.prog1.ws2324.graph;
2 |
3 |
4 | import java.util.ArrayList;
5 | import java.util.HashMap;
6 | import java.util.List;
7 | import java.util.Map;
8 |
9 | public class GraphNode {
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/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/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/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/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/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/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/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/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 extends CanFly> 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/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/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/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/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/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/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/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/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/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 extends Number> abc = new ArrayList();
31 |
32 |
33 | }
34 |
35 | }
36 |
--------------------------------------------------------------------------------
/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/de/tuberlin/mcc/prog1winf/datastructures/BinTree.java:
--------------------------------------------------------------------------------
1 | /**
2 | *
3 | */
4 | package de.tuberlin.mcc.prog1winf.datastructures;
5 |
6 | import java.util.ArrayList;
7 | import java.util.Collections;
8 |
9 | public class BinTree {
10 | Node root;
11 |
12 |
13 | void insert(int val) {
14 | if (root == null) root = new Node(val);
15 | else root.insert(val);
16 | }
17 |
18 | void printInOrder() {
19 | if (root != null) System.out.println(root.inOrder());
20 | }
21 |
22 | public static void main(String[] args) {
23 | ArrayList 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/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 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/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 |
--------------------------------------------------------------------------------
/src/de/tuberlin/mcc/prog1winf/datastructures/SinglyLinkedList.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 SinglyLinkedList {
12 |
13 | /** head of the list */
14 | private ListEntry head;
15 |
16 | /**
17 | * inserts a values at the end of the list
18 | *
19 | * @param value
20 | * value which shall be added
21 | */
22 | public void append(Object value) {
23 | if (head == null) {
24 | // insert at head
25 | head = new ListEntry();
26 | head.value = value;
27 | return;
28 | }
29 | // find end
30 | ListEntry current = head, last = head;
31 | while (current != null) {
32 | last = current;
33 | current = current.next;
34 | }
35 | current = new ListEntry();
36 | current.value = value;
37 | last.next = current;
38 | }
39 |
40 |
41 | public void appendNeu(Object value){
42 | ListEntry neu = new ListEntry();
43 | neu.value = value;
44 | if(head==null){
45 | head = neu;
46 | return;
47 | }
48 | ListEntry temp = head;
49 | while(temp.next!= null)temp = temp.next;
50 |
51 | temp.next = neu;
52 | }
53 |
54 | /**
55 | * inserts a value at the head of the list
56 | *
57 | * @param value
58 | * value which shall be added
59 | */
60 | public void insertAtHead(Object value) {
61 | if (head == null) {
62 | head = new ListEntry();
63 | head.value = value;
64 | return;
65 | }
66 | ListEntry newHead = new ListEntry();
67 | newHead.value = value;
68 | newHead.next = head;
69 | head = newHead;
70 | }
71 |
72 | /**
73 | * inserts a value after the entry with the value "after" or at the end of
74 | * the list if the value is not contained
75 | *
76 | * @param value
77 | * value which shall be added
78 | * @param after
79 | * desired predecessor for value
80 | */
81 | public void insertAfter(Object value, Object after) {
82 | if (head == null) {
83 | head = new ListEntry();
84 | head.value = value;
85 | return;
86 | }
87 | ListEntry newone = new ListEntry();
88 | newone.value = value;
89 | ListEntry current = head;
90 | while (current.next != null && current.value != after)
91 | current = current.next;
92 | ListEntry temp = current.next;
93 | current.next = newone;
94 | newone.next = temp;
95 | }
96 |
97 | /**
98 | *
99 | *
100 | * @param position
101 | * @return the value at position "position" or null if the list has less
102 | * entries
103 | */
104 | public Object getValue(int position) {
105 | int counter = 0;
106 | ListEntry current = head;
107 | while (current != null && position != counter++)
108 | current = current.next;
109 | if (current == null)
110 | return null;
111 | else
112 | return current.value;
113 | }
114 |
115 | /**
116 | * deletes the specified value if contained in this list. Note: this method
117 | * uses reference comparison, not equals()!
118 | *
119 | * @param value
120 | */
121 | public void deleteValue(Object value) {
122 | if (head == null)
123 | return; // empty list
124 | ListEntry current = head, last = head;
125 | if (current.value == value) {
126 | // it's the head element
127 | head = head.next;
128 | return;
129 | }
130 | while (current.next != null && current.value != value) {
131 | last = current;
132 | current = current.next;
133 | }
134 | if (current.value != value)
135 | return; // not in this list
136 | last.next = current.next;
137 |
138 | }
139 |
140 | /*
141 | * (non-Javadoc)
142 | *
143 | * @see java.lang.Object#toString()
144 | */
145 | @Override
146 | public String toString() {
147 | ListEntry current = head;
148 | if (current == null)
149 | return "[]";
150 | String res = "[";
151 | while (current != null && current.next != null) {
152 | res += current.value + ",";
153 | current = current.next;
154 | }
155 | res += current.value + "]";
156 | return res;
157 | }
158 |
159 | public static void main(String[] args) {
160 | SinglyLinkedList sll = new SinglyLinkedList();
161 | System.out.println(sll);
162 | sll.append(5);
163 | System.out.println(sll);
164 | sll.append(7);
165 | System.out.println(sll);
166 | sll.insertAtHead(8);
167 | System.out.println(sll);
168 | sll.insertAfter(11, 5);
169 | System.out.println(sll);
170 | System.out.println(sll.getValue(1));
171 | sll.deleteValue(5);
172 | System.out.println(sll);
173 | sll.deleteValue(8);
174 | System.out.println(sll);
175 | sll.deleteValue(7);
176 | System.out.println(sll);
177 | sll.deleteValue(11);
178 | System.out.println(sll);
179 | }
180 |
181 | }
182 |
--------------------------------------------------------------------------------
/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 extends Number> 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/de/tuberlin/mcc/prog1winf/generics/NutzungCollections.java:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/dbermbach/prog1winf-examples/dff70a1e5d3a322aca34d48d1d681170989ca034/src/de/tuberlin/mcc/prog1winf/generics/NutzungCollections.java
--------------------------------------------------------------------------------
/src/de/tuberlin/mcc/prog1winf/outlook/Example.java:
--------------------------------------------------------------------------------
1 | package de.tuberlin.mcc.prog1winf.outlook;
2 |
3 | import de.tuberlin.mcc.prog1winf.birdshow.Bird;
4 |
5 | public class Example {
6 |
7 | //static block
8 | static {
9 | System.out.println("Executing static block");
10 | /*instructions in the static block are executed when the classloader loads the class
11 | * and also creates all class variables.*/
12 |
13 | }
14 |
15 | public static void main(String[] args) {
16 | usingAnonymousClasses();
17 | usingInnerClasses();
18 | }
19 |
20 | static void usingInnerClasses() {
21 | //Using inner classes
22 |
23 | //This creates an instance of a class that contains inner classes
24 | OtherClass oc = new OtherClass();
25 | //This creates an instance of an inner class
26 | OtherClass.InnerClass oic = oc.new InnerClass();
27 | //This creates an instance of a static inner class
28 | OtherClass.StaticInnerClass osic = new OtherClass.StaticInnerClass();
29 |
30 | /*
31 | * It is possible to declare a class inside another class (at the same level
32 | * where we define methods or fields). These so-called inner classes can be
33 | * static or non-static and only exist together with their surrounding context.
34 | * There classname (see above) is "OuterClassname.InnerClassname".
35 | * Inner classes are usually used to hide implementation details and are, hence,
36 | * very often private classes, i.e., they are only accessible inside the surrounding
37 | * class. A typical example for using an inner class would be when implementing a
38 | * linked list data structure: The outer class would be "LinkedList", the "ListElement"
39 | * class (which is not supposed to be exposed to users of LinkedList or anyone else)
40 | * would be implemented as a (private) inner class.
41 | *
42 | * Static inner classes vs. non-static inner classes:
43 | * All kinds of inner classes only exist together with a surrounding context. In the case
44 | * of static inner classes, the surrounding context is the class. Therefore, it is
45 | * possible (see example above) to instantiate an instance of a static inner class simply
46 | * by referring to the classname of the outer class. Instance of non-static inner classes
47 | * are bound to a specific instance of the outer class (they are essentially part of it).
48 | * This can best be seen in the example of the linked list above: Instances of the
49 | * (non-static) inner class for list elements are part of a specific linked list instance,
50 | * i.e., in this case the list for which they have been created. This is also reflected in
51 | * the way that the oic object above is instantiated - the new operator requires a reference
52 | * to an instance of the outer class.
53 | *
54 | * All inner classes can access the methods and fields of their surrounding context (including
55 | * private ones!) as they are essentially a part of "it". This means that:
56 | * - static inner classes can access all class variables and class methods of the surrounding class
57 | * - non-static inner classes can access all class and instance variables/methods of the surrounding
58 | * class. Inside a non-static inner class "this" refers to the instance of the inner class. The "this"
59 | * reference to the surrounding instance can be retrieved via "OuterClassName.this".
60 | */
61 |
62 | }
63 |
64 |
65 | static void usingAnonymousClasses(){
66 | // Anonymous classes
67 | //Example 1
68 | Runnable r = new Runnable() {
69 | @Override
70 | public void run() {
71 | System.out.println("I'm running!");
72 | }
73 | };
74 | new Thread(r).start();
75 | //Example 2
76 | AbstractClass ac = new AbstractClass() {
77 | @Override
78 | void foobar() {
79 | //doing nothing
80 | }
81 | };
82 | System.out.println("Class and package name for object r: "+r.getClass().getName());
83 | System.out.println("Class and package name for object ac: "+ac.getClass().getName());
84 |
85 | /*whenever you need a quick implementation of an interface/abstract class
86 | but will only need it exactly one time, it is usually not worth creating
87 | a separate class somewhere. In those cases, an anonymous class can be created:
88 |
89 | => 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/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/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/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/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/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 |
--------------------------------------------------------------------------------