Results 1 to 7 of 7
  1. #1
    armedrabbit is offline Member
    Join Date
    Mar 2011
    Posts
    10
    Rep Power
    0

    Unhappy Help with java

    This illustrates one of the important functions of methods. They should guarantee that the instance data of the object is always valid. In this case the methods (and constructor) prevent the balance from ever being negative. They also prevent reject invalid parameters.

    You should test all the modified methods and constructors but you do not need to submit a Tester class


    So this is what i have so far, can anyone help me with it?

    /**
    A bank account has a balance that can be changed by
    deposits and withdrawals.
    */
    public class BankAccount
    {
    public static void main(String[] args)
    {
    double balance;
    int transaction;

    if (balance < 0)
    System.out.println("ERROR NEGATIVE AMOUNT");
    else
    /**
    Constructs a bank account with a zero balance.
    */
    public BankAccount()
    {
    balance = 0;
    }

    /**
    Constructs a bank account with a given balance.
    @param initialBalance the initial balance
    */
    public BankAccount(double initialBalance)
    {
    balance = initialBalance;
    }

    /**
    Deposits money into the bank account./**

    Im not exactly sure if i have to make a tester class or not, because i do not know how to combine the classes.


    /**
    A class to test the BankAccount class.
    */
    public class BankAccountTester
    {
    /**
    Tests the methods of the BankAccount class.
    @param args not used
    */
    public static void main(String[] args)
    {

    BankAccount harrysChecking = new BankAccount();

    harrysChecking.deposit(2000);
    harrysChecking.withdraw(500);
    System.out.println(harrysChecking.getBalance());

    System.out.println("Expected: 1500");
    harrysChecking.endOfMonth();
    System.out.println(harrysChecking.getBalance());
    System.out.println("Expected: 1498");
    harrysChecking.deposit(1000);
    harrysChecking.withdraw(500);
    harrysChecking.endOfMonth();
    System.out.println(harrysChecking.getBalance());
    System.out.println("Expected: 1996");
    }
    }
    Last edited by armedrabbit; 03-07-2011 at 05:54 PM. Reason: accidently double paste

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    in the methods where you want to only accept a positive number just put an if clause to test that they are positive. If the argument is positive, continue as normal, if not print that the number needs to be positive and don't make any changes.

  3. #3
    armedrabbit is offline Member
    Join Date
    Mar 2011
    Posts
    10
    Rep Power
    0

    Default

    Quote Originally Posted by sunde887 View Post
    in the methods where you want to only accept a positive number just put an if clause to test that they are positive. If the argument is positive, continue as normal, if not print that the number needs to be positive and don't make any changes.
    can you give me an example? where do i put the if statement?

  4. #4
    Junky's Avatar
    Junky is online now Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,760
    Rep Power
    7

    Default

    To be blunt, your code is a mess.

    You have the BankAccount constructors and methods inside a main method. You also have 2 BankAccountTester classes, one inside BankAccount and one separate. Clean your code up and things might look better and be easier to see what needs to be done.

  5. #5
    armedrabbit is offline Member
    Join Date
    Mar 2011
    Posts
    10
    Rep Power
    0

    Default

    sorry Junky, i accidently double pasted. but i'll try to fix it right now...

  6. #6
    armedrabbit is offline Member
    Join Date
    Mar 2011
    Posts
    10
    Rep Power
    0

    Default

    Here's my updated program can you guys tell me if i did it right? :confused:


    /**
    A bank account has a balance that can be changed by
    deposits and withdrawals.
    */
    public class BankAccount
    {

    double balance;
    int transaction;
    /**
    Constructs a bank account with a zero balance.
    */
    public BankAccount()
    {

    if (balance < 0)
    System.out.println("ERROR NEGATIVE AMOUNT");
    }

    /**
    Constructs a bank account with a given balance.
    @param initialBalance the initial balance
    */
    public BankAccount(double initialBalance)
    {
    balance = initialBalance;
    initialBalance = 0;
    }

    /**
    Deposits money into the bank account.
    @param amount the amount to deposit
    */
    public void deposit(double amount)
    {
    double newBalance = balance + amount;
    balance = newBalance;
    transaction = transaction +1;
    if (balance < 0)
    System.out.println("ERROR NEGATIVE AMOUNT");

    }


    /**
    Withdraws money from the bank account.
    @param amount the amount to withdraw
    */
    public void withdraw(double amount)
    {
    double newBalance = balance - amount;
    balance = newBalance;
    transaction = transaction +1;
    if (balance < 0)
    System.out.println("ERROR NEGATIVE AMOUNT");
    }

    /**
    Gets the current balance of the bank account.
    @return the current balance
    */
    public double getBalance()
    {
    if (balance < 0)
    System.out.println("ERROR NEGATIVE AMOUNT");
    return balance;
    }
    /**
    Charges monthly fee
    */
    public void endOfMonth()
    {
    double newbalance = balance - transaction;
    balance = newbalance;
    transaction = 0;
    }
    }


    -----------------------------------------------------------------------

    /**
    A class to test the BankAccount class.
    */
    public class BankAccountTester
    {
    /**
    Tests the methods of the BankAccount class.
    @param args not used
    */
    public static void main(String[] args)
    {

    BankAccount harrysChecking = new BankAccount();

    harrysChecking.deposit(2000);
    harrysChecking.withdraw(2100);
    System.out.println(harrysChecking.getBalance());


    harrysChecking.endOfMonth();
    System.out.println(harrysChecking.getBalance());

    harrysChecking.deposit(1000);
    harrysChecking.withdraw(500);
    harrysChecking.endOfMonth();
    System.out.println(harrysChecking.getBalance());

    }
    }

  7. #7
    juhiswt is offline Member
    Join Date
    Mar 2011
    Posts
    30
    Rep Power
    0

    Default

    import java.util.Scanner;
    class BankAccount
    {
    public Scanner S=new Scanner(System.in);
    public double balance,amnt,initialBalance;




    /**
    Constructs a bank account with a given balance.
    @param initialBalance the initial balance
    */
    void newBankAccount()
    {
    System.out.println("Enter The initail Balance");
    double initialBalance=S.nextDouble();

    if(initialBalance<0)
    balance = 0;
    else
    balance=initialBalance;
    }

    /**
    Deposits money into the bank account.*/
    void deposit()
    {
    System.out.println("Enter The amount to be deposited");
    double amnt=S.nextDouble();

    if(amnt<0)
    {
    balance=balance;
    System.out.println("This value can not be deposited");
    }
    else
    balance=balance+amnt;
    }

    /** Withrawl money from bank account*/
    void withdrawls()
    {

    System.out.println("Enter The amount to be withdrwaled");
    double amnt=S.nextDouble();
    if(amnt<0)
    {
    balance=balance;
    System.out.println("This value can not be withdrawdled");
    }
    else
    balance=balance-amnt;
    }
    void getBalance()
    {
    System.out.println("The Balance is:"+balance);
    }
    }



    /**
    A class to test the BankAccount class.
    */
    public class BankAccountTester
    {
    /**
    Tests the methods of the BankAccount class.
    @param args not used
    */
    public static void main(String[] args)
    {

    BankAccount h = new BankAccount();

    Scanner S=new Scanner(System.in);
    int ch;


    do
    {
    System.out.println("1.To create a new account");
    System.out.println("2.To deposit the amount in account");
    System.out.println("3.To Withdrawal tha amount from account");
    System.out.println("4.to get the current balance");
    System.out.println("5.Exit");

    System.out.println("Enter the choice");
    ch=S.nextInt();
    switch(ch)
    {
    case 1: h.newBankAccount();
    break;
    case 2:
    h.deposit();
    break;
    case 3:
    h.withdrawls();
    break;
    case 4: h.getBalance();
    break;
    case 5: System.exit(0);
    break;
    default:
    System.out.println("Invalid");
    }
    }while(ch>1||ch<5);
    }



    }

Similar Threads

  1. Mod of Negative Numbers
    By Venny in forum New To Java
    Replies: 7
    Last Post: 01-28-2011, 05:32 AM
  2. Byte value comes out negative
    By Bluefox815 in forum New To Java
    Replies: 1
    Last Post: 07-03-2010, 03:52 AM
  3. I don't want negative money [BUG]
    By anthonym2121 in forum New To Java
    Replies: 1
    Last Post: 04-07-2009, 08:06 AM
  4. Replies: 1
    Last Post: 12-28-2008, 10:25 AM
  5. Negative elements
    By swikar.java in forum New To Java
    Replies: 6
    Last Post: 12-15-2008, 04:48 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
  •