CSC 130 Power Series

profileIT_homework2
seriestester1.zip

SeriesTester/.classpath

SeriesTester/.project

SeriesTester org.eclipse.jdt.core.javabuilder org.eclipse.jdt.core.javanature

SeriesTester/.settings/org.eclipse.jdt.core.prefs

eclipse.preferences.version=1 org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7 org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve org.eclipse.jdt.core.compiler.compliance=1.7 org.eclipse.jdt.core.compiler.debug.lineNumber=generate org.eclipse.jdt.core.compiler.debug.localVariable=generate org.eclipse.jdt.core.compiler.debug.sourceFile=generate org.eclipse.jdt.core.compiler.problem.assertIdentifier=error org.eclipse.jdt.core.compiler.problem.enumIdentifier=error org.eclipse.jdt.core.compiler.source=1.7

SeriesTester/bin/seriesTester/FibonacciSequence.class

package seriesTester;
public synchronized class FibonacciSequence {
    private int firstNumber;
    private int secondNumber;
    public void FibonacciSequence();
    public boolean validStartNumbers(int, int);
    public void printSequence(int);
    public void printSequence(int, int, int);
    private void setSecondNumber(int);
    private void setFirstNumber(int);
    public void resetSequence();
}

SeriesTester/bin/seriesTester/PowerSeries.class

package seriesTester;
public synchronized class PowerSeries {
    private math2 myMath;
    private double value_of_x;
    private double number_of_terms;
    public void PowerSeries();
    public void setX(double);
    public void setNumOfTerms(double);
    public double run_eTo_xSeries();
    public double run_sin_Of_xSeries();
    public double run_PowerSeries();
}

SeriesTester/bin/seriesTester/SeriesTester.class

package seriesTester;
public synchronized class SeriesTester {
    private static java.util.Scanner keyboard;
    private static FibonacciSequence myFibonacci;
    private static PowerSeries myPower;
    static void <clinit>();
    public void SeriesTester();
    public static void main(String[]);
    private static void powerSeries();
    private static void fibonociiSeries();
    private static String selectSerries();
}

SeriesTester/bin/seriesTester/math2.class

package seriesTester;
public synchronized class math2 {
    public void math2();
    public int factorial(int);
}

SeriesTester/src/seriesTester/SeriesTester.java

SeriesTester/src/seriesTester/SeriesTester.java

package  seriesTester ;

import  java . util . Scanner ;

public   class   SeriesTester
{

