aspose file tools*
The moose likes Beginning Java and the fly likes What's wrong with this 'Change Machine' code? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "What Watch "What New topic
Author

What's wrong with this 'Change Machine' code?

Vadim Konkov
Greenhorn

Joined: Sep 30, 2008
Posts: 8
I am stuck with Eclipse for my programming course. All code is listed below. The prof wants "Declaration of a class with private fields, a constructor, and getters". Please let me know if that is what I have.

Also I want to know if my code will allow the calculation of change (given the purchase price and payment amount) in the least amount of coins possible.

Let me know if there is any way to make this code more efficient, and I'd appreciate if someone could tell me what is wrong with the "System.out.Println()" part.

On a side note, how would I go about importing some JUnitTest files (.jar format) into eclipse?



Thanks in advance for looking and any help. =]

[ September 30, 2008: Message edited by: Vadim Konkov ]

[ October 01, 2008: Message edited by: Vadim Konkov ]

[ October 01, 2008: Message edited by: Vadim Konkov ]
[edit]Newlines to shorten long line. CR[/edit]
[ October 01, 2008: Message edited by: Campbell Ritchie ]
Gamini Sirisena
Ranch Hand

Joined: Aug 05, 2008
Posts: 375
The structure of your program is messed up and it seems to lack the main
logic to accomplish your task. I think you better read
this
section
.

There are a number of errors and improvements that your code needs..

-within the class body you can have instance variables and methods among
other things..
- System.out.println... is statement and you cannot have them floating in
the class body but only within the body of a method.

- when invoking a method you have to say GetToonies()
(emphasis parenthesis)
- To access non static methods from a static method (
e.g. the "main" method) you need to create an object of the required Class
and call methods on that.


i.e.

CoinDispenser cd = new CoinDispenser();
cd.GetToonies();

- getter setter looks like the following. (Follow the code formatting and
style in the class below. i.e indentation, capitalization etc.)




-on adding jars to your project.. I guess right click on your project,
properties-> Java build path -> add external jars should work..

[ October 01, 2008: Message edited by: Gamini Sirisena ]
[ October 01, 2008: Message edited by: Gamini Sirisena ]
Stephen Davies
Ranch Hand

Joined: Jul 23, 2008
Posts: 352
Hi Vadim, I agree with Gamini, your code is somewhat confused, it needs a little cleaning up,it appears to me a little rushed, take it line by line or function by function.

Short, of doing your homework for you, I can point out some confusions in your code:

  • You have no Constructor, and whilst this is not vital in the program, you did mention that your teacher required it. so there needs to be a CoinDespenser(){}Declaration somehwere.
  • Your getters should be by convention, public not private, there is no point to them if they cannot be accessed outsdide the class. Additionally the javaBean, naming Convention for getters and setters are camelCase with first letter as lowercase. Thus, your getter GetToonies should be getToonies
  • I am uncertain as to why your casting an int to an int in all your return statements For example in your getToonies method you have
    return ChangeLeft = (int)(TotalChange%2.00);
    . The result of a mathematical operation is always alteast an Integer (short byte int long etc), plus you have expressley decalred a private variable ChangeLeft;as an Int (Also again here you should see the convention on capitals in variable naming). If you wish to use a double or float decalre it as such.You also use /2.00, this is a floating point number, so you could declare the TotalChange variable as a double or float,if you wish monetary accuracy.
  • The final statements your varibale declarations are a little confusing. you shave written:




  • Is this a method? if so you require the () brackets,unlessits some static convention in which I can't advise you, Im not that knowledgeable yet

  • Your System.out.print statement is a little long and convoluted. However most importantly as Gamini mentioned, it should be in a method for, its even outside the main method in this case. Yet in this case, it perhaps (anybody may correct me here!) would be better to also follow Gaminis advice, and put the variable decalrations as instance variables (within the class delcaration) and call a new instance of the class within main. So for example. (Pseudo-code):




  • Then here in main you could either then assign some local variables to store your instance variables from the getters (eg int tchg = c.getTotalChange()) so when it comes to printing you can call System.out.println on the local variables:

    EG.

  • If your using a call to a method, dont forget the brackets()! In your print statement your calling your getters without the brackets,which will make the compiler think that its a variable which cant be found. Remember, sysout..(getLoonies() etc etc).
  • Finally, is where is your initialization? The code cannot run unless you have a start point for the whole program. Where can a user enter the amount to be calculated? You have no method handling the input of a sum , nor a constructor to initialize that sum. Yes the Compiler will set all the initial ints to 0, but then in your calculations you divide by and modulo 0 all the time, which wont work. Where can the actual amount be set? I think you need a Constructor perhaps or a Method, to get the sum to be used.




  • You say you are "stuck with eclipse" I wouldn't say that a bad thing, Eclipse is a powerful IDE, I use it as my main tool. Despite the debate whether you should use IDE's when learning ( and I take no sides here), it should have certainly picked up some of these errors we have mentioned, did it underline any of your code in yellow or red when you were writing?

    Did it compile? I,would be surprised if it did!

    However, keep up the practice, it can only help, and, as I do alot, dont be afraid to use JavaRanch for help!

    Good Luck with your assignment!

    Steve

    [ October 02, 2008: Message edited by: Stephen Davies ]
    [ October 02, 2008: Message edited by: Stephen Davies ]

    be a well encapsulated person, don't expose your privates, unless you public void getWife()!
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 39810
        
      28
    Originally posted by Stephen Davies:
  • I am uncertain as to why your casting an int to an int in all your return statements

  • If you wish to use a double or float decalre it as such.You also use /2.00, this is a floating point number, so you could declare the TotalChange variable as a double or float,if you wish monetary accuracy.
    The %2.00 bit will produce a double from that expression, hence the cast.
    You should avoid floating-point arithmetic at all costs for money; there is useful discussion in this current thread.
    Stephen Davies
    Ranch Hand

    Joined: Jul 23, 2008
    Posts: 352
    Campbell, thanks for that, thats a good bit of info!
    Vadim Konkov
    Greenhorn

    Joined: Sep 30, 2008
    Posts: 8
    Old thread. Please close.
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 39810
        
      28
    We don't close old threads; we only close threads if we think there is something wrong with them.

    CR
     
    With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
     
    subject: What's wrong with this 'Change Machine' code?