Help with Overloading constructors

cannot find symbol error message

Page 1 of 1

10 Replies - 2306 Views - Last Post: 24 March 2010 - 02:23 PM Rate Topic: -----

#1 justabeginner  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 17-February 10

Help with Overloading constructors

Posted 24 March 2010 - 12:54 PM

I have an assignment that practices overloading constructors and methods. I understand the concept of overloading, but I cannot get my driver program to execute without errors so that I can modify it as assigned. Can someone please take a look at my code (particularly withing the constructors) and see what I might be missing? My driver program is TestAccount.java and my constructors are in Account.java

//*******************************************************
// TestAccount.java
//
// A simple driver to test the overloaded methods of 
// the Account class.
//*******************************************************

import java.util.Scanner;
import java.lang.String;

public class TestAccount
{
    public static void main(String[] args)
    {
	String name;
	double balance;
	long acctNum;
	Account acct;

	Scanner scan = new Scanner(System.in);

	System.out.println("Enter account holder's first name");
	name = scan.next();
	acct = new Account(name);
	System.out.println("Account for " + name + ":");
	System.out.println(acct);

	System.out.println("\nEnter initial balance");
	balance = scan.nextDouble();
	acct = new Account(balance,name);
	System.out.println("Account for " + name + ":");
	System.out.println(acct);

	System.out.println("\nEnter account number");
	acctNum = scan.nextLong();
	acct = new Account(balance,name,acctNum);
	System.out.println("Account for " + name + ":");
	System.out.println(acct);

	System.out.print("\nDepositing 100 into account, balance is now ");
	acct.deposit(100);
	System.out.println(acct.getBalance());
	System.out.print("\nWithdrawing $25, balance is now ");
	acct.withdraw(25);
	System.out.println(acct.getBalance());
	System.out.print("\nWithdrawing $25 with $2 fee, balance is now ");
	acct.withdraw(25,2);
	System.out.println(acct.getBalance());

	System.out.println("\nBye!");
    }
}

//*******************************************************
// Account.java
//
// A bank account class with methods to deposit to, withdraw from,
// change the name on, and get a String representation
// of the account.
//*******************************************************

import java.util.Random;
public class Account
{
  private double balance;
  private String name;
  private long acctNum;

  //----------------------------------------------
  //Constructor -- initializes balance, owner, and account number
  //----------------------------------------------
  public Account(double initBal, String owner, long number)
  {
    balance = initBal;
    name = owner;
    acctNum = number;
  }
  
  //Constructor -- initializes balance, owner, and account number 
  //---------------------------------------------- 
  public Account (int initBal, String owner) 
  { 
    balance = initBal; 
	 name = owner; 
	 Random generator = new Random();
	 acctNum = generator.nextLong();
	 System.out.println ("A random integer: " + acctNum);
	// acctNum = Math.random(long); // some code to random generate an account number
        //not sure if my random method to generate the account number is working 
  } 
  
  //Constructor -- initializes balance, owner, and account number 
  //---------------------------------------------- 
  public Account (String owner) 
  { 
    balance =0; 
	 name = owner; 
	 acctNum = 99999; // some code to random generate an account number 
         //just entered in 99999 until I get my random method to generate the account number
} 

  //----------------------------------------------
  // Checks to see if balance is sufficient for withdrawal.
  // If so, decrements balance by amount; if not, prints message.
  //----------------------------------------------
  public void withdraw(double amount)
  {
    if (balance >= amount)
       balance -= amount;
    else
       System.out.println("Insufficient funds");
  }

  //----------------------------------------------
  // Adds deposit amount to balance.
  //----------------------------------------------
  public void deposit(double amount)
  {
    balance += amount;
  }

  //----------------------------------------------
  // Returns balance.
  //----------------------------------------------
  public double getBalance()
  {
    return balance;
  }
  //----------------------------------------------
  // Returns a string containing the name, account number, and balance.
  //----------------------------------------------
  public String toString()
  {
	return "Name:" + name + 
        "\nAccount Number: " + acctNum +
        "\nBalance: " + balance; 
  }
}



Anyone have any suggestions on my class constructors???

This post has been edited by Dogstopper: 24 March 2010 - 01:34 PM
Reason for edit:: Just ONE [code] before the code and one [/code] after


Is This A Good Question/Topic? 0
  • +

Replies To: Help with Overloading constructors

#2 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 724
  • View blog
  • Posts: 4,470
  • Joined: 24-October 08

Re: Help with Overloading constructors