     /**
     *  @param  args
     *          Author Larry R Shannon This is the demo/testing program for the
     *          Fibonacci/Power series project. The algorithm for the demo part of
     *          this project is as follows: Variables needed: State Variables:
     *          Scanner keyboard is used to read data from the input data buffer.
     *          (the keyboard) FibonacciSequence myFibonacci is the class object
     *          that contains the data and methods needed to explore the Fibonacci
     *          sequence. Built by student programmer. PowerSeries myPower is the
     *          class object that contains the data and methods needed to explore
     *          three power series examples. Built by student programmer. main
     *          local variables: boolean anotherRound tests to see if the user
     *          would like to rerun the program. boolean yesNo is a place holder
     *          for the test of the user's response at the keyboard. String
     *          selection is used to hold the user's menu choice. Explain purpose
     *          of program to user. set anotherRound equal to true(force run again
     *          unless changed) enter do while loop call choice menu enter do
     *          while loop (condition for exit is one of the proper menu choices)
     *          Ask user to choose between running the Fibonacci sequence or the
     *          Power Series. store choice of "a or b" in selection Check choice
     *          and loop until proper choice return selection to main method check
     *          choice via a switch if Fibonacci Call Fibonacci method if Power
     *          Series Call Power Series method Ask user if they would like to run
     *          the program again if yes loop back to enter do while loop else
     *          exit
     * 
     *          Fibonacci method: int count is the number of terms in the sequence
     *          segment boolean isValid is used to indicate a valid pair in the
     *          Fibonacci sequence. int firstNumber is the first number of an
     *          adjacent pair in the Fibonacci sequence. int secondNumber is the
     *          second number of an adjacent pair in the Fibonacci sequence. Ask
     *          user to enter the number of terms in a Fibonacci sequence
     *          beginning at the 0,1 number pair. Store the user response in count
     *          call the myFibonacci.printSequence(count) object method to print
     *          the desired sequence. enter do while loop that checks for valid
     *          pair of adjacent Fibonacci numbers ask user to enter two adjacent
     *          numbers in Fibonacci sequence Check for validity if invalid loop
     *          back to enter do while loop else exit loop with valid pair stored
     *          in firstNumber and secondNumber ask user for the number of terms
     *          to print in this Fibonacci sequence segment print Fibonacci
     *          sequence segment with starting points firstNumber and secondNumber
     *          for count number of terms reset Fibonacci sequence object to 0,1
     *          first terms.
     * 
     *          PowerSeries: Method variables double functionAnswer used to hold
     *          the function answer double yourMathSeriesAnswer used to hold the
     *          power series answer double valueOfX "x" value of power series int
     *          numberOfTerms used to hold the number of terms in the power series
     * 
     *          compare e to the x power function and series Ask user for "x"
     *          value (between 0 and 1 exclusive, meaning up to but not including
     *          1 and 0) store the user entry in valueOfX Ask for the number of
     *          terms in the power series (1 - 20 for this series) store the user
     *          entry in numberOfTerms Run the Java Math library method .exp(x)
     *          and store return value into functionAnswer set myPower object
     *          state variable valueOfX to valueOfX set myPower object state
     *          variable numberOfTerms to local numberOfTerms Run myPower object
     *          run_eTo_xSeries() method and store return value into
     *          yourMathSeriesAnswer (series term values print out while
     *          calculating) print
     *          "The Java Math library method exp() returns the value of " +
     *          functionAnswer; print
     *          "Your power series answer, for the same function, is " +
     *          yourMathSeriesAnswer;
     * 
     * 
     * 
     *          compare sin of x function and it's power series Ask user for "x"
     *          value (between 0 and 90 inclusive, this gives us degrees) convert
     *          this to radians by multiplying by PI over 180 store the result
     *          into valueOfX Ask for the number of terms in the power series (1 -
     *          17 for this series) We restrict the series to 1 through 17 because
     *          further terms exceed the capability of this program to represent
     *          properly. They have a tendency to go to negative and positive
     *          infinity. store the user entry in numberOfTerms Run the Java Math
     *          library method .sin(valueOfX) and store return value into
     *          functionAnswer set myPower object state variable valueOfX to
     *          valueOfX set myPower object state variable numberOfTerms to local
     *          numberOfTerms Run myPower object run_sin_Of_xSeries() method and
     *          store return value into yourMathSeriesAnswer (series term values
     *          print out while calculating) print
     *          "The Java Math library method sin() returns the value of " +
     *          functionAnswer; print
     *          "Your power series answer, for the same function, is " +
     *          yourMathSeriesAnswer;
     * 
     * 
     * 
     *          compare the 1/(1 - x) function and it's power series, where x is
     *          restricted to the values of 0 through 1 exclusive. Ask user for
     *          "x" value (between 0 and 1 exclusive, meaning up to but not
     *          including 1 and 0) store the user entry in valueOfX Ask for the
     *          number of terms in the power series (1 - 20 for this series) store
     *          the user entry in numberOfTerms Run the 1/(1 - x) function and
     *          store the resultant value into functionAnswer set myPower object
     *          state variable valueOfX to valueOfX set myPower object state
     *          variable numberOfTerms to local numberOfTerms Run myPower object
     *          run_PowerSeries() method and store return value into
     *          yourMathSeriesAnswer (series term values print out while
     *          calculating) print "The 1/(1 - x) function returns the value of "
     *          + functionAnswer; print
     *          "Your power series answer, for the same function, is " +
     *          yourMathSeriesAnswer;
     * 
     * 
     * 
     */
     // private static math2 myMath = new math2();
     private   static   Scanner  keyboard  =   new   Scanner ( System . in );
     private   static   FibonacciSequence  myFibonacci  =   new   FibonacciSequence ();
     private   static   PowerSeries  myPower  =   new   PowerSeries ();

