Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a Java program that takes in two int values (x and y) from the user and fr

ID: 3668146 • Letter: W

Question

Write a Java program that takes in two int values (x and y) from the user and from these values creates two arrays: one descending, and the other randomly taken from the previous and yet unique. Do not print a prompt for the user. Just enter the two numbers. • As in the previous program, the first variable, x, will be used to create an integer array of size x, which should be filled starting with the value of x and descending to 1 in the last index (i.e. if x is 10, it should create an array of length 10 with the first index containing 10 and the last index containing 1). • The second variable, y, will be used to create a second integer array of size y, which should be filled with random numbers taken from our first array. This time, however, it should have no repeating values (you can assume the length chosen for the second array will be shorter than the first). • You should use the java.util.Random class, and give it the seed 123L (the ‘L’ is for long integer value) when choosing the index to take from your x array and put it into your y array. • Have a loop print out your second array on one line with spaces in between each number. Input Example: > java Prog1b 25 20 Output Example: (If you use the previous input example, your output should be this exactly— except your ID string, of course) Program 1, Patty Kraft, masc1615 18 25 24 11 5 16 13 15 22 3 10 1 21 9 14 17 8 4 6 20 (Notice that there are no repeating numbers, as is required)

Explanation / Answer

In some cases only it is giving unique values ...

package arrarrandom;
import java.lang.*;
import java.io.*;
import java.util.Scanner;
import java.util.Random;
public class ArrayRandom {
int x,y;
int[] array=new int[100];
int[] array2=new int[100];
public ArrayRandom(int x,int y)throws IOException
{
this.x=x;
this.y=y;
}
public void generateArray1(int first)
{
int i,j;
for(i=first;i>0;i--)
{
array[i]=i;
}
System.out.println("The first array elements");
for(i=first;i>0;i--)
{
System.out.print(" "+array[i]);
}
System.out.println();
}
public void generateArray2(int second)
{
Random r=new Random();
int i,j;
for(i=0;i<y;i++)
{
  
array2[i]=r.nextInt(x);
  
}
System.out.println("Second array elements");
for(i=0;i<y;i++)
{
System.out.print(" "+array2[i]);
}
System.out.println();
}
public static void main(String[] args) throws IOException
{
int first,second;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
Scanner sc= new Scanner(System.in);
System.out.println("Enter the two numbers X and Y");
first=Integer.parseInt(br.readLine());
second=Integer.parseInt(br.readLine());
ArrayRandom ar=new ArrayRandom(first,second);
ar.generateArray1(first);
ar.generateArray2(second);
  
}
  
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote