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:
- A list with the values greater than average.
- A list with the values less than average.
- 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...
I don't know what is going wrong. :(