Results 1 to 3 of 3

Thread: Elements 11th Java Assignment

  1. #1
    Join Date
    Jun 2007
    Posts
    532
    Mentioned
    1 Post(s)
    Quoted
    68 Post(s)

    Default Elements 11th Java Assignment

    Hello!


    Write a program with an array that is initialized with test data. Use any primitive data type of your choice. The program should also have the following methods:

    •getTotal. This method should accept a one-dimensional array as its argument and return the total of the values in the array.

    •GetAverage. This method should accept a one-dimensional array as its argument and return the average of the values in the array.

    •GetHighest. This method should accept a one-dimensional array as its argument and return the highest of the values in the array.

    •GetLowest. This method should accept a one-dimensional array as its argument and return the lowest of the values in the array.

    Demonstrate each of the methods in the program using the data from the
    following four one-dimensional arrays.
    // Some arrays of various types.

    int[] iarray = { 2, 1, 9, 7, 3 };
    float[] farray = { 3.5F, 4.6F, 1.7F, 8.9F, 2.1F };
    double[] darray = { 98.7, 89.2, 55.1, 77.6, 99.9 };
    long[] larray = {100, 500, 200, 300, 400 };


    Question: How do I get it to accept the other arrays?

    Code:

    java Code:
    import java.util.Scanner;

    public class VarnickPass11 {
        public static void main(String[] args) {
            final int ARRAY_SIZE = 5;
            int[] numbers = new int[ARRAY_SIZE];
            getValues(numbers);
            getTotal(numbers);
            getAverage(numbers);
            getHighest(numbers);
            getLowest(numbers);
        }

        public static int getTotal(int[] x) {
            int total = 0;
            for (int index = 0; index < x.length; index++) {
                total += x[index];
            }
            System.out.println("The total is " + total);
            return total;
        }

        public static double getAverage(int[] x) {
            int total = 0;
            for (int index = 0; index < x.length; index++) {
                total += x[index];
            }
            double average = (total / 2.0);
            System.out.println("The average is " + average);
            return average;
        }

        public static int getHighest(int[] x) {
            int highest = x[0];
            for (int index = 1; index < x.length; index++) {
                if (x[index] > highest)
                    highest = x[index];
            }
            System.out.println("The highest value is " + highest);
            return highest;
        }

        public static int getLowest(int[] x) {
            int lowest = x[0];
            for (int index = 1; index < x.length; index++) {
                if (x[index] < lowest)
                    lowest = x[index];
            }
            System.out.println("The lowest value is " + lowest);
            return lowest;
        }

        private static void getValues(int[] array) {
            Scanner keyboard = new Scanner(System.in);
            System.out.println("Enter a series of " + array.length + " numbers.");

            for (int index = 0; index < array.length; index++) {
                System.out.print("Enter number " + (index + 1) + ": ");
                array[index] = keyboard.nextInt();
            }
        }
    }
    Last edited by core; 11-22-2013 at 09:11 PM. Reason: fixed code formatting
    Finished B.S. Program in Radiology!!

    Projects: A big one! Total secret! hehe

  2. #2
    Join Date
    Jan 2012
    Posts
    2,568
    Mentioned
    35 Post(s)
    Quoted
    356 Post(s)

    Default

    If it must be within a single method, then to accept all one-dimensional arrays, the parameter must be a parent class of all the primitive wrapper classes.
    Then you can downcast the argument and proceed with your calculations.

    Hint: the argument passed must then be something like:
    Integer[] numbers = {2, 1, 9, 7, 3 };

  3. #3
    Join Date
    Jun 2007
    Posts
    532
    Mentioned
    1 Post(s)
    Quoted
    68 Post(s)

    Default

    Quote Originally Posted by Sin View Post
    You familiar with overloading?
    I am not.
    Finished B.S. Program in Radiology!!

    Projects: A big one! Total secret! hehe

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •