Skip to content

Commit f30e769

Browse files
committed
add things
1 parent 7fe9be7 commit f30e769

File tree

6 files changed

+243
-11
lines changed

6 files changed

+243
-11
lines changed
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
package de.dhbwka.exercise.collections;
2+
3+
import lombok.Getter;
4+
import lombok.Setter;
5+
6+
import java.util.LinkedList;
7+
import java.util.List;
8+
9+
/**
10+
* Binary tree
11+
*
12+
* @author Leonhard Gahr
13+
*/
14+
@Getter
15+
@Setter
16+
public class BinaryTree<T extends Comparable<T>> {
17+
private T node;
18+
private BinaryTree<T> left;
19+
private BinaryTree<T> right;
20+
21+
/**
22+
* empty constructor creates empty tree
23+
*/
24+
public BinaryTree() {
25+
this(null);
26+
}
27+
28+
/**
29+
* Create tree with node value but no right or left tree
30+
*
31+
* @param node the node value
32+
*/
33+
public BinaryTree(T node) {
34+
this(node, null, null);
35+
}
36+
37+
/**
38+
* Create full tree
39+
*
40+
* @param node the node value
41+
* @param left the left tree
42+
* @param right the right tree
43+
*/
44+
public BinaryTree(T node, BinaryTree<T> left, BinaryTree<T> right) {
45+
this.node = node;
46+
this.left = left;
47+
this.right = right;
48+
}
49+
50+
/**
51+
* add a value to the tree
52+
*
53+
* @param value the value to add
54+
* @return whether the add was successful or a duplicate
55+
*/
56+
public boolean add(T value) {
57+
if (this.node == null) {
58+
this.node = value;
59+
return true;
60+
}
61+
62+
if (this.node.compareTo(value) < 0) {
63+
if (this.left == null) {
64+
this.left = new BinaryTree<>(value);
65+
return true;
66+
}
67+
return this.left.add(value);
68+
} else if (this.node.compareTo(value) > 0) {
69+
if (this.right == null) {
70+
this.right = new BinaryTree<>(value);
71+
return true;
72+
}
73+
return this.right.add(value);
74+
}
75+
76+
return false;
77+
}
78+
79+
/**
80+
* return a sorted list of all node values
81+
*
82+
* @return the list of node values
83+
*/
84+
public List<T> traverse() {
85+
LinkedList<T> result = new LinkedList<>();
86+
if (this.node != null) {
87+
result.addAll(this.left != null ? this.left.traverse() : new LinkedList<>());
88+
result.add(this.node);
89+
result.addAll(this.right != null ? this.right.traverse() : new LinkedList<>());
90+
}
91+
return result;
92+
}
93+
94+
@Override
95+
public String toString() {
96+
return traverse().toString();
97+
}
98+
99+
public static void main(String[] args) {
100+
BinaryTree<Integer> tree = new BinaryTree<>(12);
101+
tree.add(2);
102+
tree.add(1);
103+
tree.add(5);
104+
tree.add(22);
105+
106+
System.out.println(tree);
107+
}
108+
}

