You are on page 1of 1

Concept - Array Difficulty Level 2

Create a class MailBox with below attributes:

int - id

String - name

String - creationDate

Make all the attributes private.Create corresponding getters and setters.

Create a constructor which takes all parameters in the above sequence. The
constructor should set the value of attributes to parameter values inside the
constructor.

Create a class MailBoxDemo with main method

Create the below static method sortMailBoxById in the MailBoxDemo class.

sortMailBoxById(MailBox[] objArray)

The method will sort the array based on id and return the sorted array.

Create an array of 5 MailBox objects in the main method


Refer below sample main method and test the output:

Call the above static method from the main method

public class MailBoxDemo {


public static void main(String args[]){
MailBox mailbox1= new MailBox(73,"kdqktps","glwiegn");
MailBox mailbox2= new MailBox(77,"qjftnil","fbigazj");
MailBox mailbox3= new MailBox(95,"ukeadms","egndouz");
MailBox mailbox4= new MailBox(87,"njhzbtc","orcmqza");
MailBox mailbox5= new MailBox(29,"dyifepd","avfznpr");

MailBox[] objArray= {mailbox1,mailbox2,mailbox3,mailbox4,mailbox5};

MailBox[] objArrayRes= sortMailBoxById(objArray);


System.out.println("Displaying contents of array: ");

for(MailBox mailbox:objArray){
System.out.println(mailbox.getId()+" " + mailbox.getName()+" " +
mailbox.getCreationDate()+" ");
}
}}
Output
Displaying contents of array:
29 dyifepd avfznpr
73 kdqktps glwiegn
77 qjftnil fbigazj
87 njhzbtc orcmqza
95 ukeadms egndouz

You might also like