     public   static   void  main ( String []  args )
     {
         boolean  anotherRound  =   true ;
         boolean  yesNo  =   false ;
         String  selection  =   "" ;
         System . out . println ( "This program generates a sequence of numbers" );
         System . out . println ( "based on your selection of the type of series and" );
         System . out . println ( "the starting and ending points in that series." );
         do
         {
            selection  =  selectSerries ();
             switch   ( selection )
             {
             case   "A" :
             case   "a" :
                fibonociiSeries ();
                 break ;
             case   "B" :
             case   "b" :
                powerSeries ();
                 break ;
             }
             System . out . println ( "Would you like to look at another sequence?" );
             do
             {
                 System . out . println ( "Please enter \"Y\" for Yes and \"N\" for No." );
                selection  =  keyboard . next ();
                 if   ( selection . equalsIgnoreCase ( "y" ))
                 {
                    anotherRound  =   true ;
                    yesNo  =   true ;
                 }   else   if   ( selection . equalsIgnoreCase ( "n" ))
                 {
                    anotherRound  =   false ;
                    yesNo  =   true ;
                 }   else
                    yesNo  =   false ;
             }   while   ( ! yesNo );

         }   while   ( anotherRound );
     }

     private   static   void  powerSeries ()
     {
         /**
         * The power series class allows the user to examine the conversion effects
         * of different variable inputs.
         */
         boolean  quit  =   false ;
         String  yesNo  =   "" ;
         double  functionAnswer  =   0.0 ;
         double  yourMathSeriesAnswer  =   0.0 ;
         double  valueOfX  =   0.0 ;
         int  numberOfTerms  =   0 ;
         System . out
                 . println ( "In mathematics there are several functions that can be found my using a power series." );
         System . out
                 . println ( "First we will look at the convergence of a power series for e to the x power." );
         System . out . println ();
         do
         {
             System . out
                     . println ( "Please enter the value for \"x\". (Between the values of 0 and 1 exclusive)" );
            valueOfX  =  keyboard . nextDouble ();
             System . out
                     . println ( "Please enter a value for the number of terms in the series. (from 1 to 20)" );
            numberOfTerms  =  keyboard . nextInt ();

             // factorNum = myMath.factorial(numberOfTerms);
             // System.out.println("Factorial of " + numberOfTerms + " is " +
             // factorNum);
            functionAnswer  =   Math . exp ( valueOfX );
            myPower . setX ( valueOfX );
            myPower . setNumOfTerms ( numberOfTerms );
            yourMathSeriesAnswer  =  myPower . run_eTo_xSeries ();
             System . out . println ();
             System . out
                     . println ( "The Java Math library method exp() returns the value of "
                             +  functionAnswer );
             System . out . println ( "Your power series answer, for the same function, is "
                     +  yourMathSeriesAnswer );
             System . out . println ();
             System . out . println ( "Would you like to try again?" );
             do
             {

                 System . out . println ( "Please answer \"y\" for Yes or \"n\" for No." );
                yesNo  =  keyboard . next ();
             }   while   ( ! yesNo . equalsIgnoreCase ( "y" )   &&   ! yesNo . equalsIgnoreCase ( "n" ));
             if   ( yesNo . equalsIgnoreCase ( "y" ))
                quit  =   false ;
             else
                quit  =   true ;
         }   while   ( ! quit );

         System . out
                 . println ( "Next we will look at the convergence of a power series for sin of X." );
         do
         {
             System . out
                     . println ( "Please enter the value for \"x\". (In degree between the values of 90 and 0 inclusive)" );
            valueOfX  =  keyboard . nextDouble ()   *   ( 3.14159265   /   180 );

             System . out
                     . println ( "Please enter a value for the number of terms in the series. (from 1 to 17)" );
            numberOfTerms  =  keyboard . nextInt ();

            functionAnswer  =   Math . sin ( valueOfX );
            myPower . setX ( valueOfX );
            myPower . setNumOfTerms ( numberOfTerms );
            yourMathSeriesAnswer  =  myPower . run_sin_Of_xSeries ();
             System . out . println ();
             System . out
                     . println ( "The Java Math library method sin() returns the value of "
                             +  functionAnswer );
             System . out . println ( "Your power series answer, for the same function, is "
                     +  yourMathSeriesAnswer );
             System . out . println ();
             System . out . println ( "Would you like to try again?" );
             do
             {

                 System . out . println ( "Please answer \"y\" for Yes or \"n\" for No." );
                yesNo  =  keyboard . next ();
             }   while   ( ! yesNo . equalsIgnoreCase ( "y" )   &&   ! yesNo . equalsIgnoreCase ( "n" ));
             if   ( yesNo . equalsIgnoreCase ( "y" ))
                quit  =   false ;
             else
                quit  =   true ;
         }   while   ( ! quit );

         System . out
                 . println ( "Finally, we will look at the convergence of a power series for the series:"
                         +   "\n\n      Sum from n = 0 to n = infinity x to the nth.\n" );
         System . out . println ( "For the values of x from 0 to 1 exclusive." );
         do
         {
             System . out
                     . println ( "Please enter the value for \"x\". (from 0 to 1 exclusive)" );
            valueOfX  =  keyboard . nextDouble ();

             System . out
                     . println ( "Please enter a value for the number of terms in the series. (from 1 to 20)" );
            numberOfTerms  =  keyboard . nextInt ();

            functionAnswer  =   1   /   ( 1   -  valueOfX );
            myPower . setX ( valueOfX );
            myPower . setNumOfTerms ( numberOfTerms );
            yourMathSeriesAnswer  =  myPower . run_PowerSeries ();
             System . out . println ();
             System . out . println ( "The Function 1/(1 - x) returns the value of "
                     +  functionAnswer );
             System . out . println ( "Your power series answer, for the same function, is "
                     +  yourMathSeriesAnswer );
             System . out . println ();
             System . out . println ( "Would you like to try again?" );
             do
             {

                 System . out . println ( "Please answer \"y\" for Yes or \"n\" for No." );
                yesNo  =  keyboard . next ();
             }   while   ( ! yesNo . equalsIgnoreCase ( "y" )   &&   ! yesNo . equalsIgnoreCase ( "n" ));
             if   ( yesNo . equalsIgnoreCase ( "y" ))
                quit  =   false ;
             else
                quit  =   true ;
         }   while   ( ! quit );
     }

