Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Define (i.e. add) a Java class FibonacciProgressionBigInt to re-implement FibonacciProgression subclass to rely on the BigInteger class (Consult Java API on how to use BigInteger).

Define (i.e. add) a Java class FibonacciProgressionBigInt to re-implement FibonacciProgression subclass to rely on the BigInteger class (Consult Java API on how to use BigInteger). That is, you need to use BigInteger for the progression instead of long. Test this new implementation in the test class with real big integer! (Hints: you may need to add a new abstract class (the one with some changes on the abstract class used in previous steps).

CODE:

import java.util.*; /** Generates a simple progression. By default: 0, 1, 2, ... */ abstract class Progression { // mofidied - added abstract keyword // instance variable protected long current; /** Constructs a progression starting at zero. */ public Progression() { this(0); } /** Constructs a progression with given start value. */ public Progression(long start) { current = start; } /** Returns the next value of the progression. */ public long nextValue() { long answer = current; advance(); // this protected call is responsible for advancing the current value return answer; } /** Advances the current value to the next value of the progression. */ abstract protected void advance(); // - modified - added abstract keyword and removed the implementation /** Prints the next n values of the progression, separated by spaces. */ public void printProgression(int n) { System.out.print(nextValue()); // print first value without leading space for (int j=1; j < n; j++) System.out.print(" " + nextValue()); // print leading space before others System.out.println(); // end the line } } class ArithmeticProgression extends Progression { protected long increment; /** Constructs progression 0, 1, 2, ... */ public ArithmeticProgression() { this(1, 0); } // start at 0 with increment of 1 /** Constructs progression 0, stepsize, 2*stepsize, ... */ public ArithmeticProgression(long stepsize) { this(stepsize, 0); } // start at 0 /** Constructs arithmetic progression with arbitrary start and increment. */ public ArithmeticProgression(long stepsize, long start) { super(start); increment = stepsize; } /** Adds the arithmetic increment to the current value. */ protected void advance() { current += increment; } } class FibonacciProgression extends Progression { protected long prev; /** Constructs traditional Fibonacci, starting 0, 1, 1, 2, 3, ... */ public FibonacciProgression() { this(0, 1); } /** Constructs generalized Fibonacci, with give first and second values. */ public FibonacciProgression(long first, long second) { super(first); prev = second - first; // fictitious value preceding the first } /** Replaces (prev,current) with (current, current+prev). */ protected void advance() { long temp = prev; prev = current; current += temp; } } class GeometricProgression extends Progression { protected long base; /** Constructs progression 1, 2, 4, 8, 16, ... */ public GeometricProgression() { this(2, 1); } // start at 1 with base of 2 /** Constructs progression 1, b, b^2, b^3, b^4, ... for base b. */ public GeometricProgression(long b) { this(b, 1); } // start at 1 /** Constructs geometric progression with arbitrary base and start. */ public GeometricProgression(long b, long start) { super(start); base = b; } /** Multiplies the current value by the geometric base. */ protected void advance() { current *= base; // multiply current by the geometric base } } /** Test program for the progression hierarchy. */ public class TestProgression { public static void main(String[] args) { Progression prog; // test ArithmeticProgression System.out.print("Arithmetic progression with default increment: "); prog = new ArithmeticProgression(); prog.printProgression(10); System.out.print("Arithmetic progression with increment 5: "); prog = new ArithmeticProgression(5); prog.printProgression(10); System.out.print("Arithmetic progression with start 2: "); prog = new ArithmeticProgression(5, 2); prog.printProgression(10); // test GeometricProgression System.out.print("Geometric progression with default base: "); prog = new GeometricProgression(); prog.printProgression(10); System.out.print("Geometric progression with base 3: "); prog = new GeometricProgression(3); prog.printProgression(10); // test FibonacciProgression System.out.print("Fibonacci progression with default start values: "); prog = new FibonacciProgression(); prog.printProgression(10); System.out.print("Fibonacci progression with start values 4 and 6: "); prog = new FibonacciProgression(4, 6); prog.printProgression(8); } } /* Output from main method: Arithmetic progression with default increment: 0 1 2 3 4 5 6 7 8 9 Arithmetic progression with increment 5: 0 5 10 15 20 25 30 35 40 45 Arithmetic progression with start 2: 2 7 12 17 22 27 32 37 42 47 Geometric progression with default base: 1 2 4 8 16 32 64 128 256 512 Geometric progression with base 3: 1 3 9 27 81 243 729 2187 6561 19683 Fibonacci progression with default start values: 0 1 1 2 3 5 8 13 21 34 Fibonacci progression with start values 4 and 6: 4 6 10 16 26 42 68 110 */

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

The Accidental Data Scientist

Authors: Amy Affelt

1st Edition

1573877077, 9781573877077

More Books

Students also viewed these Databases questions

Question

What are the largest 2 - cliques in this network

Answered: 1 week ago