Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I need help writing the main method for these codes below so i can run them.. Ac

ID: 3935788 • Letter: I

Question

I need help writing the main method for these codes below so i can run them..

Account.java

public class Account {

private double balance;
private int acctNum;

public Account (int num)
{
balance = 0.0;
acctNum = num;
}
public void deposit (double amt)
{
if (amt >0)
balance +=amt;
else
System.out.println("Account.deposit(...): "
+"cannot deposit negative amount.");
}
public void withdraw (double amt)
{
if (amt>0)
balance -=amt;
else
System.err.println("Account.withdraw(...): "
+"cannot withdraw negative amount.");

}
public double getBalance()
{
return balance;
}
public double getAccountNumber()
{
return acctNum;
}
public String toString()
{
return "Acc " + acctNum + ": " + "balance = "+ balance;   
}
public final void print()
{
System.out.println( toString());
}

}

______________________________

SavingsAccount.java

public class SavingsAccount extends Account {
private double interest;

public SavingsAccount(int acctNum, double interest) {
super(acctNum);
this.interest=interest;
}

public double getInterest() {
double x= getBalance() + getBalance()*interest;
return x;

// public void setInterest((interest))
// this.interest=interest;
}
public void AddInterest (double interest) {
double x = super.getBalance() * interest;
super.deposit(x);
}
public String toString() {
return super.toString()+" Interest : " + interest;
}
}

_________________________________

CheckingAccount.java
public class CheckingAccount extends Account {
private double limit;

public CheckingAccount(int acctNum, double limit) {
super(acctNum);
this.limit=limit;
}

public double getLimit() {
return this.limit;
}

public void setLimit(double limit) {
this.limit=limit;
}
public void withdraw (double limit) {
if (limit <= this.limit)
super.withdraw(limit);
else {
System.out.println(" Sorry, Limit Exceeded" );
}

}


public String toString() {
return super.toString() +"Limit : "+limit;
}
}

________________________________

Bank.java

public class Bank {

public static void main(String[] args) {
Account[] accounts ={new SavingsAccount(2, 0.25),new CheckingAccount(23, 50)};
for(int i=0; i<accounts.length;i++) {
accounts[i].deposit(50000);
  
}
SavingsAccount s=(SavingsAccount)accounts[0];

s.AddInterest(0.025);
  
System.out.println("Interest :"+s.getInterest());
CheckingAccount ca=(CheckingAccount)accounts[1];
  
System.out.println("____ Setting the limit 20000____");
ca.setLimit(20000);
  
System.out.println("____ Trying to Withdrawn amount 21000____");
ca.withdraw(21000);
  
System.out.println("____ Trying to Withdrawn amount 19000____");
ca.withdraw(19000);
System.out.println("Balance after withdrawn amount:"+ca.getBalance());
}
}

_

Explanation / Answer

Please follow the code and comments for the description :

CODE :

a) Account.java :

public class Account { // class to run the code

private double balance; // instance variables
private int acctNum;

public Account(int num) { // constructor
balance = 0.0;
acctNum = num;
}

public void deposit(double amt) { // method to deposit the amount to the account
if (amt > 0) {
balance += amt;
} else {
System.out.println("Account.deposit(...) : cannot deposit negative amount."); // message to the user
}
}

public void withdraw(double amt) { // method to withdraw the amount
if (amt > 0) {
balance -= amt;
} else {
System.err.println("Account.withdraw(...): cannot withdraw negative amount."); // message to the user
}
}

  
public void setBalance(double bal) { // getters and setters
this.balance = bal;
}
  
public double getBalance() {
return balance;
}

public double getAccountNumber() {
return acctNum;
}

@Override
public String toString() { // message method
return "Acc " + acctNum + ": " + "balance = " + balance;
}

public final void print() {// print the data
System.out.println(toString());
}
}


b) SavingsAccount.java :

public class SavingsAccount extends Account { // child class for the account class

private double interest; // instance variables

public SavingsAccount(int acctNum, double interest) { // constructor
super(acctNum);
this.interest = interest;
}

public double getInterest() { // method to get the interest value
double x = getBalance() + getBalance() * interest;
return x;
}

public void AddInterest(double interest) { // method to add the interest to the balance
double x = super.getBalance() * interest;
super.deposit(x);
}

@Override
public String toString() { // to string method
return super.toString() + " Interest : " + interest;
}
}


c) CheckingAccount.java :

public class CheckingAccount extends Account { // class that is the child of the account class

private double limit; // instance variables

public CheckingAccount(int acctNum, double limit) { // constructor
super(acctNum);
this.limit = limit;
}

public double getLimit() { // getters and setters for the amount value
return this.limit;
}

public void setLimit(double limit) {
this.limit = limit;
}

@Override
public void withdraw(double limit) { // method ot withdraw the amount from the account
if (limit <= this.limit) {
super.withdraw(limit);
} else {
System.out.println("Sorry, Limit Exceeded");
}
}

@Override
public String toString() { // to string method
return super.toString() + "Limit : " + limit;
}
}


d) Bank.java :

public class Bank { // class to run the code

public static void main(String[] args) { // driver method
Account[] accounts = {new SavingsAccount(2, 0.25), new CheckingAccount(23, 50)}; // array of accounts with the other class as objects
for (int i = 0; i < accounts.length; i++) {
accounts[i].deposit(50000); assigning the initial deposits
}
System.out.println("Successfully deposited the given amount."); // message to the user
SavingsAccount s = (SavingsAccount) accounts[0];
s.AddInterest(0.025); // add the interest to the account

System.out.println("Interest earned is : " + s.getInterest()); // get the total interest with the balance
double totalBal = s.getInterest();
  
CheckingAccount ca = (CheckingAccount) accounts[1]; // calling the object to the checking account class
System.out.println("____ Updating the account balance..____");
ca.setBalance(totalBal); // set the updated balance
  
System.out.println("____ Setting the limit to 20000..____");
ca.setLimit(20000); // set the limit to withdraw

System.out.println("____ Trying to Withdrawn the amount of 21000..____");
ca.withdraw(21000);

System.out.println("____ Trying to Withdrawn the amount of 19000..____");
ca.withdraw(19000);
System.out.println("Balance after the amount withdrawn : " + ca.getBalance()); // get the updated balance
}
}

OUTPUT :

Successfully deposited the given amount.
Interest earned is : 64062.5
____ Updating the account balance..____
____ Setting the limit to 20000..____
____ Trying to Withdrawn the amount of 21000..____
Sorry, Limit Exceeded
____ Trying to Withdrawn the amount of 19000..____
Balance after the amount withdrawn : 45062.5

Hope this is helpful.