Sort from an array of objects into stacks without using java.util

Joined
Jul 6, 2021
Messages
1
Reaction score
0
I want to create an object array of 50 people by randomly generating their ages and IDs. After that I must classify each of the people generated in the list in 2 stack arrays, where the first stack will contain the people that have an age lower than 18 and the second stack will contain the people older than 18. The classification of the people, which consists of removing each of the elements of the initial list and inserting them in the corresponding stack, must be a process that is performed automatically. I already made the first part but I'm having problems with the classification since I can't use the java.util to create the stacks. I'd really appreciate it if you could help me with this.

Java:
public class Stacks01 {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    // TODO code application logic here
    //create array of employee object 
    Person[] person = new Person[50] ;
    Person[] minors = new Person[50];
    Person[] adults = new Person[50];


     for (int i = 0; i < 50; i++)
     {
         person[i] = new Person (generatorId(100000000, 999999999), generatorAge(0,100));
     }
    
     for (int j = 0; j < 50; j++)
     {
         System.out.println("Person "+(j+1)+": ");
         person[j].showData();
        
     }
    
}

//Person class with Id and age as attributes
static class Person{
  int Id;
  int age;
  //Person class constructor
  Person(int pId, int e){
     Id = pId;
     age = e;
  }
 
  public void showData(){
    System.out.print("Id: "+Id + "  " + " age: "+age);
    System.out.println();
}
}


public static int generatorId(int min, int max){
double id;
    id = ((Math.random() * (max - min)) + min);

return (int) id;
}

public static int generatorAge(int min, int max){
double age;
    age = ((Math.random() * (max - min)) + min);

return (int) age;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,703
Messages
2,569,328
Members
44,612
Latest member
Billysak

Latest Threads

Top