$24
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:
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.
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.
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
(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:
A method that gets the nth term in a sequence. This method requires a parameter for n and returns the nth term.
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.
(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:
a private eld for the rst term in the sequence, type: double.
a default constructor that sets the rst term to 1.
a constructor that sets the rst term using a parameter value.
A method that returns the rst term in the sequence.
A toString method that returns a String of the rst 10 terms in a sequence in a comma delimited list.
(15%) Write a class called ArithmeticSequence that extends the Sequence class and implements the SequenceOps interface. This class should include the following:
a private eld for the common di erence.
a default constructor that initializes the rst term to 1 and the common di erence to 0.
a constructor that uses parameters to initialize the common di erence and the rst term.
(15%) Write a class called GeometricSequence that extends the Sequence class and implements the SequenceOps interface. This class should include the following:
a private eld for the common ratio.
a default constructor that initializes the rst term to 1 and the common ratio to 1.
a constructor that uses parameters to initialize the common ratio and the rst term.
(15%) Write a class called FibonacciSequence that extends the Sequence class and implements the SequenceOps interface. This class should include the following:
a private eld for the second term.
a default constructor that initializes the rst term to 0 and the second term to 1.
a constructor that uses parameters to initialize the the rst and second terms.
(20%) Write a main method in a le called SequenceTest.java. Your main method should:
Create a Sequence variable
Read from the input le SEQ.dat, which is formatted as follows:
The rst item on a line is a letter that represents the type of sequence (A, G, or F ).
the second item on a line is a double value that represents the rst term in the sequence.
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.
Use toString and other methods to produce the output in the following format (for each line of input):
Print to standard out: ‘The rst 10 terms in the sequence are:’
Print to standard out a comma-delimited list of the rst 10 terms.
Print to standard out: ‘The sum of the rst 5 terms =’ the sum of the rst 5 terms.
(10%) Provide comments where appropriate
Assignment 3 Restrictions
(-10) Do not use any reference variables for ArithmeticSequence, GeometricSequence, or FibonacciSequence. Use only a single Sequence variable in you main method.
(-10) Do not override toString method in ArithmeticSequence, GeometricSequence, or FibonacciSequence class.
(-5) The toString method must return a String and cannot print to standard out. All printing should be done in the main method.
(-10) Do not use recursion.
Assignment 3 Submission Submit on Blackboard:
SequenceOps.java
Sequence.java
ArithmeticSequence.java
GeometricSequence.java
FibonacciSequence.java
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.