Question

. In the method main, prompt the user for a non-negative integer and store it in...

. In the method main, prompt the user for a non-negative integer and store it in an int variable num (Data validation is not required for the lab). Create an int array whose size equals num+10. Initialize the array with random integers between 0 and 50 (including 0 and excluding 50). Hint: See Topic 4 Decision Structures and File IO slides page 42 for how to generate a random integer between 0 (inclusive) and bound (exclusive) by using bound in the nextInt method. 2. Define the following methods:

1) printArray accepts a one-dimensional array as its argument and print out the contents of the array.

2) getTotal accepts a one-dimensional array as its argument and returns the total of the values in the array. Try to use an enhanced for loop in the method.

3) getAverage accepts a one-dimensional array as its argument and returns the average of the values in the array as a floating-point value. Hints: Call getTotal method to avoid repetition of code.

4) getHighest accepts a one-dimensional array as its argument and returns the highest value in the array.

5) getLowest accepts a one-dimensional array as its argument and returns the lowest value in the array.

3. Call the printArray method to output the contents of the array.

4. Set the last element of the array to be the value of the eighth element minus twice the fifth element. Output the contents of the array again.

5. Display the total, the average, the highest, and the lowest value of the array by calling the above methods.

Starter code

port java.util.Random;
import java.util.Scanner;


