• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Code issue

 
Ranch Hand
Posts: 126
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Happy Thanksgiving!!!  This is part of a code that I'm working on:

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?
 
Bartender
Posts: 10980
87
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
On line 2 you are returning, nothing is permitted after that.
If you take away the 'return', 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.)
 
Bartender
Posts: 1251
87
Hibernate jQuery Spring MySQL Database Tomcat Server Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think method getBalance() should return only balance and It is better to have to separate methods for deposit and withdraw.

Daniel Martos wrote:If balance becomes less than zero, set balance = 0.

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 ?  
I think we should not let balance become less than 0. If your requirement allows balance to be in negative then It would be wrong logic to set balance as 0 when It is suppose to be -100.

If you don't want withdraw process to happen when available balance is less than withdrawal amount then you can write condition for that in withdraw method and print appropriate message else have enough balance? then minus withdraw amount from balance.
 
lowercase baba
Posts: 13091
67
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?
 
Carey Brown
Bartender
Posts: 10980
87
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?


Sorry, I thought deposit and withdrawal was cumulative.
 
Daniel Martos
Ranch Hand
Posts: 126
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?



Let me be more specific, here's the question and my full code:

Write and test a program that contains the follows classes:

 
Sheriff
Posts: 28368
99
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Ranch Hand
Posts: 126
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.



lol, I can only do what I'm asked to do.
 
Paul Clapham
Sheriff
Posts: 28368
99
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay, let's get back to your original problem.

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?



One thing I notice: in some cases you're supposed to "set balance = 0". And you don't have any code which does that. You have code which returns 0 in those cases, but no code which sets balance = 0.

Your requirements are sort of murky ("returns balance as equal to balance + deposit-withdraw") but my interpretation is you're supposed to do this:

1. Calculate the new value of the balance variable.

2. Return it.

So why don't you fix your code to do things in that order?

(It's also possible that if balance becomes less than zero, you're supposed to return the negative value while also setting balance to zero. It's not impossible to interpret the requirements that way (I should mention that I'm an expert in misinterpreting instructions) but I'd say that would be going over the top in terms of shoddy design.)
 
Sheriff
Posts: 8988
652
Mac OS X Spring VI Editor BSD Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
Liutauras Vilda
Sheriff
Posts: 8988
652
Mac OS X Spring VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Such instruction takes all creativity out of student, and even more worse, suggests to use poorly named variables as 'chkAct1', hell knows what it means - we could guess only.

Instructions wrote:Use system.out.println statements

That wouldn't compile, Java is a case sensitive, so there should be 'System.out.println()'.
 
Daniel Martos
Ranch Hand
Posts: 126
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Ranch Hand
Posts: 126
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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:



• 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.
 
Liutauras Vilda
Sheriff
Posts: 8988
652
Mac OS X Spring VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Daniel Martos wrote:Will do, but back to the original question.  How to I set balance to 0?


I'd suggest not to implement any new things until you ensure what you have now is correct.
Until which point of the instructions you done your code and tested?
 
Liutauras Vilda
Sheriff
Posts: 8988
652
Mac OS X Spring VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Wait, don't mix up things. Which class you're talking about?

BankAccount class, have you done that fully? Tested?

Well, looking to BankAccount and CheckingAccount I think I see something incorrect. For instance:

1. BankAccount. Let's assume you have 100$. You set deposit 50, so you end up with balance 150. getBalance gives you 150, all good.
2. CheckingAccount. It inherits balance and deposit. Balance is 150, deposit is 50. So from the CheckingAccount you call getBalance(), what your current code gives? return balance = balance + deposit - withdraw.

That means if I'm not mistaken, that to the current 150, you'd add again 50, then subtract 'withdraw' whatever that might be. So where this extra 50 coming from?


 
Liutauras Vilda
Sheriff
Posts: 8988
652
Mac OS X Spring VI Editor BSD Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Basically you did wrongly your setDeposit in BankAccount. Read instructions. set deposit should set instance variable. getBalance should do some calculations. But not both.
Forget CheckingAccount and TestAccountYourName classes for now.

Do one class, add main method temporary for testing purpose and make sure it does what instructions say. Don't do your own interpretations on what it should do extra.


[edit] ignore that, I confused with reading different instructions part. Sorry.
 
Liutauras Vilda
Sheriff
Posts: 8988
652
Mac OS X Spring VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Liutauras Vilda wrote:1. 'balance' and 'deposits' make private.

You'll be able to access in the subclass via super.getBalance(), etc.
 
Liutauras Vilda
Sheriff
Posts: 8988
652
Mac OS X Spring VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Instruction are as hell confusing I should say. I'm confusing myself now.

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.


Your written code below (adding so we could see in one place)

Alright, this one it seems as it has been requested in instructions. Just make those two instance variables 'private' as I mentioned initially.
 
Liutauras Vilda
Sheriff
Posts: 8988
652
Mac OS X Spring VI Editor BSD Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.


Your code:


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?
 
Daniel Martos
Ranch Hand
Posts: 126
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?



Sorry I've been away a few days, I will try this. thanks.
 
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
reply
    Bookmark Topic Watch Topic
  • New Topic