Java Training Course/JT06: Difference between revisions
Jump to navigation
Jump to search
imported>Gfis new |
imported>Gfis Version 2 |
||
Line 1: | Line 1: | ||
==Skeleton of Class ''Rational''== | ==Skeleton of Class ''Rational''== | ||
As announced in [[Java Training Course/JT05|JT05]], we now implement a useful type of objects with operations as a Java class. Below you will find a skeleton for the code. The task of this session is | |||
/* | * Fill in the bodies of the following methods. | ||
* Thereby make as much use of other methods as possible. | |||
* All Rational return values should be <em>reduce</em>d. | |||
* Insert proper documentation comments. | |||
* Arrange the methods of the class in alphabetical order (since there are many). | |||
===Desired Methods=== | |||
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 | |||
===Starting Code=== | |||
/* Fractions of integers | |||
* @(#) $Id$ | * @(#) $Id$ | ||
* 2017-09-23, Georg Fischer | * 2017-09-23, Georg Fischer | ||
Line 13: | Line 33: | ||
* return reduced fractions where GCD(numerator, denominator) = 1. | * return reduced fractions where GCD(numerator, denominator) = 1. | ||
* The methods have name, parameters and return values analogous to methods | * The methods have name, parameters and return values analogous to methods | ||
* of the Java object type BigInteger, see | * of the Java object type BigInteger, see | ||
* https://docs.oracle.com/javase/7/docs/api/java/math/BigInteger.html | * https://docs.oracle.com/javase/7/docs/api/java/math/BigInteger.html | ||
*/ | */ | ||
public class Rational { | public class Rational { | ||
//---------------- | |||
// Internal Properties | |||
//---------------- | |||
/** value above the bar, before the slash */ | /** value above the bar, before the slash */ | ||
private int numerator; | private int numerator; | ||
/** value below the bar, after the slash */ | /** value below the bar, after the slash */ | ||
private int denominator; | private int denominator; | ||
/* Implement the following methods, and make use of basic operations. | |||
All Rational return values should be <em>reduce</em>d. | |||
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 | /** No-args constructor, creates 1/1 | ||
*/ | */ | ||
Line 28: | Line 73: | ||
denominator = 1; | denominator = 1; | ||
} // no-args constructor | } // no-args constructor | ||
/** Constructor with numerator, creates a/1 | /** Constructor with numerator, creates a/1 | ||
* @param a numerator | * @param a numerator | ||
Line 36: | Line 81: | ||
denominator = 1; | denominator = 1; | ||
} // constructor(int) | } // constructor(int) | ||
/** Constructor with numerator and denominator, creates a/b | /** Constructor with numerator and denominator, creates a/b | ||
* @param a numerator | * @param a numerator | ||
Line 45: | Line 90: | ||
denominator = b; | denominator = b; | ||
} // constructor(int, int) | } // constructor(int, int) | ||
/** Constructor from a String representation, creates a/b | /** Constructor from a String representation, creates a/b | ||
* @param str String of the form "a/b" | * @param str String of the form "a/b" | ||
Line 55: | Line 100: | ||
slashPos = str.length() - 2; | slashPos = str.length() - 2; | ||
} | } | ||
numerator = Integer.parseInt(str. | numerator = Integer.parseInt(str.substring(0, slashPos)); | ||
denominator = Integer.parseInt(str. | denominator = Integer.parseInt(str.substring(slashPos + 1)); | ||
} // constructor(String) | } // constructor(String) | ||
//---------------- | |||
// Internal getters | |||
//---------------- | |||
/** Gets the numerator | /** Gets the numerator | ||
* @return the numerator of <em>this</em> Rational | * @return the numerator of <em>this</em> Rational | ||
*/ | */ | ||
private int getNum() { | private int getNum() { | ||
return numerator; | |||
} // getNum() | } // getNum() | ||
/** Gets the denominator | /** Gets the denominator | ||
* @return the denominator of <em>this</em> Rational | * @return the denominator of <em>this</em> Rational | ||
*/ | */ | ||
private int getDen() { | private int getDen() { | ||
return denominator; | |||
} // getDen() | } // getDen() | ||
/ | //---------------- | ||
// Public methods | |||
//---------------- | |||
/** Returns the greatest common divisor of 2 integers. | /** Returns the greatest common divisor of 2 integers. | ||
* @param a 1st integer | * @param a 1st integer | ||
Line 91: | Line 131: | ||
public int gcd(int a, int b) { | public int gcd(int a, int b) { | ||
int result = 0; | int result = 0; | ||
// not yet implemented | |||
return result; | return result; | ||
} // gcd(int,int) | } // gcd(int,int) | ||
/** Returns the least common multiple of 2 integers. | /** Returns the least common multiple of 2 integers. | ||
* @param a 1st integer | * @param a 1st integer | ||
Line 101: | Line 142: | ||
public int lcm(int a, int b) { | public int lcm(int a, int b) { | ||
int result = a * b; | int result = a * b; | ||
if (result < 0) { // make | if (result < 0) { // absolute, make positive | ||
result = - result; | |||
} // abs | } // abs | ||
return result / gcd(a, b); | return result / gcd(a, b); | ||
} // lcm(int,int) | } // lcm(int,int) | ||
/** Reduces the fraction, that means: | |||
* divides the numerator and the denominator by | |||
* their greatest common divisor, if that is > 1, | |||
* and makes the denominator always positive. | |||
*/ | |||
public Rational reduce() { | |||
int common = gcd(numerator, denominator); | |||
if (common > 1) { | |||
numerator /= common; | |||
denominator /= common; | |||
} | |||
if (denominator < 0) { | |||
numerator = - numerator; | |||
denominator = - denominator; | |||
} | |||
return this; | |||
} // reduce() | |||
/** Returns the difference between <em>this</em> Rational and a second. | /** Returns the difference between <em>this</em> Rational and a second. | ||
* @param rat2 2nd Rational | * @param rat2 2nd Rational | ||
Line 112: | Line 171: | ||
*/ | */ | ||
public Rational subtract(Rational rat2) { | public Rational subtract(Rational rat2) { | ||
return this.add(rat2.negate()); | |||
} // subtract(Rational) | } // subtract(Rational) | ||
/** Returns | /** Returns <em>this</em> Rational as a String. | ||
* @return a String of the form "a/b", or only "a" if b is 1. | |||
* @return | |||
*/ | */ | ||
public | 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) { | public static void main(String[] args) { | ||
System.out.println( | 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 | } // main | ||
} // Rational | } // Rational |
Revision as of 18:20, 24 September 2017
Skeleton of Class Rational
As announced in JT05, we now implement a useful type of objects with operations as a Java class. Below you will find a skeleton for the code. The task of this session is
- Fill in the bodies of the following methods.
- Thereby make as much use of other methods as possible.
- All Rational return values should be reduced.
- Insert proper documentation comments.
- Arrange the methods of the class in alphabetical order (since there are many).
Desired Methods
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
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 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 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 the fraction, that means: * divides the numerator and the denominator by * their greatest common divisor, if that is > 1, * and makes the denominator always positive. */ public Rational reduce() { int common = gcd(numerator, denominator); if (common > 1) { numerator /= common; denominator /= common; } 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