Create a program called NumberArray.java OUTPUT: What size array would you like?
ID: 3725630 • Letter: C
Question
Create a program called NumberArray.java
OUTPUT:
What size array would you like?: 3
Printing Single Diminsion Array Values
Array Value [0] = 200
Array Value [1] = 300
Array Value [2] = 400
Sum is 900
Average is 300
Printing Single Diminsion Array Values
Array Value [0] = 4
Array Value [1] = 17
Array Value [2] = 22
Array Value [3] = 8
Array Value [4] = 35
Sum is 86
Average is 17
************************************
// Program NumberArray.java
package numberarray;
import java.util.Scanner;
public class NumberArray
{
//declare a single diminsion array to store integers called wholeNumbers
//create an overloaded constructors with the parameter int size
{
this.wholeNumbers = /*initialize with an array of integers based on the size parameter */
int initzValue = 100;
for (int x=0; x < this.wholeNumbers.length; x++)
this.wholeNumbers[x] = initzValue+=100;
}
//create an overloaded constructor with the parameter int [] wholeNumbers
public NumberArray( int [] wholeNumbers) { this.wholeNumbers =/* initialize with the passed in parameter */ }
//create a setter for the attribute wholeNumbers
//create a getter for the attribute wholeNumbers
//created an overloaded method called showNumbers without parameters and a void return type
{
int total = 0;
System.out.println("Printing Single Diminsion Array Values");
for(int x = 0; x < /*create a for loop based on the length of the array wholeNumbers */ ; ++x){
System.out.println("Array Value [" + x + "] = " + wholeNumbers[x]);
total += wholeNumbers[x];
}
System.out.println("Sum is " + total);
System.out.println("Average is " + (total / wholeNumbers.length) );
}
//created an overloaded method called showNumbers with a parameter of int [] arrayOfNumbers and a void return type
{
int tot = 0;
System.out.println("Printing Single Diminsion Array Values");
for(int x = 0; x < arrayOfNumbers.length; ++x){
System.out.println("Array Value [" + x + "] = " + /*display the array element based on x */ );
tot += /*assign the array element based on x */
}
System.out.println("Sum is " + tot);
System.out.println("Average is " + (tot / arrayOfNumbers.length) );
}
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
System.out.print(" What size array would you like?: ");
NumberArray inputNumbers = new NumberArray(input.nextInt());
inputNumbers.showNumbers(inputNumbers.getWholeNumbers());
// create an int array called someNums and initialize it to 4, 17, 22, 8 and 35
NumberArray defaultNumbers = /*use the new operator and call the overloaded constructor NumberArray, passing the array someNums */
defaultNumbers.showNumbers();
}
}
Explanation / Answer
import java.util.Scanner; public class NumberArray { //declare a single diminsion array to store integers called wholeNumbers int[] wholeNumbers; //create an overloaded constructors with the parameter int size NumberArray(int size) { this.wholeNumbers = new int[size]; /*initialize with an array of integers based on the size parameter */ int initzValue = 100; for (int x=0; xRelated Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.