PDA

View Full Version : Elements 11th Java Assignment



Element17
11-22-2013, 05:44 PM
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:

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();
}
}
}

riwu
11-23-2013, 01:25 AM
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 };

Element17
11-23-2013, 04:16 PM
You familiar with overloading?

I am not.