Results 1 to 3 of 3
  1. #1
    Devigili22 is offline Member
    Join Date
    Sep 2012
    Posts
    1
    Rep Power
    0

    Default Copy constructor/Inheritance help

    starting to deeply learn Java at my school, and I have finally hit my wall where I have no idea where I'm going wrong. I will list 4 classes, one is the AccountDriver or main method, BankAccount, the parent class which CheckingAccount and SavingsAccount are derived from. Now my issue is in SavingsAccount. Here is the code...

    AccountDriver

    Java Code:
     //Demonstrates the BankAccount and derived classes
    
        import java.text.*;			// to use Decimal Format
    
        public class AccountDriver
        {
    	public static void main(String[] args)
    	{
    		double put_in = 500;
    		double take_out = 1000;
    
    		DecimalFormat myFormat;
    		String money;
    		String money_in;
    		String money_out;
    		boolean completed;
    
    		// to get 2 decimals every time
    		myFormat = new DecimalFormat("#.00");
    
    		//to test the Checking Account class
    		CheckingAccount myCheckingAccount =
    				new CheckingAccount ("Benjamin Franklin", 1000);
    		System.out.println ("Account Number "
    			+ myCheckingAccount.getAccountNumber() +
    			" belonging to " + myCheckingAccount.getOwner());
    		money  = myFormat.format(myCheckingAccount.getBalance());
    		System.out.println ("Initial balance = $" + money);
    		myCheckingAccount.deposit (put_in);
    		money_in = myFormat.format(put_in);
    		money  = myFormat.format(myCheckingAccount.getBalance());
    		System.out.println ("After deposit of $" + money_in
    			+ ",  balance = $" + money);
    		completed = myCheckingAccount.withdraw(take_out);
    		money_out = myFormat.format(take_out);
    		money  = myFormat.format(myCheckingAccount.getBalance());
    		if (completed)
    		{
    			System.out.println ("After withdrawal of $" + money_out
    					+ ",  balance = $" + money);
    		}
    		else
    		{
    			System.out.println ("Insuffient funds to withdraw $"
    					+ money_out	+ ",  balance = $" + money);
    		}
    		System.out.println();
    
    		//to test the savings account class
    		SavingsAccount yourAccount =
    				new SavingsAccount ("William Shakespeare", 400);
    		System.out.println ("Account Number "
    				+ yourAccount.getAccountNumber() +
    				" belonging to " + yourAccount.getOwner());
    		money  = myFormat.format(yourAccount.getBalance());
    		System.out.println ("Initial balance = $" + money);
    		yourAccount.deposit (put_in);
    		money_in = myFormat.format(put_in);
    		money  = myFormat.format(yourAccount.getBalance());
    		System.out.println ("After deposit of $" + money_in
    				+ ",  balance = $" + money);
    		completed = yourAccount.withdraw(take_out);
    		money_out = myFormat.format(take_out);
    		money  = myFormat.format(yourAccount.getBalance());
    		if (completed)
    		{
    			System.out.println ("After withdrawal of $" + money_out
    				+ ",  balance = $" + money);
    		}
    		else
    		{
    			System.out.println ("Insuffient funds to withdraw $"
    				+ money_out	+ ",  balance = $" + money);
    		}
    		yourAccount.postInterest();
    		money  = myFormat.format(yourAccount.getBalance());
    		System.out.println ("After monthly interest has been posted,"
    				+ "balance = $"	+ money);
    		System.out.println();
    
    
    		// to test the copy constructor of the savings account class
    		SavingsAccount secondAccount =
    				new SavingsAccount (yourAccount,5);
    		System.out.println ("Account Number "
    				+ secondAccount.getAccountNumber()+
    				" belonging to " + secondAccount.getOwner());
    		money  = myFormat.format(secondAccount.getBalance());
    		System.out.println ("Initial balance = $" + money);
    		secondAccount.deposit (put_in);
    		money_in = myFormat.format(put_in);
    		money  = myFormat.format(secondAccount.getBalance());
    		System.out.println ("After deposit of $" + money_in
    				+ ", balance = $" + money);
    		secondAccount.withdraw(take_out);
    		money_out = myFormat.format(take_out);
    		money  = myFormat.format(secondAccount.getBalance());
    		if (completed)
    		{
    			System.out.println ("After withdrawal of $" + money_out
    					+ ",  balance = $" + money);
    		}
    		else
    		{
    			System.out.println ("Insuffient funds to withdraw $"
    					+ money_out	+ ",  balance = $" + money);
    		}
    		System.out.println();
    
    		//to test to make sure new accounts are numbered correctly
    		CheckingAccount yourCheckingAccount =
    				new CheckingAccount ("Issac Newton", 5000);
    		System.out.println ("Account Number "
    				+ yourCheckingAccount.getAccountNumber()
    				+ " belonging to "
    				+ yourCheckingAccount.getOwner());
    
    	}
        }
    BankAccount(Parent class)

    Java Code:
    //Defines any type of bank account
    
        public abstract class BankAccount
        {
    	// class variable so that each account has a unique number
    	protected static int numberOfAccounts = 100001;
    
    	// current balance in the account
    	private double balance;
    	// name on the account
    	private String owner;
    	// number bank uses to identify account
    	private String accountNumber;
    
    	//default constructor
    	public BankAccount()
    	{
    		balance = 0;
    		accountNumber = numberOfAccounts + "";
    		numberOfAccounts++;
    	}
    
    	//standard constructor
    	public BankAccount(String name, double amount)
    	{
    		owner = name;
    		balance = amount;
    		accountNumber = numberOfAccounts + "";
    		numberOfAccounts++;
    	}
    
    	//copy constructor
    	public BankAccount(BankAccount oldAccount, double amount)
    	{
    		owner = oldAccount.owner;
    		balance = amount;
    		accountNumber = oldAccount.accountNumber;
    	}
    
    	//allows you to add money to the account
    	public void deposit(double amount)
    	{
    		balance = balance + amount;
    	}
    
    	//allows you to remove money from the account if
    	//enough money is available
    	//returns true if the transaction was completed
    	//returns false if the there was not enough money
    	public boolean withdraw(double amount)
    	{
    		boolean completed = true;
    
    		if (amount <= balance)
    		{
    			balance = balance - amount;
    		}
    		else
    		{
    			completed = false;
    		}
    		return completed;
    	}
    
    	//accessor method to balance
    	public double getBalance()
    	{
    		return balance;
    	}
    
    	//accessor method to owner
    	public String getOwner()
    	{
    		return owner;
    	}
    
    	//accessor method to account number
    	public String getAccountNumber()
    	{
    		return accountNumber;
    	}
    
    	//mutator method to change the balance
    	public void setBalance(double newBalance)
    	{
    		balance = newBalance;
    	}
    
    	//mutator method to change the account number
    	public void setAccountNumber(String newAccountNumber)
    	{
    		accountNumber = newAccountNumber;
    	}
        }
    CheckingAccount

    Java Code:
    public class CheckingAccount extends BankAccount {
    	
    	private double fee = .15;
    	
    	private String accountNumber = getAccountNumber() + "-10";	
    
        public CheckingAccount(String name, double amount) 
        {
        	
        	super(name, amount);
        	
        	setAccountNumber(accountNumber);
    			
    
    	}
        
    	public boolean withdraw(double amount)
    	{
    		double finalAmount = amount + fee;
    		
    		super.withdraw(finalAmount);
    		
    		boolean completed = true;
    		
    		return completed;
    		
    	}
        	
        }
    And finally, SavingsAccount

    Java Code:
    public class SavingsAccount extends BankAccount{
    	
    	double rate = .025;
    	
    	static int savingsNumber = 0;
    	
    	private String accountNumber = super.getAccountNumber();
    
    	
    	//Default constructor
    	public SavingsAccount(String name, double amount){
    		
    		super(name, amount);
    		
    		accountNumber = accountNumber + "-" + Integer.toString(savingsNumber);
    		
    	}
    
    	
    	//Copy constructor
    	public SavingsAccount(SavingsAccount oldAccount, double amount)
    	{			
    		
    		savingsNumber++;
    		
    		accountNumber = oldAccount.accountNumber + Integer.toString(savingsNumber);
    	}
    
    	
    	public void postInterest(){
    		double interestAmount = getBalance() * .025/12;
    		
    		double finalAmount = getBalance() + interestAmount;
    		
    		setBalance(finalAmount);
    	}
    
    	
    	public String getAccountNumber()
    	{
    		return accountNumber;
    	}
    
    }
    So here is the output when the AccountDriver is run...

    Java Code:
    Account Number 100001-10 belonging to Benjamin Franklin
        Initial balance = $1000.00
        After deposit of $500.00,  balance = $1500.00
        After withdrawal of $1000.00,  balance = $499.85
    
        Account Number 100002-0 belonging to William Shakespeare
        Initial balance = $400.00
        After deposit of $500.00,  balance = $900.00
        Insuffient funds to withdraw $1000.00,  balance = $900.00
        After monthly interest has been posted,balance = $901.88
        
        Account Number 100002-01 belonging to null  //**Should be 10002-1 and William Shakespeare**
        Initial balance = $.00                                 //**Should be $5.00**
        After deposit of $500.00, balance = $500.00  //**Should be changed in accordance with an initial balance of $5.00
        Insuffient funds to withdraw $1000.00,  balance = $500.00 **"   "**
    
        Account Number 100004-10 belonging to Issac Newton //**Should be # 10003-10
    So my issues are listed with the stars and I have spent 2 hours trying different methods either getting same or worse results. Hopefully someone can point out where I'm going wrong. Also note that I can not change AccountDriver or BankAccount. Only the classes I create (CheckingAccount and SavingsAccount) can be edited. Thanks a bunch guys.
    Last edited by Devigili22; 09-06-2012 at 03:54 PM.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default Re: Copy constructor/Inheritance help

    Have you tried debugging the code by adding lots of println statements that print out the values of variable as they are set and changed so you can see what the computer sees when it executes the code?
    That is what I would do if I tried to debug this code.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,185
    Rep Power
    20

    Default Re: Copy constructor/Inheritance help

    Java Code:
    public SavingsAccount(SavingsAccount oldAccount, double amount)
    {
        savingsNumber++;
        accountNumber = oldAccount.accountNumber + Integer.toString(savingsNumber);
    }
    You don't call the parent constructor that takes the name and amount, so they never get set.
    That's the easy bit.

    Note in the background the no-argument constructor for BankAccount is still called, which increments numberOfAccounts, which explains why the next new account is 100004 and not 100003.
    This is the area you probably need to look at. There's hacky fixes for the current setup, but you need to think first off how you want the account number to function then model that.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Copy Constructor with Shallow Copy
    By Wnt2bsleepin in forum New To Java
    Replies: 1
    Last Post: 04-11-2012, 01:42 AM
  2. What is the point of a copy constructor?
    By ozzyman in forum New To Java
    Replies: 3
    Last Post: 01-05-2012, 11:59 AM
  3. array copy in class constructor.
    By Juukamen in forum New To Java
    Replies: 2
    Last Post: 10-29-2011, 01:07 AM
  4. Inheritance and Constructor Problem
    By iratus in forum New To Java
    Replies: 9
    Last Post: 05-10-2011, 08:35 AM
  5. copy constructor
    By shadow7 in forum New To Java
    Replies: 6
    Last Post: 12-29-2009, 07:56 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •