Why does this code not execute? It gaves me Error: Could not find or load main c
ID: 3843009 • Letter: W
Question
Why does this code not execute? It gaves me Error: Could not find or load main class lastIndex. Someone please help and fix.
// Each BankAccount object represents one user's account
// information including name and balance of money.
public class BankAccount {
String name;
double balance;
//field to represent transactionFee, with default value 0.00
double transactionFee = 0.00;
//method that allows deposit of money
public void deposit(double amount) {
balance = balance + amount;
}
<><><><><> I CREATE ANOTHER CLASS TO TEST <><><><><><>
public class BankAccountTester {
public static void main(String[] args) {
BankAccount account = new BankAccount();
//set transaction fee
account.setTransactionFee(2);
//deposit amount to account
account.deposit(1000);
//print balance
System.out.println("Balance: "+account.balance);
//withdraw amount from account
account.withdraw(500);
//print balance
System.out.println("Balance: "+account.balance);
//the balance is now 498, now try to withdraw 498
//but it doesnot allow withdrwal as balance - (amount + transactionFee) will be negative
//the balance remains same
//withdraw amount from account
account.withdraw(498);
//print balance
System.out.println("Balance: "+account.balance);
}
}
Explanation / Answer
First the last index error is because in given code the class BankAccount is not closed
public class BankAccount {
String name;
double balance;
double transactionFee = 0.00;
public void deposit(double amount) {
balance = balance + amount;
}
} //this braces needs to be in place.
Second if you try to run this code after adding braces then also it will throw errors as in the main class it is trying to call setTransactionFee(), withdraw() functions which are not defined in BankAccount class. So add these functions as per requirement and could would work.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.