Description of program: You are to write a program name numbers.java . You are t
ID: 3662152 • Letter: D
Question
Description of program:
You are to write a program name numbers.java. You are to use arrays as your data structure.
Here are some requirements for the program:
1)The program must prompt the user to enter the amount of groups of numbers he/she wants to generate (must be between 1 and 5)
2)A group will be made up of two sets of numbers - a subgroup (set) of 5 numbers and another subgroup (set) of 1 number.
3)The numbers must be randomly generated from the range of 1 - 69 for Sub Group 1 and 1 - 26 for Sub Group 2.
4)There must be no repetition of numbers in the same subgroup.
5)However, if the user wants for example 2 groups, then it is possible for these two groups to differ by only 1 number (so repetition between groups are allowed providing that the 2 groups are not identical).
6)You are to use arrays to store the groups of numbers.
7)The program must also prompt the user asking him/her if he/she wants to generate another set of groups of numbers. If answer is "yes" rerun the program. If "no" terminate the program. If "yes" the random generator must start back from time 0.
8)Sort the numbers in Group 1 in ascending order for each group.
9)Then display all of the groups one group per line on the screen in the order of SubGroup 1 followed by the single number in SubGroup 2.
You are write a program name numbers. Java. You are to use arrays as your data structure. The program must prompt the user to enter the amount of groups of numbers he/she wants to generate (must be between 1 and 5) A group will be made up of two sets of numbers-a subgroup (set) of 5 numbers and another subgroup (set) of 1 number. The numbers must be randomly generated from the range of 1 - 69 for Sub group 1 and 1 - 26 for Sub Group 2. There must be no repetition of numbers in the same subgroup. However, if the user wants for example 2 groups, then it possible for these two groups to differ by only 1 number (so repetition between groups are allowed proving that the 2 groups are not identical. You are to use arrays to store the groups of numbers. The program must also prompt the user asking him/her if he/she wants to generate another set of groups of numbers. if answer is "Yes" rerun the program. If "no" terminate the program. If "yes" the random generator must start back from time 0. Sort the numbers in Group 1 in ascending order for each group. Then display all of the groups one group perline on the screen in the order of Subgroup 1 followed by the single number in SubGroup 2.Explanation / Answer
As per given problem statement with conditions written this below numbers.java code ..
with well written comments and of each functions and main method ..
numbers.java
import java.util.*;
public class numbers
{
public static void main(String []args)
{
// Created array1 integer array of size 5 by 5
int [][] array1=new int [5][5];
// Created array2 integer array of size 5
int[] array2=new int[5];
// Crated temporary variables
int i,cards,j;
String yorn;
char c;
// Reading Group values in between 1 and 5 from console
Scanner in=new Scanner(System.in);
// Logic for generating groups between 1 and 5 using random number generator
do
{
Random number = new Random();
do{
System.out.print("Enter Number of cards must be between 1 and 5:");
cards=in.nextInt();
}while(cards<1||cards>5); // Checking range is between 1 and 5
for(i=0;i<cards;i++)
{
array2[i]=number.nextInt(46) +1;
for(j=0;j<5;j++)
WiningNumbers(array1,i,5,56,number);
// Calling the WiningNumbers function
}
// Displaying the Groups data as per user input value
System.out.println(" Your Tickets are");
for(i=0;i<cards;i++)
{
System.out.print("Ticket "+(i+1)+": ");
for(j=0;j<5;j++)
System.out.print(array1[i][j]+" ");
System.out.println(":: "+array2[i]);
}
System.out.print(" play again (y/n) ");
yorn=in.next();
c=yorn.charAt(0);
}while(c=='Y'||c=='y');
// end of do while loop
}
// This function will compare the duplicate present or not in the generated array
public static boolean NoDuplicates(int a[][],int card,int n)
{
int i;
boolean dup=false;
if(n==0)
return false;
for(i=0;i<=n-1;i++)
if(a[card][n]==a[card][i])
dup=true;
return dup;
}
// This function will show the possible wining numbers in the array
public static void WiningNumbers(int a[][],int card,int n,int max, Random number)
{
int i;
boolean dup=true;
for(i=0;i<n;i++)
{
while(dup)
{
a[card][i]=number.nextInt(max) +1;
dup=NoDuplicates(a,card,i);
}
dup=true;
}
sort(a,card,n);
}
// This function will sort the array
public static void sort(int a[][],int card,int n)
{
int i,j,t;
for(i=0;i<n-1;i++)
for(j=i+1;j<n;j++)
if(a[card][i]>a[card][j])
{
t=a[card][i];
a[card][i]=a[card][j];
a[card][j]=t;
}
return;
}
}
Output: