All pastes #1240285 Raw Edit

Untitled

public java v1 · immutable
#1240285 ·published 2008-10-30 04:35 UTC
rendered paste body
import java.util.*;import java.io.*;/* * AUTHOR    : Dylan Johnston * DATE      : 10/29/2008 * DUE       : LE MON DAY * NOTES     : C# is a much prettier language :D * WHATTODO  : * Create a class that allows you to add and subtract * imaginary numbers using the addition and subtract- * ion operators (+ and -). Write a test program to d * emonstrate your class. * Math requirements: * Imaginary # A = 5 + 2i   B = 3-6i *		       A + B = B + A = 8 - 4i *		       A - B = 2 + 8i *		       B - A = -2 - 8i  */class ImaginaryNumbers {    public static void main(String[] args) {        try {                String    input;        Scanner   in = new Scanner( System.in );        Hashtable ht = new Hashtable();               System.out.print( "Enter some imaginary numbers!\n# " );                input = in.nextLine().replace( " ", "" );                String[] splits = input.split( "i" );        // Loop through each string that's been split with i then add it to a hashtable.        for ( int i = 0; i < splits.length; i++ ) {            if ( splits[i].length() < 4 )                continue;            String number = splits[i].split( "=" )[1];            int pos = number.indexOf( '+' );                                   if ( pos < 0 )                pos = number.indexOf( '-' );                        int real = StringToInt( number.substring( 0, pos ) );            int subs = StringToInt( number.substring( pos ) );            ht.put( splits[i].charAt(0), new Imaginary( real, subs ) );        }        System.out.println( "What would you like to do ( ex A + B + B )?\nType nothing ( or CTRL-C ) to quit." );                //Keep looking for input.        while ( (input = in.nextLine()).length() != 0 ) {            input = input.replace( " ", "" );            if ( input.charAt(0) == '-' )                continue;            Imaginary total = new Imaginary( ((Imaginary)ht.get(input.charAt(0))).real, ((Imaginary)ht.get(input.charAt(0))).imaginary );                            if ( total == null ) {                total = new Imaginary( 0, 0 );                System.out.println( input.charAt(0) + ": was never defined!" );            }               for ( int i = 0; i < input.length() - 1; i++ ) { //Don't bother since we always check i+1                try {                    if ( input.charAt(i) == '+' )                        total.Add( ( Imaginary )ht.get(input.charAt(i+1)) );                    else if ( input.charAt(i) == '-' )                        total.Subtract( ( Imaginary )ht.get(input.charAt(i+1)) );                }                catch ( Exception e ) {                    System.out.println( input.charAt(i+1) + ": was never defined!" );                }            }            System.out.println( " = " + total.String() );        }                }        catch( NoSuchElementException e ) {            //Filter out that Ctrl-C exception garbage        }    }        //Really just to get rid of the + in front of numbers without having this code pasted twice.    public static int StringToInt( String s ) {        if ( s.charAt(0) == '+' )            s = s.substring( 1 );                    return Integer.parseInt( s );    }    }// In C# my operator overloading kicked this class' ass.class Imaginary {    public int real;    public int imaginary;    public Imaginary( int real, int imaginary ) {        this.real = real;        this.imaginary = imaginary;    }    public void Add( Imaginary i2 ) {        real      += i2.real;        imaginary += i2.imaginary;    }    public void Subtract( Imaginary i2 ) {        real -= i2.real;        imaginary -= i2.imaginary;    }    public String String() {        return Integer.toString(real) + ((imaginary > 0) ? "+" : "" )+ Integer.toString(imaginary) + "i";    }}