Antwort mit KI erzeugt
Lösung:
Um die Aufgabe zu lösen, müssen wir zunächst eine Klasse
Fraction definieren, die Brüche darstellt und die Grundoperationen wie Addition und Multiplikation unterstützt. Anschließend erstellen wir die Klasse
Testat mit der Methode
apply, die die beschriebenen Anforderungen erfüllt.
Hier ist der vollständige Java-Code:
java
class Fraction {
private int numerator;
private int denominator;
// Konstruktor
public Fraction(int numerator, int denominator) {
if (denominator == 0) {
throw new IllegalArgumentException("Denominator cannot be zero");
}
this.numerator = numerator;
this.denominator = denominator;
simplify();
}
// Methode zur Bruchaddition
public Fraction add(Fraction other) {
int newNumerator = this.numerator * other.denominator + other.numerator * this.denominator;
int newDenominator = this.denominator * other.denominator;
return new Fraction(newNumerator, newDenominator);
}
// Methode zur Bruchmultiplikation
public Fraction multiply(Fraction other) {
int newNumerator = this.numerator * other.numerator;
int newDenominator = this.denominator * other.denominator;
return new Fraction(newNumerator, newDenominator);
}
// Methode zur Vereinfachung des Bruchs
private void simplify() {
int gcd = gcd(numerator, denominator);
numerator /= gcd;
denominator /= gcd;
}
// Methode zur Berechnung des ggT (größter gemeinsamer Teiler)
private int gcd(int a, int b) {
if (b == 0) {
return a;
}
return gcd(b, a % b);
}
@Override
public String toString() {
return numerator + "/" + denominator;
}
}
class Testat {
public static Fraction apply(Fraction[] arr, Fraction x) {
if (arr.length < 3) {
return x.multiply(new Fraction(2, 1)); // Rückgabe von 2 * x
} else {
Fraction sum = arr[0].add(arr[1]).add(x); // Summe aus den ersten zwei Elementen in arr und x
return sum;
}
}
public static void main(String[] args) {
// Testfälle
Fraction[] arr1 = { new Fraction(1, 1), new Fraction(1, 2), new Fraction(1, 3), new Fraction(1, 4), new Fraction(1, 5), new Fraction(1, 6), new Fraction(1, 7) };
Fraction x1 = new Fraction(1, 7);
System.out.println(apply(arr1, x1)); // Erwartete Ausgabe: 23/14
Fraction[] arr2 = { new Fraction(1, 1), new Fraction(1, 2), new Fraction(1, 3), new Fraction(1, 4), new Fraction(1, 5), new Fraction(1, 6) };
Fraction x2 = new Fraction(2, 7);
System.out.println(apply(arr2, x2)); // Erwartete Ausgabe: 25/14
Fraction[] arr3 = { new Fraction(1, 2), new Fraction(1, 3), new Fraction(1, 4), new Fraction(1, 5), new Fraction(1, 6) };
Fraction x3 = new Fraction(6, 5);
System.out.println(apply(arr3, x3)); // Erwartete Ausgabe: 61/30
Fraction[] arr4 = { new Fraction(2, 1), new Fraction(1, 3), new Fraction(1, 4) };
Fraction x4 = new Fraction(3, 5);
System.out.println(apply(arr4, x4)); // Erwartete Ausgabe: 44/15
Fraction[] arr5 = { new Fraction(1, 5), new Fraction(1, 6) };
Fraction x5 = new Fraction(1, 2);
System.out.println(apply(arr5, x5)); // Erwartete Ausgabe: 1/1
Fraction[] arr6 = { new Fraction(1, 5) };
Fraction x6 = new Fraction(2, 7);
System.out.println(apply(arr6, x6)); // Erwartete Ausgabe: 4/7
Fraction[] arr7 = {};
Fraction x7 = new Fraction(2, 7);
System.out.println(apply(arr7,x7)); // Erwartete Ausgabe: 4/7
}
}
Erklärung:
1. Die Klasse
Fraction repräsentiert Brüche und unterstützt Grundoperationen wie Addition (
add) und Multiplikation (
multiply).
2. Die Methode
simplify vereinfacht den Bruch durch Berechnung des größten gemeinsamen Teilers (
gcd).
3. Die Klasse
Testat enthält die statische Methode
apply, welche die beschriebenen Anforderungen erfüllt:
- Wenn das Array
arr weniger als drei Elemente hat, wird das doppelte von
x zurückgegeben.
- Wenn
arr mindestens drei Elemente hat, wird die Summe von
x und den ersten beiden Elementen von
arr zurückgegeben.
4. Im
main-Methode werden die Testfälle gemäß der Aufgabenstellung überprüft.
Die Methode
apply nutzt die definierten Methoden zur Bruch-Addition und -Multiplikation, um das erwartete Resultat zu erzielen.