need help writing this using java Geometric Sequences Description: A geometric s
ID: 652741 • Letter: N
Question
need help writing this using java
Geometric Sequences
Description:
A geometric sequence is defined by the following (closed-form) formula:
an = a0·rn-1
This can also be written using the recursive relation:
an = r·an-1
Where an represents the n-th term in the sequence.
For example, the sequence
an = 12 ·an-1, with a0 = 10
is given by:
10, 5, 52, 54, ...
Objective:
Create a class GeometricSequence with the following methods:
geometricSequence that takes three parameters:aNaught: the first element of the sequence (i.e. a0)ratio: the ratio to multiply by (i.e. r)index: the index of the term in the sequence to find (i.e. n)and finds the n-th term in the sequence using the closed form (i.e. non-recursive) formula. that takes the same three parameters, but uses the second definition to find the solution recursively.A main method that asks for the first term in the sequence, a0; the ratio, r; and the index, n; and calls each of the previous methods.
In addition, geometricSequenceRecursive should print a stack trace to show how many steps were required to arrive at the solution. It must only print the stack trace once, at the deepest level of recursion. You may use the following method to print the stack trace:
public static void printStackTrace() { StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace(); for ( int i = 2; i < stackTrace.length - 1; i++ ) { System.out.println( " " + (stackTrace.length - i - 1) + ". " + stackTrace[i] ); } }
Note that some IDEs may print a longer stack trace than others. You should only concern yourself with elements in the stack trace that share the name of your class,GeometricSequence. That is elements of the form:
GeometricSequence.geometricSequenceRecursive(...)
Example Dialog:
Enter the first term in the sequence: 10 Enter the ratio: .5 Enter the index of the term to find: 4 The 4th term in the sequence using an iterative solution: 1.25 The 4th term in the sequence using a recursive solution: 4. GeometricSequence.geometricSequenceRecursive(...) 3. GeometricSequence.geometricSequenceRecursive(...) 2. GeometricSequence.geometricSequenceRecursive(...) 1. GeometricSequence.geometricSequenceRecursive(...) . . . 1.25
Explanation / Answer
import java.util.*;
//Create a class GeometricSequence with the following methods:
public class GeometricSequence{
// geometricSequence that takes three parameters:
// aNaught: the first element of the sequence (i.e. a0)
// ratio: the ratio to multiply by (i.e. r)
// index: the index of the term in the sequence to find (i.e. n)
// and finds the n-th term in the sequence using the closed form (i.e. non-recursive) formula.
public static double geometricSequence(int aNaught, double ratio, int index){
double result = aNaught;
for(int i=0; i<index-1; i++)
result = result*ratio;
return result;
}
// geometricSequenceRecursive that takes the same three parameters, but uses the second definition to find the solution recursively.
public static double geometricSequenceRecursive(int aNaught, double ratio, int index){
if(index==1) {
printStackTrace();
return aNaught;
}
else return ratio*geometricSequenceRecursive(aNaught, ratio, index-1);
}
public static void main(String[] args)
{
//A main method that asks for the first term in the sequence, a0; the ratio, r; and the index, n; and calls each of the previous methods.
int a0;
double r;
int index;
Scanner in = new Scanner(System.in);
System.out.println("Enter the first term in the sequence: ");
a0 = in.nextInt();
System.out.println("Enter the ratio: ");
r = in.nextDouble();
System.out.println("Enter the index of the term to find: ");
index = in.nextInt();
System.out.println("The "+index+"th term in the sequence using an iterative solution:");
System.out.println(geometricSequence(a0, r, index));
System.out.println("The "+index+"th term in the sequence using a recursive solution:");
System.out.println(geometricSequenceRecursive(a0, r, index));
}
//In addition, geometricSequenceRecursive should print a stack trace to show how many steps were required to arrive at the solution.
//It must only print the stack trace once, at the deepest level of recursion. You may use the following method to print the stack trace:
public static void printStackTrace()
{
StackTraceElement[] stackTrace =
Thread.currentThread().getStackTrace();
for ( int i = 2; i < stackTrace.length - 1; i++ ) {
System.out.println( " " + (stackTrace.length - i - 1)
+ ". " + stackTrace[i] );
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.