I have looked around this site for awhile trying to find the answer to this problem but the answers are always too specific to a particular program to help me. The code I am having trouble on is when I try and instantiate a new ArithmeticSequence
object using the constructor defined in the ArithmeticSequence
class. However, Netbeans is telling me this is a non static variable being referenced from static context. I tried changing the class ArithmeticSequence
to static but this didn't work either, as a new error arose which only allowed me to use the default constructor. What I'm trying to do is instantiate mySequence
as a new ArithmeticSequence using the two values read from the file SEQ.in
. I get the error message on the mySequence = new ArithmeticSequence(FT, SI);
line. Anyways, here's the code:
import java.io.*;
import java.util.Scanner;
public class SeqTest {
public static void main(String[] args) {
Sequence mySequence;
try (Scanner sc = new Scanner(new FileReader("SEQ.in"))) {
if (sc.hasNext()) {
for (int i = 0; i < 3; i++) {
String Line = sc.next();
Character type = Line.charAt(0);
double FT = sc.nextDouble();
double SI = sc.nextDouble();
switch (type) {
case 'A':
mySequence = new ArithmeticSequence(FT, SI);
}
System.out.println("The first 10 terms in the sequence are:");
System.out.println(mySequence.toString());
System.out.println("The sum of the first 5 terms = " + mySequence.getNthSum(5));
}
}
} catch (FileNotFoundException fnfe) {
System.out.println(fnfe);
}
}
interface SequenceOps {
public double getNthTerm(int n);
public double getNthSum(int n);
}
abstract static class Sequence implements SequenceOps {
private double firstTerm;
public void Sequence() {
firstTerm = 1;
}
public void Sequence(double j) {
firstTerm = j;
}
public double getFirstTerm() {
return firstTerm;
}
@Override
public String toString() {
String q = "";
for (int i = 10; i >= 0; i--) {
q += getNthTerm(i) + ", ";
}
return q;
}
}
public class ArithmeticSequence extends Sequence {
private double comDif;
public void ArithmeticSequence() {
Sequence(1);
comDif = 0;
}
public void ArithmeticSequence(double j, double k) {
Sequence(j);
comDif = k;
}
@Override
public double getNthTerm(int n) {
return getFirstTerm() + (n - 1) * comDif;
}
@Override
public double getNthSum(int n) {
double q = 0;
for (int i = n; i >= 0; i--) {
double j = getNthTerm(i);
q += j;
}
return q;
}
}
public class GeometricSequence extends Sequence {
private double ratio;
public void GeometricSequence() {
Sequence(1);
ratio = 1;
}
public void GeometricSequence(double i, double k) {
Sequence(i);
ratio = k;
}
@Override
public double getNthTerm(int n) {
return getFirstTerm() * (Math.pow(ratio, (n - 1)));
}
@Override
public double getNthSum(int n) {
double q = 0;
for (int i = n; i >= 0; i--) {
q += getNthTerm(i);
}
return q;
}
}
public class FibonacciSequence extends Sequence {
private double secondTerm;
public void FibonacciSequence() {
Sequence(1);
secondTerm = 1;
}
public void FibonacciSequence(double j, double i) {
Sequence(j);
secondTerm = i;
}
@Override
public double getNthTerm(int n) {
double q = 1.6180339887;
return secondTerm * Math.pow(q, n) + getFirstTerm() * Math.pow(q, n);
}
@Override
public double getNthSum(int n) {
double q = 0;
for (int i = n; i >= 0; i--) {
q += getNthTerm(i);
}
return q;
}
}
}