Hank
Hank

Reputation: 13

Creating java program assistance

I have to create a java program with two classes and the challenge is =

"Enter in 10 numbers. Calculate the average and display all numbers greater than the average."

I am fairly new to java and I have no idea on what I am doing and how to send array values from one class to another.

  import BreezySwing.KeyboardReader;
    import javax.swing.*;

    public class Average {
        public static void main(String[] args) {
            KeyboardReader reader = new KeyboardReader();
            System.out.print('\u000C');
            AverageTest at = new AverageTest();
            int numberArray[];
            int i;
            numberArray = new int[10];
            for (i = 0; i < 10; i++) {
                numberArray[i] = reader.readInt("Enter a number: ");
                at.setnumber(numberArray);
            }
        }
    }

import javax.swing.*;
import BreezySwing.*;
public class AverageTest
{
    private int number[];
    private int a;

    public void setnumber(int number)
    {
        number = numberArray;
    }
}

Upvotes: 1

Views: 76

Answers (2)

CH.Z
CH.Z

Reputation: 31

there are 3 steps about this issue: 1.Enter in 10 numbers. 2.Calculate the average. 3.display all numbers greater than the average.

you have done the step 1,that's great. and I can see that you are trying to do the step 2.

here's the suggestion of your issue:

if you want to send array values from A class to B,you just need to invoke the method of B in the A correctly.

I think I know what you are trying to do. the problem of your code that you can't send array values from one class to another is because the method's parameter type is not matching.

the method public void setnumber(int number),the parameter is an int type,and you try to refer it to an int array,this's wrong.

first, you need to change the method's definition to public void setnumber(int[] numberarray),and try to figure out why we have to write like this. then finish the step 2. Hope it'll help.

Upvotes: 0

Nagendra Varma
Nagendra Varma

Reputation: 2305

import java.util.Scanner;

public class AverageTest {

    public static void main(String[] args) {
        int[] array = new int[10];
        // Try with resources, automatically closes the reader once the work is done
        // Read 10 integers from the standard input
        try (Scanner reader = new Scanner(System.in);) {
            for (int i = 0; i < 2; i++) {
                System.out.println("Enter a number: ");
                array[i] = reader.nextInt();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        // we have an array with 10 numbers, now create an average object by passing
        // this array to the Average class constructor
        Average averageObj = new Average(array);
        // Compute the average
        float average = averageObj.average();
        System.out.println("Average: " + average);
        System.out.println("Numbers greater than average: ");
        // Print out the numbers which are greater than or equal to the average
        for (int i = 0; i < array.length; i++) {
            if (array[i] >= average) {
                System.out.println(array[i]);
            }
        }
    }

}

class Average {
    private int[] array;

    public Average(int[] array) {
        if (array == null || array.length == 0) {
            throw new IllegalArgumentException("Array cannot be null or empty");
        }
        this.array = array;
    }

    public int[] getArray() {
        return array;
    }

    /**
     * Computes the average of the given array and returns it.
     */
    public float average() {
        int sum = 0;
        for (int i = 0; i < array.length; i++) {
            sum += array[i];
        }
        return (float) sum/array.length;
    }

}

Upvotes: 2

Related Questions