Page 1 of 3 123 LastLast
Results 1 to 20 of 41

Thread: Class help

  1. #1
    Shaolin is offline Member
    Join Date
    Nov 2007
    Posts
    38
    Rep Power
    0

    Default 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:

    Java 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 ;

  2. #2
    JT4NK3D's Avatar
    JT4NK3D is offline Member
    Join Date
    Nov 2007
    Posts
    50
    Rep Power
    0

    Default

    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

  3. #3
    Shaolin is offline Member
    Join Date
    Nov 2007
    Posts
    38
    Rep Power
    0

    Default

    Thanks. I've done that. Here is the updated code:

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

    Java Code:
    possible loss of precision
    found   : double
    required: int
            return accountbalance + amount ;

  4. #4
    JT4NK3D's Avatar
    JT4NK3D is offline Member
    Join Date
    Nov 2007
    Posts
    50
    Rep Power
    0

    Default

    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.

  5. #5
    Shaolin is offline Member
    Join Date
    Nov 2007
    Posts
    38
    Rep Power
    0

    Default

    accountBalance is double.

    Do I need to change:

    public int deposit( double amt )

    -TO-

    public double deposit( double amt )


    ??

  6. #6
    JT4NK3D's Avatar
    JT4NK3D is offline Member
    Join Date
    Nov 2007
    Posts
    50
    Rep Power
    0

    Default

    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

  7. #7
    JT4NK3D's Avatar
    JT4NK3D is offline Member
    Join Date
    Nov 2007
    Posts
    50
    Rep Power
    0

    Default

    and remember if you ever declare a method without void or a return type, its automatically void.

  8. #8
    Shaolin is offline Member
    Join Date
    Nov 2007
    Posts
    38
    Rep Power
    0

    Default

    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:

    Java 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 ?

  9. #9
    blackstone is offline Member
    Join Date
    Nov 2007
    Posts
    6
    Rep Power
    0

    Default

    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.

  10. #10
    Shaolin is offline Member
    Join Date
    Nov 2007
    Posts
    38
    Rep Power
    0

    Default

    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. #11
    blackstone is offline Member
    Join Date
    Nov 2007
    Posts
    6
    Rep Power
    0

    Default

    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.

  12. #12
    Shaolin is offline Member
    Join Date
    Nov 2007
    Posts
    38
    Rep Power
    0

    Default

    So its not wise to output error messages in classes ? If yes, how come ?

    I might return 0 or something.

  13. #13
    blackstone is offline Member
    Join Date
    Nov 2007
    Posts
    6
    Rep Power
    0

    Default

    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.

  14. #14
    Shaolin is offline Member
    Join Date
    Nov 2007
    Posts
    38
    Rep Power
    0

    Default

    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:

    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;
          	}
          }
    This code doesnt work though.

  15. #15
    JT4NK3D's Avatar
    JT4NK3D is offline Member
    Join Date
    Nov 2007
    Posts
    50
    Rep Power
    0

    Lightbulb try this

    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;
          	}
          }
    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 02:12 PM. Reason: fergot stuff

  16. #16
    Shaolin is offline Member
    Join Date
    Nov 2007
    Posts
    38
    Rep Power
    0

    Default

    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() ?

  17. #17
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    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;
        }
    }
    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:
    boolean CheckWithdrawal(double value)
    But the type signature of your CheckWithdrawal method is this:
    Java 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:
    Java 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:
    Java Code:
    //   boolean       double
    if(checkWithdraw > balance )
    which results in the second compile error
    Java 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
    Java Code:
    public boolean CheckWithdrawal(boolean checkWithdraw)
    to this
    Java Code:
    public boolean CheckWithdrawal(double checkWithdraw)

  18. #18
    Shaolin is offline Member
    Join Date
    Nov 2007
    Posts
    38
    Rep Power
    0

    Default

    Excellent, fixed!

    I have some more questions.

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

    Java 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 ?

  19. #19
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    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.

  20. #20
    Shaolin is offline Member
    Join Date
    Nov 2007
    Posts
    38
    Rep Power
    0

    Default

    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():

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

    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*/
    
    }
    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 09:26 PM.

Page 1 of 3 123 LastLast

Similar Threads

  1. Replies: 0
    Last Post: 04-02-2008, 07:04 AM
  2. Replies: 0
    Last Post: 02-17-2008, 09:03 AM
  3. Replies: 0
    Last Post: 02-17-2008, 09:01 AM
  4. Inner class accessing outer class
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-17-2008, 08:59 AM
  5. Replies: 5
    Last Post: 01-03-2008, 07:54 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
  •