Hey , this is for java could someone help please, also please use only what is i
ID: 3914897 • Letter: H
Question
Hey , this is for java could someone help please, also please use only what is in package do not add extra things. The first pic is the description of the assignment, the bottom pics are the actually program. It just needs you to write the code accordingly. Thank you very much! ay to try out CrazyArray.java. This program uses indirect referencing of at is, it uses an array value an array index. You'll want to draw yourself a picture of the y a in this one, and trace through this program very slowly. Use arrows to belp yourself Compile and execute CrazyArray.java and check your answers found during the trace. Completing Methods with Array Parameters 1. E dit file MyArray.java. Notice how the main method is already complete. You will want to complete the bodies of four methods involving arrays. 2. First off, let's write a void method named £i11Up which can be used to fill up an array of six items with integers that are input from the keyboard. Notice how this function takes an array a as a parameter. You just need to add a for loop here that iterates six times and each time inputs an integer using the nextlnt0 method, and stores it in each slot of the array 3. Our next method call will be to a void method named printout which is used to print out all of the contents of our array a on the same line. It should be set up very similar to method Fi1lup. Include an output prompt as shown on the back. 4. Your third method will be a value returning method named sumUp which takes as input our array a and returns to the calling function the sum of the items in this array. Once again, you will need to use a for loop. Don't forget to flush your local sum variable to zero prior to this loop. Also, since this method is "value-returning" we need to include a return statement at the end of it that returns the integer value representing the sum of our array. For example, return suma 5. Your fourth and final method is a value-returning method named posCount which takes as input our array a and returns to the calling function the number of integers in the array which are positive. You will need to declare a local count variable and remember to set it to zero prior to your loop. Within your loop, use a single-alternative i f statement to check and see if an array item is positive. Once again, you'll also need a return statement. 6. Compile and test your program using the sample input on the back as well as some sample input of your own.Explanation / Answer
Below is the solution:
MyArray.java:
package chegg;
import java.util.*;
public class MyArray {
final static int NUM_ITEMS = 6; //
public static void main(String[] args)
{
int[] a = new int[NUM_ITEMS];
fillUp(a); //fill the array
printOut(a); //print the array
System.out.println(" Array Sum : " + sumUp(a)); //print the sum of the array
System.out.println("Num of Positive Values : " + posCount(a)); //count the input array element positive
}
//fillUp method that will fill up the array
private static void fillUp (int[] a)
{
Scanner input= new Scanner(System.in); //scanner for input
//Array has been initialized with integers
for (int i = 0; i < a.length; i++)
{
String userInput =input.next(); //take input from the user
a[i] = Integer.parseInt(userInput); //convert the input string into integer
}
}
//printOut method that will print the array
private static void printOut (int[] a)
{
System.out.println(" PrintOut of Array ");
for (int i = 0; i < NUM_ITEMS; i++)
System.out.print(a[i] + " ");
System.out.println();
System.out.println("------------------------");
}
//sumUp method that will sum of the array
private static int sumUp (int[] a)
{
int sum = 0; //initialize sum with zero
for (int i = 0; i < NUM_ITEMS; i++) //loop to the num_items
sum += a[i]; //add the array one by one and store in sum variable
return sum; //return sum
}
//posCount method that will count the positive value in the array
private static int posCount (int[] a)
{
int count = 0; //variable to count the positive number
for (int i = 0; i < NUM_ITEMS; i++) //loop to the num_items
if (a[i] > 0) //check if the array element is greater than 0
count += 1;
return count; //return count
}
}
sample output:
7
-9
30
2
7
76
PrintOut of Array
7 -9 30 2 7 76
------------------------
Array Sum : 113
Num of Positive Values : 5