Objektorientierte Programmierung - Entwurf einer Klasse Rational.java Daniel Huson Info I, WS 2005 Analyse und Design • Anforderung: wir wollen mit „Rationalen Zahlen“, Q, rechnen • Beispiele: 55/9 × 9/7 ½+33/8 3/9==6/18? • Brauchen: – Grundrechnenarten – Eingabe/Ausgabe – Hilfsfunktionen Analyse und Design • Anforderung: Rechnen mit „Rationalen Zahlen“ • Klasse: Rational.java • Variablen: numerator (Zähler) denominator (Nenner) • Methoden: add, subtract, multiple, divide, equals, clone, parse, toString, toFloat, sign • Private Methoden: normalize, gcd Datei Rational.java public class Rational { // Variables: private int numerator; private int denominator; // Constructor: public Rational () { numerator=0; denominator=1; } // Methods: ... } Datei Rational.java, Forts. public class Rational { // Variables: private int numerator; private int denominator; // Constructor: public Rational () { numerator=0; denominator=1; } public Rational (int p,int q) { numerator=p; denominator=q; normalize(); } } Datei Rational.java, Forts. public class Rational { ... // Methods: public Rational add (Rational r) { int p=numerator*r.denominator+ r.numerator*denominator; int q=denominator*r.denominator; return new Rational(p,q); } } Datei Rational.java, Forts. public class Rational { ... // Methods: public Rational subtract (Rational r) { int p=numerator*r.denominatorr.numerator*denominator; int q=denominator*r.denominator; return new Rational(p,q); } } Datei Rational.java, Forts. public class Rational { ... // Methods: public void multiple (Rational r){ int p=numerator*r.numerator; int q=denominator*r.denominator; return new Rational(p,q); } } Datei Rational.java, Forts. public class Rational { ... // Methods: public void divide (Rational r) { int p=numerator*r.denominator; int q=denominator*r.numerator; return new Rational(p,q); } } Datei Rational.java, Forts. public class Rational { ... // Methods: public boolean equals (Rational r) { return numerator=r.numerator && denominator==r.denominator; } public Object clone (Rational r) { return new Rational(numerator,denominator); } } Datei Rational.java, Forts. public class Rational { ... // I/O methods: public Rational parse (String str) { StringTokenizer st=new StringTokenizer(str); int p=Integer.parseInt(st.nextToken(“/“)); int q=Integer.parseInt(st.nextToken()); return new Rational(p,q); } public String toString () { return numerator+“/“+denominator; } } Datei Rational.java, Forts. public class Rational { ... // Private methods: private void normalize (){ if(denominator==0) { numerator=0; denominator=1; } else if(denominator<0) { numerator= -numerator; denominator= -denominator; } int z=lcd(denominator,numerator); numerator/=z; denominator/=z; } Datei Rational.java, Forts. public class Rational { ... // Private methods: private int lcd (int a, int b){ while (a != b) if (a > b) a = a - b; else b = b - a; return a; } } public static void main (String[] args) throws Exception { BufferedReader r=new BufferedReader(new InputStreamReader(System.in)); while(true) { System.out.print("? "); String str=r.readLine(); if(str==null) break; StringTokenizer st=new StringTokenizer(str); Rational a=new Rational(); a.parse(st.nextToken(" ")); char ch=(st.nextToken(" ")).charAt(0); Rational b=new Rational(); b.parse(st.nextToken(" ")); Rational result; switch(ch){ case '+': result=a.add(b); break; case '-': result=a.subtract(b); break; case '*': result=a.multiple(b); break; case '/': result=a.multiple(b); break; default: System.err.println("Unknown command: "+ch); result=new Rational(0,1); } System.out.println(a+" "+ch+" "+b+" = "+result); } } Programmlauf: ? 1/3 + 1/3 1/3 + 1/3 = 2/3 ? 9/7 + 3/4 9/7 + 3/4 = 57/28 ? 57/28 - 3/4 57/28 - 3/4 = 9/7 ? 1/2 * 1/3 1/2 * 1/3 = 1/6 ? 1/2 / 1/2 1/2 / 1/2 = 1/4 ?