I have the following piece of code called Code1.
http://pastebin.com/tc0Vd8xh
When I run this, the sketch does not work.
However when I replace "i=+50" for "i = i + 50" the code works.
My question is why the "i=+50" bit does not work?
As far as I know "i=+50" is proper Java and Processing is based on Java.
I tried to Google about "i=+50" but Google does not process non-alphanumeric characters.
So I came here and I searched in previous questions before asking here. Anyone, any idea why "i=+50" does not work?
The statement i=+50 is the assignment of positive 50 to i. That is why it compiles, but doesn't add 50 to i on each loop. As #RoelHarbers and #ByoTic mentioned, you actually want i += 50
You're using =+, which is not a java operator (or an operator in any other language I know of)
The proper syntax is:
i+=50
Because it's i+=50 and not i=+50.
i =+ 50 is not going to do what you want, it is going to initialize i with 50. Instead, use i+=50, this going to add the 50 to whatever value that i holds.
Related
I have a vague memory of my professor from way back ~10 years ago saying that it would be a good practice to write conditions which check a boolean value, with double brackets, unless if you specifically check if it is true or is false.
So instead of
if(isColdOutside) {}
You should do
if((isColdOutside)) {}
And the reason is that if you do single-bracket, the compiler is going to issue a warning to tell you that maybe you forgot to check the value, since there is no == in the condition. So by using double-brackets, you tell the compiler "This is what i meant to do, just check if this is truthy".
He mostly taught Java stuff, that's why I thought it might be a Java thing.
But now, I wanted to look for it, and I can't find anything about this. I tried searching for variations of stuff like "double bracket condition" or "boolean condition warning", and I got no significant results.
Does this syntax/convention have a name? Is it even real, or did I just dream it?
After 20 years of working with Java I have never heard of this. I don't think it is a thing. And if it is a thing, then no one I know would think it was something other than a typo.
Other languages like the Bash shell in linux have multi-bracket differences, but Java, not so much.
I have been going through the Java interview questions asked by my company and came across one that I can't seem to find the solution.
Here is the question:
Please write a method (function) accepting as single parameter a
string and reversing the order of the words in this string.
The " " is the word separator and any other char is considered as being part of a word. In order to simplify, please consider that there is always one space between the words.
Important - You are NOT allowed to use other strings or arrays or other data structures containing several elements - just plain atomic variables such as integers, chars etc.
Also, it is not allowed to use any other language specific string function other than the function giving you the length of the string.
Expected result:
"hello my beautiful world" -> "world beautiful my hello"
So, I can't use: chars[], str.split(), str.charAt(), str.substring(), StringBuilder, another declaration of String.
Should I use recursion to do it?
Since, String is Immutable and uses encapsulation,
There is no solution to your problem. You can't update the values directly, no setters are available and without the access to the getters (since you can only use .length), you can't read the value.
So, I would suggest to respond that Immutability and encapsulation prevent you from doing so.
In real life as a software engineer, you'll sometimes be asked to do things that are technically impossible or even nonsensical. Sometimes the person asking will be someone important like your boss or a big customer.
If someone actually asks you this interview question, then you're in one of those situations. That makes this question pretty interesting, and you might want to figure out what the best way to answer really is.
If someone asked me, this is how I would answer, and as an interviewer, this is the kind of answer I would award the most points for:
1) Explain how it's technically impossible to meet the requirements, but do it without making me feel stupid. This shows diplomacy.
2) Figure out what I really want. In this case, the interviewer probably wants to see if you know how to reverse the words in a string using low-level operations. This is a perfectly reasonable C language question, for example. Figuring out what the interviewer really wants shows experience and judgement.
3) Provide an answer that gives me what I want. Write this method in Java, but take a StringBuilder instead of a string, and call only length(), charAt(), and setCharAt(). This shows the expertise that the interviewer wants to see.
I have been thinking to solve any problem like 1+2*4-5 with user entering it and program to solve it. I've read some questions on this site about storing arithmetic operator and the solution says to check by using switch which can't be applied here. I would be thankful if anybody could suggest any idea of how to make it.
I had a similar exercise not long ago, but in the question it was stated that the seperation is a space. So the user input would be 1 + 2 * 4 - 5, and i solved it that way. I will give you some tips but not paste the whole code.
-you read the input as a String
-you can use the String.split() method to devide the String into the pieces you need and they will be put in an array.(in this case: strArray[0]='1',strArray[1]='+', etc)
-you will need a for-loop to go trough every String in the array:
-the decimals will need to be converted to integers with the Integer.parseInt() method.
-The + - * / will need to be put in switch-statement.
(be careful how you construct your loop, think about how many times you want to go trough it and what you need in each loop)
I hope these tips helped.
if(word.equals(" ")){
}
I have this IF statment, what i would like it to also do is the following..
if(word.equals(" ") OR word.equals(".") ){
}
I know the above code will not work, but im looking how to implement something like that? Anyone know how to do an if or, or do i have to use else if?
Android uses Java, and the boolean OR operator in Java (and in pretty much every single language with a C-inspired syntax) is ||.
if (word.equals(" ") || word.equals("."))
Here is a document describing the Java operators. You may also want to read it from the start since it covers the basics of Java.
There are thousands of tutorials on Google explaining the basics of the Java language that you could also use.
Probably, the reason the question was asked is that you must do this test BEFORE trying to do a .parseDouble on 'word' or else the app will crash
I'm trying to use this formula in JAVA : (-ln(1-L))/L
I'm not sure how to use ln in java.
Math.log(d) returns the natural logarithm (base e) of a double value.
So the java code will be,
double result = (-Math.log(1-L))/L;
(but note that it's better to have variable names in lower-case - i.e. l instead of L)
I also had no idea, but since it's a common math function, I checked the Math class in the API.
Here you go: the log method
EDIT: Sorry for broken link, Markdown is fixed now. Also I realized right after I posted this answer that it sounds snarky, which was not my intent; re-wordings just make it seems snarky AND sarcastic, though. I just wanted to make the point that the API really is useful for methods that could be reasonably expected to come up a lot.