• 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

Memory Calculator

 
Ranch Hand
Posts: 274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

yet another problem on another code I am working on, which is the memory calculator. I have created a method to get the menu option from the user, however, when I return the option back to the main method, it is not recognized in my switch statement, and says that I need to create a variable "option". What might I be doing wrong?

 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

kennith stomps wrote:. . . . What might I be doing wrong? . . .

Putting too much code in the main method. That should contain one statement ideally.
Making too many things static. Get out of the static content of the main method as fast as possible and then stop writing static.

When you get a value returned from a method, you want to use it. You are calling the method in line 33 and not using that value anywhere. If you don't use it immediately, it will disappear into cyber‑limbo never to be seen again. That is why you are getting a compiler error.
 
kennith stomps
Ranch Hand
Posts: 274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I was not wanting to use static but it was giving me errors until I did, as for using my value returned from method immediately, I am using it immediately on my switch statement line 36
 
kennith stomps
Ranch Hand
Posts: 274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And in regards to the too much content in my main method, what exactly do you mean by this, as I only have my getMenuOption method, where else should I store the content that is in my main method.
would it be better to keep track of current value in main and perform all of the operations in a seperate driver class?
 
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why do lines 36 to 50 have to be in the main() method? Why can't they be in a method with a name which tells the reader what they are supposed to do? (I know, right now the code is just a stub to be filled in later, but you already know what it's supposed to do, right?)
 
kennith stomps
Ranch Hand
Posts: 274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Here is my updated code, I have used 2 classes, and am running into some errors in my main, the methods are not functioning properly,
as my calling and passing code is off, however I still am not quite used to passing between classes and methods.
Can you tell which calls are wrong? And how one could fix them.











 
Greenhorn
Posts: 22
1
IntelliJ IDE C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

kennith stomps wrote:
Here is my updated code, I have used 2 classes, ...


That's good.

kennith stomps wrote:
and am running into some errors in my main, the methods are not functioning properly,
as my calling and passing code is off, ...


Can you be more specific?

Some suggestions:
Don't use static methods if you don't need to. Do getMenuOption and getOperand have to be static?
You can solve your issue by using an instance of your Calculator.

You may consider to move the getMenuOption and getOperand methods to your Calculator class as well. But keep your main class as small as possible.
You can use your CalcDriver class for calculations.

This won't compile:

Get rid of the first line.

And you are calling methods from CalcDriver while you don't have a reference to it. Your compiler therefore complains about lines 18, 20, 22, 24 and 26.
Finally, there are some issues with some of your calculation methods in CalcDriver, but you might start off with the things I adviced above.
 
kennith stomps
Ranch Hand
Posts: 274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
all of my methods   give me this error

The method getCurrentValue(double) in the type Calculator is not applicable for the arguments ()
 
kennith stomps
Ranch Hand
Posts: 274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is what I have so far, do not seem to know what it is that will combine these 2 classes together, as getMenuOption and my entire switch statement is throwing an error, anyone know what will fix this?

line 9 error - the method getMenuOption() is undefined for the type Calculator

line 11 error - Multiple markers at this line
- choice cannot be resolved to a variable
- The method getOperand() is undefined for the type Calculator
- The method getCurrentValue(double) in the type Calculator is not applicable for the
arguments ()





 
Indigo Montes
Greenhorn
Posts: 22
1
IntelliJ IDE C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

kennith stomps wrote:
line 9 error - the method getMenuOption() is undefined for the type Calculator


You defined your getMenuOption() and getOperand() methods in class CalcDriver, but you call it directly in your Calculator class. Therefore they are undefined in class Calculator.

kennith stomps wrote:
line 11 error - Multiple markers at this line
- choice cannot be resolved to a variable


- You use switch(choice), without defining choice first. Also, you call getMenuOption(), but you ignore what it returns. You probably meant something like:


kennith stomps wrote:
line 11 error - Multiple markers at this line
- The method getCurrentValue(double) in the type Calculator is not applicable for the
arguments ()


