A class is similar to a struct in the C language in that it
stores related data in fields, where the fields can be different
types.
Open the file src/average/Average.java. You will find a class
named Average.
This class defines a method computeAverage() that takes in an
array of integers and returns the average of this numbers. You are
required to implement this method.
Lab 01 - Core Exercise - - Average A class is similar to a struct in the C language in that it stores related data in fields, where the fields can be different types. 1. Open the file src/average/Average.java. You will find a class named Average. 2. This class defines a method computeAverage () that takes in an array of integers and returns the average of this numbers. You are required to implement this method. o To complete this task, you need to compute the sum of the numbers and the total number of elements; o Use a for loop to traverse the list of numbers and compute the sum of the numberl o Use nums.length to get the length of the array, after the sum has been computed. 3. Next, define a main() method. Note: Every Java application needs one class with a main() method. This class is the entry point for the Java application and is the class name passed to the java command to run the application. The interpreter executes the code in the main () method when the program starts, and is the point from which all other classes and corresponding methods are invoked. VSCode will recognise the main method and provide you with a "Run" button to run the code if you have the correct extensions installed. 4. Inside the main() method, initialise an array of the numbers 1 - 6 (integers) and invoke the method computeAverage(), passing it as an argument. Hint: computeAverage() is an instance method, so you will need to create an instance of the class Average and invoke the method on this instance. 5. Assign the result of this method to a variable and print the variable in the format: The average is {average}. The pipeline for this repository will run a simple test on your code to check that it works as expected. You can run the tests yourself locally using bash tests.sh, though this will run tests for all exercises in the lab unless you modify it.
1 package average; 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 public class Average { **/ * Returns the average of an array of numbers * * @param the array of integer numbers * @return the average of the numbers */ public float computeAverage (int[] nums) { float result=0, sum=0; for (int i=0;i<nums.length; i++) sum+=nums; result=sum/nums.length; return result; } public static void main(String[] args) { int[] array=new int[]{10,20,30,40,55}; Average a=new Average(); System.out.println("The average of numbers in the input array: "+a. a. average (array)); } { }
A class is similar to a struct in the C language in that it stores related data in fields, where the fields can be diffe
-
- Site Admin
- Posts: 899603
- Joined: Mon Aug 02, 2021 8:13 am