Hello everygone!! I signed up here hoping I will receive some help. I realize that every 2nd person writes: I am new...bla bla bla. I am new:)...I was sitting last 2 days trying to solve the issue and I think I am close but I don't see something.
This is my assignment. take a look what I have to do and how I tried to solve it.
this is what I was told to do (it is just a part of it):
- this part I have problem with calculation.
Write a Java class (C2_6Progression.java) that extends the Progression class so that each value in the progression is the floor of the square root of the previous value. You are not allowed to use the Math.sqrt() method from the Java Math library. You should include a default constructor that has 65,536 as the first vlue and a parametric constructor that starts with a specified non-negative integer as the first value. You must name your class C2_6Progression .
- this part I am not sure how to write at all -
Implement a Java class (PrimeProgression) that extends the Progression class so that each value in the progression is the smallest prime number that is greater than the previous one. The default constructor PrimeProgression() use 2 as the initial value. The constructor PrimeProgression(BigInteger start) sets the cur value to the smallest prime number that is equal to or greater than start.
I will appreciate for any tips, help...anything.
and this is the whole code:
package myprogression;
import java.math.BigInteger;
public class MyProgression {
/** This is a main method !!!!!!!!!!!!! Test program for the progression classes */
public static void main(String[] args) {
MyProgression prog;
// test ArithProgression
System.out.println("Arithmetic progression with default increment:");
prog = new ArithProgression();
prog.printProgression(10);
System.out.println("Arithmetic progression with increment 5:");
prog = new ArithProgression(new BigInteger("5"));
prog.printProgression(10);
// test GeomProgression
System.out.println("Geometric progression with default base:");
prog = new GeomProgression();
prog.printProgression(10);
System.out.println("Geometric progression with base 3:");
prog = new GeomProgression(new BigInteger("3"));
prog.printProgression(10);
// test FibonacciProgression
System.out.println("Fibonacci progression with default start values:");
prog = new FibonacciProgression();
prog.printProgression(10);
System.out.println("Fibonacci progression with start values 4 and 6:");
prog = new FibonacciProgression(new BigInteger("4"), new BigInteger("6"));
prog.printProgression(10);
// test C2_5Progression - homework part 2
System.out.println("C2_5Progression progression with default start values:");
prog = new C2_5Progression();
prog.printProgression(10);
System.out.println("C2_5Progression progression with start values 2 and 200:");
prog = new C2_5Progression(new BigInteger("2"), new BigInteger("200"));
prog.printProgression(20);
// C2_6Progression
System.out.println("C2_6Progression progression with start value 65,536:");
prog = new C2_6Progression(new BigInteger("65536"));
prog.printProgression(10);
}
/** First value of the progression. */
protected BigInteger first;
/** Current value of the progression. */
protected BigInteger cur;
/** Default constructor. */
MyProgression() {
cur = first = BigInteger.ZERO;
}
/** Resets the progression to the first value.
*
* @return first value
*/
protected BigInteger firstValue() {
cur = getFirst();
return cur;
}
/** Advances the progression to the next value.
*
* @return next value of the progression
*/
protected BigInteger nextValue() {
cur = cur.add(BigInteger.ONE);
return cur ; // default next value
}
/** Prints the first n values of the progression.
*
* @param n number of values to print
*/
public void printProgression(int n) {
System.out.print(firstValue());
for (int i = 2; i <= n; i++)
System.out.print(" " + nextValue());
System.out.println(); // ends the line
}
/**
* @return the first
*/
public BigInteger getFirst() {
return first;
}
/**
* @param first the first to set
*/
public void setFirst(BigInteger first) {
this.first = first;
}
}
/**
* Arithmetic progression starts here.
*/
class ArithProgression extends MyProgression {
/** Increment. */
protected BigInteger inc;
// Inherits variables first and cur.
/** Default constructor setting a unit increment. */
ArithProgression() {
this(BigInteger.ONE);
}
/** Parametric constructor providing the increment. */
ArithProgression(BigInteger increment) {
inc = increment;
}
/** Advances the progression by adding the increment to the current value.
*
* @return next value of the progression
*/
@Override
protected BigInteger nextValue() {
cur = cur.add(inc);
return cur;
}
// Inherits methods firstValue() and printProgression(int).
}
/**
* Geometric Progression starts here.
*/
class GeomProgression extends MyProgression {
/** Base. */
protected BigInteger base;
// Inherits variables first and cur.
/** Default constructor setting base 2. */
GeomProgression() {
this(BigInteger.ONE);
}
/** Parametric constructor providing the base.
*
* @param b base of the progression.
*/
GeomProgression(BigInteger b) {
base = b;
first = BigInteger.ONE;
cur = first;
}
/** Advances the progression by multiplying the base with the current value.
*
* @return next value of the progression
*/
@Override
protected BigInteger nextValue() {
cur = cur.multiply(base);
return cur;
}
// Inherits methods firstValue() and printProgression(int).
}
/**
* Fibonacci progression starts here.
*/
class FibonacciProgression extends MyProgression {
/** Previous value. */
BigInteger prev;
// Inherits variables first and cur.
/** Default constructor setting 0 and 1 as the first two values. */
FibonacciProgression() {
this(BigInteger.ZERO, BigInteger.ONE);
}
/** Parametric constructor providing the first and second values.
*
* @param value1 first value.
* @param value2 second value.
*/
FibonacciProgression(BigInteger value1, BigInteger value2) {
first = value1;
prev = value2.subtract(value1); // fictitious value preceding the first
}
/** Advances the progression by adding the previous value to the current value.
*
* @return next value of the progression
*/
@Override
protected BigInteger nextValue() {
BigInteger temp = prev;
prev = cur;
cur = cur.add(temp);
return cur;
}
// Inherits methods firstValue() and printProgression(int).
}
/* class created by me: homework 1 point 2 - C2_5Progression */
class C2_5Progression extends MyProgression {
/** Previous value. */
BigInteger prev;
// Inherits variables first and cur.
/** Default constructor setting 0 and 1 as the first two values. */
C2_5Progression() {
this(BigInteger.ZERO, BigInteger.ONE);
}
/** Parametric constructor providing the first and second values.
*
* @param value1 first value.
* @param value2 second value.
*/
C2_5Progression(BigInteger value1, BigInteger value2) {
first = value1;
prev = value1.subtract(value2); // fictitious value preceding the first
}
/** Advances the progression by adding the previous value to the current value.
*
* @return next value of the progression
*/
@Override
protected BigInteger nextValue() {
BigInteger temp = prev;
prev = cur;
cur = (cur.subtract(temp)).abs();
return cur;
}
}
//**************************
class C2_6Progression extends MyProgression {
/** Previous value. */
BigInteger prev;
// Inherits variables first and cur.
/** Default constructor */
C2_6Progression() {
this(new BigInteger("65536"));
}
/** Parametric constructor
*
* @param value1 first value.
* @param value2 second value.
*/
C2_6Progression(BigInteger first) {
if (first.compareTo(BigInteger.ZERO) > 0) {
first = cur;
}
else {
cur = new BigInteger("negative value"); // when negative
}
}
/** Advances the progression by adding the previous value to the current value.
*
* @return next value of the progression
*/
@Override
protected BigInteger nextValue() {
BigInteger temp = first;
if (temp.compareTo(BigInteger.ZERO) > 0) {
cur = sqrt(prev);
return cur;
}
else {
cur = new BigInteger("-1"); // Returns -1 if the value passed is negative
return cur;
}
}
protected BigInteger sqrt(BigInteger n) {
BigInteger r;
for (r = BigInteger.valueOf(1); ((r.multiply(r)).compareTo(n)) <= 0; r = r.add(BigInteger.ONE)){
if(r.multiply(r) == BigInteger.ZERO) {
return r;
}
else {
r = r.subtract(BigInteger.ONE);
return r;
}
}
return r;
}
}//end of the class c2_6Progression
//***********************************************
//**PrimeProgression class starts here.
class PrimeProgression extends MyProgression {
/** Previous value. */
BigInteger prev;
// Inherits variables first and cur.
/** Default constructor setting 0 and 1 as the first two values. */
PrimeProgression() {
this(new BigInteger("2"));
}