3) Write a program that populates a java.util.Arraylist variable with 10 random
ID: 3607309 • Letter: 3
Question
3) Write a program that populates a java.util.Arraylist variable with 10 random integers in [1, 20), asks the user to provide a value, and counts how many numbers are equal to the value entered by the user. 4) Write a program that populates a java.util.Arraylist variable with 20 random integers in [1, 100] and prints the location of the smallest (the minimum) integer. Note: Exercises 3 and 4 are similar to Exercises 1 and 2 in Week 8 lab, respectively. On Week 8 you used an array to store the values and this week you will be using a java.util.Arraylist variable.Explanation / Answer
Hi I have written two programs. Please check the code and test it once and let me know any querries.
CountProgram.java
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;
public class CountProgram {
public static int randInt(int min, int max) {
Random rand = new Random();
int randomNum = rand.nextInt((max - min) + 1) + min;
return randomNum;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
ArrayList<Integer> randomList = new ArrayList<Integer>();
for(int i=0;i<10;i++){
randomList.add(randInt(1,20));
}
System.out.println("Enter a Number");
int num = sc.nextInt();
int count = 0;
for(int a:randomList){
if(a==num){
count++;
}
}
System.out.println("Count of numbers equal is:"+count);
}
}
Output:
Enter a Number
5
Count of numbers equal is:1
LocationProgram.java
import java.util.ArrayList;
import java.util.Collections;
import java.util.Random;
import java.util.Scanner;
public class LocationProgram {
public static int randInt(int min, int max) {
Random rand = new Random();
int randomNum = rand.nextInt((max - min) + 1) + min;
return randomNum;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
ArrayList<Integer> randomList = new ArrayList<Integer>();
for(int i=0;i<20;i++){
randomList.add(randInt(1,100));
}
int minIndex = randomList.indexOf(Collections.min(randomList));
System.out.println("Minimum Index is:"+minIndex+" Minimum Value:"+randomList.get(minIndex));
System.out.println("Minimum Index with traversing list is:"+getIndexOfMin(randomList)+" Minimum Value:"+randomList.get(getIndexOfMin(randomList)));
}
public static int getIndexOfMin(ArrayList<Integer> data) {
int min = Integer.MAX_VALUE;
int index = -1;
for (int i = 0; i < data.size(); i++) {
int f = data.get(i);
if (Integer.compare(f, min) < 0) {
min = f;
index = i;
}
}
return index;
}
}
Output:
Minimum Index is:11 Minimum Value:1
Minimum Index with traversing list is:11 Minimum Value:1
For second i have written two types techniques for finding the minimum value.
Please test it and let me know any querries. Thank you. All the best.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.