Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 17 of 17

Thread: Designing an Account Class

  1. #1
    Junior Member
    Join Date
    Sep 2011
    Posts
    24
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Cool Designing an Account Class

    Design a class named Account that contains:

    • A private int data field named id for the account(default 0)
    • A private double data field named balance for the account (default 0)
    • A private double data field named annualInterestRate that stores the current interest rate (default 0). Assume all accounts have the same interest rate.
    • A private Date data field named dateCreated that stores the date when the account was created.
    • A no-arg constructor that creates a default account.
    • A constructor that creates an account with the specific id and initial balance.
    • The accessor and mutator methods for id, balance, and annualInterestRate.
    • The accessor method for dateCreated
    • A method named getMonthlyInterestRate() that returns the monthly interest rate.
    • A method named withdraw that withdraws a specified amount from the account
    • A method named deposit that deposits a specified amount to the account.



    Write a test program that creates an Account object with an account ID of 1122, a balance of $20,000, and an annual interest rate of 4.5%. Use the withdraw method to withdraw $2,500, use the deposit method to deposit $3,000 and print the balanace, the monthly interest, and the date when this account was created.
    Here is my attempt:
    import java.util.Date;
     
    public class AccountProblem {
        public static void main(String[] args) {
    //create an instance  object of class Stock
            Account myAccount = new Account(1122, 20000.00, 0.045);
            myAccount.withdraw(2500.00);
            myAccount.deposit(3000.00);
    //display balance, monthly interest and date of account
            System.out.println("Balance: " + myAccount.balance);
            System.out.println("Monthly Interest: " + myAccount.getMonthlyInterestRate());
            System.out.println("Account created on: " + myAccount.dateCreated);
        }
    }
     
    class Account {
    //define var1, var2
        int id;
        double balance;
        double annualInterestRate;
        Date dateCreated;
    //no arg constructer
        Account() {
            id = 0;
            balance = 0.0;
            annualInterestRate = 0.0;
        }
    //constructor with specific id and initial balance
        Account(int newId, double newBalance) {
            id = newId;
            balance = newBalance;
        }
    //accessor/mutator methods for id, balance, and annualInterestRate
        public int getId() {
            return id;
        }
        public double getBalance() {
            return balance;
        }
        public double getAnnualInterestRate() {
            return annualInterestRate;
        }
        public void setId() {
            id = newId;
        }
        public void setBalance() {
            balance = newBalance;
        }
        public void setAnnualInterestRate() {
            annualInterestRate = newAnnualInterestRate;
        }
    //accessor method for dateCreated
        public void setDateCreated() {
            dateCreated = newDateCreated;
        }
    //define method getMonthlyInterestRate
        double getMonthlyInterestRate() {
            return annualInterestRate/12;
        }
    //define method withdraw
        double withdraw(double amount) {
            return balance -= amount;
        }  
    //define method deposit
        double deposit(double amount) {
            return balance += amount;
        }
    }

    My code does not compile. And I get the following error messages. I have no clue what the error messages mean and how I would fix them.

    AccountProblem.java:10: cannot find symbol
    symbol : constructor Account(int,double,double)
    location: class Account
    Account myAccount = new Account(1122, 20000.00, 0.045);
    ^
    AccountProblem.java:48: cannot find symbol
    symbol : variable newId
    location: class Account
    id = newId;
    ^
    AccountProblem.java:51: cannot find symbol
    symbol : variable newBalance
    location: class Account
    balance = newBalance;
    ^
    AccountProblem.java:54: cannot find symbol
    symbol : variable newAnnualInterestRate
    location: class Account
    annualInterestRate = newAnnualInterestRate;
    ^
    AccountProblem.java:58: cannot find symbol
    symbol : variable newDateCreated
    location: class Account
    dateCreated = newDateCreated;
    ^
    5 errors
    How can I get my program to compile and work?


  2. #2
    Junior Member
    Join Date
    Nov 2011
    Posts
    8
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Designing an Account Class

    ...edited by moderator


    If you have any questions just ask.
    Last edited by copeg; November 4th, 2011 at 02:25 PM.

  3. #3
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Designing an Account Class

    mccolem, please read the forum rules and the following: http://www.javaprogrammingforums.com...n-feeding.html

    m2msucks, take each error at a time. Cannot find symbol refers to the fact that the compiler cannot find what you are attempting to call. The first error on the list:
    Account myAccount = new Account(1122, 20000.00, 0.045);
    You attempt to access a constructor in the Account class with 3 parameters - there is no constructor of that definition. Another error:
    public void setBalance() {
            balance = newBalance;
        }

    Where is the newBalance variable? For each cannot find symbol error, closely inspect your code and think like the computer - search for each variable to make sure it can be found.

  4. #4
    Junior Member
    Join Date
    Sep 2011
    Posts
    24
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Designing an Account Class

    "You attempt to access a constructor in the Account class with 3 parameters - there is no constructor of that definition."
    I'm trying to access this constructor:
    //constructor with specific id and initial balance
        Account(int newId, double newBalance) {
            id = newId;
            balance = newBalance;
        }
    How would I access this constructor?

    "Where is the newBalance variable?"
    I don't know. Where in my code do I put it?

  5. #5
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Designing an Account Class

    Think like the computer for a second. You try to create an Account object using the following code:
    Account myAccount = new Account(1122, 20000.00, 0.045);

    This attempt to use this constructor makes the compiler look for a constructor of type:
    public Account(int a, double b, double c)
    ...

    The Account class does not contain a constructor like this, so if you wish to create an Account object like this, then you must write a constructor that accepts 3 parameters: an int and 2 doubles. Alternatively, the Account class has other constructors available (one with zero parameters and another with 2 parameters). See the following link for more information:
    Providing Constructors for Your Classes (The Java™ Tutorials > Learning the Java Language > Classes and Objects)

    As for the other errors, take a look at the following:
    public void setBalance() {
            balance = newBalance;
        }

    What is newBalance? Does this method need a parameter - perhaps defined as newBalance? See the following: Defining Methods (The Java™ Tutorials > Learning the Java Language > Classes and Objects)

  6. The Following User Says Thank You to copeg For This Useful Post:

    m2msucks (November 6th, 2011)

  7. #6
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Designing an Account Class

    Did you post this at the java-forums as well? If so, I highly recommend reading the links below

    This thread has been cross posted here:

    http://www.java-forums.org/new-java/50754-designing-account-class.html

    Although cross posting is allowed, for everyone's benefit, please read:

    Java Programming Forums Cross Posting Rules

    The Problems With Cross Posting

    Last edited by copeg; November 5th, 2011 at 10:46 AM.

  8. #7
    Junior Member
    Join Date
    Sep 2011
    Posts
    24
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Designing an Account Class

    Yes I did. I'm guessing you are doWhile over on that forum. I fixed up my code and got it to compile.

    Here is the revised code:
    import java.util.Date;
     
    public class AccountProblem {
    	public static void main(String[] args) {
    //create an instance  object of class Stock
    		Account myAccount = new Account(1122, 20000.00, 0.045);
    		myAccount.withdraw(2500.00);
    		myAccount.deposit(3000.00);
    //display balance, monthly interest and date of account
    		System.out.println("Balance: " + myAccount.balance);
    		System.out.println("Monthly Interest: " + myAccount.getMonthlyInterestRate());
    		System.out.println("Account created on: " + myAccount.dateCreated);
    	}
    }
     
    class Account {
    //define var1, var2
    	int id;
    	double balance;
    	double annualInterestRate;
    	Date dateCreated;
    //no arg constructer
    	Account() {
    		id = 0;
    		balance = 0.0;
    		annualInterestRate = 0.0;
    	}
    //constructor with specific id and initial balance
    	Account(int newId, double newBalance) {
    		id = newId;
    		balance = newBalance;
    	}
    	Account(int newId, double newBalance, double newAnnualInterestRate) {
    		id = newId;
    		balance = newBalance;
    		annualInterestRate = newAnnualInterestRate;
    	}
    //accessor/mutator methods for id, balance, and annualInterestRate
    	public int getId() {
    		return id;
    	}
    	public double getBalance() {
    		return balance;
    	}
    	public double getAnnualInterestRate() {
    		return annualInterestRate;
    	}
    	public void setId(int newId) {
    		id = newId;
    	}
    	public void setBalance(double newBalance) {
    		balance = newBalance;
    	}
    	public void setAnnualInterestRate(double newAnnualInterestRate) {
    		annualInterestRate = newAnnualInterestRate;
    	}
    //accessor method for dateCreated
    	public void setDateCreated(Date newDateCreated) {
    		dateCreated = newDateCreated;
    	}
    //define method getMonthlyInterestRate
    	double getMonthlyInterestRate() {
    		return annualInterestRate/12;
    	}
    //define method withdraw
    	double withdraw(double amount) {
    		return balance -= amount;
    	}	
    //define method deposit
    	double deposit(double amount) {
    		return balance += amount;
    	}
    }

    The only problem is my output.
    Balance: 20500.0
    Monthly Interest: 0.00375
    Account created on: null
    I don't know why null is showing up. How can I get the date to show up? And thanks.

  9. #8
    Junior Member
    Join Date
    Nov 2011
    Posts
    8
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Designing an Account Class

    You need to initialise the Date class object, check Date (Java 2 Platform SE v1.4.2) for guidance.

  10. #9
    Junior Member
    Join Date
    Oct 2011
    Posts
    10
    My Mood
    Cheerful
    Thanks
    0
    Thanked 1 Time in 1 Post

    Thumbs up Re: Designing an Account Class

    ...edited by moderator
    Last edited by copeg; November 5th, 2011 at 11:40 PM.

  11. #10
    Junior Member
    Join Date
    Sep 2011
    Posts
    24
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Designing an Account Class

    @mccolem I just read the whole thing and I still have no idea what to type.

    @copeg Why was ravindu's post edited? I want to know what it said.
    Last edited by m2msucks; November 6th, 2011 at 02:03 PM.

  12. #11
    Junior Member
    Join Date
    Sep 2011
    Posts
    24
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Designing an Account Class

    Nevermind I got it.

    Here's the finished code:
    import java.util.Date;
     
    public class AccountProblem {
    	public static void main(String[] args) {
    //create an instance  object of class Stock
    		Account myAccount = new Account(1122, 20000.00, 0.045);
    		myAccount.withdraw(2500.00);
    		myAccount.deposit(3000.00);
    //display balance, monthly interest and date of account
    		System.out.println("Balance: " + myAccount.balance);
    		System.out.println("Monthly Interest: " + myAccount.getMonthlyInterestRate());
    		//System.out.println("Account created on: " + myAccount.dateCreated);
    		java.util.Date dateCreated = new java.util.Date();
    		System.out.println("Account created on: " + dateCreated.toString());
    	}
    }
     
    class Account {
    //define var1, var2
    	int id;
    	double balance;
    	double annualInterestRate;
    	Date dateCreated;
    //no arg constructer
    	Account() {
    		id = 0;
    		balance = 0.0;
    		annualInterestRate = 0.0;
    	}
    //constructor with specific id and initial balance
    	Account(int newId, double newBalance) {
    		id = newId;
    		balance = newBalance;
    	}
    	Account(int newId, double newBalance, double newAnnualInterestRate) {
    		id = newId;
    		balance = newBalance;
    		annualInterestRate = newAnnualInterestRate;
    	}
    //accessor/mutator methods for id, balance, and annualInterestRate
    	public int getId() {
    		return id;
    	}
    	public double getBalance() {
    		return balance;
    	}
    	public double getAnnualInterestRate() {
    		return annualInterestRate;
    	}
    	public void setId(int newId) {
    		id = newId;
    	}
    	public void setBalance(double newBalance) {
    		balance = newBalance;
    	}
    	public void setAnnualInterestRate(double newAnnualInterestRate) {
    		annualInterestRate = newAnnualInterestRate;
    	}
    //accessor method for dateCreated
    	public void setDateCreated(Date newDateCreated) {
    		dateCreated = newDateCreated;
    	}
    //define method getMonthlyInterestRate
    	double getMonthlyInterestRate() {
    		return annualInterestRate/12;
    	}
    //define method withdraw
    	double withdraw(double amount) {
    		return balance -= amount;
    	}	
    //define method deposit
    	double deposit(double amount) {
    		return balance += amount;
    	}
    }

  13. #12
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Designing an Account Class

    @copeg Why was ravindu's post edited? I want to know what it said.
    Its called spoon-feeding, which that poster was guilty of elsewhere as well. I presume you don't want to be handed code with no explanation as to what was done and why - denying you the opportunity to problem solve (and learn one of the more fundamental concepts of programming). It is both alluded to in the forum rules and clearly explained in the following: http://www.javaprogrammingforums.com...n-feeding.html

  14. #13
    Junior Member
    Join Date
    Sep 2011
    Posts
    24
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Designing an Account Class

    Ok I understand.

  15. #14
    Junior Member
    Join Date
    Mar 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Designing an Account Class

    Hi first post here and am new to java. I'm teaching myself by working through the textbook that this problem comes from. It looks like your main problem here is that your not assigning a value to the dateCreated Date object in your no arg constructor (or the arg constructor for that matter). Also the assignment doesn't require a mutator method for the Date object (setDate), since this would give people the ability to tamper with the created Account in a manner that isn't wanted (assignment only asks for accessor method for dateCreated). All the data fields that have default values can be assigned in the data section at the top of the class and don't need to be set in the no arg constructor but that doesn't really matter I guess (you can do it in constructor if you want). Here's how I created Account class. I've included some comments inside the code to point out the problems and my thinking...

    import java.util.Date;
     
    public class Account {
    	// Private data fields for Account object
    	private int id = 0; // Assign default value
    	private double balance = 0; // Assign default value
    	private double annualInterestRate = 0; // Assign default value
    	private Date dateCreated; // Here I'm just letting the compiler know there is a Date object associated with each Account object 
                                                // but no value for this object is referenced; value referenced when Account object is created by constructors
     
    	// no arg Constructor for Account
    	public Account() {
    		dateCreated = new Date(); // Here is where I believe you go wrong, create Date object when Account object is created (new Date())
                                                         // and assign it's reference to dateCreated, this is why you're getting null returned.
                                                         // You've got a Date object but it never gets a reference to any actual data.
                                                         // What you've done is sort of like creating an Array object but not populating it with data; basically a
                                                         // shortcut that doesn't point to anything hence null, empty.
                                                         // Also note that I don't have to assign default values for other data fields since I assigned them above.
    	}
     
    	// Constructor with id and balance args
    	public Account(int newID, double newBalance) {
    		dateCreated = new Date(); // And again for argument constructor
    		id = newID;
    		balance = newBalance;
    	}
     
    	// Accessor method for id
    	public int getID() {
    		return id;
    	}
     
    	// Mutator method for id
    	public void setID(int newID) {
    		id = newID;
    	}
     
    	// Accessor method for balance
    	public double getBalance() {
    		return balance;
    	}
     
    	// Mutator method for balance
    	public void setBalance(double newBalance) {
    		balance = newBalance;
    	}
     
    	// Accessor method for annualInterestRate
    	public double getAnnualInterestRate() {
    		return annualInterestRate;
    	}
     
    	// Mutator method for annualInterestRate
    	public void setAnnualInterestRate(double newAnnualInterestRate) {
    		annualInterestRate = newAnnualInterestRate;
    	}
     
    	// Accessor method for dateCreated; NOTE: no mutator method for dateCreated required...
    	public Date getDateCreated() {
    		return dateCreated;
    	}
     
    	// Method for returning monthlyInterestRate calculated from annualInterestRate
    	public double getMonthlyInterestRate() {
    		return annualInterestRate/12;
    	}
     
    	// Method for withdrawing specified amount
    	public void withdraw(double withdrawAmount) {
    		balance -= withdrawAmount;
    	}
     
    	// Method for depositing specified amount
    	public void deposit(double depositAmount) {
    		balance += depositAmount;
    	}
    }

    And here's the test method to show it works.

    public class Exc87 {
     
    	public static void main(String[] args) {
    		Account test = new Account(1122, 20000);
    		test.setAnnualInterestRate(4.5);
    		test.withdraw(2500);
    		test.deposit(3000);
    		System.out.printf("The current balance is $%1.2f.\n", test.getBalance());
    		System.out.println("The monthly interest is " + test.getMonthlyInterestRate() + "%.");
    		System.out.println("The account was created on " + test.getDateCreated().toString() + ".");
     
    	}
     
    }

    Happy coding

  16. #15
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,163
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Designing an Account Class

    @MasterCylinder This thread is over one year old.
    If you don't understand my answer, don't ignore it, ask a question.

  17. #16
    Junior Member
    Join Date
    Mar 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Designing an Account Class

    Yeah mostly posted because I came across it while trying to do same exercise, it helped me with a problem or two I had but seemed like it could use a little more explanation for beginners like me since that is who mostly will be doing this problem, it's from chapter 8 of an introduction to java book. I know the original poster has moved on and finished by now but I thought it might help others like me work through the problem. LOL sorry but ease up it's my 1st post

  18. #17
    Junior Member
    Join Date
    Jul 2014
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Designing an Account Class

    Quote Originally Posted by MasterCylinder View Post
    I know the original poster has moved on and finished by now but I thought it might help others like me work through the problem.)
    I'm glad you posted, I was also assigned this problem and stumbled across this post when trying to figure out if I was doing my getter and setter methods correctly. I was glad to see your example of the test program since I'm working on one of these as weell and was kind of confused as to how it works. I'm new here too and still really new to Java in general, so I really appreciate people posting their input on things like these even if it's an old post. Like you said, even if the OP has figured it out and moved on, there could still be others out there come across it later and find your answer handy. :-)

Similar Threads

  1. Problem in designing Web Browser
    By Rupinder in forum Java Networking
    Replies: 2
    Last Post: January 29th, 2012, 09:18 PM
  2. What is the best way of designing this object?
    By aussiemcgr in forum Java Theory & Questions
    Replies: 5
    Last Post: May 25th, 2011, 06:38 AM
  3. Problem with designing threads & abstraction
    By gilme in forum What's Wrong With My Code?
    Replies: 6
    Last Post: June 14th, 2010, 06:48 AM
  4. Create a java small class for a bank account!!?
    By jon123 in forum Object Oriented Programming
    Replies: 2
    Last Post: March 24th, 2010, 11:00 AM
  5. Problem in JSP Page Designing
    By vinothkumarrvk in forum JavaServer Pages: JSP & JSTL
    Replies: 0
    Last Post: March 10th, 2010, 01:09 AM