File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Class Help! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Class Help!" Watch "Class Help!" New topic
Author

Class Help!

Beth Laguardia
Ranch Hand

Joined: Jun 01, 2008
Posts: 34
Hello everyone i am playing with creating an object using class the probem i cannot make it run. Its just a simple program hope you can all help me:

The error: Missing return statement





This is the first few times that i went outside of main and i am not very much familiar yet. What is wrng wih my program?
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24166
    
  30

Hi Beth,

More properly, this won't compile, right? You get error messages from the Java compiler. The most obvious one is that although getValue() is declared to return "double", it doesn't actually return anything. It needs to use the "return" keyword to return a value. It should be the job of the code that calls getValue() to print the results. So getValue should end with "return total;" .

There may be other errors but that's the one that jumps out at me. If you get any more, please post the messages -- always a good idea, as it saves other people the trouble of cutting, pasting, and compiling your code.


[Jess in Action][AskingGoodQuestions]
Beth Laguardia
Ranch Hand

Joined: Jun 01, 2008
Posts: 34
Thank you sir!

I changes the double getAmount to void getAmount and it works properly!

DO you think I constructed a good program? IS there a better way to do his using class?
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24166
    
  30

There are lots of things you could do to improve this and make it a better example of proper object-oriented principles. For example, member variables should almost always be -- with very few exceptions -- private. Instead of setting one class's member variables from another class, as you've done here, it's more appropriate to pass the values as arguments to a method. After making those members private, there are two different things you might do to get the program working again. First, you could give Carinderia a constructor that took two arguments, ans and qty, and set the member variables from those argments inside the constructor. The other idea would be to eliminate the member variables entirely, and instead make ans and qty be arguments to the getAmount() method. This second one actually makes more sense, since those variables are only used by one method, and there's no reason for the values to exist between calls to getAmount().

The other thing is the "one responsibility" principle. In general, a method should do one thing, and do it well. Your getAmount() method does two things: compute a value, and display a report about the value. If you left qty, ans, and total as member variables, then you might have two methods, computeAmount() and printReport(), which shared those member variables and the caller is intended to call first one, then the other. Another way to change things would be to have getAmount() not print a report, but instead use the "return total" I suggested earlier; then main() can capture the result in a variable and print the report itself.

This is all a good learning experience. You're welcome to come back and show us your changes for further discussion.
Beth Laguardia
Ranch Hand

Joined: Jun 01, 2008
Posts: 34
Originally posted by Ernest Friedman-Hill:
Hi Beth,

It needs to use the "return" keyword to return a value. It should be the job of the code that calls getValue() to print the results. So getValue should end with "return total;" .



Hello sir,

I re-read your reply and followed what you said. Here's my new program hat is working:






Thank you
Beth Laguardia
Ranch Hand

Joined: Jun 01, 2008
Posts: 34
Originally posted by Ernest Friedman-Hill:
There are lots of things you could do to improve this and make it a better example of proper object-oriented principles. For example, member variables should almost always be -- with very few exceptions -- private. Instead of setting one class's member variables from another class, as you've done here, it's more appropriate to pass the values as arguments to a method. After making those members private, there are two different things you might do to get the program working again. First, you could give Carinderia a constructor that took two arguments, ans and qty, and set the member variables from those argments inside the constructor. The other idea would be to eliminate the member variables entirely, and instead make ans and qty be arguments to the getAmount() method. This second one actually makes more sense, since those variables are only used by one method, and there's no reason for the values to exist between calls to getAmount().

The other thing is the "one responsibility" principle. In general, a method should do one thing, and do it well. Your getAmount() method does two things: compute a value, and display a report about the value. If you left qty, ans, and total as member variables, then you might have two methods, computeAmount() and printReport(), which shared those member variables and the caller is intended to call first one, then the other. Another way to change things would be to have getAmount() not print a report, but instead use the "return total" I suggested earlier; then main() can capture the result in a variable and print the report itself.

This is all a good learning experience. You're welcome to come back and show us your changes for further discussion.



Hello again Sir,

Will follow your advice and will be back with an improvement i hope.
[ June 23, 2008: Message edited by: Beth Laguardia ]
Beth Laguardia
Ranch Hand

Joined: Jun 01, 2008
Posts: 34
Hello,

I redo my program to make it better. I used constructor but i cannot run it. The errors say that the symbols menuAns and qtyAns cannot be found.

Please help,

Here's the code;






What's wrong with my program please.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24166
    
  30

MenuAns and menuAns are not the same -- Java is case-sensitive.
Beth Laguardia
Ranch Hand

Joined: Jun 01, 2008
Posts: 34
Thank you Sir!

My program is working perfectly now.

Thank you agan.
 
 
subject: Class Help!
 
Similar Threads
two simple troubles.
Incompatible types error
Ton of problems
wrong value
my last question for this program.