CSCI2010L Programming II Lab 4 Sequence Generator Create a Bluej project called
ID: 3749695 • Letter: C
Question
CSCI2010L Programming II Lab 4 Sequence Generator Create a Bluej project called Lab04-YOUR NAME. Then, create a class called SeqGen that implements the generation of 3 different kinds of integer sequence. The first sequence type is called "mxThis is a sequence of integers from 2 up to and including a maximum value. So max 5 would generate the sequence 12345 The second sequence type is called "minmax This is a sequence of integers starting at the entered minimum value and progressing up to and including the max value. So minmax 2 6 would generate the sequence: 23456 The third type of sequence is called "maxdiv. This is a sequence of integers starting at the entered minimum value and progressing up to and including max, but only the values evenly divisible by div. So maxdiv 2 303 Would generate the sequence:36 9 The SeqGen class should have a constructor for each sequence type. The SeqGen class should also contain a method hasNextValue that returns true if there is a next value in the current sequence and retarms false when there are no more values in this sequence. The method getNextValae should retursn the next available value in the cutrent sequence. Always call hasNextValue before calling getNextValue. If hasNextValue returns false, the value of getNextValae is undefined ou should test your SeqGen class by creating another class called SeqGenMain. Within SeqGenMain there is a main method that creates objects of the SeqGen class and tests all the methods in that class. The SeqGenMain class should prompt the user for a sequence (as described above) and then call the hasNextValue and getNextValue methods to print the sequences. Note that all input and output must occur in the SeqGenMain class. The set of commands that are recognized by this main method should be: max, minmax, maxdiy, and end. If a command accepts 1, 2, or 3 integer operands, then these operands must be accepted on the same input line as the corresponding command. The end command just prints a message that the program is ending and then terminates the main method The completed lab assignment should contain a project with 2 classes. When your lab 4 software is completed and Javadoc comments are added for all classes, methods, parameters, and return values, submit your entire project as a compressed zip file in the Canvas site for the Lab. Here is an example of a user interacting with this program (see other side)Explanation / Answer
ScreenShot
------------------------------------------
Program
//Package for input
import java.util.Scanner;
//Create a class
class SeqGen{
//Member variables
private int start;
private int end;
private int divVal;
//constructor
SeqGen(int s,int e,int div){
start=s;
end=e;
divVal=div;
}
//Checking sequence end
public boolean hasNext(int n) {
if(end>=n) {
return true;
}
else {
return false;
}
}
//Display sequence
public void getNextValue() {
for(int i=start;i<=end;i++) {
if(hasNext(i)) {
if(i%divVal==0) {
System.out.println(i);
}
}
}
}
}
//Main class for test
public class SeqGenMain {
public static void main(String[] args) {
//Scanner object for input read
Scanner sc=new Scanner(System.in);
//Variables for user choice read and split
String ch="";
String[] arr;
//User prompt
System.out.println("Enter a command(max,minmax,maxdiv,end)");
ch=sc.nextLine();
//loop until end
while(!ch.equals("end")) {
//Split command
arr=ch.split(" ");
//according to command
switch(arr[0]) {
//Print from 1 to max
case "max":
SeqGen sg=new SeqGen(1,Integer.parseInt(arr[1]),1) ;
System.out.println("The sequence from 1 to max is:");
sg.getNextValue();
break;
//Print from min to max
case "minmax":
SeqGen sg1=new SeqGen(Integer.parseInt(arr[1]),Integer.parseInt(arr[2]),1) ;
System.out.println("The sequence from min to max is:");
sg1.getNextValue();
break;
//Print from min to max divisible by given
case "maxdiv":
SeqGen sg2=new SeqGen(Integer.parseInt(arr[1]),Integer.parseInt(arr[2]),Integer.parseInt(arr[3])) ;
System.out.println("The sequence from 1 to max mod div is:");
sg2.getNextValue();
break;
}
//Prompt again
System.out.println("Enter a command(max,minmax,maxdiv,end)");
ch=sc.nextLine();
}
//end command execution
System.out.println("Program Terminated Normally.");
sc.close();
System.exit(0);
}
}
------------------------------------
Output
Enter a command(max,minmax,maxdiv,end)
max 5
The sequence from 1 to max is:
1
2
3
4
5
Enter a command(max,minmax,maxdiv,end)
minmax 5 10
The sequence from min to max is:
5
6
7
8
9
10
Enter a command(max,minmax,maxdiv,end)
maxdiv 2 22 4
The sequence from 1 to max mod div is:
4
8
12
16
20
Enter a command(max,minmax,maxdiv,end)
end
Program Terminated Normally.