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 10 of 10

Thread: Can't get my program to work!! Please help!

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

    Question Can't get my program to work!! Please help!

    Hello! I have wrote this program over and over again, can someone please tell me what I am missing, it looks like its a small error I am not catching.

    Here is what I am trying to accomplish:

    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 what I have so far:

    import java.util.Date;
     
    public class Assign22 {
     
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
     
    Account account1 = new Account(1122, 20000, .045);
    account1.withdraw(2500);
    account1.deposit(3000);
    System.out.println("Date Created:" + java.util.Date dateCreated = new java.util.Date());
    System.out.println("Account ID:" + account1.id);
    System.out.println("Balance:" + account1.getBalance());
    System.out.println("Interest Rate:" + account1.getAnnualInterestRate());
    System.out.println("Balance after withdraw of 2500:" + account1.getAnnualInterestRate());
    System.out.println("Balance after deposit of 3000:" + account1.getAnnualInterestRate());
    System.out.println("Monthly Interest:" + account1.id);
     
    System.out.println("Process completed.");
    }
     
    class Account {
    //define variables
    private int id;
    private double balance; // balance for account
    private double annualInterestRate; //stores the current interest rate
    private Date dateCreated; //stores the date account created
     
    //no arg construtor
    	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;	
    	}
    }
     
    }

    This is the error that I am receiving: Not sure what I missed here:

    "Syntax error on token "Date", , expected after this token dateCreated cannot be resolved to a variable at Assign22.main(Assign22.java:12)"


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

    Default Re: Can't get my program to work!! Please help!

    dateCreated cannot be resolved to a variable
    Where is the variable: dateCreated defined? The compiler can not find its definition.

    Check the syntax of the line where the error is. It looks weird.
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: Can't get my program to work!! Please help!

    I thought that I created the variable here. Is it created incorrectly?

    class Account {
    //define variables
    private int id;
    private double balance; // balance for account
    private double annualInterestRate; //stores the current interest rate
    private Date dateCreated; //stores the date account created


    Quote Originally Posted by Norm View Post
    Where is the variable: dateCreated defined? The compiler can not find its definition.

    Check the syntax of the line where the error is. It looks weird.

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

    Default Re: Can't get my program to work!! Please help!

    Yes that looks like a definition for variables inside of the Account class.
    at Assign22.main(Assign22.java:12)
    The error message referred to line 12 which is in the main() method in the Assign22 class.

    Do you know about scope?
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: Can't get my program to work!! Please help!

    So here is the output that I want:

    Date Created: Fri May 16 09:33:15 EDT 2014
    Account ID: 1122
    Balance: 20000.0
    Interest Rate: 0.045
    Balance after withdraw of 2500: 17500.0
    Balance after deposit of 3000: 20500.0
    Monthly Interest: 76.875

    Process completed.


    Now that I have gotten my program to compile, how do I do math operations? I want to include the balance after withdraw, and deposit, what do I call here. I also want to create a space between Monthly Interest line, and process completed. I think I might be using the wrong terminology. Please help, this is my code:

    public class Assing22 {
     
    	public static void main(String[] args) {
    		Account account = new Account(1122, 20000, .045);
    		account.withdraw(2500);
    		account.deposit(3000);
    		System.out.println("Date Created: " + account.getDateCreated());
    		System.out.println("Account ID:" + account.id);
    		System.out.println("Balance:" + account.getBalance());
    		System.out.println("Interest Rate:" + account.getAnnualInterestRate());
    		System.out.println("Balance after withdraw of 2500:");
    		System.out.println("Balance after deposit of 3000:");
    		System.out.println("Monthly Interest:" + account.getMonthlyInterest());
     
     
    		System.out.println("Process completed.");
     
     
    	}
     
    }
    class Account {
    public int id;
    public double balance;
    public double annualInterestRate;
    public java.util.Date dateCreated;
     
    public Account() {
    dateCreated = new java.util.Date();
    }
     
    public Account(int id, double balance, double annualInterestRate) {
    this.id = id;
    this.balance = balance;
    this.annualInterestRate = annualInterestRate;
    dateCreated = new java.util.Date();
    }
     
    public int getId() {
    return this.id;
    }
     
    public double getBalance() {
    return balance;
    }
     
    public double getAnnualInterestRate() {
    return annualInterestRate;
     
    }
     
    public void setId(int id) {
    this.id =id;
    }
     
    public void setBalance(double balance) {
    this.balance = balance;
    }
     
    public void setAnnualInterestRate(double annualInterestRate) {
    this.annualInterestRate = annualInterestRate;
    }
     
    public double getMonthlyInterest() {
    return balance * (annualInterestRate / 12);
    }
     
    public java.util.Date getDateCreated() {
    return dateCreated;
    }
     
    public void withdraw(double amount) {
    balance -= amount;
    }
     
    public void deposit(double amount) {
    balance += amount;
    }
     
    }

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

    Default Re: Can't get my program to work!! Please help!

    how do I do math operations?
    Can you explain what the problem is?
    Use the math operators: +,-,*,/ with some numeric operands as required. Assign the results to a variable.

    create a space between Monthly Interest line, and process completed.
    Print a blank line. Either include an extra "\n" character in the String to print
    or use the println() method.

    What does the program print out now?

    BTW The code in the Account class needs formatting. Nested statements should be indented like they are in the main() method.
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: Can't get my program to work!! Please help!


    Where the lines prints: "Balance after withdraw of 2500: I want it to say 17500.0, what math operations to do I do, to get the original balance of 20500 to subtract 2500 from the withdraw, I create a method for them.

    Quote Originally Posted by Norm View Post
    Can you explain what the problem is?
    Use the math operators: +,-,*,/ with some numeric operands as required. Assign the results to a variable.

    I also was able to get the blank line printed with your help, thanks!

    Quote Originally Posted by Norm View Post
    Print a blank line. Either include an extra "\n" character in the String to print
    or use the println() method.

    What does the program print out now?

    BTW The code in the Account class needs formatting. Nested statements should be indented like they are in the main() method.

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

    Default Re: Can't get my program to work!! Please help!

    what math operations to do I do, to get the original balance of 20500 to subtract 2500 from the withdraw
    Use the - operator to subtract one value from another:
    4 - 3 gives 1
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Jul 2014
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Can't get my program to work!! Please help!

    Here your upadated and working code_;
    ------------------------------------------------
    ******** Code removed
    ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,, ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
    output:-
    ate Created:Thu Jul 03 16:20:52 IST 2014
    Account ID:1122
    Balance:20500.0
    Interest Rate:0.045
    Balance after withdraw of 2500:0.045
    Balance after deposit of 3000:0.045
    Monthly Interest:1122
    Process completed.
    Last edited by Norm; July 3rd, 2014 at 06:44 AM. Reason: Please don't do OPs work for him

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

    Default Re: Can't get my program to work!! Please help!

    @Vasim Hayat please read: http://www.javaprogrammingforums.com...n-feeding.html
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Program does not work
    By cldance5678 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: September 27th, 2013, 09:28 PM
  2. HELP!! Cant get point program to work
    By Lashickk in forum What's Wrong With My Code?
    Replies: 10
    Last Post: July 30th, 2013, 01:26 AM
  3. Program doesn't work!
    By Mrcinica in forum What's Wrong With My Code?
    Replies: 15
    Last Post: May 16th, 2013, 10:23 AM
  4. [SOLVED] (Beginner) Program doesnt work
    By moneyman021 in forum What's Wrong With My Code?
    Replies: 10
    Last Post: January 15th, 2012, 04:28 AM
  5. my run program does not work
    By rman27bn in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 16th, 2009, 09:13 AM