Task - Dynamic Polymorphism and Anonymous Inner Class #14
Replies: 55 comments 4 replies
-
class BankAccount {
int accNo;
String accHolder;
double accBalance;
BankAccount() {
accNo = 0;
accHolder = "";
accBalance = 0.0;
}
BankAccount(int accNo, String accHolder, double accBalance) {
this.accNo = accNo;
this.accHolder = accHolder;
this.accBalance = accBalance;
}
public String toString(){
return "Account Number: "+accNo + " Account Holder Name: "+accHolder + " Account Balance: "+accBalance;
}
}
class savingsAccount extends BankAccount {
double rateOfInterest;
savingsAccount() {
super();
rateOfInterest = 0.0;
}
savingsAccount(int accNo, String accHolder, double accBalance, double rateOfInterest) {
super(accNo, accHolder, accBalance);
this.rateOfInterest = rateOfInterest;
}
public String toString(){
return "Account Number: "+accNo + " Account Holder Name: "+accHolder + " Account Balance: "+accBalance + " Rate of Interest: "+rateOfInterest;
}
public void getYearlyInterest() {
System.out.println("Yearly Interest: " + (accBalance * rateOfInterest * 12) / 100);
}
public void getComputedInterest(int years) {
double CI=(accBalance*Math.pow((1/rateOfInterest),years));
System.out.println("Compunded Interest: " + CI);
}
}
class currentAccount extends BankAccount {
double avgDailyTransactions;
currentAccount() {
avgDailyTransactions = 0.0;
}
currentAccount(double avgDailyTransactions) {
this.avgDailyTransactions = avgDailyTransactions;
}
public String toString(){
return "Account Number: "+accNo + " Account Holder Name: "+accHolder + " Account Balance: "+accBalance + "Avg. Daily Transactions: "+avgDailyTransactions;
}
}
public class Account {
public static void main(String[] args) {
BankAccount b1=new BankAccount(123,"Rohit Naikade",12000.00);
System.out.println(b1);
savingsAccount s1 = new savingsAccount(123,"Rohit Naikade",12000.00,2.5);
s1.getYearlyInterest();
s1.getComputedInterest(2);
System.out.println(s1);
}
} |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
|
`import java.util.Scanner; class GeneralAccount { } class SavingsAccount extends GeneralAccount { } class CurrentAccount extends GeneralAccount { } public class AccountDriver { }` |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
|
`class BankAccount } }` |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
package collection.Annonymous;
class BankAccount {
int accNo;
String accHolder;
double accBalance;
BankAccount() {
accNo = 0;
accHolder = "";
accBalance = 0.0;
}
BankAccount(int accNo, String accHolder, double accBalance) {
this.accNo = accNo;
this.accHolder = accHolder;
this.accBalance = accBalance;
}
public String toString() {
return "Account Number: " + accNo + " Account Holder Name: " + accHolder + " Account Balance: " + accBalance;
}
}
class savingsAccount extends BankAccount {
double rateOfInterest;
savingsAccount() {
super();
rateOfInterest = 0.0;
}
savingsAccount(int accNo, String accHolder, double accBalance, double rateOfInterest) {
super(accNo, accHolder, accBalance);
this.rateOfInterest = rateOfInterest;
}
public String toString() {
return "Account Number: " + accNo + " Account Holder Name: " + accHolder + " Account Balance: " + accBalance
+ " Rate of Interest: " + rateOfInterest;
}
public void getYearlyInterest() {
System.out.println("Yearly Interest: " + (accBalance * rateOfInterest * 12) / 100);
}
public void getComputedInterest(int years) {
double CI = (accBalance * Math.pow((1 / rateOfInterest), years));
System.out.println("Compunded Interest: " + CI);
}
}
class currentAccount extends BankAccount {
double avgDailyTransactions;
currentAccount() {
avgDailyTransactions = 0.0;
}
currentAccount(double avgDailyTransactions) {
this.avgDailyTransactions = avgDailyTransactions;
}
public String toString() {
return "Account Number: " + accNo + " Account Holder Name: " + accHolder + " Account Balance: " + accBalance
+ "Avg. Daily Transactions: " + avgDailyTransactions;
}
}
public class Annonymous {
public static void main(String[] args) {
BankAccount b = new BankAccount(100, "Vignesh Vishwa", 10000.0);
System.out.println(b);
savingsAccount s = new savingsAccount(100, "Vignesh Vishwa", 10000.0, 1.5);
s.getYearlyInterest();
s.getComputedInterest(2);
System.out.println(s);
}
} |
Beta Was this translation helpful? Give feedback.
-
import java.util.Scanner;
class BankAccount {
static int count = 1;
int accNo;
private double accBalance;
private String accHolderName;
public BankAccount(String accHolderName) {
this.accNo = count;
count++;
this.accHolderName = accHolderName;
}
public BankAccount(String accHolderName, double accBalance) {
this(accHolderName);
this.accBalance = accBalance;
}
public BankAccount(BankAccount account) {
this.accNo = account.accNo;
this.accHolderName = account.accHolderName;
this.accBalance = account.accBalance;
}
public int getAccNo() {
return accNo;
}
public double getAccBalance() {
return accBalance;
}
public void setAccBalance(double accBalance) {
this.accBalance = accBalance;
}
public String getAccHolderName() {
return accHolderName;
}
public void setAccHolderName(String accHolderName) {
this.accHolderName = accHolderName;
}
@Override
public String toString() {
return "accNo : " + accNo + "\naccHolderName : " + accHolderName +
"\naccBalance : " + accBalance + "\n";
}
public static BankAccount copy(BankAccount acc) {
return new BankAccount(acc);
}
}
class SavingAccount extends BankAccount {
static double rateOfIntrest;
public SavingAccount(String accHolderName) {
super(accHolderName);
this.setAccHolderName(accHolderName);
}
public SavingAccount(String accHolderName, double accBalance) {
super(accHolderName, accBalance);
}
public double getRateOfIntrest() {
return rateOfIntrest;
}
public static void setRateOfIntrest(double roi) {
rateOfIntrest = roi;
}
@Override
public String toString() {
return "Savings Account :-\n" + super.toString();
}
double getYearlyInterest() {
return (this.getAccBalance() * rateOfIntrest / 100);
}
double getComputedInterest(int years) {
return this.getYearlyInterest() * years;
}
}
class CurrentAccount extends BankAccount {
double avgDailyTransaction;
public CurrentAccount(String accHolderName) {
super(accHolderName);
}
public CurrentAccount(String accHolderName, double accBalance) {
super(accHolderName, accBalance);
}
public CurrentAccount(double avgDailyTransaction, String accHolderName) {
super(accHolderName);
this.avgDailyTransaction = avgDailyTransaction;
}
public CurrentAccount(double avgDailyTransaction, String accHolderName,
double accBalance) {
super(accHolderName, accBalance);
this.avgDailyTransaction = avgDailyTransaction;
}
public double getAvgDailyTransaction() {
return avgDailyTransaction;
}
public void setAvgDailyTransaction(double avgDailyTransaction) {
this.avgDailyTransaction = avgDailyTransaction;
}
@Override
public String toString() {
return "Current Account :-\n" + super.toString() + "avgDailyTransaction : " +
this.avgDailyTransaction + "\n";
}
double getTotalTransactionAmount(int days) {
return avgDailyTransaction * days;
}
double getYearlyTransaction() {
return getTotalTransactionAmount(365);
}
}
public class Bank {
public static void classesDemos() {
BankAccount generalAcc = new BankAccount("Kaif");
System.out.println(generalAcc);
SavingAccount savingsAcc = new SavingAccount("Yaari");
System.out.println(savingsAcc);
SavingAccount.setRateOfIntrest(3.4);
savingsAcc.setAccBalance(20000);
System.out.println("getYearlyIntrest : " + savingsAcc.getYearlyInterest());
System.out.println("getComputedInterest of 3 years : " +
savingsAcc.getComputedInterest(3) + "\n");
CurrentAccount currentAcc = new CurrentAccount(3, "Bhaavs", 1200.50);
currentAcc.setAvgDailyTransaction(400);
System.out.println(currentAcc);
System.out.println("getYearlyTransaction : " +
currentAcc.getYearlyTransaction());
System.out.println("getTotalTransactionAmount of 10 days : " +
currentAcc.getTotalTransactionAmount(10) + "\n");
}
public static BankAccount salaryAccount(String name) {
return new BankAccount(name) {
double salary = 50000;
double pfAmount = 6000;
double incomeTaxRate = 5.5;
double getYearlyTax() {
return salary * incomeTaxRate * 12 / 100;
}
double getInHandSalary() {
return (salary - getYearlyTax() / 12 - pfAmount);
}
@Override
public String toString() {
return super.toString() + "salary : " + this.salary + ", pfAmount : " +
this.pfAmount + "\n Yearly Tax : " + getYearlyTax() + "\nIn Hand Salary(Monthly) : "
+ getInHandSalary() + "\n";
}
};
}
public static void main(String[] args) {
classesDemos();
Scanner scanner = new Scanner(System.in);
System.out.print("Enter no. of Accounts you wanna Create : ");
int n = scanner.nextInt();
SavingAccount[] savingsArray = new SavingAccount[n];
CurrentAccount[] currentArray = new CurrentAccount[n];
BankAccount[] salaryArray = new BankAccount[n];
for (int i = 0; i < n; i++) {
savingsArray[i] = new SavingAccount("saving " + (i + 1), 20000.0 * i);
currentArray[i] = new CurrentAccount("current " + (i + 1));
salaryArray[i] = salaryAccount("Salary " + (i + 1));
System.out.println(savingsArray[i]);
System.out.println(currentArray[i]);
System.out.println(salaryArray[i]);
}
BankAccount copySavingAccount = new BankAccount(savingsArray[0]);
System.out.println("Copy Account -\n" + copySavingAccount);
BankAccount copyCurrentAccount = BankAccount.copy(currentArray[0]);
System.out.println("Copy Account -\n" + copyCurrentAccount);
scanner.close();
}
} |
Beta Was this translation helpful? Give feedback.
-
import java.util.Scanner;
class BankAccount {
int accNo;
String accHolderName;
int accBalance;
public BankAccount(int accNo, String accHolderName, int accBalance) {
this.accNo = accNo;
this.accHolderName = accHolderName;
this.accBalance = accBalance;
}
public int getAccNo() {
return accNo;
}
public void setAccNo(int accNo) {
this.accNo = accNo;
}
public String getAccHolderName() {
return accHolderName;
}
public void setAccHolderName(String accHolderName) {
this.accHolderName = accHolderName;
}
public int getAccBalance() {
return accBalance;
}
public void setAccBalance(int accBalance) {
this.accBalance = accBalance;
}
public void MoreFeatures() {
}
public BankAccount copy(BankAccount obj) {
return new BankAccount(this.accNo, this.accHolderName, this.accBalance);
}
@Override
public String toString() {
return "BankAccount [accBalance=" + accBalance + ", accHolderName=" + accHolderName + ", accNo=" + accNo + "]";
}
}
class SavingsAccount extends BankAccount {
double rateOfInterest;
public SavingsAccount(double rateOfInterest, int accBalance, String accHolderName, int accNo) {
super(accBalance, accHolderName, accNo);
this.rateOfInterest = rateOfInterest;
}
public SavingsAccount(int accNo, String accHolderName, int accBalance) {
super(accNo, accHolderName, accBalance);
}
public double getRateOfInterest() {
return rateOfInterest;
}
public void setRateOfInterest(double rateOfInterest) {
this.rateOfInterest = rateOfInterest;
}
public double getComputedInterest(int year) {
return ((accBalance * rateOfInterest * year) / 100);
}
public double getYearlyInterest() {
return this.getComputedInterest(1);
}
@Override
public String toString() {
return "SavingsAccount [Yearly interest = " + getYearlyInterest() + ", Computed interest: "
+ getComputedInterest(5) + "]";
}
}
class CurrentAccount extends BankAccount {
int avgDailyTransaction;
public CurrentAccount(int avgDailyTransaction, int accBalance, String accHolderName, int accNo) {
super(accNo, accHolderName, accNo);
this.avgDailyTransaction = avgDailyTransaction;
}
public CurrentAccount(int accNo, String accHolderName, int accBalance) {
super(accNo, accHolderName, accBalance);
}
public int getAvgDailyTransaction() {
return avgDailyTransaction;
}
public void setAvgDailyTransaction(int avgDailyTransaction) {
this.avgDailyTransaction = avgDailyTransaction;
}
public int getYearlyTransaction() {
return getTotalTransactionAmount(365);
}
public int getTotalTransactionAmount(int days) {
return avgDailyTransaction * days;
}
@Override
public String toString() {
System.out.println("CurrentAccount: ");
return super.toString() + String.format("%nCurrentAccount: Avg Daily Transaction= %d", avgDailyTransaction);
}
}
public class DriverClass {
public static BankAccount salaryAccount(int accNo, String accHolderName, int accBalance) {
return new BankAccount(accNo, accHolderName, accBalance) {
int salary = 25000;
int pfAmount = 100;
double incomeTaxRate = 4;
public double getYearlyTax() {
return salary * 12 * incomeTaxRate / 100;
}
public double getInHandSalary() {
return (salary * 12) - getYearlyTax() - (pfAmount * 12);
}
@Override
public void MoreFeatures() {
System.out.println("Yearly tax are: " + this.getYearlyTax());
System.out.println("Inhand Salary are: " + this.getInHandSalary());
}
@Override
public String toString() {
System.out.print("SalaryAccount: ");
return super.toString() + String.format("%nSalary: %d%n pfAmount: %d%n incomeTaxRate: %f%n", salary,
pfAmount, incomeTaxRate);
}
};
}
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.println("Enter number of objects to create: ");
int n = s.nextInt();
BankAccount[] BankAccountArr = new BankAccount[n];
SavingsAccount[] savingsAccountArr = new SavingsAccount[n];
CurrentAccount[] currentAccountArr = new CurrentAccount[n];
BankAccount[] salaryAccountArr = new BankAccount[n];
for (int i = 0; i < n; i++) {
BankAccountArr[i] = new BankAccount(i + 1, "GeneralAccount", (i+1));
savingsAccountArr[i] = new SavingsAccount(i + 1, "SavingsAccount", (i+1));
currentAccountArr[i] = new CurrentAccount(i + 1, "CurrentAccount", (i+1));
salaryAccountArr[i] = salaryAccount(i + 1, "GeneralAccount", (i+1));
}
for (int i = 0; i < n; i++) {
System.out.println(BankAccountArr[i]);
System.out.println(savingsAccountArr[i]);
System.out.println(currentAccountArr[i]);
System.out.println(salaryAccountArr[i]);
}
}
}
|
Beta Was this translation helpful? Give feedback.
-
TASK-DYNAMIC POLYMORPHISM AND INNER CLASSAccountInheritance.javaimport java.util.Scanner;
abstract class SalaryAccount
{
double salary;
double pfdeduction;
double incomeTaxRate;
abstract double getYearlyTax();
abstract double getInHandSalary();
public SalaryAccount(double salary, double pfdeduction, double incomeTaxRate) {
super();
this.salary = salary;
this.pfdeduction = pfdeduction;
this.incomeTaxRate = incomeTaxRate;
}
}
class BankAccount
{
private String AcHolderName;
private int AcNo;
private double balance;
public BankAccount(String acHolderName, int acNo, double balance) {
super();
AcHolderName = acHolderName;
AcNo = acNo;
this.balance = balance;
}
public String getAcHolderName() {
return AcHolderName;
}
public void setAcHolderName(String acHolderName) {
AcHolderName = acHolderName;
}
public int getAcNo() {
return AcNo;
}
public void setAcNo(int acNo) {
AcNo = acNo;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
@Override
public String toString() {
return "\nAccountHolderName =" + AcHolderName + "\nAccount No =" + AcNo + "\nBalance =" + balance ;
}
}
class SavingAccount extends BankAccount
{
double rate=0;
double year;
double yearlyInterest;
double computedInterest;
public double getRate() {
return rate;
}
public void setRate(double rate) {
this.rate = rate;
}
public double getYear() {
return year;
}
public void setYear(double year) {
this.year = year;
}
public SavingAccount(String acHolderName, int acNo, double balance,double rate,int year) {
super(acHolderName, acNo, balance);
this.rate=rate;
this.year=year;
}
public double getYearlyRateOfInterest()
{
yearlyInterest=(getBalance() * year * rate)/100;
return yearlyInterest;
}
public double getComputedInterest(int years)
{
computedInterest=(getBalance() * Math.pow((1/rate),years));
return computedInterest;
}
@Override
public String toString() {
return "\nAcHolderName = "+ getAcHolderName() + "\nAcNo = " + getAcNo() + "\nbalance = " + getBalance() + "\nYearly Interest =" + getYearlyRateOfInterest()
+ "\ncomputed Interest =" + getComputedInterest((int) year) ;
}
}
class CurrentAccount extends BankAccount
{
double avgDailyTransaction;
int year;
int days;
double TotalTransaction;
public CurrentAccount(String acHolderName, int acNo, double balance,int year,double avgDailyTransaction,int days) {
super(acHolderName, acNo, balance);
this.days=days;
this.year=year;
this.avgDailyTransaction=avgDailyTransaction;
}
public double getAvgDailyTransaction() {
return avgDailyTransaction;
}
public void setAvgDailyTransaction(double avgDailyTransaction) {
this.avgDailyTransaction = avgDailyTransaction;
}
public double getYearlyTransaction()
{
return avgDailyTransaction*year;
}
public double getTotalTransactionAmount(int days)
{
TotalTransaction=avgDailyTransaction*days;
return TotalTransaction;
}
@Override
public String toString() {
return "\nAcHolderName= "+ getAcHolderName() + ",\nAccount No= " + getAcNo() + "\nbalance = " + getBalance() + "\nAverageDailyTransaction = " + getAvgDailyTransaction() +"\nyearly transaction amount "+getYearlyTransaction()+"\nTotal Transaction Amount :" + getTotalTransactionAmount(days);
}
}
public class AccountInheritance {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter account number :");
int acnum=sc.nextInt();
System.out.println("Enter account holder name :");
String name=sc.next();
System.out.println("Enter account balance :");
double bal=sc.nextDouble();
System.out.println("Enter no of year :");
int year=sc.nextInt();
System.out.println("Enter rate of interst:");
double rate=sc.nextDouble();
System.out.println("Enter daily average transaction amount :");
double avgtransaction=sc.nextDouble();
int days=year*365;
System.out.println("Enter no of account you want to create");
int n=sc.nextInt();
System.out.println("Entering Salary account details :");
System.out.println("Enter the salary :");
double salary=sc.nextDouble();
System.out.println("Enter pf deducation amount :");
double pfdeduction=sc.nextDouble();
System.out.println("Enter income tax rate :");
double incomeTaxRate=sc.nextDouble();
BankAccount generalAcc=new BankAccount(name,acnum,bal);
SavingAccount savingsAcc=new SavingAccount(name,acnum,bal,rate,year);
CurrentAccount currentAcc=new CurrentAccount(name,acnum,bal,year,avgtransaction,days);
System.out.println("BANK ACCOUNT DETAILS");
System.out.println(generalAcc);
System.out.println("-------------------------------");
System.out.println("SAVING ACCOUNT DETAILS");
System.out.println(savingsAcc);
System.out.println("-------------------------------");
System.out.println("CURRENT ACCOUNT DETAILS");
System.out.println(currentAcc);
System.out.println("-------------------------------");
SalaryAccount salaryAcc=new SalaryAccount(salary,pfdeduction,incomeTaxRate) {
public double getYearlyTax()
{
return (salary*incomeTaxRate*12)/100;
}
@Override
double getInHandSalary() {
return (salary - getYearlyTax()/12-pfdeduction);
}
};
System.out.println("Salary Account Details");
System.out.println("---------------------------------------");
System.out.println("\nSalary = "+ salary+"\npf_Deduction amount = "+pfdeduction+"\nIncome tax Rate = "+incomeTaxRate+"\nTotal Salary = "+ salaryAcc.getInHandSalary());
System.out.println("---------------------------------------");
BankAccount ob[]=new BankAccount[n];
SavingAccount ob1[]=new SavingAccount[n];
CurrentAccount ob2[]=new CurrentAccount[n];
for(int i=0;i<n;i++)
{
System.out.println("Bank Account clone objects....."+i);
ob[i]=new BankAccount(name,acnum,bal);
System.out.println(ob[i]);
System.out.println();
}
for(int i=0;i<n;i++)
{
System.out.println("SavingAccount clone objects....."+i);
ob1[i]=new SavingAccount(name,acnum,bal,rate,year);
System.out.println(ob1[i]);
System.out.println();
}
for(int i=0;i<n;i++)
{
System.out.println("Current Account clone objects....."+i);
ob2[i]=new CurrentAccount(name,acnum,bal,year,avgtransaction,days);
System.out.println(ob2[i]);
System.out.println();
}
sc.close();
}
}
Output |
Beta Was this translation helpful? Give feedback.
-
part-1:class BankAccount{
long accountNumber;
String accountHolderName;
double accountBalance;
public long getAccountNumber() {
return accountNumber;
}
public void setAccountNumber(long accountNumber) {
this.accountNumber = accountNumber;
}
public String getAccountHolderName() {
return accountHolderName;
}
public void setAccountHolderName(String accountHolderName) {
this.accountHolderName = accountHolderName;
}
public double getAccountBalance() {
return accountBalance;
}
public void setAccountBalance(double accountBalance) {
this.accountBalance = accountBalance;
}
}
class SavingsAccount extends BankAccount{
double rateOfInterest;
public double getRateOfInterest() {
return rateOfInterest;
}
public void setRateOfInterest(double rateOfInterest) {
this.rateOfInterest = rateOfInterest;
}
public double getComputedInterest(int years){
return (accountBalance*years*rateOfInterest)/100;
}
public double getYearlyInterest(){
return getComputedInterest(1);
}
@Override
public String toString(){
return ("AccountNumber="+ accountNumber+"\n AccountHolder="+ accountHolderName +"\n AccountBalance="+ accountBalance+"\n rateOfInterest="+ rateOfInterest);
}
}
class CurrentAccount extends BankAccount{
double averageDailyTransactions;
public double CurrentAccount(double averageDailyTransactions){
return averageDailyTransactions=0.0;
}
public double getAverageDailyTransactions() {
return averageDailyTransactions;
}
public void setAverageDailyTransactions(double averageDailyTransactions) {
this.averageDailyTransactions = averageDailyTransactions;
}
public double getTotalTransactionAmount(int days){
return days*averageDailyTransactions;
}
public double getYearlyTransaction(){
return getTotalTransactionAmount(100);
}
@Override
public String toString(){
return ("AccountNumber="+ accountNumber+"\n AccountHolder="+ accountHolderName +"\n AccountBalance="+ accountBalance+"\n averageDailyTransaction="+ averageDailyTransactions);
}
}
public class Account {
public static void main(String[] args){
SavingsAccount account = new SavingsAccount();
System.out.println("Savings Account");
account.setAccountBalance(10000);
account.setAccountNumber(76478238888888L);
account.setAccountHolderName("tamil");
account.setRateOfInterest(5);
System.out.println("ComputedInterest of savings account is:"+account.getComputedInterest(2));
System.out.println("YearlyInterest of savings account is:"+account.getYearlyInterest());
System.out.println("Account Details of savings account is:"+account.toString());
System.out.println();
CurrentAccount account2 = new CurrentAccount();
System.out.println("Current ACcount");
account2.setAccountBalance(20000);
account2.setAccountNumber(764782388667688L);
account2.setAccountHolderName("saratha");
account2.setAverageDailyTransactions(10);
System.out.println("Total transaction of Current account is:"+account2.getTotalTransactionAmount(30));
System.out.println("YearlyTransaction of of Current account is:"+account2.getYearlyTransaction());
System.out.println("Account Details of savings account is:"+account2.toString());
System.out.println();
System.out.println("SalaryAccount");
SalaryAccount salaryAcc=new SalaryAccount(){
int salary=30000;
int pfDeduction=3600;
int incomeTaxRate=5000;
public double getYearlyTax(){
return 12*incomeTaxRate;
}
public double getYearlyInHandSalary(){
return 12*(salary-(pfDeduction+incomeTaxRate));
}
};
System.out.println("YearlyInhandSalary of SalaryAccount is:"+ salaryAcc.getYearlyInHandSalary());
System.out.println("Yearly Tax of SalaryAccount is:"+ salaryAcc.getYearlyTax());
}
}Output: |
Beta Was this translation helpful? Give feedback.
-
Dynamic Polymorphism and Anonymous inner classimport java.util.Scanner;
abstract class salaryaccount{ //salary account
double salary = 0;
double pfdeduction=0;
double incometaxrate;
abstract double getYearlyTax();
abstract double getyearlyhandsalary();
public salaryaccount(double salary, double pfdeduction, double incometaxrate) {
this.salary = salary;
this.pfdeduction = pfdeduction;
this.incometaxrate = incometaxrate;
}
@Override
public String toString() {
return "salaryaccount [salary=" + salary + ", pfdeduction=" + pfdeduction + ", incometaxrate=" + incometaxrate
+ ", getYearlyTax()=" + getYearlyTax() + ", getyearlyhandsalary()=" + getyearlyhandsalary() + "]";
}
}
class bankaccount{ //Bank Account
long accNo;
String accHolderName;
double balance;
bankaccount(long accNo, String accHolderName, double balance){
super();
this.accNo=accNo;
this.accHolderName=accHolderName;
this.balance=balance;}
@Override
public String toString() {
return "bankaccount [accNo=" + accNo + ", accHolderName=" + accHolderName + ", balance=" + balance + "]";
}
}
class savingsaccount extends bankaccount{ //savings account
double rateofinterest;
double year;
double yearlyinterest;
double compoundinterest;
savingsaccount(long accNo, String accHolderName, double accBalance,double rateofinterest,int year){
super(accNo, accHolderName, accBalance);
this.rateofinterest=rateofinterest;
this.year=year;
}
public double getyearlyinterest(){
return yearlyinterest=(balance*year*rateofinterest)/100;
}
public double getcompundInterest(int years){
return compoundinterest=(balance*Math.pow(1/rateofinterest,year))/100;
}
@Override
public String toString() {
return "savingsaccount [rateofinterest=" + rateofinterest + ", year=" + year + ", yearlyinterest="
+ getyearlyinterest() + ", compoundinterest=" + getcompundInterest((int)year) + "]";
}
}
class currentaccount extends bankaccount{ //current Account
double dailytransaction;
int year;
int days;
double totaltransaction;
public currentaccount(long accNo, String accHolderName, double balance, double dailytransaction,int days,int year){
super(accNo, accHolderName, balance);
this.dailytransaction = dailytransaction;
this.days=days;
this.year=year;
}
public double getYearlyTransaction(){
double yearlytransaction = dailytransaction*year;
return yearlytransaction;
}
public double gettotaltranscation(int days){
return totaltransaction=dailytransaction*days;
}
@Override
public String toString() {
return "currentaccount [dailytransaction=" + dailytransaction + ", days=" + days + ", totaltransaction="
+ gettotaltranscation(days) + ", year=" + year + "]";
}
}
public class Banking { // driver class
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
System.out.println("enter the account number");
long accNo=sc.nextLong();
System.out.println("enter the account holder name");
String accHolderName=sc.next();
System.out.println("enter the account balance");
double balance=sc.nextDouble();
System.out.println("enter the rate of interest");
double rateofinterest=sc.nextDouble();
System.out.println("enter the year");
int year=sc.nextInt();
System.out.println("enter the daily average transcation");
double dailytransaction=sc.nextDouble();
int days=year*365;
System.out.println("enter the number accounts to create");
int noofobjects=sc.nextInt();
System.out.println("salary account details..");
System.out.println("enter the salary ");
double salary=sc.nextDouble();
System.out.println("enter the pfdeduction");
double pfdeduction=sc.nextDouble();
System.out.println("enter the income tax rate ");
double incometaxrate=sc.nextDouble();
bankaccount generalAcc=new bankaccount(accNo,accHolderName,balance);
savingsaccount savingsAcc=new savingsaccount(accNo,accHolderName,balance,rateofinterest,year);
currentaccount currentAcc=new currentaccount( accNo, accHolderName,balance, dailytransaction, days,year);
System.out.println("bank account details...");
System.out.println(generalAcc);
System.out.println("savings account details.. ");
System.out.println(savingsAcc);
System.out.println("current account details..");
System.out.println(currentAcc);
salaryaccount salaryAcc=new salaryaccount(salary,pfdeduction,incometaxrate) { //anonymous inner class
@Override
double getYearlyTax() {
return (salary*incometaxrate*12)/100;
}
@Override
double getyearlyhandsalary() {
return (salary-getYearlyTax()/12-pfdeduction);
}
};
System.out.println("Salary account details... ");
System.out.println(salaryAcc);
bankaccount[] bankacc = new bankaccount[noofobjects]; // creating n objects of the account type
savingsaccount[] savingsAc = new savingsaccount[noofobjects];
currentaccount[] currentAc = new currentaccount[noofobjects];
for (int i = 0; i < noofobjects; i++) { // clone objects
System.out.println("clone objects..."+i);
bankacc[i]=new bankaccount(accNo,accHolderName,balance);
System.out.println(bankacc[i]);
savingsAc[i]=new savingsaccount(accNo,accHolderName,balance,rateofinterest,year);
System.out.println(savingsAc[i]);
currentAc[i]=new currentaccount(accNo, accHolderName,balance, dailytransaction, days,year);
System.out.println(currentAc[i]);
}
}}output: |
Beta Was this translation helpful? Give feedback.
-
1/* */ import java.util.*;
class BankAccount{
int accNo;
String accHolderName;
double accBalance;
public BankAccount(){
this.accNo = 0;
this.accBalance = 0.0;
this.accHolderName = "";
}
public BankAccount(int accNo, String accName, double accBalance){
this.accNo = accNo;
this.accHolderName = accName;
this.accBalance = accBalance;
}
public int getAccNo() {
return accNo;
}
public String getAccHolderName() {
return accHolderName;
}
public double getAccBalance() {
return accBalance;
}
public void setAccNo(int accNo) {
this.accNo = accNo;
}
public void setAccHolderName(String accHolderName) {
this.accHolderName = accHolderName;
}
public void setAccBalance(double accBalance) {
this.accBalance = accBalance;
}
@Override
public String toString() {
return "BankAccount [accBalance=" + accBalance + ", accHolderName=" + accHolderName + ", accNo=" + accNo + "]";
}
}
class SavingsAccount extends BankAccount{
// rateOfInterest, getYearlyInterest(), getComputedInterest(years)
double rateOfInterest;
SavingsAccount(){
super();
rateOfInterest = 0.0;
}
SavingsAccount(int accNo, String accHolder, double accBalance , double rateOfInterest){
super(accNo, accHolder, accBalance);
this.rateOfInterest = rateOfInterest;
}
public double getComputedInterest(int yr){
double CI = super.accBalance * (Math.pow((1 + this.rateOfInterest / 100), yr)) ;
return CI;
}
public double getYearlyInterest(){
double yi = (accBalance * rateOfInterest * 1) / 100;
return yi;
}
@Override
public String toString() {
return "SavingsAccount [ current account " + super.toString() + " rateOfInterest=" + rateOfInterest + "]";
}
}
class CurrentAccount extends BankAccount{
double avgDailyTransaction;
public CurrentAccount(){
super();
avgDailyTransaction = 0;
}
public CurrentAccount(int accNo, String accName, double accBalance, double avgDailyTransaction){
super(accNo, accName, accBalance);
this.avgDailyTransaction = avgDailyTransaction;
}
public double getYearlyTransaction(){
return getTotalTransactionAmount(365);
}
public double getTotalTransactionAmount(int days){
return avgDailyTransaction * days;
}
@Override
public String toString() {
return "Current Account :-\n" + super.toString() + "avgDailyTransaction : " +
this.avgDailyTransaction + "\n";
}
}
public class DyPol14{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
BankAccount generalAcc = new BankAccount(12345, "Normal Account", 1500);
System.out.println("*****general account*******");
System.out.println("name "+ generalAcc.getAccHolderName());
System.out.println("Balance " + generalAcc.getAccBalance());
System.out.println("account num " + generalAcc.getAccNo());
SavingsAccount saving = new SavingsAccount(43435, "savingAccount", 5000, 5);
System.out.println("******saving account*******");
System.out.println("Balance " +saving.getAccBalance());
System.out.println("name "+saving.getAccHolderName());
System.out.println("get yearly interest " + saving.getYearlyInterest());
System.out.println("Enter the year to get Computed Interest");
int yr = sc.nextInt();
System.out.println("compund Interest -> " + saving.getComputedInterest(yr));
System.out.println("*********current account**********");
CurrentAccount currentAcc = new CurrentAccount(45454, "Current Account", 6000, 300);
System.out.println("Balance " +currentAcc.getAccBalance());
System.out.println("name "+currentAcc.getAccHolderName());
System.out.println("account num " +currentAcc.getAccNo());
System.out.println("Enter the num of days for total transaction");
int days = sc.nextInt();
System.out.println("get total amt " + currentAcc.getTotalTransactionAmount(days));
System.out.println("get total transaction " + currentAcc.getTotalTransactionAmount(days));
//anonymous class creation
BankAccount salaryAcc = new BankAccount(54321, "salary Account", 5000){
double salary = 20000;
double pfDeduction = 1000;
double incomeTaxRate = 8.5;
double getYearlyTax() {
return (salary * 12.0) * incomeTaxRate / 100;
}
double getYearlyInHandSalary(){
double tax = salary * incomeTaxRate / 100;
return (salary - tax - pfDeduction);
}
@Override
public String toString(){
return String.format("salary %f %n Income tax rate %f %n yearly tax %f %n in hand salary %f" , salary,incomeTaxRate, getYearlyTax(), getYearlyInHandSalary());
}
};
System.out.println("**anonymous class *****************");
System.out.println(salaryAcc);
}
} |
Beta Was this translation helpful? Give feedback.
-
import java.util.Scanner;
class BankAccount{
long accNo;
String accHolderName;
double accBalance;
public BankAccount(long accNo,String accHolderName, double accBalance) {
super();
this.accNo = accNo;
this.accHolderName = accHolderName;
this.accBalance = accBalance;
}
public long getAccNo() {
return accNo;
}
public void setAccNo(long accNo) {
this.accNo = accNo;
}
public String getAccHolderName() {
return accHolderName;
}
public void setAccHolderName(String accHolderName) {
this.accHolderName = accHolderName;
}
public double getAccBalance() {
return accBalance;
}
public void setAccBalance(double accBalance) {
this.accBalance = accBalance;
}
@Override
public String toString() {
return "BankAccount [accBalance=" + accBalance + ", accHolderName=" + accHolderName + ", accNo=" + accNo + "]";
}
BankAccount copyFunction(BankAccount obj){
return new BankAccount(obj.getAccNo(), obj.getAccHolderName(), obj.getAccBalance());
}
}
class SavingAccount extends BankAccount{
private static double rateOfInterest = 0;
double rateOfinterest;
public SavingAccount(long accNo,String accHolderName, double accBalance,double rateOfinterest){
super(accNo, accHolderName, accBalance);
this.rateOfinterest = rateOfinterest;
}
public double getRateOfInterest() {
return rateOfInterest;
}
public void setRateOfInterest(double rateOfInterest) {
this.rateOfinterest = rateOfInterest;
}
double getYearlyInterst(){
return(getAccBalance()*rateOfinterest*1)/100;
}
double getComputedInterest(int year){
return(getAccBalance()*rateOfinterest*year)/100;
}
@Override
public String toString() {
return "SavingAccount [rateOfinterest=" + rateOfinterest + "]";
}
}
class CurrentAccount extends SavingAccount{
double avgDailyTransaction;
public CurrentAccount(long accNo, String accHolderName, double accBalance, double avgDailyTransaction){
super(accNo, accHolderName, accBalance, avgDailyTransaction);
this.avgDailyTransaction = avgDailyTransaction;
}
public double getAvgDailyTransaction() {
return avgDailyTransaction;
}
public void setAvgDailyTranaction(double avgDailyTransaction){
this.avgDailyTransaction = avgDailyTransaction;
}
double getYearlyTranaction() {
return avgDailyTransaction*365;
}
double gettotalTransactionAmount(int days){
return avgDailyTransaction*days;
}
@Override
public String toString() {
return "CurrentAccount [avgDailyTransaction=" + avgDailyTransaction + "]";
}
}
public class Discussion14 {
public static void main(String[] args) {
BankAccount generalAcc = new BankAccount(3425,"Komal",10000);
SavingAccount savingAcc = new SavingAccount(3425,"Komal",10000,10);
CurrentAccount currentAcc = new CurrentAccount(3425,"Komal",10000,2000);
System.out.println(generalAcc);
System.out.println(savingAcc);
System.out.println(currentAcc);
BankAccount salaryAcc = new BankAccount(3425,"Komal",10000){
double monthlySalary=60000,
monthlyPfDeduction = 3000;
double incomeTaxRate=3.5;
double getYearlyTax(){
return (monthlySalary*incomeTaxRate*12/100);
}
double getYearlyInHandSalary(){
return (monthlySalary*12-getYearlyTax()-monthlyPfDeduction * 12);
}
@Override
public String toString() {
return (super.toString() + "Yearly Tax="+getYearlyTax() + ",In hand Salary="+getYearlyInHandSalary()) ;
}
};
System.out.println(salaryAcc.toString());
Scanner sc= new Scanner(System.in);
System.out.println("Enter the number of object:");
int n=sc.nextInt();
BankAccount[] accounts = new BankAccount[n];
for(int i=0; i<n; i++) {
System.out.println("Enter account number");
int acNo =sc.nextInt();
System.out.println("Enter name:");
String name = sc.next();
System.out.println("Enter balance");
double balance = sc.nextDouble();
accounts[i] = new BankAccount(acNo,name,balance);
}
System.out.println("Display details");
for(int i=0; i<n;i++){
System.out.println(accounts[i].toString());
}
BankAccount b = generalAcc.copyFunction(generalAcc);
System.out.println(b);
}
} |
Beta Was this translation helpful? Give feedback.
-
import java.util.Scanner;
class BankAccount{
long accNo;
String accHolderName;
double accBalance;
public BankAccount(long accNo,String accHolderName, double accBalance) {
super();
this.accNo = accNo;
this.accHolderName = accHolderName;
this.accBalance = accBalance;
}
public long getAccNo() {
return accNo;
}
public void setAccNo(long accNo) {
this.accNo = accNo;
}
public String getAccHolderName() {
return accHolderName;
}
public void setAccHolderName(String accHolderName) {
this.accHolderName = accHolderName;
}
public double getAccBalance() {
return accBalance;
}
public void setAccBalance(double accBalance) {
this.accBalance = accBalance;
}
@Override
public String toString() {
return "BankAccount [accBalance=" + accBalance + ", accHolderName=" + accHolderName + ", accNo=" + accNo + "]";
}
BankAccount copyFunction(BankAccount obj){
return new BankAccount(obj.getAccNo(), obj.getAccHolderName(), obj.getAccBalance());
}
}
class SavingAccount extends BankAccount{
private static double rateOfInterest = 0;
double rateOfinterest;
public SavingAccount(long accNo,String accHolderName, double accBalance,double rateOfinterest){
super(accNo, accHolderName, accBalance);
this.rateOfinterest = rateOfinterest;
}
public double getRateOfInterest() {
return rateOfInterest;
}
public void setRateOfInterest(double rateOfInterest) {
this.rateOfinterest = rateOfInterest;
}
double getYearlyInterst(){
return(getAccBalance()*rateOfinterest*1)/100;
}
double getComputedInterest(int year){
return(getAccBalance()*rateOfinterest*year)/100;
}
@Override
public String toString() {
return "SavingAccount [rateOfinterest=" + rateOfinterest + "]";
}
}
class CurrentAccount extends SavingAccount{
double avgDailyTransaction;
public CurrentAccount(long accNo, String accHolderName, double accBalance, double avgDailyTransaction){
super(accNo, accHolderName, accBalance, avgDailyTransaction);
this.avgDailyTransaction = avgDailyTransaction;
}
public double getAvgDailyTransaction() {
return avgDailyTransaction;
}
public void setAvgDailyTranaction(double avgDailyTransaction){
this.avgDailyTransaction = avgDailyTransaction;
}
double getYearlyTranaction() {
return avgDailyTransaction*365;
}
double gettotalTransactionAmount(int days){
return avgDailyTransaction*days;
}
@Override
public String toString() {
return "CurrentAccount [avgDailyTransaction=" + avgDailyTransaction + "]";
}
}
public class Discussion14 {
public static void main(String[] args) {
BankAccount generalAcc = new BankAccount(3425,"Komal",10000);
SavingAccount savingAcc = new SavingAccount(3425,"Komal",10000,10);
CurrentAccount currentAcc = new CurrentAccount(3425,"Komal",10000,2000);
System.out.println(generalAcc);
System.out.println(savingAcc);
System.out.println(currentAcc);
BankAccount salaryAcc = new BankAccount(3425,"Komal",10000){
double monthlySalary=60000,
monthlyPfDeduction = 3000;
double incomeTaxRate=3.5;
double getYearlyTax(){
return (monthlySalary*incomeTaxRate*12/100);
}
double getYearlyInHandSalary(){
return (monthlySalary*12-getYearlyTax()-monthlyPfDeduction * 12);
}
@Override
public String toString() {
return (super.toString() + "Yearly Tax="+getYearlyTax() + ",In hand Salary="+getYearlyInHandSalary()) ;
}
};
System.out.println(salaryAcc.toString());
Scanner sc= new Scanner(System.in);
System.out.println("Enter the number of object:");
int n=sc.nextInt();
BankAccount[] accounts = new BankAccount[n];
for(int i=0; i<n; i++) {
System.out.println("Enter account number");
int acNo =sc.nextInt();
System.out.println("Enter name:");
String name = sc.next();
System.out.println("Enter balance");
double balance = sc.nextDouble();
accounts[i] = new BankAccount(acNo,name,balance);
}
System.out.println("Display details");
for(int i=0; i<n;i++){
System.out.println(accounts[i].toString());
}
BankAccount b = generalAcc.copyFunction(generalAcc);
System.out.println(b);
}
} |
Beta Was this translation helpful? Give feedback.





Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
PART - 1:
PART - 2:
Beta Was this translation helpful? Give feedback.
All reactions