Posted 24 March 2010 - 01:15 PM

please edit your posts and :code:
Was This Post Helpful? 0
  • +
  • -

#3 justabeginner  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 17-February 10

Re: Help with Overloading constructors

Posted 24 March 2010 - 01:21 PM

//*******************************************************
// TestAccount.java
//
// A simple driver to test the overloaded methods of 
// the Account class.
//*******************************************************

import java.util.Scanner;
import java.lang.String;

public class TestAccount
{
    public static void main(String[] args)
    {
	String name;
	double balance;
	long acctNum;
	Account acct;

	Scanner scan = new Scanner(System.in);

	System.out.println("Enter account holder's first name");
	name = scan.next();
	acct = new Account(name);
	System.out.println("Account for " + name + ":");
	System.out.println(acct);

	System.out.println("\nEnter initial balance");
	balance = scan.nextDouble();
	acct = new Account(balance,name);
	System.out.println("Account for " + name + ":");
	System.out.println(acct);

	System.out.println("\nEnter account number");
	acctNum = scan.nextLong();
	acct = new Account(balance,name,acctNum);
	System.out.println("Account for " + name + ":");
	System.out.println(acct);

	System.out.print("\nDepositing 100 into account, balance is now ");
	acct.deposit(100);
	System.out.println(acct.getBalance());
	System.out.print("\nWithdrawing $25, balance is now ");
	acct.withdraw(25);
	System.out.println(acct.getBalance());
	System.out.print("\nWithdrawing $25 with $2 fee, balance is now ");
	acct.withdraw(25,2);
	System.out.println(acct.getBalance());

	System.out.println("\nBye!");
    }
}

//*******************************************************
// Account.java
//
// A bank account class with methods to deposit to, withdraw from,
// change the name on, and get a String representation
// of the account.
//*******************************************************

import java.util.Random;
public class Account
{
  private double balance;
  private String name;
  private long acctNum;

  //----------------------------------------------
  //Constructor -- initializes balance, owner, and account number
  //----------------------------------------------
  public Account(double initBal, String owner, long number)
  {
    balance = initBal;
    name = owner;
    acctNum = number;
  }
  
  //Constructor -- initializes balance, owner, and account number 
  //---------------------------------------------- 
  public Account (int initBal, String owner) 
  { 
    balance = initBal; 
	 name = owner; 
	 Random generator = new Random();
	 acctNum = generator.nextLong();
	 System.out.println ("A random integer: " + acctNum);
        //not sure if my random method is working properly to generate the account number
	// acctNum = Math.random(long); // some code to random generate an account number
  } 
  
  //Constructor -- initializes balance, owner, and account number 
  //---------------------------------------------- 
  public Account (String owner) 
  { 
    balance =0; 
	 name = owner; 
	 acctNum = 99999; // some code to random generate an account number
         //just entered 99999 as account number until I get my random method to generate it 
  } 

  //----------------------------------------------
  // Checks to see if balance is sufficient for withdrawal.
  // If so, decrements balance by amount; if not, prints message.
  //----------------------------------------------
  public void withdraw(double amount)
  {
    if (balance >= amount)
       balance -= amount;
    else
       System.out.println("Insufficient funds");
  }

  //----------------------------------------------
  // Adds deposit amount to balance.
  //----------------------------------------------
  public void deposit(double amount)
  {
    balance += amount;
  }

  //----------------------------------------------
  // Returns balance.
  //----------------------------------------------
  public double getBalance()
  {
    return balance;
  }
  //----------------------------------------------
  // Returns a string containing the name, account number, and balance.
  //----------------------------------------------
  public String toString()
  {
	return "Name:" + name + 
        "\nAccount Number: " + acctNum +
        "\nBalance: " + balance; 
  }
}

Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10175
  • View blog
  • Posts: 37,571
  • Joined: 27-December 08

Re: Help with Overloading constructors

Posted 24 March 2010 - 01:30 PM

What error(s) are you experiencing?
Was This Post Helpful? 0
  • +
  • -

#5 justabeginner  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 17-February 10

Re: Help with Overloading constructors

Posted 24 March 2010 - 01:33 PM

View Postmacosxnerd101, on 24 March 2010 - 12:30 PM, said:

What error(s) are you experiencing?


TestAccount.java:35: cannot find symbol
symbol : constructor Account(double,java.lang.String)
location: class Account
acct = new Account(balance,name);
^
TestAccount.java:52: withdraw(double) in Account cannot be applied to (int,int)
acct.withdraw(25,2);
^
2 errors
Was This Post Helpful? 0
  • +
  • -

