Results 1 to 20 of 41
Thread: Class help
- 11-14-2007, 02:57 AM #1
Member
- Join Date
- Nov 2007
- Posts
- 38
- Rep Power
- 0
Class help
Hey Guys,
I'm defining my class for my mini java project. I'm very new to all of this. I'm trying to figure out how the following code works and whether it is correct:
If the amount is $20 and I want to return the total in "accountbalance", is the above code correct ? I don't know if it should be:Java Code:private int accountbalance; public deposit( double amount ) { return accountbalance = accountbalance + amount ; }
return accountbalance = accountbalance + amount ;
-OR-
return accountbalance + amount ;
- 11-14-2007, 04:05 AM #2
The correct return statement would be return accountbalance + amount;
alternatively i think you could either declare a new variable and assign accountbalance + amount, or you could initialize accountbalance to be accountbalance + amount and return that ( use shorthand accountbalance += amount, shorthand for accountbalance = acountbalance + amount)
personally, i'd stick with return accountbalance + amount.
hope that helped :p
- 11-14-2007, 04:09 AM #3
Member
- Join Date
- Nov 2007
- Posts
- 38
- Rep Power
- 0
Thanks. I've done that. Here is the updated code:
But I keep getting this error when I build the class:Java Code:public int deposit( double amount ) { return accountbalance + amount ; }
Java Code:possible loss of precision found : double required: int return accountbalance + amount ;
- 11-14-2007, 04:26 AM #4
hmm.. the argument amount is a double, so any number would be a double.. unless you declared an int variable, and called the method with the name of the int variable for the argument. but then it would have been found int required double, so i'm guessing its the problem with accountbalance.. i would have to see the rest of the class(es) to figure anything else out.
- 11-14-2007, 04:29 AM #5
Member
- Join Date
- Nov 2007
- Posts
- 38
- Rep Power
- 0
accountBalance is double.
Do I need to change:
public int deposit( double amt )
-TO-
public double deposit( double amt )
??
- 11-14-2007, 04:38 AM #6
yes thats it... you said it returns a int in the method signature thing but with return statement returned a double ( even if just account balance or just amount is double, value will be superior primitive - double.) change that to a public double right on
- 11-14-2007, 04:39 AM #7
and remember if you ever declare a method without void or a return type, its automatically void.
- 11-14-2007, 05:02 AM #8
Member
- Join Date
- Nov 2007
- Posts
- 38
- Rep Power
- 0
Thanks, I don't fully understand what you meant by your last post.
I have another question, I want to use a boolean, here is my function:
My question is, on the declaration line, do I need to declare a bool between the brackets ?Java Code:public boolean isOpen(boolean i) { if(i == false) { return false; /*Not Open*/ } else { return true; /*Is Open*/ } }
public boolean isOpen(boolean i)
-OR-
public boolean isOpen()
Which one is correct ? Or the better practise ?
- 11-14-2007, 05:09 AM #9
Member
- Join Date
- Nov 2007
- Posts
- 6
- Rep Power
- 0
Yes, you do need to have the boolean in the parentheses.
public boolean isOpen(boolean i)
would be the correct way to do it. The boolean inside the parentheses passes the argument (or parameter, whatever you like to call it) to the method so that the method can use that variable. Without it there, you wouldn't be able to use i in your method.
- 11-14-2007, 05:19 AM #10
Member
- Join Date
- Nov 2007
- Posts
- 38
- Rep Power
- 0
Thanks. Yet another question. Is the following code ok:
Java Code:public double takeMoneyOut( double amount ) { double totalBalance; totalBalance = balance + overdraftLimit ; if( amount > totalBalance ) { // error message } else { // return totalBalance } }
- 11-14-2007, 05:32 AM #11
Member
- Join Date
- Nov 2007
- Posts
- 6
- Rep Power
- 0
Sure! It doesn't do a whole lot at the moment, but everything in the code is good. The only thing I noticed was that there is a possibility that you don't return anything, which would cause an error. Since you said that the method would return a double, it has to eventually return a double.
The case where it wouldn't return a double would be if amount > totalBalance. In that case, you say it is going to give an error message. It might be better to return something like a -1 (or some other obscure value that it shouldn't be returning) and then handle the error outside of the method, wherever you call it.
Erm... does that make sense? The main thing I'm trying to say is that if you say you are going to return a double, you have to return a double no matter what.
- 11-14-2007, 05:41 AM #12
Member
- Join Date
- Nov 2007
- Posts
- 38
- Rep Power
- 0
So its not wise to output error messages in classes ? If yes, how come ?
I might return 0 or something.
- 11-14-2007, 05:53 AM #13
Member
- Join Date
- Nov 2007
- Posts
- 6
- Rep Power
- 0
Not necessarily. I'm not exactly an expert on what is right and what is wrong, but I'm guessing that in some cases it would be perfectly fine and even logical to print errors messages in the class. In your case, however, you need to return a double. If you wanted to print out an error and then return the double as well, that might work.
0 might not work, if the amount being taken out is exactly the same as the amount in the balance.
- 11-14-2007, 06:13 AM #14
Member
- Join Date
- Nov 2007
- Posts
- 38
- Rep Power
- 0
No problem. Ok Im trying to do something else. From my function "takeMoneyOut" I want it to call another boolean function which will check if the dollars being taken out is over the accountbalance limit and thus return a true or false. But I dont know how I can do this. See code:
This code doesnt work though.Java Code:public double takeMoneyOut( double amount ) { double totalBalance; totalBalance = balance + overdraftLimit ; if( CheckWithdrawal(amount) == false ) { // error message } else { // return totalBalance } } public boolean CheckWithdrawal(boolean checkWithdraw) { if(checkWithdraw > balance ) { return true; } else { return false; } }
- 11-14-2007, 02:10 PM #15
try this
if that doesn't work at the if statement instead of if checkwithdrawal(amount) == false try making a boolean variable and assigning yourObject.CheckWithdrawal(amount); then doing if ( yourbool == false ) code.Java Code:i think it will work after changing these things: public double takeMoneyOut( double amount ) { double totalBalance; totalBalance = balance + overdraftLimit ; if( CheckWithdrawal(amount) == false ) { [B]System.err.println("You tried to withdraw more than you have.");[/B] } else { [B]return totalBalance[/B] } } public boolean CheckWithdrawal(boolean checkWithdraw) { if(checkWithdraw > balance ) { return true; } else { return false; } }
btw u could do the error with system.out, err just means its an errorLast edited by JT4NK3D; 11-14-2007 at 02:12 PM. Reason: fergot stuff
- 11-14-2007, 05:01 PM #16
Member
- Join Date
- Nov 2007
- Posts
- 38
- Rep Power
- 0
Thanks, I get the following errors when I compile it:
code: if( CheckWithdrawal(amount) == false )
error: CheckWithdrawal(boolean) in Account cannot be applied to (double)
if( CheckWithdrawal(amount) == false )
code: if(check > balance )
error: operator > cannot be applied to boolean,double
if(check > balance )
In essence what I'm doing is comparing a boolean against a value. How can I use the object one ? would I have to call it from main() ?
- 11-14-2007, 08:05 PM #17
In the takeMoneyOut method the argument "amount" is of type double. The if statement in this method is calling the CheckWithdrawal method sending the value of the "amount" variable. So this method call is to a (n apparantly non-existent) method with this type signature:Java Code:public double takeMoneyOut( double amount ) { double totalBalance; totalBalance = balance + overdraftLimit; if( CheckWithdrawal(amount) == false ) { // error message } else { // return totalBalance } } public boolean CheckWithdrawal(boolean checkWithdraw) { if(checkWithdraw > balance ) { return true; } else { return false; } }
But the type signature of your CheckWithdrawal method is this:Java Code:boolean CheckWithdrawal(double value)
So java cannot find a method with the type signature that your takeMoneyOut method is calling, ie, java cannot find a takeMoneyOut method that takes a (type) double argument. This causes the first compile error:Java Code:boolean CheckWithdrawal(boolean checkWithdraw)
In the CheckWithdrawal method the argument "checkWithdraw" is (primitive type) boolean. In the if statement the condition has a type mis-match:Java Code:error: CheckWithdrawal(boolean) in Account cannot be applied to (double) if( CheckWithdrawal(amount) == false )
which results in the second compile errorJava Code:// boolean double if(checkWithdraw > balance )
To compare these two variables they must be the same type. Java is very prissy/picky about variable types. This is to avoid confusion: so everyone is very clear about what types are being passed around/manipulated by the jvm (java virtual machine).Java Code:error: operator > cannot be applied to boolean,double if(check > balance )
So how to fix this?
Change this
to thisJava Code:public boolean CheckWithdrawal(boolean checkWithdraw)
Java Code:public boolean CheckWithdrawal(double checkWithdraw)
- 11-14-2007, 08:13 PM #18
Member
- Join Date
- Nov 2007
- Posts
- 38
- Rep Power
- 0
Excellent, fixed!
I have some more questions.
1. what does the ++ mean when it is infront of a variable e.g.
Also, a conversion constructor:Java Code:int n; n = ++MyAccount;
1. What is it and whats its purpose ?
2. If I wanted to create one to deal with the customers withdrawal/deposits how would I do that ?
- 11-14-2007, 08:53 PM #19
what does the ++ mean when it is infront of a variable
Look at the last two paragraphs on this page Assignment, Arithmetic, and Unary Operators.
conversion constructor
Seems to be a C++ term; I'm not familiar with it in java. If you can give some
more detail we might be able to come up with a java solution for what you want to do.
- 11-14-2007, 09:24 PM #20
Member
- Join Date
- Nov 2007
- Posts
- 38
- Rep Power
- 0
Thanks. I guess my friend coined up that term then. I will ask him what he meant by that.
If I have the following line in main():
and in the class I haveJava Code:trans t1 = new Trans( account1, Operation.WITHDRAWAL, 100 ) ;
If I want to proccess the values from the line from main() in the trans class, how would I do that ?Java Code:public class trans { private Operation op ; /*Deposit or Withdrawal*/ private Account acc ; /*account for which a transaction*/ private double amt ; /*amount of money involved in that transaction, deposit or withdrawal*/ }Last edited by Shaolin; 11-14-2007 at 09:26 PM.
Similar Threads
-
what is the Priority for execution of Interface class and a Abstract class
By Santoshbk in forum Advanced JavaReplies: 0Last Post: 04-02-2008, 07:04 AM -
Accessing inner class from outer class (an example)
By Java Tip in forum Java TipReplies: 0Last Post: 02-17-2008, 09:03 AM -
An example of accessing outer class from inner class
By Java Tip in forum Java TipReplies: 0Last Post: 02-17-2008, 09:01 AM -
Inner class accessing outer class
By Java Tip in forum Java TipReplies: 0Last Post: 02-17-2008, 08:59 AM -
Accessing one class from another class through swing
By kbyrne in forum AWT / SwingReplies: 5Last Post: 01-03-2008, 07:54 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks