/* * =================================================================== * GeometricSeries.java : Compute sum of a geometric series of complex * objects. * * Written By: Mark Austin April 2009 * =================================================================== */ 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 Geometric Series Computer"); System.out.println("-----------------------------------------"); // Prompt user for coefficients in geometric series... System.out.println("Please enter complex number \"a\""); // Coefficient cA .... System.out.print("Coefficent a : Real : "); sLine = getTextFromConsole(); cA.dReal = Double.valueOf(sLine).doubleValue(); System.out.print("Coefficent a : Imaginary : "); sLine = getTextFromConsole(); cA.dImaginary = Double.valueOf(sLine).doubleValue(); // Number of terms in the series ..... System.out.println("Please enter number of terms in series \"a\""); System.out.print("No of terms in series : "); sLine = getTextFromConsole(); 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( "======================================= " ); Complex cSum = new Complex(); for (int ii = 1; ii <= NoTermsInSeries; ii = ii + 1) { // Compute s^(ii) ...... // .... fill in details here .... // Add s^(ii) to series sum ...... cSum = cSum.Add( cB ); } System.out.println( "Summation 1 = " + cSum.toString() ); // Compute sum of geometric series using Horner's rule .... System.out.println( "" ); System.out.println( "Summation using Horner's formula" ); System.out.println( "======================================= " ); Complex cI = new Complex(); cI.dReal = 1.0; cI.dImaginary = 0.0; // Fill in details here ..... for (int ij = 1; ij <= NoTermsInSeries - 1; ij = ij + 1) { // Fill in details here ..... } System.out.println( "Summation 2 = " + cSum2.toString() ); // Use geometric formula to series sum.... System.out.println( "" ); System.out.println( "Summation with geometric-series formula " ); System.out.println( "======================================= " ); Complex cB = new Complex(); for (int ij = 1; ij <= NoTermsInSeries; ij = ij + 1) { // fill in details here ...... } // fill in details here ...... } /* * ============================================================ * Method getTextFromConsole(): Read line of text from console * (keyboard input).... * * Input : None. * Output : String inLine -- character string of keyboard input * ============================================================ */ public static String getTextFromConsole() { String inLine = ""; // Create buffered reader for keyboard input stream.... BufferedReader inStream = new BufferedReader ( new InputStreamReader(System.in)); // Try to read input from keyboard .... try { inLine = inStream.readLine(); } catch (IOException e) { System.out.println("IOException: " + e); } return inLine; } }