<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>http://teherba.org/tehowiki/index.php?action=history&amp;feed=atom&amp;title=Java_Training_Course%2FJT07</id>
	<title>Java Training Course/JT07 - Revision history</title>
	<link rel="self" type="application/atom+xml" href="http://teherba.org/tehowiki/index.php?action=history&amp;feed=atom&amp;title=Java_Training_Course%2FJT07"/>
	<link rel="alternate" type="text/html" href="http://teherba.org/tehowiki/index.php?title=Java_Training_Course/JT07&amp;action=history"/>
	<updated>2026-04-14T22:40:02Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.42.1</generator>
	<entry>
		<id>http://teherba.org/tehowiki/index.php?title=Java_Training_Course/JT07&amp;diff=335&amp;oldid=prev</id>
		<title>imported&gt;Gfis: Program Skeleton</title>
		<link rel="alternate" type="text/html" href="http://teherba.org/tehowiki/index.php?title=Java_Training_Course/JT07&amp;diff=335&amp;oldid=prev"/>
		<updated>2017-09-26T20:17:46Z</updated>

		<summary type="html">&lt;p&gt;Program Skeleton&lt;/p&gt;
&lt;a href=&quot;http://teherba.org/tehowiki/index.php?title=Java_Training_Course/JT07&amp;amp;diff=335&amp;amp;oldid=334&quot;&gt;Show changes&lt;/a&gt;</summary>
		<author><name>imported&gt;Gfis</name></author>
	</entry>
	<entry>
		<id>http://teherba.org/tehowiki/index.php?title=Java_Training_Course/JT07&amp;diff=334&amp;oldid=prev</id>
		<title>imported&gt;Gfis: Created page with &quot;==Class &#039;&#039;Rational&#039;&#039;== We now leave the conventional programming style of C (and Pascal, BASIC, FORTRAN etc.), and we enter the field of real object-oriented programming. We r...&quot;</title>
		<link rel="alternate" type="text/html" href="http://teherba.org/tehowiki/index.php?title=Java_Training_Course/JT07&amp;diff=334&amp;oldid=prev"/>
		<updated>2017-09-26T20:01:34Z</updated>

		<summary type="html">&lt;p&gt;Created page with &amp;quot;==Class &amp;#039;&amp;#039;Rational&amp;#039;&amp;#039;== We now leave the conventional programming style of C (and Pascal, BASIC, FORTRAN etc.), and we enter the field of real object-oriented programming. We r...&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;==Class &amp;#039;&amp;#039;Rational&amp;#039;&amp;#039;==&lt;br /&gt;
We now leave the conventional programming style of C (and Pascal, BASIC, FORTRAN etc.), and we enter the field of real object-oriented programming. We rewrite the class &amp;#039;&amp;#039;Ratio&amp;#039;&amp;#039; from [[Java Training Course/JT06|JT06]], and we remove the &amp;lt;code&amp;gt;static&amp;lt;/code&amp;gt; qualifier from most methods. In the new class &amp;lt;code&amp;gt;Rational&amp;lt;/code&amp;gt; the first (implied) parameter is the current object &amp;lt;code&amp;gt;this&amp;lt;/code&amp;gt;in most cases. Instead of:&lt;br /&gt;
    public static Ratio subtract(Ratio rat1, Ratio rat2) {&lt;br /&gt;
        return add(rat1, negate(rat2));&lt;br /&gt;
    } // subtract(Ratio, Ratio)&lt;br /&gt;
we write:&lt;br /&gt;
    public Rational subtract(Rational rat2) {&lt;br /&gt;
        return this.add(rat2.negate());&lt;br /&gt;
    } // subtract(Rational)&lt;br /&gt;
