Results 1 to 10 of 10
Like Tree1Likes
  • 1 Post By Fubarable

Thread: Designing an Account Class

  1. #1
    son012189 is offline Member
    Join Date
    Oct 2010
    Posts
    38
    Rep Power
    0

    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:
    Java 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;
    	}
    //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 program won't compile and I get several error messages. I don't know how to correct all the errors. Even if I were to correct all these errors I know my program still wouldn't function correctly. How can I get this program to work?

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Designing an Account Class

    Quote Originally Posted by son012189 View Post
    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:
    Java 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;
    	}
    //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 program won't compile and I get several error messages. I don't know how to correct all the errors. Even if I were to correct all these errors I know my program still wouldn't function correctly. How can I get this program to work?
    • What errors?: if you have any, you should post the exact error message, else how will we know what's wrong.
    • Don't try to add good code to bad code. If you're not using an IDE, always compile early and often, and always fix any compilation errors before trying to add any more code.
    • Remember that setter methods require a parameter. How can you set a variable to anything if you don't tell it what to set it to?

  3. #3
    son012189 is offline Member
    Join Date
    Oct 2010
    Posts
    38
    Rep Power
    0

    Default Re: Designing an Account Class

    These are the errors I get:
    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
    setter method??? Do you mean mutator method? and where exactly do I put the parameter.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Designing an Account Class

    Quote Originally Posted by son012189 View Post
    setter method??? Do you mean mutator method?
    Yes.


    and where exactly do I put the parameter.
    The tutorials on methods will show you how to create methods that accept parameters and how to use them. Please have a look!

  5. #5
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default Re: Designing an Account Class

    Is this your post as well: Designing an Account Class ? If so, please read the links provided in that thread regarding crossposting
    Last edited by doWhile; 11-05-2011 at 04:45 PM.

  6. #6
    son012189 is offline Member
    Join Date
    Oct 2010
    Posts
    38
    Rep Power
    0

    Default Re: Designing an Account Class

    @Fubarable Uh okay.

    @doWhile Sorry I didn't know cross posting wasn't allowed on this site. It won't happen again. You can close this thread now.

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Designing an Account Class

    Quote Originally Posted by son012189 View Post
    @Fubarable Uh okay.

    @doWhile Sorry I didn't know cross posting wasn't allowed on this site. It won't happen again. You can close this thread now.
    It's not "not allowed". It's just that if you do it, please notify us and them. Nobody likes to waste time answering a question that's already been answered or repeating a point that's already been made, least of all volunteers. We appreciate your respecting this, that's all.
    doWhile likes this.

  8. #8
    son012189 is offline Member
    Join Date
    Oct 2010
    Posts
    38
    Rep Power
    0

    Default Re: Designing an Account Class

    Oh I see. I will notify both sites next time.

  9. #9
    franyie07 is offline Member
    Join Date
    Sep 2013
    Posts
    2
    Rep Power
    0

    Default Re: Designing an Account Class

    I have the same homework and so far this is what i have, I dont know to add the rest:
    import java.util.Scanner;
    import java.util.Date;
    import javax.swing.JOptionPane;

    public class Account {

    private int id;
    private double balance;
    private double annualInterestRate;
    private Date dateCreated = new Date() ;
    private double withdraw ;
    private double deposit;


    /*private double withdraw=0;
    private double deposit=0;*/

    Scanner keyboard = new Scanner(System.in) ;


    Account(){

    id=0;
    balance = 0;
    annualInterestRate = 0;
    dateCreated = new Date();


    }
    Account(int idIn, double balanceIn, double annualInterestRateIn,double withdraw,double deposit){
    id = idIn;
    balance = balanceIn ;

    annualInterestRate = annualInterestRateIn;

    }


    //Method (getters or Accessors)
    Date dateCreated(){

    return dateCreated;

    }
    int getId(){
    return id;
    }
    double getBalance(){
    return balance;
    }
    double getAnnualInterstRate(){
    return annualInterestRate/100;
    }
    double getMonthlyInterest(){

    return balance * getMonthlyInterestRate();
    }

    double getMonthlyInterestRate(){

    return annualInterestRate/12;
    }

    double getWithdraw(){
    return withdraw;
    }
    double getDeposit(){

    return deposit;
    }

    ///////////////////////////////////////////////////////////

    //Methods(setters or mutators)

    void setId(int idIn){
    System.out.print("Please enter your ID");
    idIn = keyboard.nextInt();

    while(idIn<=0){
    String input = JOptionPane.showInputDialog("Incorrect Id! Try Again.");
    idIn = Integer.parseInt(input);
    }
    id= idIn;

    }
    void setBalance(double balanceIn){
    System.out.print("Please enter your Balance");
    balanceIn = keyboard.nextInt();

    while(balanceIn<=0){
    String input = JOptionPane.showInputDialog("Incorrect Balance! Try Again.");
    balanceIn = Double.parseDouble(input);
    }
    balance= balanceIn;

    }

    void setAnnualInterestRate(double annualInterestRateIn ){
    System.out.print("Please enter your Balance");

    annualInterestRateIn = keyboard.nextInt();

    while(annualInterestRateIn<=0){
    String input = JOptionPane.showInputDialog("Incorrect annual interst Rate ! Try Again.");
    annualInterestRateIn = Double.parseDouble(input);
    }
    annualInterestRate = annualInterestRateIn;


    }
    }
    //////////////////////////////////////////////////////////////////////////////////////////////////////////

    public class AccountRunner {

    public static void main(String[] args) {
    // TODO Auto-generated method stub
    Account account;
    account = new Account(1122, 20000.0, 0.045,2500.0,3000.0);
    System.out.println ("Account number: "+ account.getId());
    System.out.println ("The account balance is: $ " + account.getBalance());
    System.out.println ("The monthly interest earned is: $ " + account.getMonthlyInterest() );
    System.out.println ("The account was created on: " + account.dateCreated());

    }

    }

  10. #10
    franyie07 is offline Member
    Join Date
    Sep 2013
    Posts
    2
    Rep Power
    0

    Default Re: Designing an Account Class

    I have the same homework and so far this is what i have, I dont know to add the rest:
    import java.util.Scanner;
    import java.util.Date;
    import javax.swing.JOptionPane;

    public class Account {

    private int id;
    private double balance;
    private double annualInterestRate;
    private Date dateCreated = new Date() ;
    private double withdraw ;
    private double deposit;


    /*private double withdraw=0;
    private double deposit=0;*/

    Scanner keyboard = new Scanner(System.in) ;


    Account(){

    id=0;
    balance = 0;
    annualInterestRate = 0;
    dateCreated = new Date();


    }
    Account(int idIn, double balanceIn, double annualInterestRateIn,double withdraw,double deposit){
    id = idIn;
    balance = balanceIn ;

    annualInterestRate = annualInterestRateIn;

    }


    //Method (getters or Accessors)
    Date dateCreated(){

    return dateCreated;

    }
    int getId(){
    return id;
    }
    double getBalance(){
    return balance;
    }
    double getAnnualInterstRate(){
    return annualInterestRate/100;
    }
    double getMonthlyInterest(){

    return balance * getMonthlyInterestRate();
    }

    double getMonthlyInterestRate(){

    return annualInterestRate/12;
    }

    double getWithdraw(){
    return withdraw;
    }
    double getDeposit(){

    return deposit;
    }

    ///////////////////////////////////////////////////////////

    //Methods(setters or mutators)

    void setId(int idIn){
    System.out.print("Please enter your ID");
    idIn = keyboard.nextInt();

    while(idIn<=0){
    String input = JOptionPane.showInputDialog("Incorrect Id! Try Again.");
    idIn = Integer.parseInt(input);
    }
    id= idIn;

    }
    void setBalance(double balanceIn){
    System.out.print("Please enter your Balance");
    balanceIn = keyboard.nextInt();

    while(balanceIn<=0){
    String input = JOptionPane.showInputDialog("Incorrect Balance! Try Again.");
    balanceIn = Double.parseDouble(input);
    }
    balance= balanceIn;

    }

    void setAnnualInterestRate(double annualInterestRateIn ){
    System.out.print("Please enter your Balance");

    annualInterestRateIn = keyboard.nextInt();

    while(annualInterestRateIn<=0){
    String input = JOptionPane.showInputDialog("Incorrect annual interst Rate ! Try Again.");
    annualInterestRateIn = Double.parseDouble(input);
    }
    annualInterestRate = annualInterestRateIn;


    }
    }
    //////////////////////////////////////////////////////////////////////////////////////////////////////////

    public class AccountRunner {

    public static void main(String[] args) {
    // TODO Auto-generated method stub
    Account account;
    account = new Account(1122, 20000.0, 0.045,2500.0,3000.0);
    System.out.println ("Account number: "+ account.getId());
    System.out.println ("The account balance is: $ " + account.getBalance());
    System.out.println ("The monthly interest earned is: $ " + account.getMonthlyInterest() );
    System.out.println ("The account was created on: " + account.dateCreated());

    }

    }

Similar Threads

  1. designing a save system
    By aianta in forum New To Java
    Replies: 4
    Last Post: 04-12-2011, 12:25 AM
  2. Replies: 2
    Last Post: 02-17-2011, 05:15 AM
  3. Help designing a program
    By np2392 in forum New To Java
    Replies: 2
    Last Post: 09-24-2010, 02:31 AM
  4. Create a java class for a bank account!!?
    By singh345 in forum New To Java
    Replies: 1
    Last Post: 03-17-2010, 04:26 PM
  5. Need help for coding and designing
    By g.ganiraju in forum New To Java
    Replies: 11
    Last Post: 11-01-2008, 04:49 AM

Posting Permissions

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