1

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;
    }
}
2
  • 4
    Please post the code you have tried, so we can help Commented Nov 28, 2017 at 4:03
  • "how to send array values from one class to another" - you posted only one class. Also - please do not post code in the comment section - as you can see it's not readable... Commented Nov 28, 2017 at 4:14

2 Answers 2

2
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;
    }

}
Sign up to request clarification or add additional context in comments.

2 Comments

What have you changed and why does it solve the issue? Can you pair the code with explanations, those will often help OP more than just code.
Code enriched with comments, please let me know if you have any problem in understanding the code.
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.