toString and equals method, Copy Constructor

  • (2 Pages)
  • +
  • 1
  • 2

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

#1 DHarrison  Icon User is offline

  • New D.I.C Head

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

toString and equals method, Copy Constructor

Posted 03 April 2013 - 03:11 PM

I need to add a toString method, and equals method, and a copy constructor to the BankAccount class. Then I need to write a test program that tests the method and constructors. I attempted to add in the toString, but I'm not sure if it's correct, and I'm stuck on the equals method and copy constructor

/**
   The BankAccount class simulates a bank account.
*/

public class BankAccount
{
   private double balance;      // Account balance

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

   public BankAccount()
   {
      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);
   }

   /**
      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 String toString()
	{
		String str1 = "Dylan Harrison";
		String str2 = "Barack Obama";
		return str1;
	}
}




And here's my test program.

public class BankAccountDemo 
{
   public static void main(String[] args)
	{
	
	BankAccount account1 = new BankAccount("Dylan Harrison");
	BankAccount account2 = new BankAccount("Barack Obama");
	System.out.println(account);
	}
}


Any help on how to complete these is very appreciated! Thanks!

Is This A Good Question/Topic? 0
  • +

Replies To: toString and equals method, Copy Constructor

#2 Flukeshot  Icon User is offline

  • A little too OCD
  • member icon

Reputation: 404
  • View blog
  • Posts: 1,006
  • Joined: 14-November 12

Re: toString and equals method, Copy Constructor

Posted 03 April 2013 - 04:42 PM

Your toString method is going to want to look something like:
public string toString() {
   return "First info: " + firstVariable + "\nSecond info: " + secondVariable;
}


Don't waste time declaring strings you aren't going to use.

Your equals method will want to maybe compare between 2 accounts by account number. So you'll want something like:
public boolean equals(BankAccount b ) {
   if(this.accountNumber == b.accountNumber)
      return true;
   return false;
}


Finally your copy constructor is for creating a duplicate of the BankAccount instance, I imagine. So you'll need to accept a bank account in a constructor, and use that to duplicate all of the fields.
BankAccount(BankAccount b ) {
   this.accountNumber = b.accountNumber;
   this.sortCode = b.sortCode;
   this.accountHolder = b.accountHolder;
   this.balance = b.balance
}


Does that make sense? :)

This post has been edited by Flukeshot: 03 April 2013 - 04:43 PM

Was This Post Helpful? 1
  • +
  • -

#3 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 03 April 2013 - 04:47 PM

Thank you very much FlukeShot! I know exactly what you mean, thanks for the help.
Was This Post Helpful? 0
  • +
  • -

#4 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 03 April 2013 - 05:18 PM

Although I am still (very) confused on the Copy Constructor. We haven't learned anything thing about them in class so I'm still confused on which items to copy and how.
Was This Post Helpful? 0
  • +
  • -

#5 Flukeshot  Icon User is offline

  • A little too OCD
  • member icon

Reputation: 404
  • View blog
  • Posts: 1,006
  • Joined: 14-November 12

Re: toString and equals method, Copy Constructor

Posted 03 April 2013 - 05:43 PM

You haven't learned about copy constructors or constructors?

A constructor is just a block of code that says: "When you build an instance of this class, make sure you do this to it".

A copy constructor would be just a constructor that duplicates an existing instance. Duplicate as much information as you are capable of.
Was This Post Helpful? 0
  • +
  • -

#6 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 2891
  • View blog
  • Posts: 9,599
  • Joined: 12-December 12

Re: toString and equals method, Copy Constructor

Posted 03 April 2013 - 05:51 PM

Your test code:

public class BankAccountDemo 
{
   public static void main(String[] args)
	{
	
	BankAccount account1 = new BankAccount("Dylan Harrison");
	BankAccount account2 = new BankAccount("Barack Obama");
	System.out.println(account);
	}
}

is incorrect as there is non 'account' variable, although you've probably discovered this already.
Was This Post Helpful? 0
  • +
  • -

#7 pbl  Icon User is offline

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

Reputation: 8316
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: toString and equals method, Copy Constructor

Posted 03 April 2013 - 06:22 PM

Kind of useless to write

   if(condition)
      return true;
   return false;

   // simply
   return condition


so from
public boolean equals(BankAccount b ) {
   if(this.accountNumber == b.accountNumber)
      return true;
   return false;
}


to
public boolean equals(BankAccount b ) {
   return this.accountNumber == b.accountNumber;
}



:^:
Was This Post Helpful? 1
  • +
  • -

#8 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 - 03:20 PM

I'm getting an error after I do the equals method. Perhaps I'm doing it completely wrong, and/or placing in it the wrong spot.

 	public boolean equals(BankAccount b ) 
	{
   	String accountName = "Dylan Harrison";
		String accountNumber = "1234567";
		
		return this.accountNumber == b.accountNumber;
	}
	public String toString() 
	{
		String accountName = "Dylan Harrison";
		String accountNumber = "1234567";
		return "Account Name: " + accountName + "\nAccount #: " + accountNumber;
	}
}



It's located at the very end of my code just above toString(which I believe is correct?)

The error I'm getting by the way is
BankAccount.java:138: error: cannot find symbol
return this.accountNumber == b.accountNumber;
^
symbol: variable accountNumber
BankAccount.java:138: error: cannot find symbol
return this.accountNumber == b.accountNumber;
^
symbol: variable accountNumber
location: variable b of type BankAccount
2 errors
Was This Post Helpful? 0
  • +
  • -

#9 Flukeshot  Icon User is offline

  • A little too OCD
  • member icon

Reputation: 404
  • View blog
  • Posts: 1,006
  • Joined: 14-November 12

Re: toString and equals method, Copy Constructor

Posted 04 April 2013 - 03:22 PM

You don't need to declare and assign the Strings within the method. The toString() method should be in your BankAccount class - and therefor it will have access to accountName and accountNumber as instance variables.

The errors are coming from your method calls. You must pass a reference to a BankAccount into your equals method or it will have nothing to compare to.

This post has been edited by Flukeshot: 04 April 2013 - 03:24 PM

Was This Post Helpful? 0
  • +
  • -

#10 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:10 PM

Alright, my BankAccount program compiles fine, no errors. I now need to write a test program that test the methods and copy constructor.

Here's my BankAccount program
/**
   The BankAccount class simulates a bank account.
*/

 public class BankAccount
{
   private double balance;      // Account balance

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

