Question

Step 1. Start Netbeans and create a project called ArrayLab Step 2. Write the main method...

Step 1. Start Netbeans and create a project called ArrayLab

Step 2. Write the main method

 Declare and initialize an array called myNums int myNums = {3, 8, 12, 4, 2, 9, 6};  Declare an int called largestNum and set it to 0.

 Write a for loop that prints the array for (int index = 0; index < myNums.length; index++) { System.out.println(myNums[index] + “ “); }

 Write a for loop that prints the array backwards  Write a for loop that sums the array. Create a variable called total and set it to 0. Step through the array and add each element to total. Print the total when the loop finishes.

 Now write a second for loop that will step through the array again. This time, instead of printing each element, it will check to see if each element is greater than largestNum. If it is, then assign that element to largestNum largestNum = myNums[index];

 Write a for loop that finds the smallest number  Write a for loop that finds the average of the numbers.

 Write a method that searches the array for a number, returns true if found, false otherwise. Call that method, searching for 9. The call the method again, searching for 7. After each call, print if found or not.  Challenge: If you want an extra challenge, write code right after you declare largestNum that asks for the size of the array, initializes the array to that size and then uses a for loop to get the elements of the array from the user.  Save your program.  Compile and execute your program. Show the teacher the results.

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

public class ArraysTest {
   public static void main(String[] args) {
       int myNums[] = { 3, 8, 12, 4, 2, 9, 6 };
       int largestNum;

       for (int index = 0; index < myNums.length; index++) {
           System.out.println(myNums[index] + " ");
       }
       largestNum = -1;
       for (int index = 0; index < myNums.length; index++) {
           if (largestNum < myNums[index])
               largestNum = myNums[index];
       }
       int smallestNum = myNums[0];
       for (int index = 1; index < myNums.length; index++) {
           if (smallestNum > myNums[index])
               smallestNum = myNums[index];
       }
       double avg = 0;
       for (int index = 0; index < myNums.length; index++) {
           avg += myNums[index];
       }
       avg = avg / myNums.length;
       System.out.println("Largest: " + largestNum);
       System.out.println("Smallest: " + smallestNum);
       System.out.println("Average: " + avg);
       if (search(9, myNums))
           System.out.println("9 Found in the array");
       else
           System.out.println("9 Not found in the array");
       if (search(7, myNums))
           System.out.println("7 Found in the array");
       else
           System.out.println("7 Not found in the array");

   }

   public static boolean search(int ele, int arr[]) {
       for (int i = 0; i < arr.length; i++)
           if (arr[i] == ele)
               return true;
       return false;
   }
}

Add a comment
Know the answer?
Add Answer to:
Step 1. Start Netbeans and create a project called ArrayLab Step 2. Write the main method...
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
  • C++ Program Int Main First Please Write one program that does the following: 1.       1.   Ask the...

    C++ Program Int Main First Please Write one program that does the following: 1.       1.   Ask the user for ten (10) grades, and store the data in an array.  Compute the average of all the grades.  Print the original ten grades and the average. a.       Declare an integer array with the name of “grades” of size 10 in the main function. b.      Create a function called “getGrades” that prompts the User for the grades and puts them in an integer array.                                                                i.      The function will receive...

  • This is JAVA language Create a .java class called MoreArrayFun.java with only a main method. This...

    This is JAVA language Create a .java class called MoreArrayFun.java with only a main method. This program will use the ArrayManager class. The final version of the program is described below, but initially use it to test your ArrayManager class as you write it. Complete the ArrayManager class as specified below: The class will have exactly two instance variables:  An array of integers  A count of the number of elements currently in the array The class will have...

  • Write a Java program that has a method called arrayAverage that accepts an arrary of numbers...

    Write a Java program that has a method called arrayAverage that accepts an arrary of numbers as an argument and returns the average of the numbers in that array. Create an array to test the code with and call the method from main to print the average to the screen. The array size will be from a user's input (use Scanner). - array size = int - avergage, temperature = double - only method is arrayAverage Output:

  • Array lists are objects that, like arrays, provide you the ability to store items sequentially and...

    Array lists are objects that, like arrays, provide you the ability to store items sequentially and recall items by index. Working with array lists involves invoking ArrayList methods, so we will need to develop some basic skills. Let's start with the code below: The main method imports java.utii.ArrayList and creates an ArrayList that can hold strings by using the new command along with the ArrayList default constructor. It also prints out the ArrayList and, when it does, we see that...

  • Part 1- Method practice Create a main method. In there, make an array of 100 random...

    Part 1- Method practice Create a main method. In there, make an array of 100 random integers(numbers between 0 and 1000). Create a method to find average of the array. Pass the array and return the number(double) that is the average In the main method, call the method created in Step 2. Print out the average Create a method to find the min value of the array. Pass the array and return the int that is the smallest.(pass the value...

  • Write a java program: Create a method fillRandom() that accepts an array of int as input...

    Write a java program: Create a method fillRandom() that accepts an array of int as input and populates it with random numbers in the range -999 to 1000 Explicitly store zero in index [0] and 900 in index [1]. (0 and 900 will be used as search keys) Create a method DisplayLastInts() that accepts an array of int as input and displays the last hundred elements to the screen in rows of 10 elements. Format the output so the 10...

  • Document2 Tell me ign Layout References Mailings Review View 1. Write a program called Lab18C that...

    Document2 Tell me ign Layout References Mailings Review View 1. Write a program called Lab18C that searches for a number in an array. T a. Write a bool function named search that receives 3 parameters: an int array, an int containing the length of the array, and an int number to search for. Return true if the number is in the array and false otherwise. b. Write a void function called fillArray with the array as a parameter that uses...

  • C# Code please with picture of code.Thanks! 1. Create a Main method, and create a 2D...

    C# Code please with picture of code.Thanks! 1. Create a Main method, and create a 2D integer array called data, with a size of five (5) by five (5). Ask the user to input the values for this array (Scanner’s nextInt) 2.Create a method called LongestPositiveSeries, that takes in a 2D array and finds the length of the longest continuous series of positive numbers in it. For example, if we had an array like this: 0 1 2 3 4...

  • You will create a program with two methods, the main() method of course and another method...

    You will create a program with two methods, the main() method of course and another method called printArray(). The main method will define an array of 5 elements. A loop will be used to prompt the user to enter 5 numeric values which will go into the 5 array elements. The main() method will then call the printArray() method passing the array by reference, and the array length by value. The printArray() method will then print the contents of the...

  • The purpose of this assignment is to familiarize you with sort algorithms. Problem Description is as follows: 8. Search Benchmarks Write a program that has at least 20 250 integers stored in an ar...

    The purpose of this assignment is to familiarize you with sort algorithms. Problem Description is as follows: 8. Search Benchmarks Write a program that has at least 20 250 integers stored in an array in ascending order. It should call a function that uses the linear search algorithm to locate one of the values. The function should keep a count of the number of comparisons it makes until it finds the value. The program then should call a function that...

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