• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Class Help!

 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Beth Laguardia
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
MenuAns and menuAns are not the same -- Java is case-sensitive.
 
Beth Laguardia
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Sir!

My program is working perfectly now.

Thank you agan.
reply
    Bookmark Topic Watch Topic
  • New Topic