Question
Part1: Passing Arrays To A Method; Processing All Elements Of An Array Create method that will take an array of integers as a parameter, named
Part1: Passing Arrays To A Method; Processing All Elements Of An Array
Create method that will take an array of integers as a parameter, named PrintArray.This method should simply print out every element of the array, 1 number per line.In main, create two integer arrays.The first should be 3 elements long, and hold the values {1, 3, 5}.The second should be 5 elements long, and hold the numbers {1, 2, 3, 5, 7}.
Use the provided ICE_18_PrintArray.java as a starting point, and fill in the missing parts (in the PrintArray method, and in main, where is asks you to print the second array).
import java.util.*;
class ArrayHelper extends Object
{
public void PrintArray( jQuery22403404550220213025_1560260066613?? )
{
// Your code goes here - use
// the "process all elements" pattern, where the work
// to be done is printing each element
}
public void getIntput()
{
}
}
public class ICE_18_PrintArray extends Object
{
public static void main(String[] args)
{
ArrayHelper ah = new ArrayHelper();
// Now set up the array stuff, which is more interesting:
int [] shortArray = new int[4];
int i;
for(i = 0; i < shortArray.length; i++)
{
shortArray[i] = (i + 1) * 2;
}
int [] longArray = new int[14];
for(i = 0; i < longArray.length; i++)
{
longArray[i] = (i + 1) * 3;
}
// print out both arrays
ah.PrintArray(shortArray);
ah.PrintArray(longArray);
}
}
Part2: Finding a particular value in an array
Building on what you learned from the previous part, use the provided ICE_18_FindInArray.java as a starting point and add the following to the end of the main method: your program should, after setting up the arrays, ask the user for a number to look for in the array.Then it should go through the array, and if it finds that value in the array, it should print out a message telling the user what the index (slot number) is where that value is stored.
It is recommended that you start by creating this functionality inside main and getting everything to work.Once that's working you should use the separate method (called FindInArray) which will be given both the array and the value to look for, and will print out the message if the value is found in the array.
import java.util.Scanner;
class ArrayHelper extends Object
{
// Copy this from the previous in class exercise
public void PrintArray()
{
}
// This is the code you need to fill in
public void FindInArray() // DON'T FORGET TO FILL IN THE STUFF IN THE PARENS!
{
}
}
public class ICE_18_FindInArray extends Object
{
public static void main(String[] args)
{
ArrayHelper ah = new ArrayHelper();
// Now set up the array stuff, which is more interesting:
int [] shortArray = new int[4];
int i;
for(i = 0; i < shortArray.length; i++)
{
shortArray[i] = (i + 1) * 2;
}
int [] longArray = new int[14];
// We want to put numbers into the long array in reverse order
// we should thus count DOWN through the slot numbers,
// and simultaneously have counter count UP
int counter = 0;
for(i = longArray.length-1; i >= 0; i--)
{
longArray[i] = (counter + 1) * 3;
counter++;
}
System.out.println("Short Array: " );
//ah.PrintArray(shortArray); // FIX THIS
System.out.println("Long Array:" );
//ah.PrintArray(longArray); // FIX THIS
// Start by writing out the 'find' logic here, in main
// (You can pick any value to search for - make sure that your
// logic does the right thing for both values that ARE
// present in the array and those that ARE NOT present.
// Once you've got the logic working make sure that you can get these commands to work:
//ah.FindInArray(shortArray, 2); // SHOULD TELL US THAT '2' WAS FOUND AT SLOT 0
//ah.FindInArray(longArray, 10); // SHOULD NOT PRINT ANYTHING
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started