CECS 174 Hmw Array the size of the we are working with arrays. You have to ask t
ID: 3832465 • Letter: C
Question
CECS 174 Hmw Array the size of the we are working with arrays. You have to ask the user to ent In this array to be declared and then initialize this array randomly menu to select from the following Then use a of the array. Modify the elements of the modifying the elements select this option you are o At any point in the program, array randomly. print the items in the array, one to a line with their index. o This will allow you to see the elements of your array, with their index Compute and print the total of the array items. of all elements of the array o Will calculate and output the summation Compute and print the average value in the array will calculate and output the average of all the elements of the amay Compute and print the smallest value in the array. Find the smallest element in the array Count and display the number of items in the array that are negative o count how many negative numbers are there in the array, output each number with its index Sort the array menu to choose the order of sorting the array This option should take you to another scending or descending order. You should call methods for Displaying the main menu 2. Getting the selection value from the user alidating your entry (for the size and for the menu Randomly generating your number 5. Printing the elements of the array 6. Finding the total of the elements Finding the smallest t in the array 9. Counting the negat 10. Sorting the array Now repeat Part I by using array of strings instead of integers. Keep some of the methods you had previously except of filling an array. We will read the array content from a file saved on our computer, and we will output the values to another file as well as the console. Also no negative strings in this array, so we don't need this method. We will add more method to explore some of the String's method Loop through each word of the array- use enhance for loop and find all words start with what the user enters -Repeat with codsWithExplanation / Answer
Hi,
Please see the class for Integer Array :
ArrayTester.java
import java.util.Arrays;
import java.util.Comparator;
import java.util.Random;
import java.util.Scanner;
public class ArrayTester {
public static void main(String [] args){
Scanner scan = new Scanner(System.in);
int sizeIntArray = 0;
Integer[] integerArray ;
//Reading the size of integer array from User
System.out.println("Please enter the size of Integer array");
sizeIntArray= Integer.valueOf(scan.nextLine());
//Intializing the Integer array with random number between 1 and 20
integerArray = generateElements(sizeIntArray);
print(integerArray);
while(true){
//Displaying Menu
System.out.println();
System.out.print("1. Modify");
System.out.print(" 2. Print");
System.out.print(" 3. Sum");
System.out.print(" 4. Average");
System.out.print(" 5. Smallest");
System.out.print(" 6. Negative Elements");
System.out.print(" 7. Sort");
System.out.print(" 8. Quit ");
String choice =scan.nextLine();
switch (choice) {
case "1": integerArray = generateElements(sizeIntArray);
print(integerArray);
break;
case "2": print(integerArray);
break;
case "3": System.out.println("Sum : "+sum(integerArray));
break;
case "4": System.out.println("Average : "+average(integerArray));
break;
case "5": System.out.println("Smallest : "+smallest(integerArray));
break;
case "6": negativeElements(integerArray);
break;
case "7": String order = "";
System.out.println("1.Ascending 2.Descending ");
order = scan.nextLine();
sort(integerArray,order);
break;
case "8":System.exit(0);
break;
default:
break;
}
}
}
/**
* Method to generate/modify elemnts in IntegerArray
* @param sizeIntArray
* @return
*/
public static Integer[] generateElements(int sizeIntArray ){
Random random = new Random();
Integer [] integerArray = new Integer[sizeIntArray];
int max =20;
int min=-10;
for(int i=0;i<sizeIntArray;i++){
int num = random.nextInt(max - min + 1) + min;
integerArray[i] =num;
}
return integerArray;
}
/**
* to Print the Integer Array
* @param integerArray
*/
public static void print(Integer[] integerArray ){
System.out.println("Elements in Array Are :");
for(int i=0;i<integerArray.length;i++){
System.out.print(integerArray[i]+" ");
}
}
/**
* to get the sum of the Integer Array elements
* @param integerArray
*/
public static int sum(Integer[] integerArray ){
int sum =0;
System.out.println("Elements in Array Are :");
for(int i=0;i<integerArray.length;i++){
sum= sum + integerArray[i];
}
return sum;
}
/**
* to get the average of the Integer Array elements
* @param integerArray
*/
public static Double average(Integer[] integerArray ){
int sum =sum(integerArray);
Double avg = (double) (sum/integerArray.length);
return avg;
}
/**
* to get the smallest of the Integer Array elements
* @param integerArray
*/
public static int smallest(Integer[] integerArray ){
int smallest =integerArray[0];
for(int i=0;i<integerArray.length;i++){
if(integerArray[i] <smallest){
smallest= integerArray[i];
}
}
return smallest;
}
/**
* to get the negativeElements of the Integer Array elements
* @param integerArray
*/
public static void negativeElements(Integer[] integerArray ){
int zero =0;
System.out.println("Negative Elements are :");
for(int i=0;i<integerArray.length;i++){
if(integerArray[i] <zero){
System.out.print (integerArray[i]+" ");
}
}
}
/**
* To sort
* @param order, integerArray
*/
public static void sort(Integer[] integerArray ,String order){
if("1".equalsIgnoreCase(order)){
Arrays.sort(integerArray);
}else if("2".equalsIgnoreCase(order)){
//Comparing in descending order
Arrays.sort(integerArray,new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
if(o1>o2){
return -1;
}
else if(o1<o2){
return +1;
}
else{
return 0;
}
}
});
}
print(integerArray);
}
}
Sample output:
Please enter the size of Integer array
8
Elements in Array Are :
7 -2 20 -3 -9 8 16 12
1. Modify 2. Print 3. Sum 4. Average 5. Smallest 6. Negative Elements 7. Sort 8. Quit
1
Elements in Array Are :
16 -9 9 4 7 14 -5 18
1. Modify 2. Print 3. Sum 4. Average 5. Smallest 6. Negative Elements 7. Sort 8. Quit
2
Elements in Array Are :
16 -9 9 4 7 14 -5 18
1. Modify 2. Print 3. Sum 4. Average 5. Smallest 6. Negative Elements 7. Sort 8. Quit
3
Elements in Array Are :
Sum : 54
1. Modify 2. Print 3. Sum 4. Average 5. Smallest 6. Negative Elements 7. Sort 8. Quit
4
Elements in Array Are :
Average : 6.0
1. Modify 2. Print 3. Sum 4. Average 5. Smallest 6. Negative Elements 7. Sort 8. Quit
5
Smallest : -9
1. Modify 2. Print 3. Sum 4. Average 5. Smallest 6. Negative Elements 7. Sort 8. Quit
6
Negative Elements are :
-9 -5
1. Modify 2. Print 3. Sum 4. Average 5. Smallest 6. Negative Elements 7. Sort 8. Quit
7
1.Ascending 2.Descending
1
Elements in Array Are :
-9 -5 4 7 9 14 16 18
1. Modify 2. Print 3. Sum 4. Average 5. Smallest 6. Negative Elements 7. Sort 8. Quit
7
1.Ascending 2.Descending
2
Elements in Array Are :
18 16 14 9 7 4 -5 -9
1. Modify 2. Print 3. Sum 4. Average 5. Smallest 6. Negative Elements 7. Sort 8. Quit
8
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.