Java Language only please 1-Scenario: Write a Java program that fills an array o
ID: 3766909 • Letter: J
Question
Java Language only please
1-Scenario:
Write a Java program that fills an array of size 50 with random numbers between 0 and 100.
Then, find the minimum number in the array and its index.
2-
Write a program that takes input of names from user. Print total names number and all names.
NOTE: Solve this problem using ArrayList and Enhanced For Loop.
Typical run of the program:
Enter a name: (Q to exit)
Ali
Enter a name: (Q to exit)
Sarah
Enter a name: (Q to exit)
David
Enter a name: (Q to exit)
Ahmad
Enter a name: (Q to exit)
Jhon
Enter a name: (Q to exit)
q
You entered 5 names which are:
Ali | Sarah | David | Ahmad | Jhon
Explanation / Answer
Scenario1.java
import java.util.Arrays;
import java.util.Random;
/**
* @author Srinivas Palli this class fills an array of size 50 with random numbers
* between 0 and 100. Then, find the minimum number in the array and its
* index
*/
public class Scenario1 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int randomArray[] = new int[50];
Random random = new Random();
int Low = 0;
int High = 100;
for (int i = 0; i < 50; i++) {
randomArray[i] = random.nextInt(High - Low) + Low;
}
System.out.println(Arrays.toString(randomArray));
int minIndex = 0, minNumber = randomArray[0];
for (int i = 1; i < 50; i++) {
if (randomArray[i] < minNumber) {
minNumber = randomArray[i];
minIndex = i;
}
}
System.out.println("Minimum Number :" + minNumber
+ " Minimum Number Index :" + minIndex);
}
}
OUTPUT:
[28, 63, 20, 30, 59, 69, 88, 40, 17, 64, 69, 59, 84, 40, 1, 7, 21, 43, 47, 3, 30, 59, 24, 6,
46, 30, 94, 33, 8, 56, 86, 22, 89, 89, 6, 3, 32, 73, 72, 86, 89, 82, 59, 97, 43, 3, 4, 30,
78, 34]
Minimum Number :1 Minimum Number Index :14
Scenario2.java
import java.util.ArrayList;
import java.util.Scanner;
/**
* @author Srinivas Palli
*
* This class takes input of names from user. Print total names number and all names
*/
public class Scenario2 {
public static void main(String[] args) {
Scanner scanner;
try {
ArrayList<String> arrayList = new ArrayList<String>();
scanner = new Scanner(System.in);
do {
System.out.println("Enter a name: (Q to exit)");
String name = scanner.nextLine();
if (name.equalsIgnoreCase("Q")) {
break;
} else {
arrayList.add(name);
}
} while (true);
System.out.println("You entered " + arrayList.size()
+ " names which are:");
for (String name : arrayList) {
System.out.print(name + " | ");
}
} catch (Exception e) {
// TODO: handle exception
}
}
}
OUTPUT:
Enter a name: (Q to exit)
srinivas
Enter a name: (Q to exit)
pavan
Enter a name: (Q to exit)
rajesh
Enter a name: (Q to exit)
kishore
Enter a name: (Q to exit)
q
You entered 4 names which are:
srinivas | pavan | rajesh | kishore
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.