     private   static   void  fibonociiSeries ()
     {
         int  count ;
         boolean  isValid  =   false ;
         int  firstNumber  =   0 ;
         int  secondNumber  =   0 ;
         System . out
                 . println ( "Please enter the number of permutations you would like to print from the Fibonocii sequence." );
        count  =  keyboard . nextInt ();
        myFibonacci . printSequence ( count );
         System . out
                 . println ( "Now let's select a short segment of the Fibonocii sequence to print." );

         do
         {
             System . out
                     . println ( "Please enter the first number in your Fibonocii sequence segment." );
            firstNumber  =  keyboard . nextInt ();
             System . out
                     . println ( "Please enter the second number in your Fibonocii sequence segment." );
            secondNumber  =  keyboard . nextInt ();
            isValid  =  myFibonacci . validStartNumbers ( firstNumber ,  secondNumber );
         }   while   ( ! isValid );

         System . out
                 . println ( "Please enter the number of permutations you would like to print from the Fibonocii sequence." );
        count  =  keyboard . nextInt ();

        myFibonacci . printSequence ( firstNumber ,  secondNumber ,  count );
        myFibonacci . resetSequence ();

     }

     private   static   String  selectSerries ()
     {
         String  selection  =   "" ;
         do
         {
             System . out . println ( "Please select from the following list" );
             System . out . println ( "A: Fibonocii Sequence" );
             System . out . println ( "B: Power Series" );
            selection  =  keyboard . next ();
         }   while   ( ! selection . equalsIgnoreCase ( "a" )
                 &&   ! selection . equalsIgnoreCase ( "b" ));
         return  selection ;

     }

}

SeriesTester/src/seriesTester/math2.java

SeriesTester/src/seriesTester/math2.java

package  seriesTester ;

public   class  math2
{
     public   int  factorial ( int  newInt )
     {
         if   ( newInt  >   0 )
            newInt  *=  factorial ( newInt  -   1 );
         else
            newInt  =   1 ;
         return  newInt ;
     }

}