Skip to content

Commit e89c8f7

Browse files
committed
new tasks;
1 parent f616912 commit e89c8f7

File tree

6 files changed

+323
-6
lines changed

6 files changed

+323
-6
lines changed

src/de/dhbwka/exercise/arrays/BubbleSort.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package de.dhbwka.exercise.arrays;
22

3+
import java.util.Arrays;
4+
35
import static de.dhbwka.exercise.utility.ScannerUtility.getInt;
46
import static de.dhbwka.exercise.utility.ScannerUtility.getIntArray;
57

@@ -10,11 +12,8 @@ public class BubbleSort {
1012
public static void main(String[] args) {
1113
final int N = getInt("Please enter the number n of elements: ");
1214

13-
int[] values = getIntArray("Enter value %d: ", N);
14-
15-
for (int i : bubbleSort(values, N)) {
16-
System.out.print(i + " ");
17-
}
15+
int[] values = bubbleSort(getIntArray("Enter value %d: ", N), N);
16+
System.out.println(Arrays.toString(values));
1817
}
1918

2019
private static int[] bubbleSort(int[] values, int n) {

src/de/dhbwka/exercise/arrays/MatrixSubtraction.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ private static int[][] randomMatrix(int n, int m) {
5757
int[][] matrix = new int[n][m];
5858

5959
for (int row = 0; row < n; row++) {
60-
matrix[row] = random.ints(m).toArray();
60+
matrix[row] = random.ints(m, 0, 101).toArray();
6161
}
6262
return matrix;
6363
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package de.dhbwka.exercise.classes;
2+
3+
/**
4+
* @author Leonhard Gahr
5+
*/
6+
public class Main {
7+
public static void main(String[] args) throws IllegalAccessException {
8+
Radio radio = new Radio();
9+
System.out.println(radio);
10+
radio.incVolume();
11+
System.out.println(radio);
12+
radio.decVolume();
13+
radio.setFrequency(200);
14+
System.out.println(radio);
15+
radio.turnOff();
16+
radio.decVolume();
17+
System.out.println(radio);
18+
radio.incVolume();
19+
System.out.println(radio);
20+
radio.turnOn();
21+
System.out.println(radio);
22+
}
23+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package de.dhbwka.exercise.classes;
2+
3+
import lombok.Getter;
4+
import lombok.Setter;
5+
6+
/**
7+
* @author Leonhard Gahr
8+
*/
9+
@Getter
10+
@Setter
11+
public class Point {
12+
private double x;
13+
private double y;
14+
15+
public Point() {
16+
this(0, 0);
17+
}
18+
19+
public Point(double[] points) {
20+
this(points[0], points[1]);
21+
}
22+
23+
Point(double x, double y) {
24+
this.x = x;
25+
this.y = y;
26+
}
27+
28+
double distToPoint(Point point) {
29+
return Math.sqrt(Math.pow(point.x - x, 2) + Math.pow(point.y - y, 2));
30+
}
31+
32+
double distToOrigin() {
33+
return distToPoint(new Point());
34+
}
35+
36+
Point mirrorXAxis() {
37+
return new Point(-this.x, this.y);
38+
}
39+
40+
Point mirrorYAxis() {
41+
return new Point(this.x, -this.y);
42+
}
43+
44+
Point mirrorOrigin() {
45+
return new Point(-this.x, -this.y);
46+
}
47+
48+
@Override
49+
public boolean equals(Object obj) {
50+
if (obj instanceof Point) {
51+
Point point = (Point) obj;
52+
return x == point.x && y == point.y;
53+
}
54+
return false;
55+
}
56+
57+
@Override
58+
public String toString() {
59+
return String.format("(%.3f / %.3f)", x, y);
60+
}
61+
}
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
package de.dhbwka.exercise.classes;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import java.util.concurrent.ThreadLocalRandom;
6+
7+
import static org.junit.jupiter.api.Assertions.assertEquals;
8+
import static org.junit.jupiter.api.Assertions.assertNotEquals;
9+
10+
/**
11+
* @author Leonhard Gahr
12+
*/
13+
14+
public class PointTest {
15+
private double[][] points = {{1, 3}, {3, 9}, {4, 2}, {-12, 22}, {123, 43}, {0, 0}, {12, 0}, {0, 21}};
16+
private double[][] pointsMirrorX = {{-1, 3}, {-3, 9}, {-4, 2}, {12, 22}, {-123, 43}, {0, 0}, {-12, 0}, {0, 21}};
17+
private double[][] pointsMirrorY = {{1, -3}, {3, -9}, {4, -2}, {-12, -22}, {123, -43}, {0, 0}, {12, 0}, {0, -21}};
18+
private double[][] pointsMirrorOrigin = {{-1, -3}, {-3, -9}, {-4, -2}, {12, -22}, {-123, -43}, {0, 0}, {-12, 0}, {0, -21}};
19+
20+
private double getDistance(double[] p1, double[] p2) {
21+
return Math.sqrt(Math.pow(p1[0] - p2[0], 2) + Math.pow(p1[1] - p2[1], 2));
22+
}
23+
24+
@Test
25+
void testNullConstructor() {
26+
Point origin = new Point();
27+
assertEquals(0, origin.getX());
28+
assertEquals(0, origin.getY());
29+
}
30+
31+
@Test
32+
void testArrayConstructor() {
33+
for (double[] point : points) {
34+
Point arrPoint = new Point(point);
35+
36+
assertEquals(point[0], arrPoint.getX());
37+
assertEquals(point[1], arrPoint.getY());
38+
}
39+
}
40+
41+
@Test
42+
void testValueConstructor() {
43+
for (double[] point : points) {
44+
Point arrPoint = new Point(point[0], point[1]);
45+
46+
assertEquals(point[0], arrPoint.getX());
47+
assertEquals(point[1], arrPoint.getY());
48+
}
49+
}
50+
51+
@Test
52+
void testEquals() {
53+
Point point1 = new Point();
54+
Point point2 = new Point();
55+
assertEquals(point1, point2);
56+
57+
point1 = new Point(1, 3);
58+
point2 = new Point(1, 3);
59+
assertEquals(point1, point2);
60+
61+
point1 = new Point(1, 3);
62+
point2 = new Point(3, 1);
63+
assertNotEquals(point1, point2);
64+
65+
point1 = new Point(3, 3);
66+
point2 = new Point(3, 1);
67+
assertNotEquals(point1, point2);
68+
69+
point1 = new Point(3, 1);
70+
point2 = new Point(-3, 1);
71+
assertNotEquals(point1, point2);
72+
73+
point1 = new Point(3, -1);
74+
point2 = new Point(3, 1);
75+
assertNotEquals(point1, point2);
76+
}
77+
78+
@Test
79+
void testMirrors() {
80+
for (int i = 0; i < points.length; i++) {
81+
Point currPoint = new Point(points[i]);
82+
assertEquals(new Point(pointsMirrorX[i]), currPoint.mirrorXAxis());
83+
assertEquals(new Point(pointsMirrorY[i]), currPoint.mirrorYAxis());
84+
assertEquals(new Point(pointsMirrorOrigin[i]), currPoint.mirrorOrigin());
85+
}
86+
}
87+
88+
@Test
89+
void testDistances() {
90+
for (int i = 0; i < points.length / 2; i++) {
91+
double distance = getDistance(points[i], points[points.length - i - 1]);
92+
Point point1 = new Point(points[i]);
93+
Point point2 = new Point(points[points.length - i - 1]);
94+
95+
assertEquals(distance, point1.distToPoint(point2));
96+
assertEquals(distance, point2.distToPoint(point1));
97+
}
98+
99+
for (double[] point : points) {
100+
double distance = getDistance(point, new double[]{0, 0});
101+
102+
assertEquals(distance, new Point(point).distToOrigin());
103+
}
104+
}
105+
106+
@Test
107+
void testToString() {
108+
for (double[] point : points) {
109+
String expect = String.format("(%.3f / %.3f)", point[0], point[1]);
110+
assertEquals(expect, new Point(point).toString());
111+
}
112+
}
113+
114+
@Test
115+
void randomTests() {
116+
ThreadLocalRandom random = ThreadLocalRandom.current();
117+
for (int i = 0; i < 1000; i++) {
118+
double[] point1Arr = new double[]{random.nextDouble(), random.nextDouble()};
119+
double[] point2Arr = new double[]{random.nextDouble(), random.nextDouble()};
120+
121+
Point point1 = new Point(point1Arr);
122+
Point point2 = new Point(point2Arr);
123+
124+
// mirrors
125+
assertEquals(new Point(new double[]{-point1Arr[0], point1Arr[1]}), point1.mirrorXAxis());
126+
assertEquals(new Point(new double[]{point1Arr[0], -point1Arr[1]}), point1.mirrorYAxis());
127+
assertEquals(new Point(new double[]{-point1Arr[0], -point1Arr[1]}), point1.mirrorOrigin());
128+
129+
// distances
130+
assertEquals(getDistance(point1Arr, point2Arr), point1.distToPoint(point2));
131+
assertEquals(getDistance(point1Arr, point2Arr), point2.distToPoint(point1));
132+
133+
assertEquals(getDistance(point1Arr, new double[]{0, 0}), point1.distToOrigin());
134+
assertEquals(getDistance(point2Arr, new double[]{0, 0}), point2.distToOrigin());
135+
136+
String expect = String.format("(%.3f / %.3f)", point1Arr[0], point1Arr[1]);
137+
assertEquals(expect, point1.toString());
138+
}
139+
}
140+
}
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
package de.dhbwka.exercise.classes;
2+
3+
import lombok.Getter;
4+
import lombok.Setter;
5+
6+
/**
7+
* Radio class
8+
*
9+
* @author Leonhard Gahr
10+
*/
11+
public class Radio {
12+
@Getter
13+
// whether radio is on
14+
private boolean on;
15+
16+
@Getter
17+
@Setter
18+
// volume of the radio (0 - 10)
19+
private int volume;
20+
21+
@Getter
22+
// frequency (85.0 - 110.0)
23+
private double frequency;
24+
25+
Radio() throws IllegalArgumentException {
26+
// initialize radio with default vars
27+
this(true, 5, 85.0);
28+
}
29+
30+
Radio(boolean on, int volume, double frequency) throws IllegalArgumentException {
31+
// check whether arguments are valid
32+
if (0 > volume || 10 < volume) {
33+
throw new IllegalArgumentException(String.format("'%d' is an invalid value for volume. Must be between 0 and 10", volume));
34+
}
35+
36+
if (85.0 > frequency || 110.0 < frequency) {
37+
throw new IllegalArgumentException(String.format("'%f' is an invalid value for frequency. Must be between 85.0 and 110.0", frequency));
38+
}
39+
40+
this.on = on;
41+
this.volume = volume;
42+
this.frequency = frequency;
43+
}
44+
45+
void incVolume() {
46+
// do nothing if the radio is off
47+
if (!this.isOn()) {
48+
return;
49+
}
50+
51+
// do nothing if the volume is max
52+
if (this.getVolume() == 10) {
53+
return;
54+
}
55+
56+
this.setVolume(this.getVolume() + 1);
57+
}
58+
59+
void decVolume() {
60+
// do nothing if the radio is off
61+
if (!this.isOn()) {
62+
return;
63+
}
64+
65+
// do nothing if the volume is min
66+
if (this.getVolume() == 0) {
67+
return;
68+
}
69+
70+
this.setVolume(this.getVolume() - 1);
71+
}
72+
73+
void turnOn() {
74+
this.on = true;
75+
}
76+
77+
void turnOff() {
78+
this.on = false;
79+
}
80+
81+
void setFrequency(double frequency) {
82+
// set frequency to default if it is out of range
83+
if (frequency < 85.0 || frequency > 110.0) {
84+
frequency = 99.9;
85+
}
86+
87+
this.frequency = frequency;
88+
}
89+
90+
@Override
91+
public String toString() {
92+
return String.format("Radio %s; Volume %d; Frequency %.2f Mhz", this.isOn() ? "on" : "off", this.getVolume(), this.getFrequency());
93+
}
94+
}

0 commit comments

Comments
 (0)