#6 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2857
  • View blog
  • Posts: 10,960
  • Joined: 15-July 08

Re: Help with Overloading constructors

Posted 24 March 2010 - 01:38 PM

For the first issue, your constructor is set to take an int, not a double. I think you want to chang this:
  public Account (int initBal, String owner) 



To this:
  public Account (double initBal, String owner) 



As for your second error, your withdraw method only takes one parameter. Here you give it 2:
acct.withdraw(25,2);


Was This Post Helpful? 0
  • +
  • -

#7 justabeginner  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 17-February 10

Re: Help with Overloading constructors

Posted 24 March 2010 - 01:49 PM

Oh, the second part of this assignment is to overload the withdraw method to accept the two parameters. Once I write that, it should work, right?
Was This Post Helpful? 0
  • +
  • -

#8 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2857
  • View blog
  • Posts: 10,960
  • Joined: 15-July 08

Re: Help with Overloading constructors

Posted 24 March 2010 - 01:50 PM

Only if you call that method in the subclass, not the superclass.
Was This Post Helpful? 0
  • +
  • -

#9 justabeginner  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 17-February 10

Re: Help with Overloading constructors

Posted 24 March 2010 - 01:51 PM

Do you think the code I wrote to generate the random account number will work?

Do you think the code I wrote to generate the random account number will work?
Was This Post Helpful? 0
  • +
  • -

#10 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2857
  • View blog
  • Posts: 10,960
  • Joined: 15-July 08

Re: Help with Overloading constructors

Posted 24 March 2010 - 01:53 PM

It should...but only for THAT constructor. If you want other constructors to this too, you will need to implement the Random there too (or better yet, make it a method).
Was This Post Helpful? 0
  • +
  • -

#11 justabeginner  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 17-February 10

Re: Help with Overloading constructors

Posted 24 March 2010 - 02:23 PM

I added the code for the random number in the third constructor as listed in the code. My program is now working but it seems to not be giving the desired output. I guess it is calling all constructors before it executes any of the methods. I thought it would call the appropriate constructor depending on what parameters the user passes in.

The assignment instructs to overload the constructor as follows:
- public Account(double initBal, String owner, long number) - initializes the balance, owner, and account number as specified
- public Account(double initBal, String owner) - initializes the balance and owner as specified; randomly generates the account number
- public Account(String owner) - initializes the owner as specified; sets the initial balance to 0 and randomly generates the account number

Does anyone make sense of this output based off what the assignment asks to do??? Also, how can I make the random number a certain number of digits and only positive?

Output:
----jGRASP exec: java TestAccount

Enter account holder's first name
Jessica
Account for Jessica:
Name:Jessica
Account Number: -2640055321003641513
Balance: 0.0

Enter initial balance
800
Account for Jessica:
Name:Jessica
Account Number: 5767259052582773183
Balance: 800.0

Enter account number
63458734578
Account for Jessica:
Name:Jessica
Account Number: 63458734578
Balance: 800.0

Depositing 100 into account, balance is now 900.0

Withdrawing $25, balance is now 875.0

Withdrawing $25 with $2 fee, balance is now 848.0

Bye!

----jGRASP: operation complete.

Some of the revised code:
 //----------------------------------------------
  //Constructor -- initializes balance, owner, and account number
  //----------------------------------------------
  public Account(double initBal, String owner, long number)
  {
    balance = initBal;
    name = owner;
    acctNum = number;
  }
  
  //Constructor -- initializes balance, owner, and account number 
  //---------------------------------------------- 
  public Account (double initBal, String owner) 
  { 
    balance = initBal; 
	 name = owner; 
	 Random generator = new Random();
	 acctNum = generator.nextLong();
  } 
  
  //Constructor -- initializes balance, owner, and account number 
  //---------------------------------------------- 
  public Account (String owner) 
  { 
    balance =0; 
	 name = owner; 
	 Random generator = new Random();
	 acctNum = generator.nextLong();
  } 

  //----------------------------------------------
  // Checks to see if balance is sufficient for withdrawal.
  // If so, decrements balance by amount; if not, prints message.
  //----------------------------------------------
  public void withdraw(double amount)
  {
    if (balance >= amount)
       balance -= amount;
    else
       System.out.println("Insufficient funds");
  }
   public void withdraw(double amount, double fee)
  {
    if (balance >= (amount+fee))
       balance -= (amount+fee);
    else
       System.out.println("Insufficient funds");
  }


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1