package oop; import java.awt.Color; import java.awt.Point; import java.util.*; import wheels.users.*; /** Samples of OOP concepts: classes and objects, interfaces, inheritance, polymorphism. * Demonstrates also overwriting, overloading, casting (and it's dangers), * reading using Scanner, etc. * @author Jan Stelovsky 2007, UH ICS */ public class OO_Concepts extends Frame { /** Contract for objects that can do additive arithmetic operations */ interface Arithmetic { /** Returns the sum of this number and the operand. * @param operand to be added * @return the sum: this + operand */ public Arithmetic add (Arithmetic operand); /** Returns the difference of this number and the operand. * @param operand to be subtracted * @return the difference: this - operand */ public Arithmetic subtract (Arithmetic operand); } /** Contract for objects that can set their properties * by asking the user to type them in */ interface Prompting { /** Asks the user for the values of object's properties. * @param prompt to display in the console */ void promptFor (String prompt); } /** Represents a fraction defined by its numerator and denominator. * Supports additive arithmetic operations. * Provides for defining the numerator and denominator by the user. */ static class Fraction implements Arithmetic, Prompting { /** The numerator. */ private int numerator = 0; /** The denominator. */ private int denominator = 0; /** Returns the sum of this fraction and the operand fraction. * Precondition: operand must be a Fraction * Postcondition: this fraction isn't changed * @param operand to be added * @return the sum: this + operand */ public Arithmetic add (Arithmetic operand) { Fraction operand_ = (Fraction) operand; Fraction result = new Fraction (); result.numerator = operand_.denominator * numerator + operand_.numerator * denominator; result.denominator = operand_.denominator * denominator; return result; } /** Returns the difference of this fraction and the operand fraction. * @param operand to be subtracted * Precondition: operand must be a Fraction * Postcondition: this fraction isn't changed * @return the difference: this - operand */ public Arithmetic subtract (Arithmetic operand) { Fraction operand_ = (Fraction) operand; Fraction result = new Fraction (); result.numerator = operand_.denominator * numerator - operand_.numerator * denominator; result.denominator = operand_.denominator * denominator; return result; } /** Asks the user for the numerator and denominator of this fraction. * Does not provide for checking of valid user input. * @param prompt to display in the console */ public void promptFor (String prompt) { Scanner scanner = new Scanner (System.in); System.out.print (prompt); System.out.print (" enumerator>"); numerator = scanner.nextInt (); System.out.print (" denominator>"); denominator = scanner.nextInt (); } } /** Represents a complex number by its real and imaginary parts. * Supports additive arithmetic operations. * Provides for defining the numerator and denominator by the user. * Supports drawing the number as a vector */ static class Complex implements Arithmetic, Prompting { /** Origin in the drawing plane.*/ public static final Point drawOrigin = new Point (350, 250); /** Scaling factor. Number of pixels for a unit.*/ public static final double scaling = 50; /** Additive zero.*/ public static final Complex origin = new Complex (); /** The real part.*/ private double realPart = 0.0; /** The imaginary part.*/ private double imaginaryPart = 0.0; /** Length in pixels of the axes.*/ private static final int axisLength = 400; /** Draws the axes. */ public static void axes () { int halfLength = axisLength / 2; Line xAxis = new Line (drawOrigin.x - halfLength, drawOrigin.y , drawOrigin.x + halfLength, drawOrigin.y); xAxis.setColor (Color.blue); Line yAxis = new Line (drawOrigin.x, drawOrigin.y - halfLength , drawOrigin.x, drawOrigin.y + halfLength); yAxis.setColor (Color.blue); } /** Creates an aditive zero. */ public Complex () { this (0, 0); } /** Creates a complex number with given real and imaginary parts. * @param realPart the real part * @param imaginaryPart the imaginary part*/ public Complex (double realPart, double imaginaryPart) { this.realPart = realPart; this.imaginaryPart = imaginaryPart; } /** Returns the sum of this number and the operand number. * Precondition: operand must be a Complex * Postcondition: this number isn't changed * @param operand to be added * @return the sum: this + operand */ public Arithmetic add (Arithmetic operand) { Complex operand_ = (Complex) operand; Complex result = new Complex (); result.realPart = realPart + operand_.realPart; result.imaginaryPart = imaginaryPart + operand_.imaginaryPart; return result; } /** Returns the difference of this number and the operand number. * Precondition: operand must be a Complex * Postcondition: this number isn't changed * @param operand to be added * @return the sum: this - operand */ public Arithmetic subtract (Arithmetic operand) { Complex operand_ = (Complex) operand; Complex result = new Complex (); result.realPart = realPart - operand_.realPart; result.imaginaryPart = imaginaryPart - operand_.imaginaryPart; return result; } /** Asks the user for the real and imaginary parts of this number. * Does not provide for checking of valid user input. * @param prompt to display in the console */ public void promptFor (String prompt) { Scanner scanner = new Scanner (System.in); System.out.print (prompt); System.out.print (" real part>"); realPart = scanner.nextDouble (); System.out.print (" imaginary part>"); imaginaryPart = scanner.nextDouble (); } /** Adds operand1 and operand2 and stores the result in result * Postcondition: this number isn't changed * @param operand1 to be added * @param operand2 to be added * @param result the result */ public void add (Complex operand1, Complex operand2, Complex result) { result.realPart = operand1.realPart + operand2.realPart; result.imaginaryPart = operand1.imaginaryPart + operand2.imaginaryPart; } /** Returns the result of adding operand1 and operand2 * Postcondition: this number isn't changed * @param operand1 to be added * @param operand2 to be added * @return the result */ public static Complex add (Complex operand1, Complex operand2) { return new Complex (operand1.realPart + operand2.realPart , operand1.imaginaryPart + operand2.imaginaryPart); } /** Returns the textual representation of this number as usual in math. * Overrides toString() in java.lang.Object * @return the textual description */ public String toString () { String string = "from superclass:"; string = string + super.toString (); return "(" + realPart + "+i*" + imaginaryPart + ")" + string; } /** Returns the real part. */ public double getRealPart () { return realPart; } /** Sets the real part. */ public void setRealPart (double realPart) { this.realPart = realPart; } /** Returns the imaginary part. */ public double getImaginaryPart () { return imaginaryPart; } /** Sets the imaginary part. */ public void setImaginaryPart (double imaginaryPart) { System.out.println ("someone is trying to set imaginaryPart"); this.imaginaryPart = imaginaryPart; } /** Draws this number in 2-dimensional plane. */ public void draw () { new Line (drawOrigin.x, drawOrigin.y , drawOrigin.x + (int) (realPart * scaling) , drawOrigin.y - (int) (imaginaryPart * scaling)); } } /** Demonstrates the concepts. */ public OO_Concepts () { Complex.axes (); Complex complex1 = new Complex (1, 1); complex1.draw (); Arithmetic number1 = complex1; Prompting prompting1 = complex1; prompting1.promptFor ("number 1"); Arithmetic number2 = number1; System.out.println ("number1 = " + number1); System.out.println ("number2 = " + number2); // Complex complex0 = new Complex (); // Complex complex1 = new Complex (1, 1); // Complex complex3 = new Complex (); // complex3.askUser (); // System.out.println (complex1 + "+" + complex3 + "=" + Complex.add (complex1, complex3)); // Complex result = new Complex (); // complex0.add (complex1, complex3, result); // System.out.println (complex1 + "+" + complex3 + "=" + result); // System.out.println (complex1 + "+" + complex3 + "=" + complex1.add (complex3)); // System.out.println (complex1 + "+" + complex3 + "=" + complex1.add (complex3)); } /** Creates and runs the program. */ public static void main (String [] arguments) { new OO_Concepts (); } }