Question

In: Computer Science

java by using Scite Objectives: 1. Create one-dimensional arrays and two-dimensional arrays to solve problems 2....

java by using Scite

Objectives:

1. Create one-dimensional arrays and two-dimensional arrays to solve problems

2. Pass arrays to method and return an array from a method

Problem 1:

Find the average of an array of numbers (filename: FindAverage.java)

Write two overloaded methods with the following headers to find the average of an array of integer values and an array of double values:

public static double average(int[] num)

public static double average(double[] num)

In the main method, first create an array of integer values with the array size of 5 and invoke the first method to get and display the average of the first array, and then create an array of double values with the array size of 6 and invoke the second method to get and display the average of the second array.

You can generate the array values using the Math.random() method with the range from 0 to 9. Keep two decimals for the average values.

Solutions

Expert Solution

Hello and Hope you're having a good day. The following code was created using Java and should definitely serve your purpose.

Program:

import java.util.*;

public class FindAverage{
  
    //static method finds int average
    public static double average(int[] num){
        double y = 0;
        for(int i=0;i<num.length;i++){
            y+=num[i];
        }
        y /= num.length;
        return y;
    }
  
    //static method finds double average
    public static double average(double[] num){
        double y = 0;
        for(int i=0;i<num.length;i++){
            y+=num[i];
        }
        y /= num.length;
        return y;
    }

    public static void main(String []args){
      
        //int block
        //creation
        int[] num = new int[5];
        for(int i=0;i<5;i++){
            num[i] = (int) ((Math.random() * (10 - 0)));
        }
     
        //display array
        System.out.println("Int Array:");
        for(int i=0;i<5;i++){
            System.out.println(num[i]);
        }
      
        //find and print average
        double s = average(num);
        System.out.println("Average of Int Array is "+s);
      
        //double block
        //creation
        double[] num2 = new double[6];
        for(int i=0;i<6;i++){
            num2[i] = Math.random() * (10 - 0);
        }
     
       //display array
        System.out.println("Double Array:");
        for(int i=0;i<6;i++){
            System.out.println(num2[i]);
        }
      
        //find and print average
        double s2 = average(num2);
        System.out.println("Average of Double Array is "+s2);
      
      
    }
}

NB: There are better methods to generate Random numbers in Java like the Random Package in Utils. Math.Random returns a double which has to be bound and typecasted to an int to suit our needs. Please look into those for more information.

I hope this has helped you. Thank you and have a nice day. Happy learning!


Related Solutions

java by using Scite Objectives: 1. Create one-dimensional arrays and two-dimensional arrays to solve problems 2....
java by using Scite Objectives: 1. Create one-dimensional arrays and two-dimensional arrays to solve problems 2. Pass arrays to method and return an array from a method Problem 2: Find the largest value of each row of a 2D array             (filename: FindLargestValues.java) Write a method with the following header to return an array of integer values which are the largest values from each row of a 2D array of integer values public static int[] largestValues(int[][] num) For example, if...
how to create BANKACCOUNT program using Arrays in JAVA.
how to create BANKACCOUNT program using Arrays in JAVA.
Create a “Main” method that contains two 2-Dimensional arrays of characters. The first array, which we...
Create a “Main” method that contains two 2-Dimensional arrays of characters. The first array, which we will call our visible field, needs to be 5 by 5 and should initially hold the underscore character “_”.  This will be used in our game of minesweeper to represent the possible locations the player can check. The second array, which we will call our hidden field, should also be 5 by 5 but filled with the capital letter "S” which means safety. However, we...
Create a program using Java. Create two different 3D arrays with random numbers. (lets name them...
Create a program using Java. Create two different 3D arrays with random numbers. (lets name them array1 and array 2) Add the two 3Darrays together and then get the average. Save the average on a separate 3D array.(lets name it array3) Then add array1 and array3 then get the average Save the average on a separate 3Darray(lets name it array 4) Then add array2 and array3 then get the average Save the average on a separate 3Darray (lets name it...
Java program problem 1 Come up with an idea for parallel arrays Create the arrays to...
Java program problem 1 Come up with an idea for parallel arrays Create the arrays to hold 5 items in each In the main(), load the arrays with data Then output the entire contents of the arrays Create a method called find1() and pass the 2 arrays to that method. problem 2 In the find1() method, ask the user to enter a value to search for Write logic to search the first array and then output the related data from...
Write a Java program that will use a two-dimensional array to solve the following tasks: 1....
Write a Java program that will use a two-dimensional array to solve the following tasks: 1. Create a method to generate a 2-dimensional array (random numbers, range 0 - 500). The array has ROW rows and COL columns, where ROW and COL are class constants. 2. Create a method to print the array. 3. Create a method to find the largest element in the array 4. Create a method to find the smallest element in the array 5. Create a...
answer in JAVA language please In this assignment you will use: one-dimensional arrays; strings and various...
answer in JAVA language please In this assignment you will use: one-dimensional arrays; strings and various string-handling methods; file input and output; console input and output; loops; and conditional program statements. Instruction You are asked to write a program that translates a Morse code message into English language. The program will ask the user to enter filenames for two input files, one for the code translation table data and the other for the coded message data, as well as an...
Longest ‘A’ Path Objectives Manipulating two-dimensional lists Using recursion to solve a problem Problem Specification Write...
Longest ‘A’ Path Objectives Manipulating two-dimensional lists Using recursion to solve a problem Problem Specification Write a Python application to find the longest ‘A’ path on a map of capital (uppercase) letters. The map is represented as a matrix (2-dimensional list) of capital letters. Starting from any point, you can go left, right, up and down (but not diagonally). A path is defined as the unbroken sequence of letters that only covers the spots with the letter A. The length...
Write a Java program that will use a two-dimensional array and modularity to solve the following...
Write a Java program that will use a two-dimensional array and modularity to solve the following tasks: Create a method to fill the 2-dimensional array with (random numbers, range 0 - 30). The array has rows (ROW) and columns (COL), where ROW and COL are class constants. Create a method to print the array. Create a method to find the largest element in the array Create a method to find the smallest element in the array Create a method to...
Write a Java program that will use a two-dimensional array and modularity to solve the following...
Write a Java program that will use a two-dimensional array and modularity to solve the following tasks: 1. Create a method to generate a 2-dimensional array (random numbers, range 0 - 500). The array has ROW rows and COL columns, where ROW and COL are class constants. 2. Create a method to print the array. 3. Create a method to find the largest element in the array 4. Create a method to find the smallest element in the array 5....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT