Inital commit

This commit is contained in:
Simon Giesel 2019-04-23 11:56:43 +02:00
commit 64604b9bf5
17 changed files with 346 additions and 0 deletions

1
.gitignore vendored Normal file
View file

@ -0,0 +1 @@
*/bin/

18
.vscode/launch.json vendored Normal file
View file

@ -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"
}
]
}

View file

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
<classpathentry kind="src" path="src"/>
<classpathentry kind="output" path="bin"/>
</classpath>

View file

@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<projectDescription>
<name>de.hhn.ai.prog2.blatt1.exceptions</name>
<comment/>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>

View file

@ -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

View file

@ -0,0 +1,11 @@
{
"configurations": [
{
"type": "java",
"name": "CodeLens (Launch) - App",
"request": "launch",
"mainClass": "app.App",
"projectName": "de.hhn.ai.prog2.blatt1.exceptions"
}
]
}

View file

@ -0,0 +1,7 @@
package app;
public class App {
public static void main(String[] args) throws Exception {
PersonService.main();
}
}

View file

@ -0,0 +1,5 @@
package app;
public enum Hobbies {
PROGRAMMIEREN, SCHLAFEN, SONSTIGES, HOBBY4, HOBBY5, HOBBY6
}

View file

@ -0,0 +1,10 @@
package app;
/**
* NoFreeTimeException
*/
public class NoFreeTimeException extends Exception {
private static final long serialVersionUID = 497594916291844818L;
}

View file

@ -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> hobbies = new ArrayList<Hobbies>();
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<Hobbies> 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 + "'" + "}";
}
}

View file

@ -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");
}
}
}

View file

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
<classpathentry kind="src" path="src"/>
<classpathentry kind="output" path="bin"/>
</classpath>

View file

@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<projectDescription>
<name>de.hhn.ai.prog2.blatt1.mathematikaufgaben</name>
<comment/>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>

View file

@ -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

View file

@ -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<de.hhn.ai.prog2.blatt1.mathematikaufgaben>",
"request": "launch",
"mainClass": "app.App",
"projectName": "de.hhn.ai.prog2.blatt1.mathematikaufgaben"
}
]
}

View file

@ -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);
}
}

View file

@ -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<Integer> getDivisors(int input) {
List<Integer> divisors = new ArrayList<Integer>();
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<Integer> 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<Integer> abundants = new ArrayList<Integer>();
List<Integer> perfects = new ArrayList<Integer>();
List<Integer> harshads = new ArrayList<Integer>();
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);
}
}