top of page

Java  Array difficulty level Handson - 1

Question

​

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

searchAccountByNumber(Account[] objArray)

​

This method will take array of Account objects and number as input and returns the position of the number if found or -1 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(7,"ewqkvae",168.0,67);
Account account2= new Account(3,"mqclvcm",123.0,74);
Account account3= new Account(57,"uenzjpt",164.0,98);
Account account4= new Account(28,"hwzlblz",130.0,5);
Account account5= new Account(4,"rmejckg",125.0,28);


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

int accountres= searchAccountByNumber(objArray, 63);
System.out.println("Output after first search: "+accountres);

int accountres1= searchAccountByNumber(objArray, 4);
System.out.println("Output after second search: "+accountres1);
}}

​

Output

​

Output after first search: -1
Output after second search: 4


Solution:


class AccountDemo{
    public static void main(String[] args) {
            Account account1= new Account(7,"ewqkvae",168.0,67);
            Account account2= new Account(3,"mqclvcm",123.0,74);
            Account account3= new Account(57,"uenzjpt",164.0,98);
            Account account4= new Account(28,"hwzlblz",130.0,5);
            Account account5= new Account(4,"rmejckg",125.0,28);
            Account[] objArray= {account1,account2,account3,account4,account5};
            int accountres= searchAccountByNumber(objArray, 4);
            System.out.println("Output after first search: "+accountres);
    }

    private static int searchAccountByNumber(Account[] objArray, int i) {
        int counter = 0;
        for (Account account : objArray) {
            if(account.number == i){
                break;
            }
            counter++;
        }
        return counter < objArray.length ? counter : -1;
        // return 0;
    }
}

class Account{
    int number;
    String acType;
    double balance;
    int noOfAccountHolders;

    /**
     * @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 1: About

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

bottom of page