Question: Introduction: Sometimes computer programs need to lump data together! The purpose of this program is to help you gain familiarity of using arrays and loops.
Introduction: Sometimes computer programs need to lump data together! The purpose of this program is to help you gain familiarity of using arrays and loops.
Objectives: Create a NumberList class that will do the following:
Modify a version of NumberList Program that I have given below
Write a program that prompts the user for the size of the array to be manipulated.
Load this array with random integers between -9 and 9 inclusive.
Write a method called evenArray that will take your random array as a parameter and print out the even numbers of that array. The original array is not changed. Consider using a call to the printArray method inside of this method to promote code reuse.
Write a method called oddArray that will take your random array as a parameter and print out the odd numbers of that array. The original array is not changed.
Write a method called negativeArray that will take your random array as a parameter and print out the negative numbers of the array. The original array is not changed.
Write a method called findSum that will take your random array as a parameter and print out the sum of the elements of the array. The original array is not changed.
Write a method called reverseArray that will take your random array as a parameter and print out the elements in reverse order (the first becomes last, the second becomes second to last, etc). The original array is not changed.
Write a method called findMin that will take your random array as a parameter and print out the minimum element of the array. The original array is not changed.
Write a method called findMax that will take your random array as a parameter and print out the maximum element of the array. The original array is not changed.
Write a method called arrayAverage that will take your random array as a parameter and display the average of the numbers in the original array. The original array is not changed.
Extra credit:
HINT: Some of these are very challenging.
Read the description and write out a list of numbers and try to figure out how you would solve this problem, what do you need to keep track of, and when/why would you might use parallel arrays
(+1/2) Write a method called arrayMedian that that will take your random array as a parameter and display the median of the numbers in the original array. You will have to order the elements first. The original array is not changed.
(+1/2) Write a method called arrayMode that will take your random array as a parameter and display the mode of the numbers in the original array. If there are multiple modes, display them all. The original array is not changed.
Note:
- Read all the comments in the shell carefully! They include hints.
- The main method has the tests in it, you just uncomment each method in the main method when you have created the actual methods.
- I have a programming shell completed, you just write in the missing pieces.
- See below what the program should output to the terminal window when run.
Terminal Window Output:
- When run, the program will look like this (user input in bold italics):
Trial 1:
Enter positive integer for size of array to build: 14
The array is loaded with the following random numbers:
2 0 -5 -2 -4 -6 -9 9 3 5 -2 3 9 9
The even numbers of the first array are:
2 0 -2 -4 -6 -2
The odd numbers of the first array are:
-5 -9 9 3 5 3 9 9
The negative numbers of the first array are:
-5 -2 -4 -6 -9 -2
The sum of the elements of the array is:
12
The array with elements reversed is:
9 9 3 -2 5 3 9 -9 -6 -4 -2 -5 0 2
The minimum element of the array is:
-9
The maximum element of the array is:
9
The average of the array is:
0.85714286
Trial 2:
Enter positive integer for size of array to build: 7
The array is loaded with the following random numbers:
-5 -7 -2 -2 4 -4 -6
The even numbers of the first array are:
-2 -2 4 -4 -6
The odd numbers of the first array are:
-5 -7
The negative numbers of the first array are:
-5 -7 -2 -2 -4 -6
The sum of the elements of the array is:
-22
The array with elements reversed is:
-6 -4 4 -2 -2 -7 -5
The minimum element of the array is:
-7
The maximum element of the array is:
4
The average of the array is:
-2.85714
Rubric:
- 2 points: Does it compile successfully?
- 2 points: Does it have good naming conventions (descriptive variable and method names)?
- 2 points: Is it readable (white space and indentations)?
- 2 points: Does it include comments to help aid? Correct heading with author and date?
- 2 points: Was it on time
- 10 points: Does it do what it is supposed to?
- 20 points: Total
| NumberList Student Shell |
| /** * YOUR NAME HERE * DATE * APCS period # * NumberList */
//This program creates and manipulates an array of //randomly generated numbers in various ways
import java.util.Scanner;
public class NumberList { public static void main(String[] args) { //creates a call to the getSizeOfArray() method, which is correctly implemented in a method down below int arraySize = getSizeOfArray(); //You must declare and instantiate an array called myArray //using the arraySize variable
//You will create methods with the names below
//loadArray(); //This method call needs myArray created above as a parameter
System.out.print(" The array is loaded with the following random numbers: "); //printArray(); //This method call needs myArray created above as a parameter
System.out.print(" The even numbers in the array are: "); //evenArray(myArray); //Displays the elements of myArray that contain even numbers
System.out.print(" The odd numbers in the array are: "); //oddArray(myArray); //Displays the elements of myArray that contain odd numbers
System.out.print(" The negative numbers in the array are: "); //negativeArray(myArray); //Displays the elements of myArray that contain negative numbers
//other methods go here
}
//Loads an array passed in as a parameter following the //guidelines in the assignment objectives //Finish the header for the method (it is not correct) //Write the code following the guidelines in the assignment objectives public static void loadArray() //method header not correct { //Load your array with random numbers
}
//Displays on one line the elements of the array to the terminal window //Then moves cursor to next line public static void printArray(int[] a) { for(int i = 0; i < a.length; i++) { System.out.print(a[i] + " "); } }
//Gets the size of the array from the user. //I suggest testing between 15-30, so that the entire array is //seen when printed to the terminal window. public static int getSizeOfArray() { Scanner reader = new Scanner(System.in); while (true) { System.out.print("Enter positive integer for size of array to build: "); int size = reader.nextInt(); if (size > 0) return size; } }
//Create a method below that loops through the array //being passed in as a parameter and prints on one line //all the elements that contain *even* numbers //Finish the method header below and write code
public static //finish method header { }
//Create a method below that loops through the array //being passed in as a parameter and prints on one line //all the elements that contain *odd* numbers //Finish the method header below and write code
public static //finish method header { // write code here }
//Create a method below that loops through the array //being passed in as a parameter and prints on one line //all the elements that contain *negative* numbers //Finish the method header below and write code
public static //finish method header { // write code here }
//Extras can go below //Here is what you might use for reverseList example
public static void reverseArray(int[] a) { // write code here }
} |
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
