Assignment 3 Solution

$30.00

Category: Tag:

Description

Assignment 3 Skills

Using Inheritance

Understanding Polymorphism

Understanding Class design

Using an Interface to de ne behaviors for a class

Assignment 3 Background

For this assignment, you will write several classes that model di erent types of sequences.

Recall:

  1. An arithmetic sequence is a sequence that has a constant di erence between terms. Given the rst term and the constant di erence, you can nd any term in the sequence.

  1. A geometric sequence is a sequence that has a constant ratio between terms. Given the rst term and the constant ratio, you may nd any term in the sequence.

  1. The Fibonacci sequence is a sequence where each term in the sequence is the sum of the two terms that precede it. The rst two terms in the Fibonnaci sequence are 0 and 1.

*We will create a generalized Fibonacci sequence*

Assignment 3 Requirements

  1. (10%) Write an interface called SequenceOps. This interface will de ne behavior for objects that have sequential properties. This interface should have the following methods de ned:

    1. A method that gets the nth term in a sequence. This method requires a parameter for n and returns the nth term.

    1. A method that sums the rst n terms in the sequence. This method requires a parameter for n and returns the sum of the rst n terms.

  1. (15%) Write an abstract class called Sequence. This class de nes properties and methods that all sequences will have. This class must implement the SequenceOps interface. This class should include the following:

    1. a private eld for the rst term in the sequence, type: double.

    1. a default constructor that sets the rst term to 1.

    1. a constructor that sets the rst term using a parameter value.

    1. A method that returns the rst term in the sequence.

    1. A toString method that returns a String of the rst 10 terms in a sequence in a comma delimited list.

  1. (15%) Write a class called ArithmeticSequence that extends the Sequence class and implements the SequenceOps interface. This class should include the following:

    1. a private eld for the common di erence.

    1. a default constructor that initializes the rst term to 1 and the common di erence to 0.

    1. a constructor that uses parameters to initialize the common di erence and the rst term.

  1. (15%) Write a class called GeometricSequence that extends the Sequence class and implements the SequenceOps interface. This class should include the following:

    1. a private eld for the common ratio.

    1. a default constructor that initializes the rst term to 1 and the common ratio to 1.

    1. a constructor that uses parameters to initialize the common ratio and the rst term.

  1. (15%) Write a class called FibonacciSequence that extends the Sequence class and implements the SequenceOps interface. This class should include the following:

    1. a private eld for the second term.

    1. a default constructor that initializes the rst term to 0 and the second term to 1.

    1. a constructor that uses parameters to initialize the the rst and second terms.

  1. (20%) Write a main method in a le called SequenceTest.java. Your main method should:

    1. Create a Sequence variable

    1. Read from the input le SEQ.dat, which is formatted as follows:

      1. The rst item on a line is a letter that represents the type of sequence (A, G, or F ).

      1. the second item on a line is a double value that represents the rst term in the sequence.

      1. the thrid item on a line is a double value that represents either the common di erence, common ratio, or second term, depending on the type of sequence.

    1. Use toString and other methods to produce the output in the following format (for each line of input):

      1. Print to standard out: ‘The rst 10 terms in the sequence are:’

      1. Print to standard out a comma-delimited list of the rst 10 terms.

      1. Print to standard out: ‘The sum of the rst 5 terms =’ the sum of the rst 5 terms.

  1. (10%) Provide comments where appropriate

Assignment 3 Restrictions

  1. (-10) Do not use any reference variables for ArithmeticSequence, GeometricSequence, or FibonacciSequence. Use only a single Sequence variable in you main method.

  1. (-10) Do not override toString method in ArithmeticSequence, GeometricSequence, or FibonacciSequence class.

  1. (-5) The toString method must return a String and cannot print to standard out. All printing should be done in the main method.

  1. (-10) Do not use recursion.

Assignment 3 Submission Submit on Blackboard:

  1. SequenceOps.java

  1. Sequence.java

  1. ArithmeticSequence.java

  1. GeometricSequence.java

  1. FibonacciSequence.java

  1. SequenceTest.java

Required Each submitted le should include your name and a statement that this is your own work. This should appear as a comment at the beginning of any code le.