Java  Array difficulty level Handson - 3

Question

Concept - Array Difficulty Level 3

Create a class Account with below attributes:

int - number

String - acType

double - balance

int - noOfAccountHolders

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 AccountDemo with main method

Create the below static method sortAccountByNumber in the AccountDemo class.

sortAccountByNumber(Account[] objArray)

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

Create an array of 5 Account 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 AccountDemo {
public static void main(String args[]){
Account account1= new Account(79,"dxqfkje",750.0,32);
Account account2= new Account(70,"yzfsutw",759.0,42);
Account account3= new Account(24,"iqqlcfr",966.0,85);
Account account4= new Account(27,"xqaibfl",733.0,75);
Account account5= new Account(79,"jxpsdpo",324.0,54);


Account[] objArray= {account1,account2,account3,account4,account5};

Account[] objArrayRes= sortAccountByNumber(objArray);
System.out.println("Displaying contents of array: ");

for(Account account:objArray){
System.out.println(account.getNumber()+" " + account.getAcType()+" " + account.getBalance()+" " + account.getNoOfAccountHolders()+" ");
}
}}


Output


Displaying contents of array: 
24 iqqlcfr 966.0 85 
27 xqaibfl 733.0 75 
70 yzfsutw 759.0 42 
79 dxqfkje 750.0 32 
79 jxpsdpo 324.0 54 


Solution:


import java.util.Arrays;
import java.util.Comparator;
import java.util.List;

class AccountDemo{
        public static void main(String[] args) {
            Account account1= new Account(79,"dxqfkje",750.0,32);
            Account account2= new Account(70,"yzfsutw",759.0,42);
            Account account3= new Account(24,"iqqlcfr",966.0,85);
            Account account4= new Account(27,"xqaibfl",733.0,75);
            Account account5= new Account(79,"jxpsdpo",324.0,54);


            Account[] objArray= {account1,account2,account3,account4,account5};

            Account[] objArrayRes= sortAccountByNumber(objArray);
            System.out.println("Displaying contents of array: ");

            for(Account account:objArray){
            System.out.println(account.getNumber()+" " + account.getAcType()+" " + account.getBalance()+" " + account.getNoOfAccountHolders()+" ");
            }
        }

        private static Account[] sortAccountByNumber(Account[]  objArray) {
            List<Account> ls = (List<Account>) Arrays.asList(objArray);
            ls.sort(new Account());
            objArray = new Account[ls.size()];
            ls.toArray(objArray);
            return objArray;
        }
    }

    class Account implements Comparator<Account>{
        int number;
        String acType;
        double balance;
        int noOfAccountHolders;

        public Account(){

        }
        /**
         * @return the number
         */
        public int getNumber() {
            return number;
        }

        /**
         * @param number the number to set
         */
        public void setNumber(int number) {
            this.number = number;
        }

        /**
         * @return the acType
         */
        public String getAcType() {
            return acType;
        }

        /**
         * @param acType the acType to set
         */
        public void setAcType(String acType) {
            this.acType = acType;
        }

        /**
         * @param balance the balance to set
         */
        public void setBalance(double balance) {
            this.balance = balance;
        }

        /**
         * @return the balance
         */
        public double getBalance() {
            return balance;
        }

        /**
         * @return the noOfAccountHolders
         */
        public int getNoOfAccountHolders() {
            return noOfAccountHolders;
        }

        /**
         * @param noOfAccountHolders the noOfAccountHolders to set
         */
        public void setNoOfAccountHolders(int noOfAccountHolders) {
            this.noOfAccountHolders = noOfAccountHolders;
        }

        public Account(int number,String acType , double balance , int noOfAccountHolders){
            this.number  = number;
            this.acType = acType;
            this.balance = balance;
            this.noOfAccountHolders = noOfAccountHolders;
        }

    @Override
    public int compare(Account o1, Account o2) {
        // TODO Auto-generated method stub
        return o1.number - o2.number;
    }
    }

 

©2018 by The real one. Proudly created with Wix.com