top of page

Java  Array difficulty level Handson - 7

Question

​

Concept - Array Difficulty Level 7

​

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 searchAccountByNoOfAccountHolders in the AccountDemo class.

​

This method will take array of Account objects and noOfAccountHolders as input and returns new array of Account objects for all values found with the given noOfAccountHolders else return null if not found.

​

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(4,"rbwgqps",630.0,33);
Account account2= new Account(56,"pwibjzs",437.0,53);
Account account3= new Account(45,"djhipnk",889.0,22);
Account account4= new Account(35,"jnwtber",980.0,99);
Account account5= new Account(7,"tyuaktj",66.0,7);


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

Account[] objResultArray1= searchAccountByNoOfAccountHolders(objArray, 17);
if(objResultArray1==null){
System.out.println("Output after first search is null. ");
}else{
System.out.println("Displaying contents of result array: ");

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

Account[] objResultArray2= searchAccountByNoOfAccountHolders(objArray, 99);
if(objResultArray2==null){
System.out.println("Output after first search is null. ");
}else{
System.out.println("Displaying contents of result array: ");

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


Output


Output after first search is null. 
Displaying contents of result array: 
35 jnwtber 980.0 99 

Solution:


import java.util.ArrayList;
import java.util.List;

class AccountDemo {
    public static void main(String args[]){
        Account account1= new Account(4,"rbwgqps",630.0,33);
        Account account2= new Account(56,"pwibjzs",437.0,53);
        Account account3= new Account(45,"djhipnk",889.0,22);
        Account account4= new Account(35,"jnwtber",980.0,99);
        Account account5= new Account(7,"tyuaktj",66.0,7);
        Account[] objArray= {account1,account2,account3,account4,account5};
    
        Account[] objResultArray1= searchAccountByNoOfAccountHolders(objArray, 17);
        if(objResultArray1==null){
            System.out.println("Output after first search is null. ");
        }else{
            System.out.println("Displaying contents of result array: ");
        
            for(Account account:objResultArray1){
            System.out.println(account.getNumber()+" " + account.getAcType()+" " + account.getBalance()+" " + account.getNoOfAccountHolders()+" ");
            }
        }
    
        Account[] objResultArray2= searchAccountByNoOfAccountHolders(objArray, 99);
        if(objResultArray2==null){
            System.out.println("Output after first search is null. ");
        }else{
            System.out.println("Displaying contents of result array: ");
            for(Account account:objResultArray2){
                System.out.println(account.getNumber()+" " + account.getAcType()+" " + account.getBalance()+" " + account.getNoOfAccountHolders()+" ");
            }
        }
    }

    private static Account[] searchAccountByNoOfAccountHolders(Account[] objArray, int i) {
        List<Account> ls = new ArrayList<>();
        for (Account account : objArray) {
            if(account.noOfAccountHolders == i){
                ls.add(account);
            }
        }
        int size = ls.size();
        if( size > 0){
            objArray = new Account[size];
            ls.toArray(objArray);
            return objArray;
        }
        return null;
    }
}

class 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;
    }
}

java handson 7: About

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

bottom of page