Question

In: Computer Science

For this question we will be using arrays and classes in Java to compute the min,...

For this question we will be using arrays and classes in Java to compute the min, max, and average value of items for a given array of integers. Complete the following using the base template provided below:

-Create methods for min, max, and average and call them from main to print out their values.

-Add a method to determine the median (http://www.mathsisfun.com/median.html) and print that value. This method is currently not in the template, so you will need to add and call it.

-Add a try/catch exception handler to each method to make sure you don't divide by zero or use an out of bounds array index.

import java.util.Arrays;

class Main {

public static void main (String args[]){
int numbers[]= {1,5,-9,12,-3,89, 18,23,4,-6};

// Find minimum (lowest) value in array using loop
System.out.println("Minimum Value = " + getMinValue(numbers));

// Find maximum (largest) value in array using loop
System.out.println("Maximum Value = " + getMaxValue(numbers));

// Determine the average of all element values in array using loop

// ADD CODE TO CALL getAvgValue AND PRINT THE AVERAGE

// ADD CODE TO SORT THE NUMBERS IN THE ARRAY, PRINT THE SORTED ARRAY
  

}

//Find maximum (largest) value in array using loop
public static int getMaxValue(int[] numbers){
int maxValue = numbers[0];
  
// ADD CODE TO ADD A LOOP, CHECK EACH ARRAY ELEMENT
// AGAINST THE CURRENT maxValue (USE AN IF STATEMENT)

return maxValue;
}

//Find minimum (lowest) value in array using loop
public static int getMinValue(int[] numbers){
int minValue = numbers[0];
  
// ADD CODE TO ADD A LOOP, CHECK EACH ARRAY ELEMENT
// AGAINST THE CURRENT minValue (USE AN IF STATEMENT)

return minValue;
}

//Find the average of an array of integers
public static double getAvgValue(int[] numbers){


// ADD CODE TO SET UP NEEDED VARIABLES, ONE TO SUM ALL ARRAY ELEMENT VALUES, AND

// Used to store the average of all elements in an array
double average = 0;

// ADD CODE TO ADD A LOOP TO COMPUTE A RUNNING TOTAL OF ALL ARRAY ELEMENTS

// ADD CODE TO COMPUTE THE AVERAGE

return average;
}


} // Main

Solutions

Expert Solution

Code For Above Problem:

import java.util.Arrays;

class Main {

        public static void main(String args[]) {
                int numbers[] = { 1, 5, -9, 12, -3, 89, 18, 23, 4, -6 };

                // Find minimum (lowest) value in array using loop
                System.out.println("Minimum Value = " + getMinValue(numbers));

                // Find maximum (largest) value in array using loop
                System.out.println("Maximum Value = " + getMaxValue(numbers));

                // ADD CODE TO CALL getAvgValue AND PRINT THE AVERAGE
                System.out.println("Average Value = " + getAvgValue(numbers));

                // ADD CODE TO SORT THE NUMBERS IN THE ARRAY, PRINT THE SORTED ARRAY
                // USED ALGORITM IS INSERTION SORT
                for (int i = 1; i < numbers.length; i++) {
                        int key = numbers[i];
                        int j = i - 1;
                        while (j >= 0 && numbers[j] > key) {
                                numbers[j + 1] = numbers[j];
                                j = j - 1;
                        }
                        numbers[j + 1] = key;
                }

                // PRINTING SORTED ARRAY
                System.out.println("Sorted array:");
                System.out.println(Arrays.toString(numbers));

        }

//Find maximum (largest) value in array using loop
        public static int getMaxValue(int[] numbers) {
                int maxValue = numbers[0];

                try {

                        // ADD CODE TO ADD A LOOP, CHECK EACH ARRAY ELEMENT
                        // AGAINST THE CURRENT maxValue (USE AN IF STATEMENT)
                        for (int i = 1; i < numbers.length; i++) {
                                if (numbers[i] > maxValue) {
                                        maxValue = numbers[i];
                                }
                        }
                } catch (Exception e) {
                        e.printStackTrace();
                }

                return maxValue;
        }

        // Find minimum (lowest) value in array using loop
        public static int getMinValue(int[] numbers) {
                int minValue = numbers[0];

                try {
                        // ADD CODE TO ADD A LOOP, CHECK EACH ARRAY ELEMENT
                        // AGAINST THE CURRENT minValue (USE AN IF STATEMENT)
                        for (int i = 1; i < numbers.length; i++) {
                                if (numbers[i] < minValue) {
                                        minValue = numbers[i];
                                }
                        }
                } catch (Exception e) {
                        e.printStackTrace();
                }

                return minValue;
        }

        // Find the average of an array of integers
        public static double getAvgValue(int[] numbers) {

                // ADD CODE TO SET UP NEEDED VARIABLES, ONE TO SUM ALL ARRAY ELEMENT VALUES, AND
                double sum = 0;
                // Used to store the average of all elements in an array
                double average = 0;

                try {
                        // ADD CODE TO ADD A LOOP TO COMPUTE A RUNNING TOTAL OF ALL ARRAY ELEMENTS
                        for (int i = 0; i < numbers.length; i++) {
                                sum += numbers[i];
                        }

                        // ADD CODE TO COMPUTE THE AVERAGE
                        average = sum / numbers.length;
                } catch (Exception e) {
                        e.printStackTrace();
                }

                return average;
        }

} // Main

Sample Run Results:

Minimum Value = -9
Maximum Value = 89
Average Value = 13.4
Sorted array:
[-9, -6, -3, 1, 4, 5, 12, 18, 23, 89]

Images Of Code:

Image Of Sample Run Results:


Related Solutions

how to create BANKACCOUNT program using Arrays in JAVA.
how to create BANKACCOUNT program using Arrays in JAVA.
Use JAVA BASICS  classes, aggregation and manipulating arrays of objects. I DO NOT WANT THE SAME SOLUTION...
Use JAVA BASICS  classes, aggregation and manipulating arrays of objects. I DO NOT WANT THE SAME SOLUTION WHICH ALREADY EXISTS ON CHEG. DO NO USE ARRAY LIST Scenario: A dog shelter would like a simple system to keep track of all the dogs that pass through the facility. The system must record for each dog: dogId (int) - must be unique name (string) age (double) - cannot be less than 0 or more than 25 breed (string) sex (char) – m...
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...
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...
In Java In this lab we will creating two linked list classes: one that is a...
In Java In this lab we will creating two linked list classes: one that is a singly linked list, and another that is a doubly linked list ( This will be good practice for your next homework assignment where you will build your own string class using arrays and linked list ) . These LinkedList classes should both be generic classes. and should contain the following methods: Print Add - Adds element to the end of the linked list. IsEmpty...
Make a Tic Tac Toe game for 2 players to play using 2D arrays and classes....
Make a Tic Tac Toe game for 2 players to play using 2D arrays and classes. Do not add more #include functions other than the ones listed (such as #include <stdio.h> etc). Using the tictactoeGame class, write a main program that uses a tictactoeGame to implement a game in which two players (you and a friend) take turns placing X’s and O’s onto the board. After each turn, the current board configuration should be displayed, and once a player connects...
(Java) Please describe how API's can be created using abstract classes, interfaces and regular classes.
(Java) Please describe how API's can be created using abstract classes, interfaces and regular classes.
JAVA I need to write a code that calculates mean and standard deviation using arrays and...
JAVA I need to write a code that calculates mean and standard deviation using arrays and OOP. This is what I have so far. I'm close but my deviation calculator method is throwing errors. Thanks so much :) import java.util.Scanner; import java.util.Arrays; public class STDMeanArray { private double[] tenUserNums = new double[10];//Initialize an array with ten index' private double mean; private double deviation; Scanner input = new Scanner(System.in);//create new scanner object /*//constructor public STDMeanArray(double tenUserNum [], double mean, double deviation){...
In Java Solve the following problem using arrays: Past A: Coupon collector is a classic statistic...
In Java Solve the following problem using arrays: Past A: Coupon collector is a classic statistic problem with many practical applications. The problem is to pick objects from a set of objects repeatedly and determine how many picks are needed for all the objects to be picked at least once. A variation of the problem is to pick cards from a shuffled deck of 52 cards repeatedly and find out how many picks are needed before you see one of...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT