From 64604b9bf5cdc3fb809f2cb17cf701700bb9f2da Mon Sep 17 00:00:00 2001 From: Simon Giesel Date: Tue, 23 Apr 2019 11:56:43 +0200 Subject: [PATCH] Inital commit --- .gitignore | 1 + .vscode/launch.json | 18 +++ de.hhn.ai.prog2.blatt1.exceptions/.classpath | 6 + de.hhn.ai.prog2.blatt1.exceptions/.project | 17 +++ .../.settings/org.eclipse.jdt.core.prefs | 7 ++ .../.vscode/launch.json | 11 ++ .../src/app/App.java | 7 ++ .../src/app/Hobbies.java | 5 + .../src/app/NoFreeTimeException.java | 10 ++ .../src/app/Person.java | 69 ++++++++++++ .../src/app/PersonService.java | 28 +++++ .../.classpath | 6 + .../.project | 17 +++ .../.settings/org.eclipse.jdt.core.prefs | 7 ++ .../.vscode/launch.json | 21 ++++ .../src/app/App.java | 10 ++ .../src/app/MathLogic.java | 106 ++++++++++++++++++ 17 files changed, 346 insertions(+) create mode 100644 .gitignore create mode 100644 .vscode/launch.json create mode 100644 de.hhn.ai.prog2.blatt1.exceptions/.classpath create mode 100644 de.hhn.ai.prog2.blatt1.exceptions/.project create mode 100644 de.hhn.ai.prog2.blatt1.exceptions/.settings/org.eclipse.jdt.core.prefs create mode 100644 de.hhn.ai.prog2.blatt1.exceptions/.vscode/launch.json create mode 100644 de.hhn.ai.prog2.blatt1.exceptions/src/app/App.java create mode 100644 de.hhn.ai.prog2.blatt1.exceptions/src/app/Hobbies.java create mode 100644 de.hhn.ai.prog2.blatt1.exceptions/src/app/NoFreeTimeException.java create mode 100644 de.hhn.ai.prog2.blatt1.exceptions/src/app/Person.java create mode 100644 de.hhn.ai.prog2.blatt1.exceptions/src/app/PersonService.java create mode 100644 de.hhn.ai.prog2.blatt1.mathematikaufgaben/.classpath create mode 100644 de.hhn.ai.prog2.blatt1.mathematikaufgaben/.project create mode 100644 de.hhn.ai.prog2.blatt1.mathematikaufgaben/.settings/org.eclipse.jdt.core.prefs create mode 100644 de.hhn.ai.prog2.blatt1.mathematikaufgaben/.vscode/launch.json create mode 100644 de.hhn.ai.prog2.blatt1.mathematikaufgaben/src/app/App.java create mode 100644 de.hhn.ai.prog2.blatt1.mathematikaufgaben/src/app/MathLogic.java diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d14e19f --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +*/bin/ \ No newline at end of file diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..47d9d17 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,18 @@ +{ + "configurations": [ + { + "type": "java", + "name": "CodeLens (Launch) - App", + "request": "launch", + "mainClass": "app.App", + "projectName": "de.hhn.ai.prog2.blatt1.exceptions" + }, + { + "type": "java", + "name": "CodeLens (Launch) - App", + "request": "launch", + "mainClass": "app.App", + "projectName": "de.hhn.ai.prog2.blatt1.mathematikaufgaben" + } + ] +} \ No newline at end of file diff --git a/de.hhn.ai.prog2.blatt1.exceptions/.classpath b/de.hhn.ai.prog2.blatt1.exceptions/.classpath new file mode 100644 index 0000000..51a8bba --- /dev/null +++ b/de.hhn.ai.prog2.blatt1.exceptions/.classpath @@ -0,0 +1,6 @@ + + + + + + diff --git a/de.hhn.ai.prog2.blatt1.exceptions/.project b/de.hhn.ai.prog2.blatt1.exceptions/.project new file mode 100644 index 0000000..9f09faa --- /dev/null +++ b/de.hhn.ai.prog2.blatt1.exceptions/.project @@ -0,0 +1,17 @@ + + + de.hhn.ai.prog2.blatt1.exceptions + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + \ No newline at end of file diff --git a/de.hhn.ai.prog2.blatt1.exceptions/.settings/org.eclipse.jdt.core.prefs b/de.hhn.ai.prog2.blatt1.exceptions/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000..0c68a61 --- /dev/null +++ b/de.hhn.ai.prog2.blatt1.exceptions/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,7 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 +org.eclipse.jdt.core.compiler.compliance=1.8 +org.eclipse.jdt.core.compiler.problem.assertIdentifier=error +org.eclipse.jdt.core.compiler.problem.enumIdentifier=error +org.eclipse.jdt.core.compiler.source=1.8 diff --git a/de.hhn.ai.prog2.blatt1.exceptions/.vscode/launch.json b/de.hhn.ai.prog2.blatt1.exceptions/.vscode/launch.json new file mode 100644 index 0000000..b4cae8e --- /dev/null +++ b/de.hhn.ai.prog2.blatt1.exceptions/.vscode/launch.json @@ -0,0 +1,11 @@ +{ + "configurations": [ + { + "type": "java", + "name": "CodeLens (Launch) - App", + "request": "launch", + "mainClass": "app.App", + "projectName": "de.hhn.ai.prog2.blatt1.exceptions" + } + ] +} \ No newline at end of file diff --git a/de.hhn.ai.prog2.blatt1.exceptions/src/app/App.java b/de.hhn.ai.prog2.blatt1.exceptions/src/app/App.java new file mode 100644 index 0000000..33cef59 --- /dev/null +++ b/de.hhn.ai.prog2.blatt1.exceptions/src/app/App.java @@ -0,0 +1,7 @@ +package app; + +public class App { + public static void main(String[] args) throws Exception { + PersonService.main(); + } +} \ No newline at end of file diff --git a/de.hhn.ai.prog2.blatt1.exceptions/src/app/Hobbies.java b/de.hhn.ai.prog2.blatt1.exceptions/src/app/Hobbies.java new file mode 100644 index 0000000..4f2aa37 --- /dev/null +++ b/de.hhn.ai.prog2.blatt1.exceptions/src/app/Hobbies.java @@ -0,0 +1,5 @@ +package app; + +public enum Hobbies { + PROGRAMMIEREN, SCHLAFEN, SONSTIGES, HOBBY4, HOBBY5, HOBBY6 +} \ No newline at end of file diff --git a/de.hhn.ai.prog2.blatt1.exceptions/src/app/NoFreeTimeException.java b/de.hhn.ai.prog2.blatt1.exceptions/src/app/NoFreeTimeException.java new file mode 100644 index 0000000..e5a842b --- /dev/null +++ b/de.hhn.ai.prog2.blatt1.exceptions/src/app/NoFreeTimeException.java @@ -0,0 +1,10 @@ +package app; + +/** + * NoFreeTimeException + */ +public class NoFreeTimeException extends Exception { + + private static final long serialVersionUID = 497594916291844818L; + +} \ No newline at end of file diff --git a/de.hhn.ai.prog2.blatt1.exceptions/src/app/Person.java b/de.hhn.ai.prog2.blatt1.exceptions/src/app/Person.java new file mode 100644 index 0000000..381ebc6 --- /dev/null +++ b/de.hhn.ai.prog2.blatt1.exceptions/src/app/Person.java @@ -0,0 +1,69 @@ +package app; + +import java.util.ArrayList; +import java.util.List; + +/** + * Person + */ +public class Person { + private String preName; + private String lastName; + private int age; + private List hobbies = new ArrayList(); + + public Person(String preName, String lastName, int age) { + setPreName(preName); + setLastName(lastName); + setAge(age); + } + + public String getPreName() { + return this.preName; + } + + public void setPreName(String preName) { + if (preName.isEmpty()) + throw new IllegalArgumentException(); + this.preName = preName; + } + + public String getLastName() { + return this.lastName; + } + + public void setLastName(String lastName) { + if (lastName.isEmpty()) + throw new IllegalArgumentException(); + this.lastName = lastName; + } + + public int getAge() { + return this.age; + } + + public void setAge(int age) { + if (age < 0) + throw new IllegalArgumentException(); + this.age = age; + } + + public List getHobbies() { + return this.hobbies; + } + + public void addHobby(Hobbies hobby) throws NoFreeTimeException { + if (!hobbies.contains(hobby)) + if (hobbies.size() > 4) + throw new NoFreeTimeException(); + else + hobbies.add(hobby); + } + + @Override + public String toString() { + return "{" + " preName='" + preName + "'" + ", lastName='" + lastName + "'" + ", age='" + age + "'" + + ", hobbies='" + hobbies + "'" + "}"; + } + +} \ No newline at end of file diff --git a/de.hhn.ai.prog2.blatt1.exceptions/src/app/PersonService.java b/de.hhn.ai.prog2.blatt1.exceptions/src/app/PersonService.java new file mode 100644 index 0000000..1703919 --- /dev/null +++ b/de.hhn.ai.prog2.blatt1.exceptions/src/app/PersonService.java @@ -0,0 +1,28 @@ +package app; + +/** + * PersonService + */ +public class PersonService { + public static void main() { + Person person = new Person("Max", "Mustermann", 18); + try { + person.addHobby(Hobbies.PROGRAMMIEREN); + person.addHobby(Hobbies.PROGRAMMIEREN); + person.addHobby(Hobbies.SCHLAFEN); + person.addHobby(Hobbies.SONSTIGES); + person.addHobby(Hobbies.HOBBY4); + person.addHobby(Hobbies.HOBBY5); + person.addHobby(Hobbies.HOBBY6); + } catch (Exception e) { + System.err.println("NoFreeTimeException"); + } + System.out.println(person); + try { + Person person2 = new Person("", "Muster", 2); + System.out.println(person2); + } catch (IllegalArgumentException e) { + System.err.println("Fehler beim erstellen einer Person"); + } + } +} \ No newline at end of file diff --git a/de.hhn.ai.prog2.blatt1.mathematikaufgaben/.classpath b/de.hhn.ai.prog2.blatt1.mathematikaufgaben/.classpath new file mode 100644 index 0000000..51a8bba --- /dev/null +++ b/de.hhn.ai.prog2.blatt1.mathematikaufgaben/.classpath @@ -0,0 +1,6 @@ + + + + + + diff --git a/de.hhn.ai.prog2.blatt1.mathematikaufgaben/.project b/de.hhn.ai.prog2.blatt1.mathematikaufgaben/.project new file mode 100644 index 0000000..4a59b98 --- /dev/null +++ b/de.hhn.ai.prog2.blatt1.mathematikaufgaben/.project @@ -0,0 +1,17 @@ + + + de.hhn.ai.prog2.blatt1.mathematikaufgaben + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + \ No newline at end of file diff --git a/de.hhn.ai.prog2.blatt1.mathematikaufgaben/.settings/org.eclipse.jdt.core.prefs b/de.hhn.ai.prog2.blatt1.mathematikaufgaben/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000..0c68a61 --- /dev/null +++ b/de.hhn.ai.prog2.blatt1.mathematikaufgaben/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,7 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 +org.eclipse.jdt.core.compiler.compliance=1.8 +org.eclipse.jdt.core.compiler.problem.assertIdentifier=error +org.eclipse.jdt.core.compiler.problem.enumIdentifier=error +org.eclipse.jdt.core.compiler.source=1.8 diff --git a/de.hhn.ai.prog2.blatt1.mathematikaufgaben/.vscode/launch.json b/de.hhn.ai.prog2.blatt1.mathematikaufgaben/.vscode/launch.json new file mode 100644 index 0000000..631db91 --- /dev/null +++ b/de.hhn.ai.prog2.blatt1.mathematikaufgaben/.vscode/launch.json @@ -0,0 +1,21 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "type": "java", + "name": "Debug (Launch) - Current File", + "request": "launch", + "mainClass": "${file}" + }, + { + "type": "java", + "name": "Debug (Launch)-App", + "request": "launch", + "mainClass": "app.App", + "projectName": "de.hhn.ai.prog2.blatt1.mathematikaufgaben" + } + ] +} \ No newline at end of file diff --git a/de.hhn.ai.prog2.blatt1.mathematikaufgaben/src/app/App.java b/de.hhn.ai.prog2.blatt1.mathematikaufgaben/src/app/App.java new file mode 100644 index 0000000..b87e966 --- /dev/null +++ b/de.hhn.ai.prog2.blatt1.mathematikaufgaben/src/app/App.java @@ -0,0 +1,10 @@ +package app; + +public class App { + public static void main(String[] args) throws Exception { + System.out.println(MathLogic.isAbundant(20)); + System.out.println(MathLogic.isPerfect(28)); + System.out.println(MathLogic.isHarshad(27)); + MathLogic.printAllLists(100); + } +} \ No newline at end of file diff --git a/de.hhn.ai.prog2.blatt1.mathematikaufgaben/src/app/MathLogic.java b/de.hhn.ai.prog2.blatt1.mathematikaufgaben/src/app/MathLogic.java new file mode 100644 index 0000000..b2eba85 --- /dev/null +++ b/de.hhn.ai.prog2.blatt1.mathematikaufgaben/src/app/MathLogic.java @@ -0,0 +1,106 @@ + +package app; + +import java.util.ArrayList; +import java.util.List; + +/** + * MathLogic + */ +public class MathLogic { + + /** + * Get a list of divisors + * + * @param input Integer from which you want to get all divisors + * @return List of Divisors + */ + private static List getDivisors(int input) { + List divisors = new ArrayList(); + for (int i = 1; i < input; i++) { + if (input % i == 0) { + divisors.add(i); + } + } + return divisors; + } + + /** + * Sum all divisors + * + * @param input number wich the divisors shall be summed + * @return added up divisors + */ + private static int sumDivisors(int input) { + List divisors = getDivisors(input); + int count = 0; + for (int divisor : divisors) { + count += divisor; + } + return count; + } + + /** + * Test entered number for abundanz + * + * @param input number wich shall be tested + * @return if `input` is abundant or not? + */ + public static boolean isAbundant(int input) { + int count = sumDivisors(input); + return count > input; + } + + /** + * Test entered number for perfect + * + * @param input number which shall be tested + * @return if `input` is perfect or not? + */ + public static boolean isPerfect(int input) { + int count = sumDivisors(input); + return count == input; + } + + /** + * Test entered number for Harshad-Zahl + * + * @param input number which shall be tested + * @return if `input` is a Harshad-Zahl or not? + */ + public static boolean isHarshad(int input) { + int num = input; + int sum = 0; + while (num > 0) { + sum = sum + num % 10; + num = num / 10; + } + return (input % sum) == 0; + } + + /** + * Print everything + * + * @param limit number from 1 where to go + */ + public static void printAllLists(int limit) { + List abundants = new ArrayList(); + List perfects = new ArrayList(); + List harshads = new ArrayList(); + for (int i = 1; i <= limit; i++) { + if (isAbundant(i)) + abundants.add(i); + if (isPerfect(i)) + perfects.add(i); + if (isHarshad(i)) + harshads.add(i); + } + System.out.println("All abundant numbers from 1 to " + limit); + System.out.println(abundants); + System.out.println("All perfect numbers from 1 to " + limit); + System.out.println(perfects); + System.out.println("All harshad numbers from 1 to " + limit); + System.out.println(harshads); + } + +} \ No newline at end of file