Question

The loop below stores the values 1 to 5 in myList: for (int i = 0;...

The loop below stores the values 1 to 5 in myList:

for (int i = 0; i < 5; i ++) {

myList [ i ] = i + 1; //store values using a loop

}

The loop below prints the values from first to last in myList:

for (int i = 0; i < myList.length; i++) {

System.out.print(myList [ i ] + " ");

}

Use the examples on the slides to complete the lab below. Please submit the Java program (XXXX_Lab71) and a screen print of the Eclipse editor showing your code and output with answers:

The class will do the following:

1) Consist of five methods:

a. main

b. public int [] fillArray()

c. public void printArray(int [] list)

d. public int findAverage(int [] list)

e. public void printHigherThanAverage(int [] list, int average)

2) The main method will call all the other methods

3) The fillArray method will create the array, ask the user for 5 values, store the values in the array and return the array.

4) The printArray method will print the array

5) The findAverage method will find the average of the elements in the arra

6) The printHigherThanAverage method will print all the numbers greater than the average.

need help writing the code in java using eclipse.

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

code:

import java.util.Scanner;
public class Lab {
   public int[] fillArray(){
       Scanner s = new Scanner(System.in);
       int[] numbers = new int[5];
       System.out.println("Enter 5 values");
       for(int i=0;i<5;i++)
           numbers[i] = s.nextInt();
       return numbers;
   }
   public void printArray(int[] list){
       System.out.println("Array numbers");
       for (int i = 0; i<list.length; i++) {
           System.out.print(list[i]+ " ");
       }
       System.out.println();
   }
   public int findAverage(int[] list){
       int sum = 0;
       //finding sum
       for(int i=0;i<list.length;i++){
           sum+=list[i];
       }
       return sum/list.length;
   }
   public void printHigherThanAverage(int[] list,int average){
       System.out.println("Greater than average numbers");
       for(int i=0;i<list.length;i++){
           //check if number is greater than average
           if(list[i]>average){
               System.out.println(list[i]+" ");
           }
       }
   }
   public static void main(String[] args) {
       Lab l = new Lab();
       int[] array = l.fillArray();
       l.printArray(array);
       int avg = l.findAverage(array);
       System.out.println("Average :"+avg);
       l.printHigherThanAverage(array,avg);
   }

}

Add a comment
Know the answer?
Add Answer to:
The loop below stores the values 1 to 5 in myList: for (int i = 0;...
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
  • . In the method main, prompt the user for a non-negative integer and store it in...

    . In the method main, prompt the user for a non-negative integer and store it in an int variable num (Data validation is not required for the lab). Create an int array whose size equals num+10. Initialize the array with random integers between 0 and 50 (including 0 and excluding 50). Hint: See Topic 4 Decision Structures and File IO slides page 42 for how to generate a random integer between 0 (inclusive) and bound (exclusive) by using bound in...

  • 1.Write code for a Java method, printArray, that takes an int array as parameter and prints...

    1.Write code for a Java method, printArray, that takes an int array as parameter and prints it out, one element per line. public static void printArray (int[] values){ Methods that do NOT return a result have “void” as return type. Notice how an array parameter type is passed, int [] }// end printArray 2.Write code for a Java method that takes in as input parameter an integer number, say num, and returns a double array of size num with all...

  • Which of the following are valid array declarations? a. int[] array- new int[10]; b. double [array...

    Which of the following are valid array declarations? a. int[] array- new int[10]; b. double [array double[10]; c. charl charArray "Computer Science"; None of the above Analyze the following code: class Test public static void main(Stringl] args) System.out.println(xMethod(10); public static int xMethod(int n) System.out.println("int"); return n; public static long xMethod(long n) System.out.,println("long"); return n The program displays int followed by 10 The program displays long followed by 10. The program does not compile. None of the above. tions 3-4 are...

  • JAVA Objectives: 1. Apply linear search algorithm 2. Apply select sort algorithm 3. Apply array iteration...

    JAVA Objectives: 1. Apply linear search algorithm 2. Apply select sort algorithm 3. Apply array iteration skill Problem description: Write the following eight methods and write a main function to test these methods // return the index of the first occurrence of key in arr // if key is not found in arra, return -1 public static int linearSearch(int arr[], int key) // sort the arr from least to largest by using select sort algorithm public stati void selectSort(int arr[])...

  • Our 1st new array operation/method is remove. Implement as follows: public static boolean remove( int[] arr,...

    Our 1st new array operation/method is remove. Implement as follows: public static boolean remove( int[] arr, int count, int key ) { 1: find the index of the first occurance of key. By first occurance we mean lowest index that contains this value. hint: copy the indexOf() method from Lab#3 into the bottom of this project file and call it from inside this remove method. The you will have the index of the value to remove from the array 2:...

  • I need help making this work correctly. I'm trying to do an array but it is...

    I need help making this work correctly. I'm trying to do an array but it is drawing from a safeInput class that I am supposed to use from a previous lab. The safeInput class is located at the bottom of this question I'm stuck and it is not printing the output correctly. The three parts I think I am having most trouble with are in Bold below. Thanks in advance. Here are the parameters: Create a netbeans project called ArrayStuff...

  • I need to write a program in java that reads a text file with a list...

    I need to write a program in java that reads a text file with a list of numbers and sorts them from least to greatest. This is the starter file. import java.util.*; import java.io.*; public class Lab3 { static final int INITIAL_CAPACITY = 5; public static void main( String args[] ) throws Exception { // ALWAYS TEST FOR REQUIRED INPUT FILE NAME ON THE COMMAND LINE if (args.length < 1 ) { System.out.println("\nusage: C:\\> java Lab3 L3input.txt\n"); System.exit(0); } //...

  • Write a program that will do the following. The main function should ask the user how...

    Write a program that will do the following. The main function should ask the user how many numbers it will read in. It will then create an array of that size. Next, it will call a function that will read in the numbers and fill the array (fillArray). Pass the array that was made in themain function as a parameter to this function. Use a loop that will read numbers from the keyboard and store them in the array. This...

  • Hi I need some help on this lab. The world depends on its successfull compilation. /*...

    Hi I need some help on this lab. The world depends on its successfull compilation. /* Lab5.java Arrays, File input and methods Read the comments and insert your code where indicated. Do not add/modify any output statements */ import java.io.*; import java.util.*; public class Lab5 { public static void main (String[] args) throws Exception { final int ARRAY_MAX = 30; // "args" is the list of tokens you put after "java Project3" on command line if (args.length == 0 )...

  • please use eclipse the sample output is incorrect CPS 2231 Chapter 8 Lab 1 Spring 2019...

    please use eclipse the sample output is incorrect CPS 2231 Chapter 8 Lab 1 Spring 2019 1. Write a class, SumOrRows. The main method will do the following methods: a. Call a method, createAndFillArray which: defines a 2 dim array of 3 rows and 4 columns, prompts the user for values and stores the values in the array. b. Calls a method, printArray, which: prints the Array as shown below c. Cails a method, calcSums, which: returns à 1 dimensional...

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