This error means that there is a method getCurrentValue() (although you didn't post it) with no parameters, but it's called with a double argument.
 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

kennith stomps wrote:I was not wanting to use static but it was giving me errors until I did,

And the correct solution is to read the link about the main method and them move all the remaining code into a non‑static method. I wish they had said instance rather than non‑static because that error message misleads people into making things static.

as for using my value returned from method immediately, I am using it immediately on my switch statement line 36

No, you aren't. You aren't using it all. You have it in line 31 or whatever, you then wrote too any blank lines and while you were reading those blank lines the returned value vanishes. You ought to get rid of those empty lines, but the returned value will still vanish unless you use it in the same line you are creating it on (i.e. 31). Or move the creation to a location where you can use it directly (36). The compiler won't notice that error, but it does notice that you haven't created an option variable.
You can “use” a value returned from a method by putting it in (or as) an repression to the right of an assignment operator =, or in (or as) an expression inside () to call something else.
 
Indigo Montes
Greenhorn
Posts: 22
1
IntelliJ IDE C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Maybe it's a good idea to read some concepts about OOP.
https://docs.oracle.com/javase/tutorial/java/concepts/index.html
 
kennith stomps
Ranch Hand
Posts: 274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay I have read the article on main, however I am still a bit lost, as if nothing is supposed to go into main, then how is it going to run the program? so all of my methods are to go into a seperate class, as well as all of my method calls? The article did not seem to mention anything on this

Anyone have a reference code I can look at to see exactly how this works
 
Indigo Montes
Greenhorn
Posts: 22
1
IntelliJ IDE C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Classes have methods and these methods can be called from within the class itself. If the visibility of the method is e.g. public, then it can be called from outside the class as well.
Suppose I want to calculate the area of a rectangle. How can we achieve that and what do we need?

Let's make a global todo list:
- We need a main class to start the application.
- We need user input for the calculation.
- We need a Rectangle class.

This is a small simple list of todos. However, it gives already a clear idea of how the program is going to be implemented. You can even start writing little pieces of code.

Now let's think about some more details.
- A rectangle has a width and a height. So these become fields in the class Rectangle.
- We need a method to calculate the area of the rectangle.
- To do the calculation, we need a Rectangle instance.


So, this is the structure of our program.
- We specify the width and height via the constructor of Rectangle. So it gets two parameters: width and height
- We need to call the area method, which should return an int:  width * height
- We need to print this area we received from calling the area method


I hope this helps.
 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Indigo Montes, welcome to the Ranch
 
kennith stomps
Ranch Hand
Posts: 274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks for all of your help, here is my updated code, I now only have 1 remaining problem, as the program does not say goodbye upon termination, and I can't seem to find where I make the program terminate if 6 is entered, does anyone see it?




 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

kennith stomps wrote:thanks for all of your help . . .

That's a pleasure

the program does not say goodbye upon termination . . .

Put goodbye one line after the end of the loop starting while (choice != 6)

Check very very carefully where that loop ends. I was a bit surprised to see where it ends myself, so maybe that information will help.
Remove all the comments saying // XYZ METHOD just before the method of that name. They don't help. Maybe you should replace them with proper documentation comments.
 
kennith stomps
Ranch Hand
Posts: 274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay, I will do that, but before I get to that, I have yet another problem that has arisen, I have not touched hardly anything other than adding a few comments here and their, and now all of a sudden my program does not run properly at all. All that it will do is display the menu, and ask for input, but nothing happens after, any idea what is causing this? Totally insane the problems that come up, I was about to submit the assignment and now this happens.





 
kennith stomps
Ranch Hand
Posts: 274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Aha, I was ending my while loop early as you said, took me forever to figure out something so simple, could that have been solved in debug?
 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

kennith stomps wrote:Aha, I was ending my while loop early as you said,

I presume you have corrected that since your last post, which still has the error in.

took me forever to figure out something so simple, could that have been solved in debug?

I have had a lot more practice; that's all. You would probably have had difficulty getting a debugger to stop in that loop because it is empty, but having a program sit there for several seconds without apparently doing something should alert you to the possibility of an infinite loop.

To those of us who have studied program semantics and that sort of thing, an infinite loop is the epitome of a program gone wrong.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic