Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Use Java 7. Follow the instructions precisely or I will not award points. Your p

ID: 3550217 • Letter: U

Question

Use Java 7. Follow the instructions precisely or I will not award points. Your program MUST run in NetBeans or I will not award points. If you think information is missing just comment. I think this question wants you to use polymorphism.





These are all the files:





/**
* From Goodrich & Tamassia, "Data Structures and Algorithms in Java,
* 4th ed, p. 71.
* A class for numeric progressions.
*/
public class Progression {

  /** First value of the progression.  */
  protected long first;

  /** Current value of the progression.  */
  protected long cur;

  /** Default constructor.  */
  Progression() {
    cur = first = 0;
  }

  /** Resets the progression to the first value.
   *
   * @return first value
   */
  protected long firstValue() {
    cur = first;
    return cur;
  }

  /** Advances the progression to the next value.
   *
   * @return next value of the progression
   */
  protected long nextValue() {
    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();
  }
}








/**
* Fibonacci progression.
*/
class FibonacciProgression extends Progression {
  /** Previous value. */
  long prev;   
  // Inherits variables first and cur.

  /** Default constructor setting 0 and 1 as the first two values. */
  FibonacciProgression() {
    this(0, 1);
  }
  /** Parametric constructor providing the first and second values.
   *
   * @param value1 first value.
   * @param value2 second value.
   */
  FibonacciProgression(long value1, long value2) {
      first = value1;
      prev = value2 - value1; // fictitious value preceding the first
  }

  /** Advances the progression by adding the previous value to the current value.
   *
   * @return next value of the progression
   */
  protected long nextValue() {
    long temp = prev;
    prev = cur;
    cur += temp;
    return cur;
  }
  // Inherits methods firstValue() and printProgression(int).
}







/**
* Geometric Progression
*/
class GeomProgression extends Progression {

  /** Base. */
  protected long base;

  // Inherits variables first and cur.

  /** Default constructor setting base 2. */
  GeomProgression() {
    this(2);
  }

  /** Parametric constructor providing the base.
   *
   * @param b base of the progression.
   */
  GeomProgression(long b) {
    base = b;
    first = 1;
    cur = first;
  }

  /** Advances the progression by multiplying the base with the current value.
   *
   * @return next value of the progression
   */
  protected long nextValue() {
    cur *= base;
    return cur;
  }

  //  Inherits methods firstValue() and printProgression(int).
}







/** Test program for the progression classes */
class TestProgression {
  public static void main(String[] args) {
    Progression 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(5);
    prog.printProgression(10);

    //System.out.println(((GeomProgression) prog).nextValue());

    // 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(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(4,6);
    prog.printProgression(10);
  }
}









/**
* Arithmetic progression.
*/
class ArithProgression extends Progression {

  /** Increment. */
  protected long inc;

  // Inherits variables first and cur.

  /** Default constructor setting a unit increment. */
  ArithProgression() {
    this(1);
  }

  /** Parametric constructor providing the increment. */
  ArithProgression(long increment) {
    inc = increment;
  }

  /** Advances the progression by adding the increment to the current value.
   *
   * @return next value of the progression
   */
  protected long nextValue() {
    cur += inc;
    return cur;
  }

  //  Inherits methods firstValue() and printProgression(int).
}

Write a Java class that extends the Progression class (the files are located in calvin/POLYMORPHISM/PROGRESSION/) so that each value in the progression is the absolute value of the difference between the two previous values. Include a default constructor that starts with 2 and 200 as the first two values, and also a parametric constructor that starts with a specified pair of numbers as the first two values. Write a program that reads in three integer parameters; the first two values and the number of elements to print. For example if thge input is 4 7 5 then the output should be 4 7 3 4 1

Explanation / Answer

/**

*New Progression Class

*

*/

public class NewProgression extends Progression{

protected long prev;

/** Default constructor setting 2 and 200 as the first two values. */

NewProgression() {

this(2, 200);

}

/** Parametric constructor providing the first and second values.

*

* @param value1 first value.

* @param value2 second value.

*/

NewProgression(long value1, long value2) {

first=value1;

prev=value2+value1;//fictious value

}

/** Advances the progression by subtracting the previous value to the current value.

*

* @return next value of the progression

*/

protected long nextValue() {

long temp = prev;

prev=cur;

cur-=temp;

if(cur<0)

cur=cur*(-1);

return cur;

}

}



/*

*

*Class to test the above NewProgression Class

*/

public class TestNewProgression {

public static void main(String[] args) {

Progression prog;

long first,second;

int terms;

Scanner in=new Scanner(System.in);

System.out.println("Enter the first values");

first=in.nextLong();

System.out.println("Enter the second value");

second=in.nextLong();

System.out.println("Enter the number of terms");

terms=in.nextInt();

System.out.println("Testing");

prog=new NewProgression();

prog.printProgression(7);

prog=new NewProgression(first,second);

prog.printProgression(terms);

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote