Skip to content

Commit e8b87b6

Browse files
committed
add new solutions for third lecture;
optimize old tasks cuz I'm bored;
1 parent 72a3b5d commit e8b87b6

File tree

13 files changed

+314
-29
lines changed

13 files changed

+314
-29
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package de.dhbwka.exercise.control;
2+
3+
import static de.dhbwka.exercise.utility.ScannerUtility.getLong;
4+
5+
/**
6+
* @author Leonhard Gahr
7+
*/
8+
public class AddUp {
9+
public static void main(String[] args) {
10+
addUpDoWhile();
11+
addUpWhile();
12+
}
13+
14+
private static void addUpDoWhile() {
15+
long num = 0;
16+
long input = 0;
17+
do {
18+
num += input;
19+
} while ((input = getLong("Enter number (<0 for abortion): ")) >= 0);
20+
21+
System.out.println("Sum: " + num);
22+
}
23+
24+
private static void addUpWhile() {
25+
long num = 0;
26+
long input;
27+
while ((input = getLong("Enter number (<0 for abortion): ")) >= 0) {
28+
num += input;
29+
}
30+
System.out.println("Sum: " + num);
31+
}
32+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package de.dhbwka.exercise.control;
2+
3+
import static de.dhbwka.exercise.utility.ScannerUtility.getLong;
4+
5+
/**
6+
* @author Leonhard Gahr
7+
*/
8+
public class Babylon {
9+
public static void main(String[] args) {
10+
long num;
11+
System.out.print("Sqrt number: ");
12+
while ((num = getLong("Sqrt number: ")) >= 0) {
13+
System.out.printf("Sqrt of %d is %.6f%n", num, sqrt(num));
14+
}
15+
}
16+
17+
private static double sqrt(double num) {
18+
double x = (num + 1) / 2;
19+
return sqrt(num, x);
20+
}
21+
22+
private static double sqrt(double num, double x) {
23+
System.out.println("xn: " + x);
24+
double newX = (x + (num / x)) / 2;
25+
if (Math.abs(newX - x) < 10e-6) {
26+
return x;
27+
}
28+
return sqrt(num, newX);
29+
}
30+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package de.dhbwka.exercise.control;
2+
3+
import static de.dhbwka.exercise.utility.ScannerUtility.getLong;
4+
5+
/**
6+
* @author Leonhard Gahr
7+
*/
8+
public class Deer {
9+
public static void main(String[] args) {
10+
long deer = getLong("Enter start number of deers: ");
11+
int counter = 0;
12+
String format = "%d: %d%n";
13+
do {
14+
deer = (long) (deer * 1.1) - 15;
15+
System.out.format(format, ++counter, deer);
16+
} while (deer < 300);
17+
}
18+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package de.dhbwka.exercise.control;
2+
3+
import static de.dhbwka.exercise.utility.ScannerUtility.getLong;
4+
5+
/**
6+
* @author Leonhard Gahr
7+
*/
8+
public class LeapYear {
9+
public static void main(String[] args) {
10+
long year;
11+
while ((year = getLong("Enter year: ")) > 0) {
12+
System.out.format("%04d is %sa leap year\n", year, isLeapYear(year) ? "" : "not ");
13+
}
14+
}
15+
16+
private static boolean isLeapYear(long year) {
17+
if (year % 4 != 0) {
18+
return false;
19+
} else if (year % 400 == 0) {
20+
return true;
21+
} else return year % 100 != 0;
22+
}
23+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package de.dhbwka.exercise.control;
2+
3+
/**
4+
* @author Leonhard Gahr
5+
*/
6+
public class MultiplicationTable {
7+
public static void main(String[] args) {
8+
for (int y = 1; y <= 10; ++y) {
9+
for (int x = 1; x <= 10; ++x) {
10+
System.out.format("%4d", y * x);
11+
}
12+
System.out.println();
13+
}
14+
}
15+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package de.dhbwka.exercise.control;
2+
3+
import java.util.Scanner;
4+
import java.util.concurrent.ThreadLocalRandom;
5+
6+
import static de.dhbwka.exercise.utility.ScannerUtility.getInt;
7+
8+
/**
9+
* @author Leonhard Gahr
10+
*/
11+
public class NumberGuess {
12+
public static void main(String[] args) {
13+
String guessTemplate = "%s, guess a number [1 - 100]: ";
14+
String guessHighTemplate = "Try %d: %d is too high.%n";
15+
String guessLowTemplate = "Try %d: %d is too low.%n";
16+
String guessCorrectTemplate = "Try %d: %d is correct.%n";
17+
String endMessage = "What would you like to do?\n" +
18+
"0 - Exit\n" +
19+
"1 - Continue ";
20+
int[] states = new int[]{0, 1};
21+
22+
Scanner scanner = new Scanner(System.in);
23+
System.out.print("What's your name? ");
24+
String name = scanner.nextLine();
25+
26+
int state = 1;
27+
while (state == 1) {
28+
int tryNum = 0;
29+
int num = ThreadLocalRandom.current().nextInt(1, 101);
30+
int guess;
31+
32+
do {
33+
guess = getInt(String.format(guessTemplate, name));
34+
35+
if (guess > num) {
36+
System.out.printf(guessHighTemplate, ++tryNum, guess);
37+
} else if (guess < num) {
38+
System.out.printf(guessLowTemplate, ++tryNum, guess);
39+
} else {
40+
System.out.printf(guessCorrectTemplate, ++tryNum, guess);
41+
}
42+
} while (num != guess);
43+
state = getInt(endMessage, states);
44+
}
45+
}
46+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package de.dhbwka.exercise.control;
2+
3+
/**
4+
* @author Leonhard Gahr
5+
*/
6+
public class ShoeSize {
7+
private static double shoeSizeToMaxCentimeter(int shoeSize) {
8+
return ((double) shoeSize) / 1.5;
9+
}
10+
11+
private static double shoeSizeToMinCentimeter(int shoeSize) {
12+
return shoeSizeToMaxCentimeter(shoeSize - 1);
13+
}
14+
15+
public static void main(String[] args) {
16+
String format = "%.2f - %.2f | %d%n";
17+
18+
System.out.println("Zentimeter | Größe");
19+
System.out.println("--------------+------");
20+
for (int i = 30; i < 50; ++i) {
21+
System.out.format(format, shoeSizeToMinCentimeter(i), shoeSizeToMaxCentimeter(i), i);
22+
}
23+
}
24+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package de.dhbwka.exercise.control;
2+
3+
/**
4+
* @author Leonhard Gahr
5+
*/
6+
public class TemperatureTable {
7+
public static void main(String[] args) {
8+
String format = "%-10d | %-4f %n";
9+
10+
System.out.format("Fahrenheit | Celsius%n");
11+
System.out.format("-----------+--------%n");
12+
for (int i = 0; i <= 300; i += 20) {
13+
System.out.format(format, i, fahrenheitToCelsius(i));
14+
}
15+
}
16+
17+
private static float fahrenheitToCelsius(int fahrenheit){
18+
return (5f/9f) * (fahrenheit - 32f);
19+
}
20+
}

src/de/dhbwka/exercise/datatypes/Round.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,17 @@ private static int retardedRound(double value) {
2525
}
2626

2727
private static int retardedRound(char[] chars, int state) {
28-
int n=chars.length-1;
28+
int n = chars.length - 1;
2929

30-
char[] temp=new char[n];
31-
System.arraycopy(chars,1,temp,0,n);
30+
char[] temp = new char[n];
31+
System.arraycopy(chars, 1, temp, 0, n);
3232
char currentChar = chars[0];
3333

3434
if (currentChar == 46 && state == NOTFOUND) {
3535
return retardedRound(temp, FOUND);
3636
} else if (state == FOUND && Character.getNumericValue(currentChar) >= 5) {
3737
return Round.chars[0] == 45 ? -1 : 1;
38-
} else if (state == FOUND && Character.getNumericValue(currentChar) < 5) {
38+
} else if (state == FOUND && Character.getNumericValue(currentChar) < 5) {
3939
return 0;
4040
}
4141
return retardedRound(temp, NOTFOUND);

src/de/dhbwka/exercise/operators/Easter.java

Lines changed: 35 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,41 +3,53 @@
33
import java.text.ParseException;
44
import java.text.SimpleDateFormat;
55
import java.util.Date;
6+
import java.util.InputMismatchException;
7+
import java.util.Scanner;
68

79
/**
810
* @author Leonhard Gahr
911
*/
1012
public class Easter {
11-
public static void main(String[] args) {
12-
int year = 2020;
13-
int offset = easter(year);
13+
public static void main(String[] args) throws ParseException {
14+
while (true) {
15+
System.out.print("Enter year: ");
16+
long year;
17+
18+
try {
19+
Scanner in = new Scanner(System.in);
20+
year = in.nextLong();
21+
} catch (InputMismatchException e) {
22+
System.out.print("Illegal character");
23+
System.out.println(String.format("Number must be between %s and %s", Long.toString(Long.MIN_VALUE), Long.toString(Long.MAX_VALUE)));
24+
continue;
25+
}
26+
27+
if (year <= 0) {
28+
break;
29+
}
30+
int offset = easter(year);
31+
32+
SimpleDateFormat myFormat = new SimpleDateFormat("dd MM yyyy");
33+
String inputString1 = "01 03 " + year;
1434

15-
SimpleDateFormat myFormat = new SimpleDateFormat("dd MM yyyy");
16-
String inputString1 = "01 03 " + year;
17-
18-
try {
1935
Date date1 = myFormat.parse(inputString1);
2036
date1.setDate(offset);
2137
System.out.println(String.format("%02d/%02d/%04d", date1.getDate(), date1.getMonth(), year));
22-
} catch (ParseException e) {
23-
e.printStackTrace();
2438
}
25-
26-
2739
}
2840

2941

30-
private static int easter(int year) {
31-
int a = year % 19;
32-
int b = year % 4;
33-
int c = year % 7;
34-
int k = year / 100;
35-
int p = (8 * k + 13) / 25;
36-
int q = k / 4;
37-
int m = (15 + k - p - q) % 30;
38-
int n = (4 + k - q) % 7;
39-
int d = (19 * a + m) % 30;
40-
int e = (2 * b + 4 * c + 6 * d + n) % 7;
41-
return (22 + d + e);
42+
private static int easter(long year) {
43+
long a = year % 19;
44+
long b = year % 4;
45+
long c = year % 7;
46+
long k = year / 100;
47+
long p = (8 * k + 13) / 25;
48+
long q = k / 4;
49+
long m = (15 + k - p - q) % 30;
50+
long n = (4 + k - q) % 7;
51+
long d = (19 * a + m) % 30;
52+
long e = (2 * b + 4 * c + 6 * d + n) % 7;
53+
return (int) (22 + d + e);
4254
}
4355
}

src/de/dhbwka/exercise/operators/IncrementDecrement.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,19 @@
66
public class IncrementDecrement {
77
public static void main(String[] args) {
88
int i = 0;
9-
int j = 0;
10-
j = ++i;
9+
int j = ++i;
1110
int k = j++ + ++i;
11+
1212
System.out.println("k: " + k);
1313
System.out.println("*: " + j++ + ++i);
1414
System.out.println(j++ + ++i);
15+
1516
int m = j++ * ++i;
17+
1618
System.out.println("m: " + m);
19+
1720
int n = --j * --i;
21+
1822
System.out.println("n: " + n);
1923
System.out.println("i: " + i);
2024
System.out.println("j: " + j);

src/de/dhbwka/exercise/operators/Priority.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,26 @@ public class Priority {
77
public static void main(String[] args) {
88
System.out.println("1: " + (5 / 2 * 2));
99
System.out.println("2: " + (9. / 2 * 5));
10+
1011
boolean a = true, b = false, c = false;
12+
1113
System.out.println("3: " + (a && b || c));
14+
1215
char ch = 'c';
16+
1317
System.out.println("4: " + ('a' + 1 < ch));
18+
1419
int i = 1, j = 2, k = 3;
20+
1521
System.out.println("5: " + (-i - 5 * j >= k + 1));
1622
i = 1;
23+
1724
if (a || (++i == 2)) {
1825
System.out.println("6: " + i);
1926
}
27+
2028
i = 1;
29+
2130
if (a | (++i == 2)) {
2231
System.out.println("7: " + i);
2332
}

0 commit comments

Comments
 (0)