Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
This is only my third program in java. I'm stuck on this part of the problem.
"The formula is not valid if T > 50 in absolute value or if v > 120 or < 3"
I'm not sure how to translate this into code while restricted to use the following:
no, if statements
no, loops
no, importing
no, new classes to be added
Thank you!
public boolean validateFormula(int T, int v) {
return !(abs(T)>50 || v > 120 || v < 3);
}
In giving you a direct answer to this problem, you might not be able to replicate it next time, or to be able to think for yourself.
In import java.util.* , Math.abs(int n) will return the absolute value of n. The || operator means 'or'. I will not assume that you know the basic structure of an if loop so here:
if(condition || condition || condition){
//execute code here
}
You have everything that you need to solve this, good luck. Furthermore, if you do have any more simple questions like this, I would suggest doing a google search instead.
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I have a test case where I need to go to Jewelry section and select the price range filter of $700.00 - $3000.00. After selecting the range I can see 1 product priced at $2100.00. Now, My question is how do I check using assert that the product price $2100 is between $700 - $3000? Do I need to get rid the $ signed? as actual result will contain $ signed while comparing with expected.
Please help?
Thanks in advance
It is very simple in Java
assertThat(mynum).isBetween(min, max);
Or
assertTrue(min <= mynum && mynum <= max);
Yes, you need to get rid of the $ sign, that's useless and as said in the comments, Java (and probably no other programming language) has a specific money type.
You can use Regex for that
"[0-9]+"
And for the assert, it's as simple as adding two conditions inside
assertTrue(price > 700 && price < 3000);
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
Below is my code in Java
public int indexOf(E item){
Node<E> node =head;
for(int i=0; i<size;i++) {
node=node.next;
if(node.data==item){
return i+1;
}
}
return -1;
}
Is my code right or do I need to improve it?
Is my code right ...
That depends on your requirements. If you are required to implement the java.util.List API, then the javadoc says this:
"... More formally, returns the lowest index i such that (o==null ? get(i)==null : o.equals(get(i))), or -1 if there is no such index."
You are using == instead of equals ... which is incorrect.
If you are not required to implement List then we cannot comment on the correctness of == ... or for that matter on any aspect of the code. (For instance the requirements could say that list element positions start at 1!!)
... or do I need to improve it?
If it doesn't meet the requirements, you need to fix that. (But we don't normally describe bug fixes as "improvements".)
If you are asking for a code review, such questions are better asked on the code-review site.
But stylistically, I wouldn't use a for loop. I'd use a while loop, and use node == null as the loop termination condition. (Your code has an implicit dependency on size being correct. If it isn't you may get NPEs.)
If data is not a primitive type, to ensure a thorough comparison you should use node.data.equals(item).
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
I was just wondering if one of these code are preferred over another.
For example:
boolean even;
if (number % 2 == 0)
even = true;
else
even = false;
alternate code:
boolean even = (number % 2 == 0);
I was thinking that one was perferred over the other because one is shorter, but I feel like the first code is easier to read (being a beginner). I just want to know the REAL answer as to why one is preferred over the other or even if one is preferred over the other?
I would go for the second one, because you avoid a branch. In the first case, you have an if-else construction. This means you have a branch in your code. The second example has no branch in the code which improves readability.
As #Dev pointed out, when you compile the Java source code to bytecode, unfortunately it generates a branch.
Technically, the compiler could choose to do something like this in assembly:
TEST eax ; test for zero of "number % 2"
PUSHF ; push flag-registers to the stack
POP ebx ; pop them into a register
SHR ebx,?? ; shift to the FZ flag
AND ebx,1 ; cancel out the other flags
However, this is only a possibility, which is most likely to be not the case.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I'm rather sure I haven't found an answer to this because I'm not sure of the correct term for what I'm trying to do, so apologies in advance if it's very straight forward/well documented.
I have a set of numbers which I need to output a number in a specific position (hundreds), IE:
For 1302, I need to output 3
For 1802, I need to output 8
etc
How can I accomplish this with Java?
I should note that this is easy with 100's ( / 100), however I can't seem to figure out how to do this when the number is > 100.
a easy way is use % (modulo) and then /
like :
(input % 1000) / 100
Edit It works if you use only int number.
If you want to keep the number,
(num / 100) % 10
should generally work.
If you want to output the second number of your number you could convert it to a String and then use the substring method (doc) to get the character you want. (of course it only works if the number you want is always located at the same position).
An example of one solution, to get you on your way :)
int i = 1302;
String hundreds = Integer.toString(i).substring(1, 2);
System.out.println(hundreds);
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
If you look at
if (!x) {
if (x == false) {
It looks like !x is better, but
if (!isSomething()) {
if (isSomething() == false) {
you can easily oversee the !
What to do? Is there a recommendation?
The hidden third option is to name your variables and methods properly.
Instead of
if (!isDisabled()) {
...
}
use
if (isEnabled()) {
...
}
or if you want to check for the negative:
boolean disabled = !isEnabled();
if (disabled) {
...
}
or add both methods:
boolean isDisabled() {
return !isEnabled();
}
Edit: I found this question: Is it bad to explicitly compare against boolean constants e.g. if (b == false) in Java?
I would stick with the notation if (!isSomething()) {. If you or others find it hard to read you can always add a little whitespace around the '!' in order to make it stand out:
if ( ! isSomething()) { or if ( !isSomething()) {
Furthermore, multiple conditional statements can become overwhelming with the following notation
if (isSomething() == false && isSomethingElse() == false && ..),
whereas its alternative is short and succinct. After a while it becomes natural to read the '!' along with the statements as "not isSomething() and not isSomethingElse()".
I don't think there is any recommendation that everyone would follow.
Do it your way, personnally, I would choose the if (!isSomething()) style :)
Especially since I already chose the if (!x) style.
if ( !isSomething() ) {
would be the best in my opinion. This way you're keeping the character count down, your code is readable and that ! does stick out at the beginning, so just by skimming through code, others can see its intention.