This is a java program. Create a polymorphic math game that prints out sequences
ID: 3820031 • Letter: T
Question
This is a java program.
Create a polymorphic math game that prints out sequences of a numbers using a recursive function and asks the user to indicate what the next number is in the sequence. Once a user chooses a function, you should tell the user if they are correct or not. To check if the user is correct, you are to use recursion to find the next number in the sequence.
What is the next number in this sequence? 8, 16, 24, 32, 40, 48, …
• 8
• 16
• 56
• 64
You are to create different classes for each sequence and then put an instance of that class into an array or ArrayList. Then, when the game is played, you should randomly pick from the list and display the sequence and potential answers.
Explanation / Answer
Please below program for the given scenario
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Random;
import java.util.Scanner;
public class Recursion {
public static void main(String[] args)
{
List<Sequence> list = new ArrayList<Sequence>();
//Put instances of sequence classes in the list
list.add(new Fibonnaci());
list.add(new MultipleOf5());
list.add(new MultipleOf8());
Random rand = new Random();
int n = rand.nextInt(2)+1;
Sequence seq = list.get(n);
System.out.print("What is the next number in this sequence? ");
//Retrieve the sequence
Integer nums[] = seq.getSequence();
for(int i=0;i<6;i++)
{
System.out.print(nums[i]+", ");
}
System.out.println("...");
List<Integer> randomNumbers = getRandomNumbers(nums);
//Put the correct answer in choices if not yet present
if(!randomNumbers.contains(nums[6]))
{
int index = rand.nextInt(3)+1;
randomNumbers.set(index, nums[6]);
}
//Show option to user
for(int i=0;i<4;i++)
{
System.out.println(randomNumbers.get(i));
}
Scanner scan = new Scanner(System.in);
//Get user choice
System.out.print("Enter the number: ");
int num = scan.nextInt();
//Check if chosen number was correct or not
if(nums[6]==num)
System.out.println("Your choice was correct");
else
System.out.println("Your choice was wrong");
scan.close();
}
//Get random numbers from the sequence array
public static List<Integer> getRandomNumbers(Integer arr[])
{
Integer[] arr1 = new Integer[10];
for(int i=0;i<10;i++)
arr1[i] = arr[i];
List<Integer> list = Arrays.asList(arr1);
Collections.shuffle(list);
return list.subList(0, 4);
}
}
//Interface for method getSequence
interface Sequence{
public Integer[] getSequence();
}
class Fibonnaci implements Sequence
{
Integer[] arr = new Integer[10];
public Fibonnaci()
{
fib(9); //Calulate fibonnaci series
}
public Integer[] getSequence()
{
return arr;
}
public int fib(int num)
{
if(num==0||num==1)
{
if(arr[num]==null)
arr[num]=1;
return 1;
}
else
{
int fib = fib(num-1) + fib(num-2);
if(arr[num]==null)
arr[num]=fib;
return fib;
}
}
}
class MultipleOf8 implements Sequence
{
Integer[] arr = new Integer[10];
public MultipleOf8()
{
table(1,8); //Table of 8 using recursion
}
public Integer[] getSequence()
{
return arr;
}
public int table(int i,int n){
int num;
if(i==11){
return 0;
}
else{
num=n*i;
arr[i-1]= num;
return (table(++i,n));
}
}
}
class MultipleOf5 implements Sequence
{
Integer[] arr = new Integer[10];
public MultipleOf5()
{
table(1,5); //Table of 5 using recursion
}
public Integer[] getSequence()
{
return arr;
}
public int table(int i,int n){
int num;
if(i==11){
return 0;
}
else{
num=n*i;
arr[i-1]= num;
return (table(++i,n));
}
}
}
Sample Output
What is the next number in this sequence? 8, 16, 24, 32, 40, 48, ...
16
56
48
40
Enter the number: 56
Your choice was correct
What is the next number in this sequence? 5, 10, 15, 20, 25, 30, ...
45
30
35
25
Enter the number: 35
Your choice was correct
What is the next number in this sequence? 5, 10, 15, 20, 25, 30, ...
30
35
20
50
Enter the number: 50
Your choice was wrong
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.