   public BankAccount()
   {
  		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)/>
	{
		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 B)/> 
	{
   	return this.balance == b.balance;
	}
	
	public String toString()
	{
	String accountName = "Dylan Harrison";
	String accountNumber = "1234567";
	String str = "Name: " + accountName + "Number: " + accountNumber;
	return str;
	}



I started my test program
public class BankAccountDemo 
{
   public static void main(String[] args)
	{
	
	BankAccount account1 = new BankAccount("Dylan Harrison", "1234567");
	System.out.println(account1);
	}


But received this error
BankAccountDemo.java:6: error: no suitable constructor found for BankAccount(String,String)
BankAccount account1 = new BankAccount("Dylan Harrison", "1234567");
^
constructor BankAccount.BankAccount(BankAccount) is not applicable
(actual and formal argument lists differ in length)
constructor BankAccount.BankAccount(String) is not applicable
(actual and formal argument lists differ in length)
constructor BankAccount.BankAccount(double) is not applicable
(actual and formal argument lists differ in length)
constructor BankAccount.BankAccount() is not applicable
(actual and formal argument lists differ in length)
1 error
Was This Post Helpful? 0
  • +
  • -

#11 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 2891
  • View blog
  • Posts: 9,599
  • Joined: 12-December 12

Re: toString and equals method, Copy Constructor

Posted 04 April 2013 - 04:19 PM

That message is quite clear: you have created three constructors, none of which accept two, string arguments.
Was This Post Helpful? 0
  • +
  • -

#12 darek9576  Icon User is offline

  • D.I.C Lover

Reputation: 198
  • View blog
  • Posts: 1,672
  • Joined: 13-March 10

Re: toString and equals method, Copy Constructor

Posted 04 April 2013 - 04:19 PM

First, your BankAccount has one field and that is "double balance". I cant see anywhere accountNumber being declared. Then, your test code takes String objects but then they are parsed to a double in the constructor so that should fail.

Finally, you equals() method takes an Object as parameter so you could do:

@Override
    public boolean equals(Object obj){
        if(this == obj) return true;
        if(obj == null) return false;
        if(this.getClass() != obj.getClass()) return false;
        BankAccount account = (BankAccount)obj;
        return this.getBalance() == obj.getBalance();
    }



And when you override equals(), you have to override hashcode() to keep the contract:

public int hashCode(){
    return balance;
}


Was This Post Helpful? 0
  • +
  • -

#13 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:26 PM

Some/most of the things in my program were already there. I had to add the toString, equals, and copy constructor.
I've got the toString method to print the desired output in the Demo program, how do I "test" the equals method and copy constructor?
Was This Post Helpful? 0
  • +
  • -

#14 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 2891
  • View blog
  • Posts: 9,599
  • Joined: 12-December 12

Re: toString and equals method, Copy Constructor

Posted 04 April 2013 - 04:33 PM

The last line in the previous posts' code by darek9576 should be:

return this.getBalance() == account.getBalance();

but is the account variable necessary? Wouldn't a temporary cast be simpler?

View PostDHarrison, on 04 April 2013 - 11:26 PM, said:

Some/most of the things in my program were already there. I had to add the toString, equals, and copy constructor.
I've got the toString method to print the desired output in the Demo program, how do I "test" the equals method and copy constructor?

Assuming you've fixed the constructor error mentioned in my previous post, you would test equality by create two instances (objects) of your class and comparing them, printing a message which confirms if they are equal, or not, and then attempt to create a copy of one of your objects.

This post has been edited by andrewsw: 04 April 2013 - 04:34 PM

Was This Post Helpful? 0
  • +
  • -

#15 Flukeshot  Icon User is offline

  • A little too OCD
  • member icon

Reputation: 404
  • View blog
  • Posts: 1,006
  • Joined: 14-November 12

Re: toString and equals method, Copy Constructor

Posted 04 April 2013 - 04:37 PM

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.

Mrs. Walker isn't going to be too pleased to find all her money is in Dylan Harrison's name!
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2