/* * =================================================================== * GeometricSeries.java : Compute sum of a geometric series of complex * objects. * * Written By : Mark Austin May, 2004 * =================================================================== */ import java.lang.Math; import java.util.*; import java.io.*; public class GeometricSeries { public static void main( String args[] ) { Complex cA = new Complex(); int NoTermsInSeries; String sLine; // Print Welcome Message System.out.println("Welcome to the Geometriic Series Computer"); System.out.println("-----------------------------------------"); // Prompt User for Coefficients in Quadratic Equation... System.out.println("Please enter complex number \"a\""); // Coefficient cA .... System.out.print("Coefficent a : Real : "); sLine = keyboardInput(); cA.dReal = Double.valueOf(sLine).doubleValue(); System.out.print("Coefficent a : Imaginary : "); sLine = keyboardInput(); cA.dImaginary = Double.valueOf(sLine).doubleValue(); // Number of terms in the series ..... System.out.print("Please enter number of terms in series \"a\":"); sLine = keyboardInput(); NoTermsInSeries = Integer.valueOf(sLine).intValue(); // Print details of input to screen ... System.out.print("The complex no you have entered is :"); System.out.println("s = " + cA.toString() ); System.out.println("No of terms in series is = " + NoTermsInSeries ); // Check that NoTermsInSeries is greater than or equal to one.. if ( NoTermsInSeries < 1 ) { System.out.println("ERROS: No of terms in series must be at least one:"); return; } // Use basic for loop to compute sumation of geometric series ..... System.out.println( "Summation with basic for-loop " ); System.out.println( "======================================= " ); ..... details of code removed (about 10 lines of Java) ...... // Compute sum of geometric series using Horner's rule .... System.out.println( "" ); System.out.println( "Summation using Horner's formula" ); System.out.println( "======================================= " ); ..... details of code removed (about 9 lines of Java) ...... // Use geometric formula to series sum.... System.out.println( "" ); System.out.println( "Summation with geometric-series formula " ); System.out.println( "======================================= " ); ..... details of code removed (about 11 lines of Java) ...... } /* * =========================================================== * Method keyboardInput() : Get line of input from keyboard * * Input : None. * Output : String sLine -- character string of keyboard input * =========================================================== */ static String keyboardInput() { String sLine; DataInputStream in = new DataInputStream(System.in); try{ sLine = in.readLine(); return sLine; } catch (Exception e){ return "error"; } } }