Java Training Course/JT06: Difference between revisions
Jump to navigation
Jump to search
imported>Gfis No edit summary |
imported>Gfis not yet object-oriented |
||
Line 1: | Line 1: | ||
== | ==Preliminary Class ''Ratio''== | ||
As | As a preparation we implement a Java class which represents fractions and arithmetic operations thereon, but the operations (''methods'' in Java terminology) are all static. They are implemented in a rather straight-forward way, and they are not yet really object-oriented. | ||
Below you will find a skeleton for the code. The task of this session is | |||
* Fill in the bodies of the following methods if necessary. | * Fill in the bodies of the following methods if necessary. | ||
* Thereby make as much use of other methods as possible. | * Thereby make as much use of other methods as possible. | ||
* Insert proper documentation comments. | * Insert proper documentation comments. | ||
* Arrange the methods of the class in alphabetical order (since there are many). | * Arrange the methods of the class in alphabetical order (since there are many). | ||
Line 213: | Line 214: | ||
} // Rational | } // Rational | ||
[[Java Training Course/JT05|< Previous: JT05]] Control Structures: The Greatest Common Divisor<br /> | [[Java Training Course/JT05|< Previous: JT05]] Control Structures: The Greatest Common Divisor<br /> | ||
[[Java Training Course/JT07|> Next: JT07]] | [[Java Training Course/JT07|> Next: JT07]] Class ''Rational'' |
Revision as of 19:34, 26 September 2017
Preliminary Class Ratio
As a preparation we implement a Java class which represents fractions and arithmetic operations thereon, but the operations (methods in Java terminology) are all static. They are implemented in a rather straight-forward way, and they are not yet really object-oriented.
Below you will find a skeleton for the code. The task of this session is
- Fill in the bodies of the following methods if necessary.
- Thereby make as much use of other methods as possible.
- Insert proper documentation comments.
- Arrange the methods of the class in alphabetical order (since there are many).
Desired Methods
public Rational abs() { return new Rational((numerator < 0 ? - numerator : numerator), denominator) } public Rational add(Rational rat2) { ... } public int compareTo(Rational rat2) { /* return -1, 0, +1 iff this < = > rat2 */ ... } public Rational divide(Rational rat2) { ... } public boolean equals(Rational rat2) { return this.compareTo(rat2) == 0; } public boolean isInteger() { return denominator == 1; } public int intValue() { return numerator / denominator; } public Rational max(Rational rat2) { return this.compareTo(rat2) > 0 ? this : rat2 } public Rational min(Rational rat2) { return this.compareTo(rat2) < 0 ? this : rat2 } public Rational multiply(Rational rat2) { ... } public Rational negate() { return new Rational(- numerator, denominator); } // public Rational reduce() is already implemented below // public Rational subtract(Rational rat2) is already implemented below
Starting Code
/* Fractions of integers * @(#) $Id$ * 2017-09-23, Georg Fischer */ // package name will later go here // imports will later go here /** Class <wm>Rational represents an integer fraction * together with the arithmetic operations on such Rationals. * The denominator is always > 0. All arithmetic operations * return reduced fractions where GCD(numerator, denominator) = 1. * The methods have name, parameters and return values analogous to methods * of the Java object type BigInteger, see * https://docs.oracle.com/javase/7/docs/api/java/math/BigInteger.html */ public class Rational { //---------------- // Internal Properties //---------------- /** value above the bar, before the slash */ private int numerator; /** value below the bar, after the slash */ private int denominator; /* Implement the following methods, and make use of basic operations. All Rational return values should be reduced. Insert proper documentation comments. Always arrange all methods of the class in alphabetical order, since there are many. */ public Rational abs() { return this; } public Rational add(Rational rat2) { return this; } public int compareTo(Rational rat2) { return 0; } // return -1, 0, +1 iff this < = > rat2 public Rational divide(Rational rat2) { return this; } public boolean equals(Rational rat2) { return this.compareTo(rat2) == 0; } public boolean isInteger() { return denominator == 1; } public int intValue() { return numerator / denominator; } public Rational max(Rational rat2) { return this; } public Rational min(Rational rat2) { return this; } public Rational multiply(Rational rat2) { return this; } public Rational negate() { return this; } // public Rational reduce() is already implemented below // public Rational subtract(Rational rat2) is already implemented below //---------------- // Constructors //---------------- /** No-args constructor, creates 1/1 */ public Rational() { numerator = 1; denominator = 1; } // no-args constructor /** Constructor with numerator, creates a/1 * @param a numerator */ public Rational(int a) { numerator = a; denominator = 1; } // constructor(int) /** Constructor with numerator and denominator, creates a/b * @param a numerator * @param b denominator */ public Rational(int a, int b) { numerator = a; denominator = b; } // constructor(int, int) /** Constructor from a String representation, creates a/b * @param str String of the form "a/b" */ public Rational(String str) { int slashPos = str.indexOf("/"); if (slashPos < 0) { str += "/1"; slashPos = str.length() - 2; } numerator = Integer.parseInt(str.substring(0, slashPos)); denominator = Integer.parseInt(str.substring(slashPos + 1)); } // constructor(String) //---------------- // Internal getters //---------------- /** Gets the numerator * @return the numerator of this Rational */ private int getNum() { return numerator; } // getNum() /** Gets the denominator * @return the denominator of this Rational */ private int getDen() { return denominator; } // getDen() //---------------- // Public methods //---------------- /** Returns the greatest common divisor of 2 integers. * @param a 1st integer * @param b 2nd integer * @return gcd(a,b), which is always positive */ public static int gcd(int a, int b) { int result = 0; // not yet implemented return result; } // gcd(int,int) /** Returns the least common multiple of 2 integers. * @param a 1st integer * @param b 2nd integer * @return lcm(a,b) */ public static int lcm(int a, int b) { int result = a * b; if (result < 0) { // absolute, make positive result = - result; } // abs return result / gcd(a, b); } // lcm(int,int) /** Reduces and normalizes the fraction, that means: * <ul> * <li>divides the numerator and the denominator by * their greatest common divisor, if that is > 1,</li> * <li>makes the denominator always positive,</li> * <li>normalizes the denominator to 1 if the numerator is 0.</li> * </ul> */ public Rational reduce() { int common = gcd(numerator, denominator); if (common > 1) { numerator /= common; denominator /= common; } if (numerator == 0) { denominator = 1; } else if (denominator < 0) { numerator = - numerator; denominator = - denominator; } return this; } // reduce() /** Returns the difference between this Rational and a second. * @param rat2 2nd Rational * @return (this - rat2) */ public Rational subtract(Rational rat2) { return this.add(rat2.negate()); } // subtract(Rational) /** Returns this Rational as a String. * @return a String of the form "a/b", or only "a" if b is 1. */ public String toString() { String result = String.valueOf(numerator); if (denominator != 1) { result += "/" + String.valueOf(denominator); } return result; } // toString() //================ /** Test program, shows a series of fixed operations * @param args String array of commandline arguments */ public static void main(String[] args) { int iarg = 1; String str0 = "0/1"; Rational rat0 = new Rational(str0); while (iarg < args.length) { String str1 = args[iarg]; Rational rat1 = new Rational(str1); System.out.println(str0 + " + " + str1 + " = " + rat0.add (rat1).toString()); System.out.println(str0 + " - " + str1 + " = " + rat0.subtract(rat1).toString()); System.out.println(str0 + " * " + str1 + " = " + rat0.multiply(rat1).toString()); System.out.println(str0 + " / " + str1 + " = " + rat0.divide (rat1).toString()); str0 = str1; rat0 = rat1; iarg ++; } // while iarg } // main } // Rational
< Previous: JT05 Control Structures: The Greatest Common Divisor
> Next: JT07 Class Rational