src/de/dhbwka/exercise/ui/Editor.java

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package de.dhbwka.exercise.ui;
2+
3+
import javax.swing.*;
4+
import java.awt.*;
5+
import java.util.ArrayList;
6+
import java.util.List;
7+
import java.util.stream.Stream;
8+
9+
/**
10+
* @author Leonhard Gahr
11+
*/
12+
public class Editor extends JFrame {
13+
public Editor() throws ClassNotFoundException, UnsupportedLookAndFeelException, InstantiationException, IllegalAccessException {
14+
super("Editor");
15+
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
16+
17+
this.setSize(640, 480);
18+
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
19+
this.setLayout(new FlowLayout());
20+
21+
createMenuBar();
22+
23+
this.setVisible(true);
24+
}
25+
26+
private List<MyMenuItem> generateFileMenuItemList() {
27+
List<MyMenuItem> myMenuItemList = new ArrayList<>();
28+
myMenuItemList.add(new MyMenuItem("New"));
29+
myMenuItemList.add(new MyMenuItem("Open", true));
30+
myMenuItemList.add(new MyMenuItem("Close", true));
31+
myMenuItemList.add(new MyMenuItem("Save"));
32+
myMenuItemList.add(new MyMenuItem("Save as"));
33+
myMenuItemList.add(new MyMenuItem("Save as website"));
34+
myMenuItemList.add(new MyMenuItem("Search", true));
35+
myMenuItemList.add(new MyMenuItem("Versions", true));
36+
myMenuItemList.add(new MyMenuItem("Websitepreview", true));
37+
myMenuItemList.add(new MyMenuItem("Print"));
38+
myMenuItemList.add(new MyMenuItem("Properties", true));
39+
myMenuItemList.add(new MyMenuItem("Exit", e -> System.exit(0), 'Q'));
40+
41+
return myMenuItemList;
42+
}
43+
44+
private List<MyMenuItem> generateEditMenuItemList() {
45+
List myMenuItemList = new ArrayList<MyMenuItem>();
46+
47+
return myMenuItemList;
48+
}
49+
50+
private void createMenuBar() {
51+
JMenuBar menuBar = new JMenuBar();
52+
53+
JMenu menuEdit = new JMenu("File");
54+
addToMenu(menuEdit, generateFileMenuItemList().stream());
55+
menuBar.add(menuEdit);
56+
57+
this.setJMenuBar(menuBar);
58+
}
59+
60+
private void addToMenu(JMenu menu, Stream<MyMenuItem> itemStream) {
61+
itemStream.forEach(item -> {
62+
menu.add(item.toJMenuItem());
63+
if (item.isFollowSpacer()) {
64+
menu.addSeparator();
65+
}
66+
});
67+
}
68+
69+
public static void main(String[] args) throws ClassNotFoundException, UnsupportedLookAndFeelException, InstantiationException, IllegalAccessException {
70+
Editor editor = new Editor();
71+
}
72+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package de.dhbwka.exercise.ui;
2+
3+
import lombok.Getter;
4+
import lombok.Setter;
5+
6+
import javax.swing.*;
7+
import java.awt.event.ActionListener;
8+
9+
/**
10+
* @author Leonhard Gahr
11+
*/
12+
13+
@Getter
14+
@Setter
15+
public class MyMenuItem {
16+
private String title;
17+
private ActionListener actionListener;
18+
private char mnemonic;
19+
private boolean followSpacer = false;
20+
21+
public MyMenuItem(String title, ActionListener actionListener, char mnemonic, boolean followSpacer) {
22+
this.title = title;
23+
this.actionListener = actionListener;
24+
this.mnemonic = mnemonic;
25+
this.followSpacer = followSpacer;
26+
}
27+
public MyMenuItem(String title, ActionListener actionListener, char mnemonic) {
28+
this.title = title;
29+
this.actionListener = actionListener;
30+
this.mnemonic = mnemonic;
31+
}
32+
33+
public MyMenuItem(String title, ActionListener actionListener, boolean followSpacer) {
34+
this.title = title;
35+
this.actionListener = actionListener;
36+
this.followSpacer = followSpacer;
37+
38+
}
39+
public MyMenuItem(String title, ActionListener actionListener) {
40+
this.title = title;
41+
this.actionListener = actionListener;
42+
43+
}
44+
45+
public MyMenuItem(String title, boolean followSpacer) {
46+
this.title = title;
47+
this.followSpacer = followSpacer;
48+
}
49+
50+
public MyMenuItem(String title) {
51+
this.title = title;
52+
}
53+
54+
public JMenuItem toJMenuItem() {
55+
JMenuItem item = new JMenuItem(title);
56+
item.addActionListener(actionListener);
57+
item.setMnemonic(mnemonic);
58+
59+
return item;
60+
}
61+
}

src/de/dhbwka/exercise/ui/TextfileViewer.java

Lines changed: 0 additions & 10 deletions
This file was deleted.

src/de/dhbwka/exercise/utility/Sort.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ public static <T extends Comparable<T>> void bogoBogoSort(T[] arr) {
9191
* The solar bitflip sort for generic arrays
9292
* <p>
9393
* check if the array is sorted, if not, wait a random amount of time, pray for having bit flips caused
94-
* by solar radiation, just in the correct order and repeat the check <b>Doesn’t work if you have EEC memory.</b>
94+
* by solar radiation, put the bits just in the correct order and repeat the check <b>Doesn’t work if you have EEC memory.</b>
9595
*
9696
* @param arr the array
9797
* @param <T> the generic type

src/de/dhbwka/exercise/utility/SortUtility.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ static boolean isSorted(int[] arr) {
8282

8383
static <T extends Comparable<T>> int partition(T[] arr, int begin, int end) {
8484
T pivot = arr[end];
85+
System.out.println(pivot);
8586
int i = (begin - 1);
8687

8788
for (int j = begin; j < end; j++) {

0 commit comments

Comments
 (0)