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
  • Write code in static void main method class, which creates a boolean array called flags, size...

    Write code in static void main method class, which creates a boolean array called flags, size 10, and using a for loop sets each element to alternating values (true at index zero, false at index one Write code in a static void main method class, which creates a float array called sales, size 5, uses an initializer list to instantiate its elements with positive values having two decimal places (example 7.25), and using a for loop reads those values of...

  • 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:

  • I want to use netbeans, can you help with the following? Create a structure called as...

    I want to use netbeans, can you help with the following? Create a structure called as a studentnode that has data and pointer to the next node Create a constructor to initialize the linked list Implement the destructor that should delete the linked list once the program finishes the execution Implement a function to add a new node into the list with value Write a method to print all the values

  • 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...

  • Write a program that will create an array of ten integers, allow the user to enter...

    Write a program that will create an array of ten integers, allow the user to enter those integers, and eventually search through the array based on index position: 1. Declare the array. You cannot assign elements to an array until it has first been created. 2. Next, run a for loop so that the user can enter an integer to save in each index position of the array. Since you this array holds ten elements , the index position starts...

  • Write a java program that has a method called sameArrayBackwards. The method takes an array of...

    Write a java program that has a method called sameArrayBackwards. The method takes an array of integers, and checks if the numbers in the array are the same going forward as going backwards and will return a boolean (true or false) depending on the result. You can assume that there is at least one element in the array. Method Header: public static boolean sameArrayBackwards (int [] arr) You will test your method in the main method of your program by...

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