Joanne
David Thompkins wrote:In fact... it may be far better if you could point me in the right direction by simply correcting my object reference variable c and it's deceleration so I can see what I've done incorrectly, as to me (a total beginner) it appears I've already declared reference variable c in the Calculate class.
David Thompkins wrote:... as to me (a total beginner) it appears I've already declared reference variable c in the Calculate class.
All things are lawful, but not all things are profitable.
Raymond Holguin wrote:It appears your not using an IDE otherwise it would be screaming at you with the errors in your code as well as how to fix them (most IDE's at least). Similar to how your importing the class import java.util.Scanner; you also need to import Calculate and any other class you wish to use inside that particular class. I would suggest using some sort of IDE instead of a text editor, especially for beginners.
All things are lawful, but not all things are profitable.
David Thompkins wrote:p.s you're correct Raymond, I'm not using an IDE as head first java (the book I'm currently reading) stated I should use a simple text editor to begin with. (Using sublime text 2).
1) Those 3 classes are not tied together or related to each other in any way, they are there own separate entity. That being the case, declaring a variable in 1 class does not allow you to call that variable in another class.
2) If you want to access another variable or class instance from the method/function of another class, then you need to pass that data via the method parameters.
3) As mentioned, you don't need/want to declare an instance of your class inside your class...there is no need. For example, you don't need to declare an instance of Calculate inside the Calculate class in order to call c.setNums(total) from the addition method. You can delete the instance declaration and simply call setNums(total)
4) You really want to declare you class instances in your main and then go from there, passing the data around and such.
2) If you want to access another variable or class instance from the method/function of another class, then you need to pass that data via the method parameters.
4) You really want to declare you class instances in your main and then go from there, passing the data around and such.
You're the compiler. You come across inou. What is it? What type is it? Without the type, how do you know if it has numsToAdd()?
So the compiler needs to know which type inou is, and if it's an object (hint: it is), an object needs to be created.
So how should you declare inou?
How do you create an object that inpu will reference?
David Thompkins wrote:I tried this...which wasn't successful
All things are lawful, but not all things are profitable.
All things are lawful, but not all things are profitable.
firstly I'd like to say that Programming is bl**dy rewarding when something finally works!!! ...
David Thompkins wrote:p.s. Is this what makes programming addictive for most people? the fact that every now and then there's a sense of achievement when it before seemed almost impossible? One day I will look back at this post and laugh.
p.s. Is this what makes programming addictive for most people? the fact that every now and then there's a sense of achievement when it before seemed almost impossible?
All things are lawful, but not all things are profitable.
Great. Now figure out how to save two inputs and pass them both at once to your addition method. Post your code and any errors. Good luck!
David Thompkins wrote:Yes I am glad I worked it out on my own (provided that means asking for more clues in this thread as they were most definitely needed, excessively trying to find solutions to the problems on a smaller scale in smaller programs, fixing errors one by one as they appear in the cmd and carrying out research over the internet, looking at other peoples solutions to getting and storing multiple input, trying to understand them and then seeing how I can use them).
Is the code I wrote what you expected when you pointed me in the right direction by suggesting the following Knute? --->
The ultimate goal being to create a calculator which performs basic tasks like subtraction, multiplication and division. I also need to be able to add more than two numbers together and so on.
Do you think that I've used the correct OO approach in the program?
All things are lawful, but not all things are profitable.
David Thompkins wrote:Surprisingly (for me at least), the code actually compiles, however when entering the '=' symbol I receive the following errors:
David Thompkins wrote:I then decided to search the internet for a way to check if numbers are present in a string variable...
and do you have any hints?
David Thompkins wrote:Good morning everyone,
I have a few questions to clarify your recent replies.
Hi Knute, to clarify and ensure I understand what you've said. scan.next calls the next method in the scan class/object in the java API which somehow (I'm wise enough to not even attempt to understand how it does this for now) takes user input, processes it inside the next method and then returns the processed input in the form of a string, correct?
so .... when looking at a method call which contains two dot operators, I first need to figure out what the first method call returns after the first dot operator (in this case it's a String like you mentioned). Then look at the code and now imagine that the next method (or whatever method call comes first) is replaced by what it returns, next I then look at what calling itsreturnedString.charAt(0) which... (correct me if I'm wrong) is me passing the charAt method the value 0 in the parenthesis of the method call (to say find and return the character which is at 0 in my string) and place variable, which happens to be decision.
I may have got this entirely wrong though, as a String is in fact a class? not a data type?
hmm I assumed after typing the above that the charAt method was also within the Scanner class, but apparently after a quick google search it's contained within the String class (which confirms what I previously wrote about is a String a class or a data type, it's clearly a class).
...and as the String class (please correct me if I'm wrong) is imported and declared to be used in all of our java programs (without us having to import it, as it's considered so essential?) that's how we can simply call the String's method... without having to say String.CharAt(0) first or import?
Apologies for all of those questions Knute I just what to ensure I understand,
Never use == false or == true or similar. They are not only poor style but also error‑prone because it is only a matter of time until you write = instead by mistake.
It is if (b)… or if (!b)…
and I'm not sure what the b represents. I'm guessing if using == true/false is bad practice then so is saying == to anything else? like if (thisNumber == 5) {...}?(b)… or if (!b)…
I've read "A Literal is the source code representation of a fixed value" on stackoverflow, they provided the following example:A char is not a character or anything; it is a number
Of course I was forgetting about the sign change operator. You will have to pretend that −123 is a single token representing a negative number, rather than what it really is, namely a positive number preceded by the sign change operator (often called unary minus). I think (not certain) that the Scanner will interpret "−123" as a number and return a negative result, so you should be all right. Just don't write − 123 with a space.
although I may be incorrect we shall see!So you get a number in the loop, then an operator, and you can stop when the operator is =. You can organise things so the input is 123 + 234 + 345 + 345 =
David Thompkins wrote:Hi Campbell,
Never use == false or == true or similar. They are not only poor style but also error‑prone because it is only a matter of time until you write = instead by mistake.
It is if (b)… or if (!b)…
I understand why you mentioned using == is prone to error, however why is it poor style? I need a substitute for the ==, so I looked atand I'm not sure what the b represents. I'm guessing if using == true/false is bad practice then so is saying == to anything else? like if (thisNumber == 5) {...}?(b)… or if (!b)…
}
Were you hinting I use the following: which I read as while equalsEntered is set to anything-but true then do this {...} is that correct?
does that also go for classes which I make myself? would I be correct in saying my poodle is not only of type dog but is of data type dog? if a create the following object:all classes are data types
Dog poodle = new Dog();
String is part of java.lang which is imported by default. I believe it is the only package that is. (Can anyone confirm?)
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime. |