Numbers (flexiblere Version von Zahlenfilter)
This commit is contained in:
parent
c12a15dc97
commit
90d7463204
39
Numbers.java
Normal file
39
Numbers.java
Normal file
@ -0,0 +1,39 @@
|
|||||||
|
public class Numbers {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
for (int i = 1; i <= 200; i++) {
|
||||||
|
if (isDivisibleBy(5, i)) {
|
||||||
|
System.out.println(i + " ist durch " + 5 + " teilbar!");
|
||||||
|
} else if (lastDigitIs(9, i)) {
|
||||||
|
System.out.println(i + " endet auf " + 9);
|
||||||
|
} else if (sumWithPredecessorIsDivisibleBy(3, i)) {
|
||||||
|
System.out.println(i + " und " + (i - 1) + " addiert ergeben " + (2*i-1) + " und " + (2*i-1) + " ist durch " + 3 + " teilbar");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static boolean isDivisibleBy( int divisor, int number) {
|
||||||
|
if (number % divisor == 0 ) {
|
||||||
|
return true;
|
||||||
|
} else {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static boolean lastDigitIs( int digit , int number) {
|
||||||
|
if (number % 10 == digit) {
|
||||||
|
return true;
|
||||||
|
} else {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static boolean sumWithPredecessorIsDivisibleBy( int divisor, int number) {
|
||||||
|
int s = 2 * number - 1;
|
||||||
|
if (isDivisibleBy(divisor, s)) {
|
||||||
|
return true;
|
||||||
|
} else {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
42
Visitenkarte.java
Normal file
42
Visitenkarte.java
Normal file
@ -0,0 +1,42 @@
|
|||||||
|
public class Visitenkarte {
|
||||||
|
|
||||||
|
private String name;
|
||||||
|
private String university;
|
||||||
|
private String street;
|
||||||
|
private int number;
|
||||||
|
private int postCode;
|
||||||
|
private String city;
|
||||||
|
|
||||||
|
|
||||||
|
public Visitenkarte (String name, String university, String street, int number, int postCode, String city) {
|
||||||
|
this.name = name;
|
||||||
|
this.university = university;
|
||||||
|
this.street = street;
|
||||||
|
this.number = number;
|
||||||
|
this.postCode = postCode;
|
||||||
|
this.city = city;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String toString() {
|
||||||
|
String e = String.format("|%-30s|", " ").replace(' ', '-');
|
||||||
|
String n = String.format("|%-30s|", name);
|
||||||
|
String u = String.format("|%-30s|", university);
|
||||||
|
String filler = String.format("|%-30s|", " ");
|
||||||
|
String s = String.format("|%-30s|", street);
|
||||||
|
String address = street + " " + number;
|
||||||
|
String a = String.format("|%-30s|", address);
|
||||||
|
String postAndCity = postCode + " " + city;
|
||||||
|
String p = String.format("|%-30s|", postAndCity);
|
||||||
|
return e + "\n" + n + "\n" + u + "\n" + filler + "\n" + a + "\n" + p + "\n" + e;
|
||||||
|
}
|
||||||
|
public static void main(String[] args) {
|
||||||
|
String n = "Prof. Dr. Georg-Simon Ohm";
|
||||||
|
String u = "TH Nürnberg";
|
||||||
|
String s = "Keßlerplatz";
|
||||||
|
int nu = 12;
|
||||||
|
int p = 90489;
|
||||||
|
String c = "Nuremberg";
|
||||||
|
Visitenkarte One = new Visitenkarte(n, u, s, nu, p, c);
|
||||||
|
System.out.println(One);
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user