Create an abstract class named Account for a bank. Include an integer field for
ID: 665278 • Letter: C
Question
Create an abstract class named Account for a bank. Include an integer field for the account number and a double field for the account balance. Also include a constructor that requires an account number and that sets the balance to 0.0. Include a set method for the balance. Also include two abstract get methods - one for each field. Create two child classes of Account: Checking and Savings. Within Checking class, the get method displays the String “Checking Account Information”, the account number, and the balance. Within the Savings class, add a field to hold the interest rate, and require the Savings constructor to accept an argument for the value of the interest rate. The Savings get method displays the String “Saving Account Information”, the account number, the balance, and the interest rate. Write an application that demonstrates you can instantiate and display both Checking and Savings objects. Save the Account.java, Checking.java, Savings.java, and DemoAccounts.java. Write an application named AccountArray in which you enter data for a mix of 5 Checking and Savings accounts. Use for loop to display the data. Save the file as AccountArray.java.
Explanation / Answer
//Account.java
import java.io.*;
public abstract class Account
{
int accno;
double accbal;
Account(int no)
{
accno = no;
accbal = 0.0;
}
public void setBal(double bal)
{
accbal=bal;
}
abstract void getAccno();
abstract void getBal();
}
//Checking.java
import java.io.*;
public class Checking extends Account
{
Checking(int no)
{
super(no);
}
public void getAccno()
{
System.out.println(" Checking Account Information");
System.out.println(" Account number is : "+accno);
}
public void getBal()
{
System.out.println(" The balance is : "+accbal);
}
}
//Savings.java
import java.io.*;
public class Savings extends Account
{
float interest;
Savings(int no,float i)
{
super(no);
interest = i;
}
public void getAccno()
{
System.out.println(" Saving Account Information");
System.out.println(" Account number is : "+accno);
}
public void getBal()
{
System.out.println(" The balance is : "+accbal);
System.out.println(" The rate of interest is : "+interest+"%");
}
}
//DemoAccounts.java
import java.io.*;
public class DemoAccounts
{
public static void main(String args[])
{
/* Instantiating and displaying both Checking and Savings objects */
Checking ch = new Checking(12001);
ch.setBal(10000.59);
ch.getAccno();
ch.getBal();
Savings sav = new Savings(19261, 10.9F);
sav.setBal(100500.72);
sav.getAccno();
sav.getBal();
}
}
//AccountArray.java
import java.io.*;
public class AccountArray
{
public static void main(String args[])
{
/* A mix of 5 Checking and Savings accounts which are initialised and values are printed*/
Checking ch[] = new Checking[5] ;
for(int i=0;i<5;i++)
{
ch[i] = new Checking(12001+10*i);
ch[i].setBal(10000.59+8*i);
ch[i].getAccno();
ch[i].getBal();
}
Savings sav[] = new Savings[5];
for(int i=0;i<5;i++)
{ double d = 10+1.2*i;
float f = (float)d;
sav[i] = new Savings(19261+3*i,f);
sav[i].setBal(100500.72+7*i);
sav[i].getAccno();
sav[i].getBal();
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.