Question: Look for some java help with this program. I just two more things added to this code. A loop at the end that will ask
Look for some java help with this program.
I just two more things added to this code. A loop at the end that will ask the user if they want to quit if they do want to quit the program stops if they don't it loads a new number sequence. After that if two more classes MultipleOf3 and MultipleOf10 where added and put in the rand choice that would be helpful
Thanks
my code.
main.java
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections; // collection package is imported as array is used
import java.util.List; // list is a part of collection package
import java.util.Random;
import java.util.Scanner; // this is imported for user input
class Main // Main Class {
public static void main(String[] args) // Main method
{
List
list.add(new MultipleOf5());// for multiple of 5
list.add(new MultipleOf8());// for the factors of 8
Random rand = new Random(); // Object is created for random class
int n = rand.nextInt(2)+1; // user input of int data type Sequence seq = list.get(n); // print statement for the input of next number in the series. System.out.print("What is the next number in this sequence? ");
Integer nums[] = seq.getSequence();// to retrieve the sequence
for(int i=0;i<6;i++) // for loop is defined {
System.out.print(nums[i]+", "); // print statement to print the numbers
}
System.out.println("..."); // print the ...
List
if(!randomNumbers.contains(nums[6])) // for putting the correct answer in choice in //the case not yet present
{ int index = rand.nextInt(3)+1; //user input of int data type.
randomNumbers.set(index, nums[6]);
} // to the options to the users for(int i=0;i<4;i++) // for loop is defined
{ System.out.println(randomNumbers.get(i)); // print the no.s
}
Scanner scan = new Scanner(System.in); // Object for the scanner class id defined // For getting user's choice
System.out.print("Enter the number: ");// print statement to enter the numbers.
int num = scan.nextInt();// For the user input // if conditional statement is defined in order to check if the chosen number is correct or //not if(nums[6]==num)
System.out.println("Your choice was correct");// print if chosen number is right
else
System.out.println("Your choice was wrong");// print if the chosen number is not //right
scan.close();// method calling
}
public static List
{ Integer[] arr1 = new Integer[10];// object is created
for(int i=0;i<10;i++) // for loop
arr1[i] = arr[i]; //defining the array
List
Collections.shuffle(list);
return list.subList(0, 4); // shows the return
}
}
Sequence.java
interface Sequence // interface is defined for the method getSequence {
public Integer[] getSequence(); //getSequence method is defined
}
Fibonnaci.java
class Fibonnaci implements Sequence // inhetritance is used and interface sequence is implemented //by the class fibonacci
{
Integer[] arr = new Integer[10]; // Object is created
public Fibonnaci() // Constructor is defined as the name is similar to the class
{ fib(9); //for the calculation of Fibonacci series
} public Integer[] getSequence() // Again getSequence method is defined
{ return arr; // shows the array to be returned
} public int fib(int num) // variable is defined
{ if(num==0||num==1) // conditional statement if else is defined
{
if(arr[num]==null)
arr[num]=1;
return 1;
}
else
{ int fib = fib(num-1) + fib(num-2); // show the calculation for the fibonacci series
if(arr[num]==null) // if array will be empty
arr[num]=fib;
return fib; // shows the return value
}
}
}
MultipleOf8.java
class MultipleOf8 implements Sequence// Again inheritance is used and class MultipleOf8 implements //interface Sequence
{ Integer[] arr = new Integer[10]; // object is created
public MultipleOf8() // Default constructor
{
table(1,8); //recursion is used for table of 8
} public Integer[] getSequence() // GetSequence method
{ return arr; // return value
}
public int table(int i,int n) // variables { int num; // variables
if(i==11)// if else conditional statement {
return 0;
}
else{ // calculation for table num=n*i;
arr[i-1]= num;
return (table(++i,n)); // returns the table
}
}
}
MultipleOf5.java
class MultipleOf5 implements Sequence // again class MultipleOf5 implements interface Sequence //through inheritance
{ Integer[] arr = new Integer[10]; // object creation
public MultipleOf5() // default constructor is defined
{ table(1,5); //recursion is used for the table of 5
}
public Integer[] getSequence() // getSequence method
{ return arr;
} // Variables are defined public int table(int i,int n) { int num; // if else conditional statement if(i==11) { return 0; // returns zero
}
else{ // calculation for the table num=n*i;
arr[i-1]= num;
return (table(++i,n)); // returns the result as table
}
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
