23.2k views
2 votes
Banks and other financial service companies offer many types of accounts for client's to invest their fund-- every one of them has a notion of their current value, but the details of calculating such value depend upon the exact nature of the account.

Write an abstract class, Account, with the following:

1. an integer static variable, nextId initialized to 10001 2. an integer instance variable, id
3. a string instance variable name
4. a constructor that accepts a single string parameter used to initialize the name instance variable. The constructor also assigns to id the value of nextId which is then incremented.
5. two accessor methods, getId, and getName which return the values of the corresponding instance variables
6. an abstract method named getValue that accepts no parameters and returns an object of type Cash.

1 Answer

3 votes

Answer:

//Abstract class declaration of Account

public abstract class Account{

//nextId initialized to 10001

private static int nextId = 10001;

private int id;

private String name;

// constructor with one argument

public Account(String passName){

name = passName;

//assign the value of nextId to id

id = nextId;

//nextId is incremented

nextId++;

}

// accessor method to return id

public int getId(){

return id;

}

// accessor method to return name

public String getName(){

return name;

}

//abstract method that return object of type Cash

//It is not implemented here.

//It will be overriden and implemented by concrete subclassed

public abstract Cash getValue();

}

Step-by-step explanation:

The abstract class Account is defined with the keyword abstract. Then inside the class, we declared and initialized an integer static variable nextId, an instance variable id and a String instance variable name.

A constructor is then defined which accept one argument of a string passName.

Two accessor methods getId and getName are declared. getId return an integer id, while getName return a string name.

Finally, an abstract method getValue is declared with no implementation and it return an object of type Cash.

User Yahya KACEM
by
6.1k points