===Desired Methods===&lt;br /&gt;
Besides the 4 basic arithmetic operations we want to implement some more useful methods. In this session, the following list should be expanded with proper documentation comments and method bodies:&lt;br /&gt;
     public Rational abs() { ... }&lt;br /&gt;
     public Rational add(Rational rat2) { ... }&lt;br /&gt;
     public int compareTo(Rational rat2) { ... }&lt;br /&gt;
     public Rational divide(Rational rat2) { ... }&lt;br /&gt;
     public boolean equals(Rational rat2) { ... }&lt;br /&gt;
     public boolean isInteger() { return denominator == 1; }&lt;br /&gt;
     public int intValue() { return numerator / denominator; }&lt;br /&gt;
     public Rational max(Rational rat2) { return this.compareTo(rat2) &amp;gt; 0 ? this : rat2 }&lt;br /&gt;
     public Rational min(Rational rat2) { return this.compareTo(rat2) &amp;lt; 0 ? this : rat2 }&lt;br /&gt;
     public Rational multiply(Rational rat2) { ... }&lt;br /&gt;
     public Rational negate() { return new Rational(- numerator, denominator); }&lt;br /&gt;
     public Rational subtract(Rational rat2) { ... }&lt;br /&gt;
     public String toString() { ... }&lt;br /&gt;
