Home > Software engineering >  Create array in class and access the array in another class
Create array in class and access the array in another class

Time:01-18

Im given a question to create a "person" class, a "staff" class that inherits the "person", a "room" class to that can contain an array (3) of Person then create all the setter and getter and a method call show() to print properties, and a main class to create a "room", add 2 "person" and 1 "staff" then print all the attributes.

Im done with the "person" and "staff" class.

    class Person{
    protected String Name;
    protected int Age;
    //... i had done the person class including the getter and setter
    

staff class that has inherit the person class and display the output

    class Staff extends Person{
      private double Salary;
    ...
    @Override
public void show(){
    System.out.println("Name: "   this.Name);
    System.out.println("Age: "   this.Age);
    System.out.println("Salary: RM "   df.format(this.Salary));
}

Im not sure with how to create an array in the Room class and how to access it in the main class. in the main class i should be (a) Create a Room. (b) Add in a Person with your name. (c) Add in a Staff with your Father’s name. (d) Add in a Person with your Mother’s name. (e) Print to the console all the Person detail.

I had tried to create an array and print out all the attributes, it works well but the question required me to create the room class so im abit confused on how to do it.

    class Main{
public static void main(String[] args) {

    Person family[] = new Person[3];
    family[0] = new Person("Me", 20);
    family[1] = new Staff("Papa", 60, 300);
    family[2] = new Person("Mama", 55);
    for(int i = 0; i<family.length; i  ) {
     family[i].show();
     System.out.println(" ");
  }
}
    }

please help me with the room and main class.

CodePudding user response:

Something like this?

Room.java:

class Room {
  private Person[] persons;

  public Room(Person[] persons){
    this.persons = persons;
  }

  public void listAllPersons() {
    // loop over array and print details
  }
// add getters and setters as you need
}

Main class:


public static void main (String[] args) {

    Person persons[] = new Person[3];
    persons[0] = new Person("Me", 20);
    persons[1] = new Staff("Papa", 60, 300);
    persons[2] = new Person("Mama", 55);

    Room room = new Room(persons);
    room.listAllPersons();
}

CodePudding user response:

Class Room could be implemented like this:

  public class Room {
    private final int capacity;
    private Person[] persons;
    private int id;

    public Room(int capacity) {
        this.capacity = capacity;
        this.persons = new Person[capacity];
    }

    /**
     *  return true if capacity of the room is not
     *  exceeded and new person was added successfully
     */
    public boolean addPerson(Person pers) {
        if (id == capacity) {
            return false;
        }
        persons[id  ] = pers;
        return true;
    }

    public void showPersons() {
        /* 
        if you are familiar with streams you can use it instead of a loop,
        otherwise remove commented limes
        Arrays.stream(persons).forEach(Person::show);
         */
        for (var pers: persons)
            pers.show();
    }
    
    public Person[] getPersons() {
        return persons;
    }
}

Class Main - your client:

public class Main {
    public static void main(String[] args) {
        Room room = new Room(3);
        room.addPerson(new Person(new Person("Me", 20)));
        room.addPerson(new Person(new Staff("Papa", 60, 300)));
        room.addPerson(new Person(new Person("Mama", 55)));
        room.showPersons();
    }
}

If something remains unclear, feel free to ask.

  •  Tags:  
  • Related