Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7854449
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T20:04:01+00:00 2026-06-02T20:04:01+00:00

Before I posted this, I read some of the previous posts and I really

  • 0

Before I posted this, I read some of the previous posts and I really don’t see anything wrong with my logic. (I’m 3 hours on this already and it’s probably gonna kill my happy hour) *I never want to know the answers, I enjoy working hard so maybe if anyone can maybe ask me a question as to what I am trying to achieve that can lead me to think of the answer using your clues or hints. It will be appreciated.* obj2 is not cloning so behind the exception stackTrace, I found that there is a nullpointer exception on the same line which means that obj2 is never getting cloned. Please help me to think a little harder.

package testbankaccount;

/**
 *
 * @author 
 */
public interface Cloneable {

}

My parent class

package testbankaccount;

/**
 *
 * @author 
 */
public abstract class BankAccount implements Cloneable, Comparable {
    private double balance; 
    private int numberofDeposits; 
    private int numberofWithdrawals; 
    private double annualInterestRate; 
    private double monthlyServiceCharges; 
    private String customerName;



protected BankAccount(){
    this(1.0, 1.0);
}

protected BankAccount(double balance, double annualInterestRate){
    this.balance = balance;
    this.annualInterestRate = annualInterestRate;
}

public void deposit(double deposit){
    balance += deposit;
    numberofDeposits++; 
}

public void withdraw(double withdrawal){
    balance -= withdrawal;
    numberofWithdrawals++;
}

public void calcInterest(){
    double monthlyInterestRate = annualInterestRate/1200;
    double monthlyInterest = balance * monthlyInterestRate;
    balance += monthlyInterest; 
}

public void setMonthlyServiceCharge(double serviceCharge){
    monthlyServiceCharges = serviceCharge;
}

public void monthlyProcess(){
    balance -= monthlyServiceCharges;
    calcInterest();
    numberofWithdrawals = 0;
    numberofDeposits = 0;
    monthlyServiceCharges = 0.0;
}
public void setBalance(double balance){
     this.balance = balance;
}

public double getBalance(){
    return balance;
}

public int getNumberWithdraws(){
    return numberofWithdrawals;
}

@Override
    public Object clone() throws CloneNotSupportedException {

        return super.clone();

      }



    @Override
public abstract String toString();

    @Override
public abstract boolean equals(Object obj);

}

My subclass

package testbankaccount;

/**
 *
 * @author Darren Wright
 */
public class SavingsAccount extends BankAccount implements Cloneable, Comparable {

private boolean status;


public SavingsAccount(){
    this(1.0,1.0);
}

public SavingsAccount(double balance, double annualInterestRate){
    super(balance,annualInterestRate);

}

public void setActivity(){
    if (getBalance() > 25.0)
     status = true;
    else status = false;
}

    @Override
public void withdraw(double withdrawal){
    if (status = true)
    super.withdraw(withdrawal);     
}

@Override
public void deposit(double deposit){
    if (status = false && (deposit + getBalance()) > 25.0)
    {
            status = true;
            super.deposit(deposit);
    }
    else if (status = true)
            super.deposit(deposit);
}

    @Override
public void monthlyProcess(){
        double result = 0.0;
        if(getNumberWithdraws() >4)
        result = getNumberWithdraws() - 4;
        setMonthlyServiceCharge(result);
        setActivity();
}
@Override
    public String toString() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

@Override
    public Object clone() throws CloneNotSupportedException {

        return super.clone();

      }

    @Override
    public boolean equals(Object obj) {
        return (getBalance() == ((SavingsAccount)obj).getBalance());
    }


  public int compareTo(Object obj) {
    if (getBalance() > ((SavingsAccount)obj).getBalance())
      return 1;
    else if (getBalance() < ((SavingsAccount)obj).getBalance())
      return -1;
    else
      return 0;
  }


}

My test class

package testbankaccount;

/**
 *
 * @author 
 */
public class TestBankAccount{

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args)  throws CloneNotSupportedException {
        SavingsAccount obj1 = new SavingsAccount(500,8.25); 
        try{
            SavingsAccount obj2 = (SavingsAccount)obj1.clone();
            System.out.println(obj1.compareTo(obj2));
            System.out.println(obj1.equals(obj2));
         }
         catch (CloneNotSupportedException ex) {
             ex.printStackTrace();
        }

    }
}

My error output

java.lang.CloneNotSupportedException: testbankaccount.SavingsAccount
    at java.lang.Object.clone(Native Method)
    at testbankaccount.BankAccount.clone(BankAccount.java:69)
    at testbankaccount.SavingsAccount.clone(SavingsAccount.java:60)
    at testbankaccount.TestBankAccount.main(TestBankAccount.java:16)
BUILD SUCCESSFUL (total time: 0 seconds)

What am I missing in my thought process? I created the interface, I implemented it and overided it in my super and subclass. My subclass refers super.clone() to the superclass and the super.clone() in the superclass I’m thinking refers to the Object’s clone method. I casted correctly in the test class but obj2 winds up being null in both the compareTo and of course the equals. What am I not thinking about?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T20:04:02+00:00Added an answer on June 2, 2026 at 8:04 pm

    You shouldn’t be creating your own Cloneable interface. You should use the one that’s built-in.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've posted some questions on this before, but it's different. So consider a small
I've posted this before, but I worded it poorly. I'm trying again with a
I know this issue was posted before, and i fixed my code according to
I know this question has been posted before... but I haven't found any answer
I am sure that the answer to this has been posted before. Forgive me
I posted a different flavour of this problem before with no joy. I'm hoping
Note this question was originally posted in 2009, before C++11 was ratified and before
Edit: This was accidentally posted twice. Original: VB.NET Importing Classes I've seen some code
Sorry to bother again. I posted this before and got help by the amazing
This is really frustrating I would appreciate some help with this. I have a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.