Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-14-2007, 03:57 AM
Member
 
Join Date: Nov 2007
Posts: 36
Shaolin is on a distinguished road
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:

Code:
private int accountbalance; public deposit( double amount ) { return accountbalance = accountbalance + amount ; }
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:

return accountbalance = accountbalance + amount ;

-OR-

return accountbalance + amount ;
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 11-14-2007, 05:05 AM
JT4NK3D's Avatar
Member
 
Join Date: Nov 2007
Posts: 50
JT4NK3D is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 11-14-2007, 05:09 AM
Member
 
Join Date: Nov 2007
Posts: 36
Shaolin is on a distinguished road
Thanks. I've done that. Here is the updated code:

Code:
public int deposit( double amount ) { return accountbalance + amount ; }
But I keep getting this error when I build the class:

Code:
possible loss of precision found : double required: int return accountbalance + amount ;
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 11-14-2007, 05:26 AM
JT4NK3D's Avatar
Member
 
Join Date: Nov 2007
Posts: 50
JT4NK3D is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 11-14-2007, 05:29 AM
Member
 
Join Date: Nov 2007
Posts: 36
Shaolin is on a distinguished road
accountBalance is double.

Do I need to change:

public int deposit( double amt )

-TO-

public double deposit( double amt )


??
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 11-14-2007, 05:38 AM
JT4NK3D's Avatar
Member
 
Join Date: Nov 2007
Posts: 50
JT4NK3D is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 11-14-2007, 05:39 AM
JT4NK3D's Avatar
Member
 
Join Date: Nov 2007
Posts: 50
JT4NK3D is on a distinguished road
and remember if you ever declare a method without void or a return type, its automatically void.
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 11-14-2007, 06:02 AM
Member
 
Join Date: Nov 2007
Posts: 36
Shaolin is on a distinguished road
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:

Code:
public boolean isOpen(boolean i) { if(i == false) { return false; /*Not Open*/ } else { return true; /*Is Open*/ } }
My question is, on the declaration line, do I need to declare a bool between the brackets ?

public boolean isOpen(boolean i)

-OR-

public boolean isOpen()

Which one is correct ? Or the better practise ?
Bookmark Post in Technorati
Reply With Quote
  #9 (permalink)  
Old 11-14-2007, 06:09 AM
Member
 
Join Date: Nov 2007
Posts: 6
blackstone is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #10 (permalink)  
Old 11-14-2007, 06:19 AM
Member
 
Join Date: Nov 2007
Posts: 36
Shaolin is on a distinguished road
Thanks. Yet another question. Is the following code ok:

Code:
public double takeMoneyOut( double amount ) { double totalBalance; totalBalance = balance + overdraftLimit ; if( amount > totalBalance ) { // error message } else { // return totalBalance } }
Bookmark Post in Technorati
Reply With Quote
  #11 (permalink)  
Old 11-14-2007, 06:32 AM
Member
 
Join Date: Nov 2007
Posts: 6
blackstone is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #12 (permalink)  
Old 11-14-2007, 06:41 AM
Member
 
Join Date: Nov 2007
Posts: 36
Shaolin is on a distinguished road
So its not wise to output error messages in classes ? If yes, how come ?

I might return 0 or something.
Bookmark Post in Technorati
Reply With Quote
  #13 (permalink)  
Old 11-14-2007, 06:53 AM
Member
 
Join Date: Nov 2007
Posts: 6
blackstone is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #14 (permalink)  
Old 11-14-2007, 07:13 AM
Member
 
Join Date: Nov 2007
Posts: 36
Shaolin is on a distinguished road
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:

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; } }
This code doesnt work though.
Bookmark Post in Technorati
Reply With Quote
  #15 (permalink)  
Old 11-14-2007, 03:10 PM
JT4NK3D's Avatar
Member
 
Join Date: Nov 2007
Posts: 50
JT4NK3D is on a distinguished road
try this
Code:
i think it will work after changing these things: public double takeMoneyOut( double amount ) { double totalBalance; totalBalance = balance + overdraftLimit ; if( CheckWithdrawal(amount) == false ) { System.err.println("You tried to withdraw more than you have."); } else { return totalBalance } } public boolean CheckWithdrawal(boolean checkWithdraw) { if(checkWithdraw > balance ) { return true; } else { return false; } }
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.
btw u could do the error with system.out, err just means its an error

Last edited by JT4NK3D : 11-14-2007 at 03:12 PM. Reason: fergot stuff
Bookmark Post in Technorati
Reply With Quote
  #16 (permalink)  
Old 11-14-2007, 06:01 PM
Member
 
Join Date: Nov 2007
Posts: 36
Shaolin is on a distinguished road
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() ?
Bookmark Post in Technorati
Reply With Quote
  #17 (permalink)  
Old 11-14-2007, 09:05 PM
Senior Member
 
Join Date: Jul 2007
Posts: 1,124
hardwired is on a distinguished road
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; } }
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:
Code:
boolean CheckWithdrawal(double value)
But the type signature of your CheckWithdrawal method is this:
Code:
boolean CheckWithdrawal(boolean checkWithdraw)
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:
Code:
error: CheckWithdrawal(boolean) in Account cannot be applied to (double) if( CheckWithdrawal(amount) == false )
In the CheckWithdrawal method the argument "checkWithdraw" is (primitive type) boolean. In the if statement the condition has a type mis-match:
Code:
// boolean double if(checkWithdraw > balance )
which results in the second compile error
Code:
error: operator > cannot be applied to boolean,double if(check > 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).
So how to fix this?
Change this
Code:
public boolean CheckWithdrawal(boolean checkWithdraw)
to this
Code:
public boolean CheckWithdrawal(double checkWithdraw)
Bookmark Post in Technorati
Reply With Quote
  #18 (permalink)  
Old 11-14-2007, 09:13 PM
Member
 
Join Date: Nov 2007
Posts: 36
Shaolin is on a distinguished road
Excellent, fixed!

I have some more questions.

1. what does the ++ mean when it is infront of a variable e.g.

Code:
int n; n = ++MyAccount;
Also, a conversion constructor:

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 ?
Bookmark Post in Technorati
Reply With Quote
  #19 (permalink)  
Old 11-14-2007, 09:53 PM
Senior Member
 
Join Date: Jul 2007
Posts: 1,124
hardwired is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #20 (permalink)  
Old 11-14-2007, 10:24 PM
Member
 
Join Date: Nov 2007
Posts: 36
Shaolin is on a distinguished road
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():

Code:
trans t1 = new Trans( account1, Operation.WITHDRAWAL, 100 ) ;
and in the class I have

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*/ }
If I want to proccess the values from the line from main() in the trans class, how would I do that ?

Last edited by Shaolin : 11-14-2007 at 10:26 PM.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
what is the Priority for execution of Interface class and a Abstract class Santoshbk Advanced Java 0 04-02-2008 08:04 AM
Accessing inner class from outer class (an example) Java Tip Java Tips 0 02-17-2008 10:03 AM
An example of accessing outer class from inner class Java Tip Java Tips 0 02-17-2008 10:01 AM
Inner class accessing outer class Java Tip Java Tips 0 02-17-2008 09:59 AM
Accessing one class from another class through swing kbyrne AWT / Swing 5 01-03-2008 08:54 AM


All times are GMT +3. The time now is 05:52 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org