===Starting Code===&lt;br /&gt;
 /* Fractions of integers&lt;br /&gt;
  * @(#) $Id$&lt;br /&gt;
  * 2017-09-23, Georg Fischer&lt;br /&gt;
  */&lt;br /&gt;
 // package name will later go here&lt;br /&gt;
 // imports will later go here&lt;br /&gt;
 &lt;br /&gt;
 /** Class &amp;lt;wm&amp;gt;Rational&amp;lt;/em&amp;gt; represents an integer fraction&lt;br /&gt;
  *  together with the arithmetic operations on such Rationals.&lt;br /&gt;
  *  The denominator is always &amp;gt; 0. All arithmetic operations&lt;br /&gt;
  *  return reduced fractions where GCD(numerator, denominator) = 1.&lt;br /&gt;
  *  The methods have name, parameters and return values analogous to methods&lt;br /&gt;
  *  of the Java object type BigInteger, see&lt;br /&gt;
  *  https://docs.oracle.com/javase/7/docs/api/java/math/BigInteger.html&lt;br /&gt;
  */&lt;br /&gt;
 public class Rational {&lt;br /&gt;
     //----------------&lt;br /&gt;
     // Internal Properties&lt;br /&gt;
     //----------------&lt;br /&gt;
     /** value above the bar, before the slash */&lt;br /&gt;
     private int numerator;&lt;br /&gt;
     /** value below the bar, after  the slash */&lt;br /&gt;
     private int denominator;&lt;br /&gt;
 &lt;br /&gt;
     //----------------&lt;br /&gt;
     // Constructors&lt;br /&gt;
     //----------------&lt;br /&gt;
     /** No-args constructor, creates 1/1&lt;br /&gt;
      */&lt;br /&gt;
     public Rational() {&lt;br /&gt;
         numerator   = 1;&lt;br /&gt;
         denominator = 1;&lt;br /&gt;
     } // no-args constructor&lt;br /&gt;
 &lt;br /&gt;
     /** Constructor with numerator, creates a/1&lt;br /&gt;
      *  @param a numerator&lt;br /&gt;
      */&lt;br /&gt;
     public Rational(int a) {&lt;br /&gt;
         numerator   = a;&lt;br /&gt;
         denominator = 1;&lt;br /&gt;
     } // constructor(int)&lt;br /&gt;
 &lt;br /&gt;
     /** Constructor with numerator and denominator, creates a/b&lt;br /&gt;
      *  @param a numerator&lt;br /&gt;
      *  @param b denominator&lt;br /&gt;
      */&lt;br /&gt;
     public Rational(int a, int b) {&lt;br /&gt;
         numerator   = a;&lt;br /&gt;
         denominator = b;&lt;br /&gt;
     } // constructor(int, int)&lt;br /&gt;
 &lt;br /&gt;
     /** Constructor from a String representation, creates a/b&lt;br /&gt;
      *  @param str String of the form &amp;quot;a/b&amp;quot;&lt;br /&gt;
      */&lt;br /&gt;
     public Rational(String str) {&lt;br /&gt;
         int slashPos = str.indexOf(&amp;quot;/&amp;quot;);&lt;br /&gt;
         if (slashPos &amp;lt; 0) {&lt;br /&gt;
             str += &amp;quot;/1&amp;quot;;&lt;br /&gt;
             slashPos = str.length() - 2;&lt;br /&gt;
         }&lt;br /&gt;
         numerator   = Integer.parseInt(str.substring(0, slashPos));&lt;br /&gt;
         denominator = Integer.parseInt(str.substring(slashPos + 1));&lt;br /&gt;
     } // constructor(String)&lt;br /&gt;
 &lt;br /&gt;
     //----------------&lt;br /&gt;
     // Internal getters&lt;br /&gt;
     //----------------&lt;br /&gt;
     /** Gets the numerator&lt;br /&gt;
      *  @return the numerator   of &amp;lt;em&amp;gt;this&amp;lt;/em&amp;gt; Rational&lt;br /&gt;
      */&lt;br /&gt;
     private int getNum() {&lt;br /&gt;
         return numerator;&lt;br /&gt;
     } // getNum()&lt;br /&gt;
 &lt;br /&gt;
     /** Gets the denominator&lt;br /&gt;
      *  @return the denominator of &amp;lt;em&amp;gt;this&amp;lt;/em&amp;gt; Rational&lt;br /&gt;
      */&lt;br /&gt;
     private int getDen() {&lt;br /&gt;
         return denominator;&lt;br /&gt;
     } // getDen()&lt;br /&gt;
 &lt;br /&gt;
     //----------------&lt;br /&gt;
     // Public methods&lt;br /&gt;
     //----------------&lt;br /&gt;
     /** Returns the greatest common divisor of 2 integers.&lt;br /&gt;
      *  @param a 1st integer&lt;br /&gt;
      *  @param b 2nd integer&lt;br /&gt;
      *  @return gcd(a,b), which is always positive&lt;br /&gt;
      */&lt;br /&gt;
     public static int gcd(int a, int b) {&lt;br /&gt;
         int result = 0;&lt;br /&gt;
         // not yet implemented&lt;br /&gt;
         return result;&lt;br /&gt;
     } // gcd(int,int)&lt;br /&gt;
 &lt;br /&gt;
     /** Returns the least common multiple of 2 integers.&lt;br /&gt;
      *  @param a 1st integer&lt;br /&gt;
      *  @param b 2nd integer&lt;br /&gt;
      *  @return lcm(a,b)&lt;br /&gt;
      */&lt;br /&gt;
     public static int lcm(int a, int b) {&lt;br /&gt;
         int result = a * b;&lt;br /&gt;
         if (result &amp;lt; 0) { // absolute, make positive&lt;br /&gt;
             result = - result;&lt;br /&gt;
         } // abs&lt;br /&gt;
         return result / gcd(a, b);&lt;br /&gt;
     } // lcm(int,int)&lt;br /&gt;
 &lt;br /&gt;
     /** Reduces and normalizes the fraction, that means: &lt;br /&gt;
      *  &amp;amp;lt;ul&amp;amp;gt;&lt;br /&gt;
      *  &amp;amp;lt;li&amp;amp;gt;divides the numerator and the denominator by&lt;br /&gt;
      *  their greatest common divisor, if that is &amp;amp;gt; 1,&amp;amp;lt;/li&amp;amp;gt;&lt;br /&gt;
      *  &amp;amp;lt;li&amp;amp;gt;makes the denominator always positive,&amp;amp;lt;/li&amp;amp;gt;&lt;br /&gt;
      *  &amp;amp;lt;li&amp;amp;gt;normalizes the denominator to 1 if the numerator is 0.&amp;amp;lt;/li&amp;amp;gt;&lt;br /&gt;
      *  &amp;amp;lt;/ul&amp;amp;gt;&lt;br /&gt;
      */&lt;br /&gt;
     public Rational reduce() {&lt;br /&gt;
         int common = gcd(numerator, denominator);&lt;br /&gt;
         if (common &amp;gt; 1) {&lt;br /&gt;
             numerator   /= common;&lt;br /&gt;
             denominator /= common;&lt;br /&gt;
         }&lt;br /&gt;
         if (numerator == 0) {&lt;br /&gt;
             denominator = 1;&lt;br /&gt;
         } else if (denominator &amp;lt; 0) {&lt;br /&gt;
             numerator   = - numerator;&lt;br /&gt;
             denominator = - denominator;&lt;br /&gt;
         }&lt;br /&gt;
         return this;&lt;br /&gt;
     } // reduce()&lt;br /&gt;
  &lt;br /&gt;
     /** Returns the difference between &amp;lt;em&amp;gt;this&amp;lt;/em&amp;gt; Rational and a second.&lt;br /&gt;
      *  @param rat2 2nd Rational&lt;br /&gt;
      *  @return (this - rat2)&lt;br /&gt;
      */&lt;br /&gt;
     public Rational subtract(Rational rat2) {&lt;br /&gt;
         return this.add(rat2.negate());&lt;br /&gt;
     } // subtract(Rational)&lt;br /&gt;
 &lt;br /&gt;
     /** Returns &amp;lt;em&amp;gt;this&amp;lt;/em&amp;gt; Rational as a String.&lt;br /&gt;
      *  @return a String of the form &amp;quot;a/b&amp;quot;, or only &amp;quot;a&amp;quot; if b is 1.&lt;br /&gt;
      */&lt;br /&gt;
     public String toString() {&lt;br /&gt;
         String result = String.valueOf(numerator);&lt;br /&gt;
         if (denominator != 1) {&lt;br /&gt;
             result += &amp;quot;/&amp;quot; + String.valueOf(denominator);&lt;br /&gt;
         }&lt;br /&gt;
         return result;&lt;br /&gt;
     } // toString()&lt;br /&gt;
 &lt;br /&gt;
     //================&lt;br /&gt;
     /** Test program, shows a series of fixed operations&lt;br /&gt;
      *  @param args String array of commandline arguments&lt;br /&gt;
      */&lt;br /&gt;
     public static void main(String[] args) {&lt;br /&gt;
         int iarg = 1;&lt;br /&gt;
         String   str0 = &amp;quot;0/1&amp;quot;;&lt;br /&gt;
         Rational rat0 = new Rational(str0);&lt;br /&gt;
         while (iarg &amp;lt; args.length) {&lt;br /&gt;
             String   str1 = args[iarg];&lt;br /&gt;
             Rational rat1 = new Rational(str1);&lt;br /&gt;
             System.out.println(str0 + &amp;quot; + &amp;quot; + str1 + &amp;quot; = &amp;quot; + rat0.add     (rat1).toString());&lt;br /&gt;
             System.out.println(str0 + &amp;quot; - &amp;quot; + str1 + &amp;quot; = &amp;quot; + rat0.subtract(rat1).toString());&lt;br /&gt;
             System.out.println(str0 + &amp;quot; * &amp;quot; + str1 + &amp;quot; = &amp;quot; + rat0.multiply(rat1).toString());&lt;br /&gt;
             System.out.println(str0 + &amp;quot; / &amp;quot; + str1 + &amp;quot; = &amp;quot; + rat0.divide  (rat1).toString());&lt;br /&gt;
             str0 = str1;&lt;br /&gt;
             rat0 = rat1;&lt;br /&gt;
             iarg ++;&lt;br /&gt;
         } // while iarg&lt;br /&gt;
     } // main&lt;br /&gt;
 &lt;br /&gt;
 } // Rational&lt;br /&gt;
[[Java Training Course/JT05|&amp;amp;lt; Previous: JT06]] Preliminary Class &amp;#039;&amp;#039;Ratio&amp;#039;&amp;#039;&amp;lt;br /&amp;gt;&lt;br /&gt;
[[Java Training Course/JT07|&amp;amp;gt; Next: JT08]] Discussion&lt;/div&gt;</summary>
		<author><name>imported&gt;Gfis</name></author>
	</entry>
</feed>