Suppose we have 500 as balance and If someone try to withdraw 600 then balance become less than zero i.e. 500 - 600 = -100 and in this situation you want to set balance = 0 ?Daniel Martos wrote:If balance becomes less than zero, set balance = 0.
Carey Brown wrote:I still think your formula is wrong. I think it should be
The way you have it, if you call getBalance() multiple times the variable 'balance' will keep feeding in to itself. I don't think that is what you want. (I'm assuming that 'balance' is a class field.)
There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
fred rosenberger wrote:
Carey Brown wrote:I still think your formula is wrong. I think it should be
The way you have it, if you call getBalance() multiple times the variable 'balance' will keep feeding in to itself. I don't think that is what you want. (I'm assuming that 'balance' is a class field.)
i think it's correct. I'm assuming i have a balance already, say $100. If i deposit $50 and take out $25, my new balance should be 100 + 50 - 25, or $125. Your formula would give 50 - 25, or $25.
Am i seeing this wrong while in my food-coma?
fred rosenberger wrote:
Carey Brown wrote:I still think your formula is wrong. I think it should be
The way you have it, if you call getBalance() multiple times the variable 'balance' will keep feeding in to itself. I don't think that is what you want. (I'm assuming that 'balance' is a class field.)
i think it's correct. I'm assuming i have a balance already, say $100. If i deposit $50 and take out $25, my new balance should be 100 + 50 - 25, or $125. Your formula would give 50 - 25, or $25.
Am i seeing this wrong while in my food-coma?
Paul Clapham wrote:So all those people complaining that it's a really shoddy design for a bank account, they are all correct. It is a really shoddy design. Unfortunately it's the design that Daniel has been told to implement, so all of those complaints are out of order. At this point Daniel just has to write code which does the goofy stuff and move on to the next assignment.
Daniel Martos wrote:A getBalance() that returns balance as equal to balance + deposit-withdraw . If balance becomes less than zero, set balance = 0.
It's saying that if (balance <0) { is an unreachable statement, and that my last } is missing a return statement. Any idea where I'm going wrong?
JLS wrote:8.8.9. Default Constructor
If a class contains no constructor declarations, then a default constructor is implicitly declared.
That wouldn't compile, Java is a case sensitive, so there should be 'System.out.println()'.Instructions wrote:Use system.out.println statements
Liutauras Vilda wrote:2 quick things.
1. 'balance' and 'deposits' make private.
2. In the constructor of BankAccount don't duplicate code, just use method 'setDeposit()' which does what you need (unfortunately but instructions suggests duplicate code, I'd still do, though, as I suggested).
By the way, remind your teacher that you cannot add 'default' constructor, instead you can add no-argument constructor, which should be fine too. And it isn't nitpicking. It might not that important here, but there are places where it is very important to get terminology right.
JLS wrote:8.8.9. Default Constructor
If a class contains no constructor declarations, then a default constructor is implicitly declared.
Daniel Martos wrote:
Liutauras Vilda wrote:2 quick things.
1. 'balance' and 'deposits' make private.
2. In the constructor of BankAccount don't duplicate code, just use method 'setDeposit()' which does what you need (unfortunately but instructions suggests duplicate code, I'd still do, though, as I suggested).
By the way, remind your teacher that you cannot add 'default' constructor, instead you can add no-argument constructor, which should be fine too. And it isn't nitpicking. It might not that important here, but there are places where it is very important to get terminology right.
JLS wrote:8.8.9. Default Constructor
If a class contains no constructor declarations, then a default constructor is implicitly declared.
Will do, but back to the original question. How to I set balance to 0? I tried this:
Daniel Martos wrote:Will do, but back to the original question. How to I set balance to 0?
You'll be able to access in the subclass via super.getBalance(), etc.Liutauras Vilda wrote:1. 'balance' and 'deposits' make private.
Instructions wrote:BankAccount
Contains the following instance variables:
• name of type String,
• deposit of type double initialized to a zero.
• balance of type double initialized to a zero
Contains the following constructors:
• A default constructor BankAccount(){}
• A 2-arg constructor BankAccount(String name, double deposit) that takes in a string value and sets the name and takes in a double value to set the deposit. Set balance = balance+deposit within this 2-arg constructor.
Contains the following methods:
• A setter for name and deposit instance variables called setName and setDeposit, respectively. When you set the deposit to a new value, also set balance = balance+deposit within the same setter setDeposit method.
• A getter for name and balance instance variables.
Instruction wrote:CheckingAccount
Has the following instance variables (in addition to whatever it inherits) :
• withdraw of the type double and gives the amount to withdraw from the current balance.
Has the following constructors:
• A default constructor called CheckingAccount()
• A 3 -arg constructor called CheckingAccount(String name, double deposit , double withdraw) and sets the instance variables to the respective values. Make sure to use super(name, deposit) to set the vales of name and email. You may use this.withdraw = withdraw to set the values of withdraw.
Has the following methods:
• A setter for instance variables withdraw and deposit.
• A getBalance() that returns balance as equal to balance + deposit-withdraw . If balance becomes less than zero, set balance = 0.
So, you're having problems with getBalance(), right? Remember I have mentioned that you can call methods from super class refering to them with keyword 'super.methodName'.
Remember, that in the superclass once you set the deposit, the balance automatically gets altered.
So, in this CheckingAccount class, do you really need to add deposit again to the balance?
If you two don't stop this rough-housing somebody is going to end up crying. Sit down and read this tiny ad:
New web page for Paul's Rocket Mass Heaters movies
https://coderanch.com/t/785239/web-page-Paul-Rocket-Mass
|