0

I recently sign up in this helpful webpage. So, I need your help Please.

I've working with GUI, using AWT (with non Swing Components). So. I must fill a 1D Array, using POO (not structured) with Random integers, from that array I have to calculate the average; having the average, from the original array, we have to select (storing) values greater than average, and less than average (I did it storing in two arrays separately) and show to AWT windows, showing 3 components:

  1. A list with the values greater than average.
  2. A list with the values less than average.
  3. The label Average.

This is the code.. (3 classes):

package arregloawt;

public class ArregloAWT {
    public static void main(String[] args) {  
    Ventana ventana = new Ventana(); //this run the Ventana constructor
    } //this is only the main
}

The Class who process the data...

package arregloawt;

import java.util.Random;
public class Media{

    public int[] vec() { //this fills the array from the method
        int vect[];
        vect = new int [20];
        Random ran = new Random();
        for (int i = 0; i < 20; i++) {
            vect[i]= ran.nextInt(100)+1;
        }
        return vect;
    }

    public int CalcMedia(){ //This calculates the Average
        int suma=0;

        for (int i = 0; i < 20; i++) {
            suma=suma+vec()[i];
        }
        return suma/20;
    }

    public int  ContMayores(){// Not Necessary
        int contM=0;
        for (int i = 0; i < 20; i++) {
            if (vec()[i]>CalcMedia()) {
                contM++;
            }
        }
        return contM;
    }

    public int [] SelecMayores(){//This Calculates who value is greater and it store in a new array
        int Mayores [];
        Mayores = new int [20];
        int j=0;
        for (int i = 0; i < 20; i++) {
            if (vec()[i]>CalcMedia()) {
                Mayores[j]= vec()[i];
                j++;
            }
        }
        return Mayores;
    }

    public int  ContMenores(){
        int contm=0;
        for (int i = 0; i < 20; i++) {
            if (vec()[i]<CalcMedia()) {
                contm++;
            }
        }
        return contm;
    }

    public int [] SelecMenores(){
        int Menores [];
        Menores = new int [20];
        int j=0;
        for (int i = 0; i < 20; i++) {
            if (vec()[i]<CalcMedia()) {
                Menores[j]= vec()[i];
                j++;
            }
        }
        return Menores;
    }


}

and Finally the Frame class....

package arregloawt;

import java.awt.*;
import java.awt.event.*;

public class Ventana extends Frame{
    Label lblTitulo = new Label();
    Label lblMedia = new Label();
    List mayores = new List(20);
    List menores = new List(20);
    Panel panel1 = new Panel();
    Media m = new Media();
    public Ventana(){
        setTitle("Mayores y Menores que la Media");
        initComponentes();
        initEvents();
        setSize(400,400);
        setResizable(false);
        setVisible(true);
    }

    private void initComponentes() {
        panel1.setLayout(new FlowLayout());
        LlenarMayores();
        LlenarMenores();
        lblMedia.setText("La Media es: "+String.valueOf(m.CalcMedia()));
        panel1.add(lblMedia);
        add(panel1);
    }

    private void initEvents() {

    addWindowListener(new ParaTerminar());}

    class ParaTerminar extends WindowAdapter
    {
    public void windowClosing(WindowEvent e)
        {System.exit(0);}
    }
    public void LlenarMayores() {

        for (int i = 0; i < 20; i++) {
            if (m.SelecMayores()[i]!=0) 
                mayores.add(String.valueOf(m.SelecMayores()[i]));
        }
        panel1.add(mayores);
    }

    public void LlenarMenores(){
        for (int i = 0; i < 20; i++) {
            if (m.SelecMenores()[i]!=0) {
                menores.add(String.valueOf(m.SelecMenores()[i]));
            }   
        }
        panel1.add(menores);
    }
}

Please Help. the result is not convincing. The result...

Result
enter image description here

I don't know what is going wrong. :(

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Allan Ramirez
  • 125
  • 1
  • 15
  • *"I've working with GUI, using AWT (with non Swing Components)."* Why use AWT? See [this answer](http://stackoverflow.com/questions/6255106/java-gui-listeners-without-awt/6255978#6255978) for many good reasons to abandon AWT using components in favor of Swing. – Andrew Thompson Apr 30 '15 at 05:32

2 Answers2

3

One thing wrong is how you're making use of the vec() method. You should call it once and store it in a variable, then access it as an array. What you are doing is calling vec() multiple times, which means it keeps recalculating different random numbers each time. For example, instead of doing this:

Mayores[j]= vec()[i];

You should have done something like this:

// Get the array early and just once. 
int[] vect = vec();
// then later when needing a number from the array...
Mayores[j]= vect[i];

By using vec()[i] you are generating a new and different set of integers each time because vec() returns a new int[] from which you get the ith entry.

Always Learning
  • 5,510
  • 2
  • 17
  • 34
0

Thanks to @stvcisco for the answer. The code was modified the class Media.java, so the new is....

package arregloawt;

import java.util.Random;
public class Media{
    int Vect[] = vec();
    public int[] vec() { //this fills the array from the method
        int vect[];
        vect = new int [20];
        Random ran = new Random();
        for (int i = 0; i < 20; i++) {
            vect[i]= ran.nextInt(100)+1;
        }
        return vect;
    }

    public int CalcMedia(){ //This calculates the Average
        int suma=0;

        for (int i = 0; i < 20; i++) {
            suma=suma+Vect[i];
        }
        return suma/20;
    }

For those who need help. this ask is ANSWERED. The Screenshot

Allan Ramirez
  • 125
  • 1
  • 15