public class Lab5 {

public static void main(String[] args) {

Scanner keyboard = new Scanner(System.in);
System.out.print("Enter a non negative integer: ");
int num = keyboard.nextInt();
int[] alpha = new int[num + 10];
Random rand = new Random();
for (int i = 0; i < alpha.length; i++) {
alpha[i] = rand.nextInt(50);

}
printArray(alpha);
System.out.println("The total is: ");
}

public static void printArray(int[] array) {
//regular for loop
for (int i = 0; i < array.length; i++) {
System.out.print(array[i] + " ");
System.out.println("");

//Enhanced For Loop
for (int n : array) {
System.out.println(n + " ");
}
System.out.println("");

}
  

public static int getTotal(int[] array) {
int total = 0;
for (int i = 0; i < array.length; i++) {
total += array[i];
}
return total;
}
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Lab5.java xx 1 package schedulingtask schedulingtaskderno; 41mport java.util.Randon; 5 import java.util.Scanner: 11 public cl13388 public static int getlotal(int[] array) { int total - 0:1 for (int i - ; i < array.length; i++) { total arrayfil: 140 444 458 public static double getAverage(int[] array) double sun-2.0; double total-get Total (array)*1.0; surtotal/(array.lengtConsole X terminated > Lab5 [lava Application C:\Program Files lava je 180 221\bin java.exe (23-Feb-2020, 10:38:18 AM) Enterhi

I have completed the function with corresponding names. So you can get them easily.

printArray() has two loops, You can use any one of them (use both if it is required to do so otherwise one loop is enough). Ihave not changed your existing code.

If you get any problem you can reach us again.

Lab5.java xx 1 package schedulingtask schedulingtaskderno; 41mport java.util.Randon; 5 import java.util.Scanner: 11 public class Labs public static void main(Strinell args) { Scanner keyboard - new Scanner(System.in); System.out.print("Enter a non negative integer: "); int num - keyboard.nextInt (); int[] alpha - new int[num + 10); Randon rand new Random(): for (int i - A; i < alpha.length; i++) { alpha[i] - rand.nextInt(50); 13 15 16 19 be 21 System.out.println("The total is: "+getlotal(alpha)); System.out.println("The averaer is: "+getAverage (alpha)); System.out.println("The highest is: "-gettighest(alpha)); System.out.println("The lowest is: "+getLowest (alpha)); setlast Element(alpha); public static void printArray(int[] array) { //regular for loop for (int i - ; i < array.length; i++) { Systen.out.print(array[i] + " "); System.out.println(""); 26 28 //Enhanced For Loop for (int n; array) { System.out.println(n + " "); 34 System.out.println(""); 29 public static int ErtTotal(int[] array) {

13388 public static int getlotal(int[] array) { int total - 0:1 for (int i - ; i < array.length; i++) { total arrayfil: 140 41 43 return total; 44 public static double getAveraer(int[] array) 46 AR double sun .e: double total-getTotal(array)*1.; Sur-total/(array.length); return sun; 49 se 51 52 public static int setllighest(int[] array) int high-6; for(int i:array) 56 57 if(i>high) high-i; return high; public static int getLowest(int[] array) 65 int low-108: for(int i:array) 67 if(i<low) lowsi; return low; 738 public static void set LastElement(int[] array)

44 458 public static double getAverage(int[] array) double sun-2.0; double total-get Total (array)*1.0; surtotal/(array.length); return sun; 49 public static int getHighest(int[] array) int high-8; for(int i array) if(i>high) high-i; return high; 61 public static int getLowest(int[] array) 63 int lowe100: for(int i:array) 1F(iclow) low ; return low; 730 public static void setLastElement(int[] array) 75 array(array.length-1)=array(7)-2* array[4]; printArray(array); 77 } 80 }

Console X terminated > Lab5 [lava Application C:\Program Files lava je 180 221\bin java.exe (23-Feb-2020, 10:38:18 AM) Enter a nuri negalive inleger: 2 The total is: 237 The average is: 19.75 The highesLis: 47 The lowest is: 4 Contents of the array in regular loop: Cunlerils of the array in enhanched loop:

Plain text:---


import java.util.Random;
import java.util.Scanner;

public class Lab5 {
   public static void main(String[] args) {
   Scanner keyboard = new Scanner(System.in);
System.out.print("Enter a non negative integer: ");
int num = keyboard.nextInt();
int[] alpha = new int[num + 10] ;
Random rand = new Random();
for (int i = 0; i < alpha.length; i++) {
alpha[i] = rand.nextInt(50);

}
  
System.out.println("The total is: "+getTotal(alpha));
System.out.println("The average is: "+getAverage(alpha));
System.out.println("The highest is: "+getHighest(alpha));
System.out.println("The lowest is: "+getLowest(alpha));
setLastElement(alpha);
   }
   public static void printArray(int[] array) {
//regular for loop
       System.out.println("Contents of the array in regular loop:");
   for (int i = 0; i < array.length; i++) {
System.out.print(array[i] + " ");
System.out.println("");
}
//Enhanced For Loop
System.out.println("Contents of the array in enhanched loop:");
for (int n : array) {
System.out.println(n + " ");
}
System.out.println("");

}
public static int getTotal(int[] array) {
int total = 0;
for (int i = 0; i < array.length; i++) {
total += array[i];
}
return total;
}
public static double getAverage(int[] array)
{
double sum=0.0;
double total=getTotal(array)*1.0;
sum=total/(array.length);
return sum;
}
public static int getHighest(int[] array)
{
int high=0;
for(int i:array)
{
if(i>high)
high=i;
}
return high;
}
public static int getLowest(int[] array)
{
int low=100;
for(int i:array)
{
if(i<low)
low=i;
}
return low;
}
  
public static void setLastElement(int[] array)
{
array[array.length-1]=array[7]-2*array[4];
printArray(array);
}


}

output--->

Enter a non negative integer: 2
The total is: 274
The average is: 22.833333333333332
The highest is: 43
The lowest is: 0
Contents of the array in regular loop:
8
41
4
39
0
15
28
17
7
43
43
17
Contents of the array in enhanched loop:
8
41
4
39
0
15
28
17
7
43
43
17

Add a comment
Know the answer?
Add Answer to:
. In the method main, prompt the user for a non-negative integer and store it in...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • For the following C# program: Let’s add one more user defined method to the program. This...

    For the following C# program: Let’s add one more user defined method to the program. This method, called ReverseDump is to output the values in the array in revere order (please note that you are NOT to use the Array.Reverse method from the Array class). The approach is quite simple: your ReverseDump method will look very similar to the Dump method with the exception that the for loop will count backwards from num 1 to 0 . The method header...

  • 1.Write code for a Java method, printArray, that takes an int array as parameter and prints...

    1.Write code for a Java method, printArray, that takes an int array as parameter and prints it out, one element per line. public static void printArray (int[] values){ Methods that do NOT return a result have “void” as return type. Notice how an array parameter type is passed, int [] }// end printArray 2.Write code for a Java method that takes in as input parameter an integer number, say num, and returns a double array of size num with all...

  • I need to make this code access the main method I suppose, but I don't know...

    I need to make this code access the main method I suppose, but I don't know how to make that happen... Help please! QUESTION: Write a method called switchEmUp that accepts two integer arrays as parameters and switches the contents of the arrays. Make sure that you have code which considers if the two arrays are different sizes. Instructor comment: This will work since you never go back to the main method, but if you did, it wouldn't print out...

  • COMPLETE THE BUCKETSORT METHOD public static void bucketSort(int[] array) {        int bucketCount = array.length/2;...

    COMPLETE THE BUCKETSORT METHOD public static void bucketSort(int[] array) {        int bucketCount = array.length/2;        int minIntValue = 0;        int maxIntValue = array.length - 1;        // Create bucket array        List<Integer>[] buckets = new List[bucketCount];        // Associate a list with each index in the bucket array           for(int i = 0; i < bucketCount; i++){            buckets[i] = new LinkedList<>();        }        //...

  • 1. What is the output when you run printIn()? public static void main(String[] args) { if...

    1. What is the output when you run printIn()? public static void main(String[] args) { if (true) { int num = 1; if (num > 0) { num++; } } int num = 1; addOne(num); num = num - 1 System.out.println(num); } public void addOne(int num) { num = num + 1; } 2. When creating an array for primitive data types, the default values are: a. Numeric type b. Char type c. Boolean type d. String type e. Float...

  • PrintArray vi Create a class called PrintArray. This is the class that contains the main method....

    PrintArray vi Create a class called PrintArray. This is the class that contains the main method. Your program must print each of the elements of the array of ints called aa on a separate line (see examples). The method getArray (included in the starter code) reads integers from input and returns them in an array of ints. Use the following starter code: //for this program Arrays.toString(array) is forbidden import java.util.Scanner; public class PrintArray { static Scanner in = new Scanner(System.in);...

  • Draw a flowchart for this program public class InsertionSort {     public static void main(String a[]) {...

    Draw a flowchart for this program public class InsertionSort {     public static void main(String a[]) {         int[] array = {7, 1, 3, 2, 42, 76, 9};         insertionSort(array);     }     public static int[] insertionSort(int[] input) {         int temp;         for (int i = 1; i < input.length; i++) {             for (int j = i; j > 0; j--) {                 if (input[j] < input[j - 1]) {                     temp = input[j];                     input[j] = input[j - 1];                     input[j - 1] = temp;                 }             }             display(input, i);...

  • Java Array The method rotateLeft() takes in a reference a to an integer array and a...

    Java Array The method rotateLeft() takes in a reference a to an integer array and a positive integer n and it returns a new array whose contents is the contents of the input array rotated to the left n places. So each element a[i] of the input array should be placed at location b[i-n] of the returned array. If the index i-n is negative, then that index should "wrap" around to the end of the output array. For example, if...

  • Java 1. Write a getCount method in the IntArrayWorker class that returns the count of the...

    Java 1. Write a getCount method in the IntArrayWorker class that returns the count of the number of times a passed integer value is found in the matrix. There is already a method to test this in IntArrayWorkerTester. Just uncomment the method testGetCount() and the call to it in the main method of IntArrayWorkerTester. 2. Write a getLargest method in the IntArrayWorker class that returns the largest value in the matrix. There is already a method to test this in...

  • Please help me with this Java project. I'm trying to create a loop so if user...

    Please help me with this Java project. I'm trying to create a loop so if user enter value > 12, will prompt them a message and take them back to "How many elements do you wan to enter?". Thanks public static void main(String[] args) {        Scanner sc = new Scanner(System.in);           int i, j;       System.out.print("\n How meny elements do you want to enter? (N should be no more than 12) ");    int num...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT