‹ Back to the paper
Question 7
10 marks
Computer ScienceISC 2026 Improvement Computer Science Paper 1ProgramObjects
Write the program described below.
A class FibList is designed to extract the terms of the Fibonacci series from a set of numbers. A…
A class FibList is designed to extract the terms of the Fibonacci series from a set of numbers.
A Fibonacci series starts with terms 0 and 1. All the other terms in this series are generated by adding the previous two terms.
[A Fibonacci series with 8 terms will be 0, 1, 1, 2, 3, 5, 8, 13]
Example: Input array = { 1, 4, 9, 3, 10, 13, 7 } then,
Output array = { 1, 3, 13 }
The details of the members of the class are given below:
Specify the class FibList giving the details of the constructor( ), void read( ), boolean checkFib(int), FibList genFib(FibList) and void display( ). Define the main( ) function to create objects and call the functions accordingly to enable the task.
| Class name | : | FibList |
|---|---|---|
| Data members/instance variables: | ||
| list[ ] | : | array to hold positive integers |
| size | : | to store the size of array |
| Methods/Member functions: | ||
| FibList(int s) | : | constructor to assign size = s |
| void read( ) | : | to accept the elements of the array |
| boolean checkFib(int n) | : | to check and return true if n is a Fibonacci term otherwise return false. |
| FibList genFib(FibList fbl) | : | to return a new object containing only Fibonacci terms from the object fbl by invoking the method checkFib( ) as per the given instructions |
| void display( ) | : | to display the elements of the original array and the array holding the Fibonacci terms |
Answer
Answer
import java.util.Scanner;
class FibList
{
int list[];
int size;
FibList(int s)
{
size = s;
list = new int[size];
}
void read()
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter " + size + " positive integers:");
for (int i = 0; i < size; i++)
list[i] = sc.nextInt();
}
boolean checkFib(int n)
{
int a = 0, b = 1;
if (n == 0)
return true;
while (b < n)
{
int c = a + b;
a = b;
b = c;
}
return (b == n);
}
FibList genFib(FibList fbl)
{
int count = 0;
for (int i = 0; i < fbl.size; i++)
if (fbl.checkFib(fbl.list[i]))
count++;
FibList temp = new FibList(count);
int j = 0;
for (int i = 0; i < fbl.size; i++)
{
if (fbl.checkFib(fbl.list[i]))
{
temp.list[j] = fbl.list[i];
j++;
}
}
return temp;
}
void display()
{
for (int i = 0; i < size; i++)
System.out.print(list[i] + " ");
System.out.println();
}
public static void main(String[] args)
{
FibList fbl = new FibList(7);
fbl.read();
FibList res = fbl.genFib(fbl);
System.out.print("Original array: ");
fbl.display();
System.out.print("Fibonacci terms: ");
res.display();
}
}Explanation: checkFib(n) tests whether n is a Fibonacci number by generating the series until the term is >= n and comparing. genFib(fbl) first counts how many elements of fbl.list are Fibonacci terms, creates a new FibList object of exactly that size, then copies only the Fibonacci terms into it and returns it. Tested (run for real) with input {1,4,9,3,10,13,7}: output was 'Original array: 1 4 9 3 10 13 7' and 'Fibonacci terms: 1 3 13', matching the example in the question.