How do I need to further customize this program?

Calculations with finite (floating) point numbers can lead to inaccuracies, so it may be desirable to use integer fractions.

In this task, an object class for representing and calculating fractions is to be created.

The headers of the required methods are already specified and may not be changed. The methods should be completed according to the comments.

Make sure to use these methods whenever possible to avoid duplication.

All methods whose return type is Fraction should create and return a new object and leave the objects used for the calculation unchanged!

The Java standard libraries Math and Integer may (but are not required) be used. To obtain a floating-point number when calculating with integers, typecasting may be necessary!

The following two methods are also already available:

public static int GCD(int x, int y): Returns the greatest common divisor of the two arguments.

public static int LCM(int x, int y): Returns the lowest common multiple of the two arguments.

 public class Fraction { private int numerator, denominator; /** * Gibt den größten gemeinsamen Teiler der beiden Argumente (Greatest Common Divider) zurück. */ public static int GCD(int x, int y) { if (y == 0) return x; return GCD(y, x % y); } /** * Gibt das kleinste gemeinsame Vielfache der beiden Argumente (Lowest Common Multiple) zurück. */ public static int LCM(int x, int y) { return (x * y) / GCD(x, y); } //Beginn der Aufgabe /** * Vollstaendig parametrisierter Konstruktor der Zaehler und Nenner * uebergeben bekommt und die entsprechenden Attribute setzt. * Negative Vorzeichen (Zahlen kleiner als Null) duerfen nur im Zaehler * auftreten (nicht im "denominator"-Attribut). * Die Uebergabe eines negativen Nenners ("denominator"-Argument) an den Konstruktor ist jedoch zulaessig. * Der Konstruktor muss also den uebergebenen Nenner pruefen und sein Vorzeichen so behandeln, * dass der resultierende Bruch (die Attribute) die genannte Restriktion erfüllt * und der Wert des Bruchs (die Argumente) unverändert bleibt * (ein negatives Vorzeichen im Nenner muss also methematisch korrekt beseitigt werden). * Wird eine Null als Nenner uebergeben, so wird das entsprechende Attribut * auf Eins gesetzt. * Jeder erzeugte Bruch wird gekuerzt (dazu soll die entsprechende Mehode su verwendet werden). */ public Fraction(int numerator, int denominator) { if(pDenominator<0){ numerator = -pNumerator; denominator = -pDenominator; } if(pDenominator == 0){ denominator = 1; } } /** * Gibt den Nenner zurueck. */ public int getDenominator() { return denominator; } /** * Gibt den Zaehler zurueck. */ public int getNumerator() { return numerator; } /** * Gibt den Bruch als Gleitkommazahl zurueck. */ public double toDouble() { double fraction = numerator / denominator; return fraction; } /** * Gibt einen String im Format * "Zaehler/Nenner" zurueck. */ public String toString() { double fraction = toDouble(); return numerator + "/" + denominator + " = " + fraction; } /** * Kuerzt (vereinfacht) den Bruch. */ public void shorten() { int factorC = GCD(numerator, denominator); numerator = numerator/factorC; denominator = denominator/factorC; } /** * Erweitert (macht gleichnamig), addiert dann den uebergebenen Bruch. */ public Fraction add(Fraction f) { numerator = numerator * f.denominator + f.numerator * denominator; denominator = denominator * f.denominator; cancel(); } /** * Multipliziert mit dem uebergebenen Bruch. */ public Fraction multiply(Fraction f) { numerator = numerator * f.numerator; denominator = denominator * f.denominator; cancel(); } /** * Bildet den Kehrwert, wenn der Zaehler ungleich Null ist. * Sonst wird der Bruch unveraendert zurueckgegeben. */ public Fraction reciprocal() { } /** * Dividiert durch den uebergebenen Bruch * (unter Verwendung von Kehrwert und Multiplikation). */ public Fraction divide(Fraction f) { numerator = numerator * f.denominator; denominator = denominator * f.numerator; cancel(); } }
(1 votes)
Loading...

Similar Posts

Subscribe
Notify of
3 Answers
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
PWolff
1 year ago

(For source code, there is the symbol in the formatting bar above the input field. This makes it much easier to read.)

Maybe it’s easier to remove and rebuild all the methods (except those of GCD and LCM – which are correct, at least for non-negative arguments, if they could also improve) (parts of the source code can be used – perhaps – as a collection of ideas). There are quite a lot of construction sites here that seem to me. The fact that a method “cancel” is called, but a method “short” is defined is still one of the smaller problems.

I would write the rules of the breaking bill into a list and edit this list.

Where I would ask back, would it be with the constructor – is it really wanted that a denominator 0 is replaced tacitly by 1? and at the reciprocal – is it really intended to return 0 unchanged? (without express written instruction confirmed by signature, I would make an exception in both cases, and also expect the customer to complain later, and then calculate him a few hours of work if he wants to have it differently later, even if it only costs me a few seconds because I have prepared this behavior)

Constructor: if – as is too often – the arguments of the constructor are as important as the fields of the object to be generated, it is necessary to use this. (unless the compiler used can generate this “oilerplate” code from itself). Then I would ask myself if the constructor should not be shortened. (Also a break is produced in the constructor, and every fracture produced is to be shortened.) – On the other hand, the task stops as if unbridged fractures were also to be taken over unabated, even if this was stupid. After all, it says that “shorten” is public. I would also like to confirm this in writing and to prepare the reasonable behaviour.

Since these methods are intended to return new objects, it is not possible to change the fields of “this” (and, of course, not those of the method argument); you have to create new variables. At the end of the method

return new Fraction(newNumerator, newDenominator);

A further consideration to be taken is whether in the present (unusable) form the signs of the return values are properly treated. If no, you have to pay attention.

And if one finds that this is each individual confusing You can also ask yourself if you don’t have to “short” or “cancel” also delegates and, of course, whether it is not sufficient to call “shorten”/”cancel” from a single place, namely the constructor.

Another question would be whether “subtract” should be included.

C# can define implicit conversions from and into own types, whether Java can do so, I have not found. If so, I would define an automatic implicit cast by int and integer in Fraction. If no, I would ask whether computing operations with integers should be defined.

As far as I know, it is common to define static methods of the same name as those referring to “this” and a method argument that accept two arguments. This is done quickly, e.g.

public static Fraction add(Fraction a, Fraction b) {
    return a.add(b); // in this case, no problem because Fraction.add returns a new object
}

… but this is not required here and would perhaps be an undesirable extension of the task.

When testing, “toDouble” probably returns 0.0 if applied to 2/3. If you can say why, you also know where the typecasts mentioned in the task must be inserted.

One more question: perhaps you should get used to stupid customer requests now. Then, m. E., it would be absolutely necessary that you also learn to sign to the customer that you have pointed him to “unusual” behavior and that he expressly wants this behavior.

(Of course I would “divide”

public Fraction divide(Fraction f) {
    return multiply(reciprocal(f));
}

simplify. But in a case like this, only after testing whether the misconduct in Division is correctly reproduced by 0.)

And even in “toString()” I would ask whether it is really desired that not only the break, but also the decimal value should be output – also connected to “=” and point out that in this way it will look quite confusing when fractures are output in composite expressions.

Ultimately, however, this is not worse than I used it from my profession (software developer) from customer requests. Seufz.

PWolff
1 year ago
Reply to  Miraaa1319

Your constructor has the useful functionality, but, as I already noted, does not meet the (i.e. abundantly insane and counterproductive) requirements.

According to this, a denominator 0 is to be replaced implied (!) by 1.

This is obviously the reason why the Unit Test fails.

Furthermore, the specification can also be understood in such a way that the constructor (and only there) is not to be shortened.

—–

As mentioned in my reply, I would be able to confirm such specifications individually in writing outside such pure teaching specifications, including the acceptance of the warning of later possibly serious and possibly difficult to find and correct misconduct. The customer is often no bit less mad enough, but at least it’s less likely to be seen in court.

In the concrete case WIRD it happens at some point in the case of an actual library that a denominator 0 is passed over, and in the case of a thrown error the application at least crashes, while otherwise it continues to calculate with a completely wrong value. As a result of similar mistakes, more often with a stroke of zig million have been destroyed.

—-

What else to think: Could you explain why

return (double) numerator / denominator;

works? (Tipp: precedence of operators)