toString and equals method, Copy Constructor

  • (2 Pages)
  • +
  • 1
  • 2

24 Replies - 1288 Views - Last Post: 05 April 2013 - 11:15 AM Rate Topic: -----

#16 DHarrison  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 07-March 13

Re: toString and equals method, Copy Constructor

Posted 04 April 2013 - 04:51 PM

View PostFlukeshot, on 04 April 2013 - 04:37 PM, said:

But your toString method will print out the same name and account number for EVERYONE who holds an account with the bank.. :(/>

Your toString method needs only to return the values, it doesn't need to create them.

public String toString() {
   return "Name: " + accountName + "Number: " + accountNumber;
}


Then it will be unique per instance of BankAccount.


Something like this for the toString method?
public String toString(String accountName, String accountNumber)
	{
	return "Name: " + accountName + "\nNumber: " + accountNumber;
	}

Was This Post Helpful? 0
  • +
  • -

#17 Flukeshot  Icon User is offline

  • A little too OCD
  • member icon

Reputation: 415
  • View blog
  • Posts: 1,030
  • Joined: 14-November 12

Re: toString and equals method, Copy Constructor

Posted 04 April 2013 - 05:10 PM

No need for the arguments either, just look at exactly what I did in my previous post.

This method should be in your BankAccount class, so it will have access to all of the data in there. Even private data. So there is no need to pass arguments into it, there is no need to create new fields, just directly return what you need to.
Was This Post Helpful? 0
  • +
  • -

#18 DHarrison  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 07-March 13

Re: toString and equals method, Copy Constructor

Posted 04 April 2013 - 07:36 PM

Alright, I've fixed that. I did the equals method.
public boolean equals(BankAccount account2)
	{
      boolean status;
      
      if (accountName.equals(account2.accountName) &&
          accountNumber == account2.accountNumber)
         status = true;  
      else
         status = false; 
      return status;
	}
	

But when i run the BankAccountDemo program, the output is

Name: null
Number: null
Exception in thread "main" java.lang.NullPointerException
at BankAccount.equals(BankAccount.java:133)
at BankAccountDemo.main(BankAccountDemo.java:10)


Here's my Demo program.
public class BankAccountDemo 
{
   public static void main(String[] args)
	{
	
	BankAccount account1 = new BankAccount("Dylan Harrison", "1234567");
	BankAccount account2 = new BankAccount("Barack Obama", "7654321");
	System.out.println(account1);
	
	if (account1.equals(account2))
         System.out.println("Both objects are the same.");
      else
         System.out.println("The objects are different.");
	}
}[code]

I have accountName and accountNumber as fields, not set to any value
[code] public class BankAccount
{
   private double balance; // account balance
	private String accountName; // account name
	private String accountNumber;  // Account number

Was This Post Helpful? 0
  • +
  • -

#19 Flukeshot  Icon User is offline

  • A little too OCD
  • member icon

Reputation: 415
  • View blog
  • Posts: 1,030
  • Joined: 14-November 12

Re: toString and equals method, Copy Constructor

Posted 05 April 2013 - 04:07 AM

View PostDHarrison, on 05 April 2013 - 02:36 AM, said:

Alright, I've fixed that. I did the equals method.
public boolean equals(BankAccount account2)
	{
      boolean status;
      
      if (accountName.equals(account2.accountName) &&
          accountNumber == account2.accountNumber)
         status = true;  
      else
         status = false; 
      return status;
	}


Again, too much going on here, as pbl mentioned, you only need

public boolean equals(Object o) {
	return this.accountNumber == (BankAccount)o.accountNumber;
}


I will apologise - the original equals method I defined wouldn't be a proper over-ride, it would be an overload as the method signature was changed. It should be equals(Object o) and then o must be cast to a BankAccount just-in-time to retrieve account info. The original way would work.. But it's technically not an over-ride.
Was This Post Helpful? 0
  • +
  • -

#20 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8328
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: toString and equals method, Copy Constructor

Posted 05 April 2013 - 07:01 AM

When you have a stack trace like

at BankAccount.equals(BankAccount.java:133)

post the whole class so we can see what is at line 133
Was This Post Helpful? 1
  • +
  • -

#21 DHarrison  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 07-March 13

Re: toString and equals method, Copy Constructor

Posted 05 April 2013 - 07:31 AM

I've changed my equals method to look like this
public boolean equals(BankAccount account2)
	{
		return this.accountName == account2.accountName;
	}


This is my BankAccount Program.
/**
   The BankAccount class simulates a bank account.
*/

 public class BankAccount
{
   private double balance; // account balance
	private String accountName; // account name
	private String accountNumber;  // Account number

   /**
      This constructor sets the starting balance
      at 0.0.
   */

   public BankAccount(String accountName, String accountNumber)
   {
  		balance = 0.0;
   }
	
	/**
      This constructor sets the starting balance
      to the value passed as an argument.
      @param startBalance The starting balance.
   */

   public BankAccount(double startBalance)
   {
      balance = startBalance;
	}
	
	/**
      This constructor sets the starting balance
      to the value in the String argument.
      @param str The starting balance, as a String.
   */

   public BankAccount(String str)
   {
      balance = Double.parseDouble(str);
   }
	
	// COPY CONSTRUCTOR
	public BankAccount(BankAccount b, String name, String number)
	{
	
		accountName = name;
		accountNumber = number;
		this.balance = b.balance;
	}
	
	/**
      The deposit method makes a deposit into
      the account.
      @param amount The amount to add to the
                    balance field.
   */

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

   /**
      The deposit method makes a deposit into
      the account.
      @param str The amount to add to the
                 balance field, as a String.
   */

   public void deposit(String str)
   {
      balance += Double.parseDouble(str);
   }

   /**
      The withdraw method withdraws an amount
      from the account.
      @param amount The amount to subtract from
                    the balance field.
   */

   public void withdraw(double amount)
   {
      balance -= amount;
   }

   /**
      The withdraw method withdraws an amount
      from the account.
      @param str The amount to subtract from
                 the balance field, as a String.
   */

   public void withdraw(String str)
   {
      balance -= Double.parseDouble(str);
   }

   /**
      The setBalance method sets the account balance.
      @param b The value to store in the balance field.
   */

   public void setBalance(double B)/>
   {
      balance = b;
   }

   /**
      The setBalance method sets the account balance.
      @param str The value, as a String, to store in
                 the balance field.
   */

   public void setBalance(String str)
   {
      balance = Double.parseDouble(str);
   }
   
   /**
      The getBalance method returns the
      account balance.
      @return The value in the balance field.
   */

   public double getBalance()
   {
      return balance;
   }
	
	public boolean equals(BankAccount account2)
	{
		return this.accountName == account2.accountName;
	}
	
	public String toString()
	{
	return "Name: " + accountName + "\nNumber: " + accountNumber;
	}
}


And my Demo Program
public class BankAccountDemo 
{
   public static void main(String[] args)
	{
	
	BankAccount account1 = new BankAccount("Dylan Harrison", "1234567");
	BankAccount account2 = new BankAccount("Barack Obama", "7654321");
	System.out.println(account1);
	System.out.println("\n" + account2);
	
	if (account1.equals(account2))
         System.out.println("Both objects are the same.");
      else
         System.out.println("The objects are different.");
	}
}


I'm no longer getting a run error. The output when ran is
Name: null
Number: null

Name: null
Number: null
Both objects are the same.
Was This Post Helpful? 0
  • +
  • -

#22 darek9576  Icon User is offline

  • D.I.C Lover

Reputation: 198
  • Posts: 1,688
  • Joined: 13-March 10

Re: toString and equals method, Copy Constructor

Posted 05 April 2013 - 07:56 AM

Your equals() method is wrong because when comparing String objects you should be using equals() method. By using == operator you are checking whether they point to the same object on the heap.

Then, you are calling a constructor that takes a String and a double but you never assign a String, so you get values of "null" since instance variables are initialized to default values.

Finally, in your main() method, you are getting the output that both objects are the same because you are doing the comparison:

if(null == null) print equal
else
    print not equal.



Why dont you just get a book and study. You keep guessing, changning your code back and forth and you dont learn anything that way. Sure, it's good to experiment but when you get the basics. So far, you dont.
Was This Post Helpful? 1
  • +
  • -

#23 Flukeshot  Icon User is offline

  • A little too OCD
  • member icon

Reputation: 415
  • View blog
  • Posts: 1,030
  • Joined: 14-November 12

Re: toString and equals method, Copy Constructor

Posted 05 April 2013 - 08:18 AM

Don't compare via account name, what if one person had 2 accounts? Are those both the same account?

What darek9576 said about comparing strings is true, but what you actually want to do is compare accountNumbers - and each account number should be a unique value.
Was This Post Helpful? 1
  • +
  • -

#24 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8328
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: toString and equals method, Copy Constructor

Posted 05 April 2013 - 09:55 AM

Your problem is your constructor

public BankAccount(String accountName, String accountNumber)  
    {  
         balance = 0.0;  
    }  


It leaves both your instance variable accountName and accountNumber to null

And Flukeshot is right, your equals method should be

return accountNumber.equals(account2.accountNumber);

assuming that no constructor left to null accountNumber

This post has been edited by pbl: 05 April 2013 - 11:17 AM
Reason for edit:: "to null" not "no null"

Was This Post Helpful? 1
  • +
  • -

#25 DHarrison  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 07-March 13

Re: toString and equals method, Copy Constructor

Posted 05 April 2013 - 11:15 AM

Well after many attempts and tries, I've been doing it wrong all along. Apparently, I'm supposed to compare Balances, like I said before, there was hardly any instructions. It works properly now. Thank you everyone for all your help!
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2