
Java Array difficulty level Handson - 4
Question
​
Concept - Array Difficulty Level 4
​
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 sortAccountByAcType in the AccountDemo class.
sortAccountByAcType(Account[] objArray)
​
The method will sort the array based on acType 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(20,"fvmirje",552.0,90);
Account account2= new Account(40,"sfvwspp",385.0,66);
Account account3= new Account(16,"tfypcof",807.0,13);
Account account4= new Account(15,"gvkhywb",627.0,51);
Account account5= new Account(29,"iglclrn",775.0,43);
Account[] objArray= {account1,account2,account3,account4,account5};
Account[] objArrayRes= sortAccountByAcType(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:
20 fvmirje 552.0 90
15 gvkhywb 627.0 51
29 iglclrn 775.0 43
40 sfvwspp 385.0 66
16 tfypcof 807.0 13
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(20,"fvmirje",552.0,90);
Account account2= new Account(40,"sfvwspp",385.0,66);
Account account3= new Account(16,"tfypcof",807.0,13);
Account account4= new Account(15,"gvkhywb",627.0,51);
Account account5= new Account(29,"iglclrn",775.0,43);
Account[] objArray= {account1,account2,account3,account4,account5};
Account[] objArrayRes= sortAccountByAcType(objArray);
System.out.println("Displaying contents of array: ");
for(Account account:objArrayRes){
System.out.println(account.getNumber()+" " + account.getAcType()+" " + account.getBalance()+" " + account.getNoOfAccountHolders()+" ");
}
}
private static Account[] sortAccountByAcType(Account[] objArray) {
List<Account> arr = Arrays.asList(objArray);
arr.sort(new Account());
arr.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.acType.compareTo(o2.acType);
}
}