Results 1 to 4 of 4
  1. #1
    siren1111 is offline Member
    Join Date
    Dec 2011
    Posts
    2
    Rep Power
    0

    Default What Am I doing Wrong?

    Hello,

    I am very new to Java and I've spent all weekend trying to fix this code:

    Java Code:
    package account;
    import java.text.*;
    
    public class Account 
    {
        //attributes
        
        private String id;
        private double balance;
        private double annualInterestRate;
        
        
        public Account()
        {
            id = "None";
            balance = 0;
            annualInterestRate = 0;
        }
        
        public Account(String id, double balance, double annualInterestRate)
        {
            this.id = id;
            this.balance = balance;
            this.annualInterestRate = annualInterestRate;
        }
        
        public String getId()
        {
             return id;
        }
        
        public void setId(String id)
        {
            this.id = id;
            
        }
        public double getBalance()
        {
            return balance;
        }
        
        public void setBalance(double balance)
        {
            this.balance = balance;
        }
        
        public double getAnnualInterestRate()
        {
            return annualInterestRate;
        }
        
        public void setAnnualInterestRate(double annualInterestRate)
        {
            this.annualInterestRate = annualInterestRate;
        }
            
        public double monthlyInterestRate;
        public double monthlyInterest;
        public static double newBalance;
        public double deposit;
        public double withdraw;
        
        public double getMonthlyInterestRate()
        {
            return monthlyInterestRate;
        }
        
        public double getMonthlyInterest()
        {
            return monthlyInterest;
        }
      
        public double getDeposit()
        {
            return deposit;
        
        }
        
        public void setDeposit(double deposit)
        {
            this.deposit = deposit;
        }
        
        
        
        public double getWithdraw()
        {
           return withdraw;
        }
        
        public void setWithdraw(double withdraw)
        {
            this.withdraw = withdraw;
        }
        
        public static double getNewBalance()
        {
             
           return newBalance ;     
        } 
    
        
        
        
        @Override
        public String toString()
        {
          NumberFormat currencyFormat = NumberFormat.getCurrencyInstance();
            String report = "Original information about myAcct: \n"
                          + "################################## \n"
                          + "# ID: "+ id + "\n"
                          + "# Balance: "+ balance + "\n"
                          + "# Annual Interest Rate: "+ annualInterestRate +"%" + "\n"
                          + "################################## \n"
                          + "                                   \n"
                          + "Information about myAcct after withdraw and deposit: \n"
                          + "################################## \n"
                          + "# ID: "+ id + "\n"
                          + "# Balance: "+ newBalance + "\n"
                          + "# Annual Interest Rate: "+ annualInterestRate +"%" + "\n"
                          + "################################## \n"
                          + "                                   \n"
                          + "The monthly interest of myAcct: "+ monthlyInterest + "\n";
            
            return report;
            }
    
       
        }
    
    
    package account;
    
    public class Main {
          public static void main(String[] args) 
          {
              Account myAcct = new Account();
                
                myAcct.setId("1122");
                myAcct.setBalance(20000);
                myAcct.setAnnualInterestRate(4.5);
                myAcct.setDeposit(3000);
                myAcct.setWithdraw(2500);
                Account.getNewBalance();
             
                
               //myAcct.deposit(3000);
               //myAcct.withdraw(2500);
                 
               
                //myAcct.newBalance(balance + deposit - withdraw);
                
                System.out.println(myAcct);
    
                
                
          }
    
        
    }

    I appreciate ANY help with this assignment! Hints, suggestions, etc. Also, please forgive if I'm not posting this question correctly. I am very green when it comes to Java.

    Thanks!
    Last edited by pbrockway2; 12-19-2011 at 10:34 PM. Reason: code tags added

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: What Am I doing Wrong?

    Hi siren1111, welcome to the forum!

    I have added "code" tags to your post. The idea is that you put [code] at the start of your code and [/code] at the end. That way formatting is preserved.

    Now, on to your question... except that there isn't one! :)

    Does the code compile? If not, and you can't understand the compiler's messages, copy and post exactly what the compiler outputs and someone is sure to explain what it is grumbling about.

    Or is the problem that the program does something odd/unwanted or unintended at runtime (including throwing a runtime exception)? In that case say what the program actually does when run as well as the correct or intended behaviour. If observed behaviour includes output (including stack traces) copy and post them rather than just saying they are "wrong".

  3. #3
    siren1111 is offline Member
    Join Date
    Dec 2011
    Posts
    2
    Rep Power
    0

    Default Re: What Am I doing Wrong?

    Hello pbrockway2!

    Thanks for your response! You are right, in my desperation I did forget to actually ask the question. Here it is:

    In line 119, I am trying to print the newBalance which is 'newBalance = balance + deposit - withdraw;'. But, I am getting zero for an answer. I am not sure if I am creating the 'constructor' correctly.

    Also, thanks for adding the code tags. I will be mindful of adding them for my next question.

    Thanks!

    Siren1111

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: What Am I doing Wrong?

    Nothing should be static here, except main(). Correcting this will involve changing "Account.getNewBalance()" to "myAcct.getNewBalance()". Avoid the temptation of making things static to shut the compiler up. The compiler is your friend: if it grumbles there is a reason that should be addressed.

    I'm not sure what newBalance is supposed to be. But you don't give it a value at any point - that is there is no line that says newBalance=something - so when you print it it will be zero.

Similar Threads

  1. Replies: 4
    Last Post: 06-11-2013, 01:37 AM
  2. What is wrong?
    By phantom06 in forum New To Java
    Replies: 2
    Last Post: 02-02-2011, 06:08 PM
  3. can't see where it went wrong..
    By Yakg in forum New To Java
    Replies: 5
    Last Post: 01-31-2011, 12:19 PM
  4. What did i do wrong
    By jpnym15 in forum New To Java
    Replies: 8
    Last Post: 11-17-2008, 10:07 AM
  5. what's wrong in here!!!
    By Annatar in forum New To Java
    Replies: 8
    Last Post: 11-14-2008, 02:55 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
  •