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

OK, I have four classes that I\'ve written and all compile fine. The problem is

ID: 3629273 • Letter: O

Question

OK, I have four classes that I've written and all compile fine. The problem is they don't print what they're supposed to, and/or don't pass when they should.** at this point, the only methods that dont do what they're supposed to are the writeCheck and testWriteCheck methods** Here are the four classes:
public class Account {
    //INSTANCE VARIABLES
private int id;
private double balance;

    //Constructor
public Account(int id, double balance){
}

    //METHODS
public int getId() {
    return id;
}

public void setId(int id){
    this.id = id;
}

public double getBalance() {
    return balance;
}

public void setBalance(double balance){
    this.balance = balance;
}

void withdraw(double d){
   if ((balance - d >= 0)){
      this.balance = (d - balance);
   }
}
void deposit(double d){
    if (d > 0){
        this.balance += d;
    }
   
}
public String toString() {
    return "Account [id=" + getId() + ", balance=" + getBalance() + "]";
}
 boolean equals(Account act){
    if ((act.id == this.id) && (act.balance == this.balance)){
        return true;
    }
    else{
        return false;
    }
}
}

 

 

public class CheckingAccount extends Account {


//INSTANCE VARIABLES
private int checkNumber;


//CONSTRUCTOR
public CheckingAccount(int id, double balance, int checkNumber) {
super(id, balance);
this.checkNumber = checkNumber;

}
//METHODS


public int getCheckNumber() {
    return checkNumber;
}
public void writeCheck(double amount) {
    if ((getBalance() - amount) >= 0) {
        setBalance(getBalance() - amount);
        checkNumber = checkNumber + 1;
    }
}

public String toString() {
    return "CheckingAccount [id = " + getId() + ", balance = " + getBalance() + ", checkNumber = " + checkNumber + " ]";
   

}

boolean equals(CheckingAccount act){
    if ((act.getId() == this.getId()) && (act.getBalance() == this.getBalance()) && (act.checkNumber == this.checkNumber)){
        return true;
    }
    else{
        return false;
    }
}

}

 

 

public class SavingsAccount extends Account {
   
    //INSTANCE VARIABLES
private double savingsRate;

//CONSTRUCTOR
public SavingsAccount(int id, double balance, double savingsRate){
    super(id, balance);
}

//METHODS
public double getSavingsRate() {
    return savingsRate;
}

public void setSavingsRate(double s) {
    if (savingsRate >= 0){       
    savingsRate = s;
}
}

void addInterest(){
    super.setBalance((super.getBalance() * savingsRate) + super.getBalance());
       
}

public String toString() {
    return "SavingsAccount [id=" + super.getId() + ", balance=" + super.getBalance() + ", savingsRate=" + savingsRate + "]";
}

boolean equals(SavingsAccount act){
    if ((act.getId() == this.getId()) && (act.getBalance() == this.getBalance()) && (act.savingsRate == this.savingsRate)){
        return true;
    }
    else{
        return false;
    }
}

}



public class TestAccounts {

    public static boolean testWriteCheck(CheckingAccount a, double amount, CheckingAccount expected) {
        a.writeCheck(amount);
        if (a.equals(expected)) {
        System.out.println("WriteCheck passes");
        return true;
        } else {
        System.out.println("writeCheck fails. expected " + expected + " not " + a);
        return false;
        }

        }
   
    public static boolean testAddInterest(SavingsAccount a, SavingsAccount expected) {

        a.addInterest();
        if (a.equals(expected)) {
        System.out.println("addInterest passes");
        return true;
        } else {
        System.out.println("addInterest fails. expected " + expected + " not " + a);
        return false;
        }

        }
   
    public static boolean testWithdraw(Account account, double amount, Account expected) {
        account.withdraw(amount);
        if (account.equals(expected)) {
            System.out.println("withdraw passes");
            return true;
        } else {
            System.out.println("withdraw fails. expected " + expected + " not " + account);
            return false;
        }
    }
   
    public static boolean testDeposit(Account account, double amount, Account expected) {
        account.deposit(amount);
        if (account.equals(expected)) {
            System.out.println("deposit passes");
            return true;
        } else {
            System.out.println("deposit fails. expected " + expected + " not " + account);
            return false;
        }
    }

    public static void withdrawFromAll(Account[] accounts, double amount) {
          for(int i=0;i<accounts.length;i++){
              accounts[i].setBalance(accounts[i].getBalance()-amount);
              }
}

   
    public static void printAccounts(Account[] accounts) {
        for(int i=0;i<accounts.length;i++){
            System.out.println(accounts[i]);
    }
}
    public static void main(String[] args) {
        // These four tests should all pass.
        testWithdraw(new Account(3, 100.0), 50.0, new Account(3, 50.0));
        testWithdraw(new Account(3, 100.0), 150.0, new Account(3, 100.0));
        testDeposit(new Account(3, 100.0), 50.0, new Account(3, 150.0));
        testDeposit(new Account(3, 100.0), -50.0, new Account(3, 100.0));
       
        // TODO: call testWriteCheck, testAddInterest, and printAccounts
        Account[]accounts = {new Account(1, 100.0), new Account(2, 200.0), new Account(3, 300.0), new Account(4,400.0) };
        testWriteCheck(new CheckingAccount(3, 100.0, 0), 50, new CheckingAccount(3, 50.0, 1));
        testWriteCheck(new CheckingAccount(2, 200.0, 0), 100.0, new CheckingAccount(2, 100.0, 1));
        testAddInterest(new SavingsAccount(1, 100.0, .2), new SavingsAccount(1, 120.0, .2));
        testAddInterest(new SavingsAccount(2, 200.0, .5), new SavingsAccount(2, 300.0, .5));
        printAccounts(accounts);
    }   
}




this is what it prints out when i run:

withdraw passes
withdraw passes
deposit fails. expected Account [id=0, balance=0.0] not Account [id=0, balance=50.0]
deposit passes
writeCheck fails. expected CheckingAccount [id = 0, balance = 0.0, checkNumber = 1 ] not CheckingAccount [id = 0, balance = 0.0, checkNumber = 0 ]
writeCheck fails. expected CheckingAccount [id = 0, balance = 0.0, checkNumber = 1 ] not CheckingAccount [id = 0, balance = 0.0, checkNumber = 0 ]
addInterest passes
addInterest passes
Account [id=0, balance=0.0]
Account [id=0, balance=0.0]
Account [id=0, balance=0.0]
Account [id=0, balance=0.0]

WHY DOES IT DO THIS?! lol. please help!  Lifesaver guaranteed!

Explanation / Answer

I'm not sure if this will fix your issue, but try changing your constructor in the Account class to: //Constructor public Account(int name, double total){ id = name; balance = total; } Since your constructor did not set your private variables to anything, I believe they are default set to 0, which I don't think you want. Maybe a similar thing is happening with the constructors in the other classes? It's hard to debug something like this when I can't really test it out myself.