2.5k views
1 vote
We want to create a class that represents a geometric sequence. A geometric sequence is a sequence

of numbers that begin at some value and then multiplies each value by some constant to get the next
value. For example, the geometric sequence 1, 2, 4, 8, 16 starts at 1 and multiplies each term by 2 to get
the next. The geometric sequence 10.8, 5.4, 2.7, 1.35 starts at 10.8 and multiplies each term by 0.5 to get
the next. The basic framework of a geometric sequence class is below:

public class GeometricSequence
{
private double initialValue;
private double multiplier;
}
We want to produce elements of the geometric sequence using codeSystem.out.println (first.next());
// Prints 1 and advances
System.out.println (first.next()); // Prints 2 and advances
System.out.println (first.next()); // Prints 4 and advances
System.out.println (first.next()); // Prints 8 and advances
System.out.println (second.next()); //Prints 10.8 and advances
System.out.println (second.next()); //Prints 5.4 and advances
System.out.println (second.next()); //Prints 2.7 and advances
What should the body of the next method be?

a) double result = initialValue;
initialValue = initialValue * multiplier;
return result;
b) return initialValue;
initialValue = initialValue * multiplier;
c) double result = initialValue;
multiplier = initialValue * multiplier;
return result;
d) initialValue = initialValue * multiplier;
return initialValue;

User Aabuhijleh
by
4.2k points

1 Answer

0 votes

Answer:

a) double result = initialValue;

initialValue = initialValue * multiplier;

return result;

Step-by-step explanation:

The next method will have the above code snippet as it body.

First, the value of initialValue is assigned to result. Next, the initialValue is re-calculated by assigning the value of multiplying the initialValue and multiplier to it. Lastly, the value of result is returned.

Therefore, when the next method is called, the value of result is returned.

User Gobernador
by
4.9k points