Arithmetic operator confusion - java

Why I'm getting two different values while using the arithmetic operators for the same value of variables. I've just altered little bit my second program, which is resulted in giving me the different output. Could anyone please tell me why?
int number=113;
int rot=0;
rot=number%10;
rot*=100+number/10;
System.out.println(rot);//333
int number=113;
int rot=0;
rot=number%10;
rot=rot*100+number/10;
System.out.println(rot);//311

In the first part you compute
rot *= 100 + number/10
which is
rot = rot * (100 + number/10)
And in the second part:
rot = rot*100 + number/10
Note that multiplication and division goes before addition and substraction.

the problem is that *= has different (lower) precedence than * and +
rot *= 100 + number/10;
is equavalent to
rot = rot * (100 + number /10);
operator precdence can be found here

It seems like the problem is operator precedence.
What this means is that num * 10 + 13 is treated like (num * 10) + 13, i.e. the () are automatically added according to the rules of the language.
The difference then, in your example, is that the first one means the following:
rot*=100+number/10;
// Is the same as this:
rot = rot * (100 + (number / 10));
Whereas the second one means the following:
rot=rot*100+number/10;
// Is the same as this:
rot = (rot * 100) + (number / 10);
Since the parenthesis are in different places, these probably evaluate to different numbers.

in the second code because of the high precedence of *. rot*100 will be calculated and to that (number/10) will be added so its (300 + 11 = 311).

Related

Can anyone explain the output process in java

int p=15, q=12, s;
s=p/q*++p%(++p%--q);
System.out.print(s);
I am trying it according to the precedence of operators and associativity but I think it should give me the output 2 but it is giving me 4 as output.
Precedence
p/q*++p%(++p%--q)
is computed as
((p / q) * (++p)) % ((++p) % (--q))
using the operator precendece rules.
Note that ++x/--x increment/decrement the variable by one and then return the result after the operation. Since they change the variable itself, the effect obviously carries over to other usages of the variable.
Evaluation
It is then evaluated left to right, the math is
((15 / 12) * (16)) % ((17) % (11))
which results in 4:
((15 / 12) * (16)) % ((17) % (11))
= (1 * 16) % 6
= 16 % 6
= 4

Why does the variable "bmi" only get the value of the variable "weight"?

public class P2A7 {
public static void main(String[] args) {
String name = args[0];
int weight = Integer.parseInt(args[1]);
double length = Double.parseDouble(args[2]);
double bmi = weight / length * length;
System.out.println(bmi);
}
}
Passing java Name 80 1.9 from the command line gives the output "80".
Why are the division and multiplication completely ignored?
Putting parentheses around "length * length" fixes this.
#Kristjan Link, This is because precedence of operator, when you write weight / (length * length) you increase precedence of multiplication *, because precedence of operator inside parentheses is always more than operator outside parentheses
Use of parentheses fix it because it increases precedence of * operator. By using parentheses you explicitly give * more precedence than / that's why multiplication expression will be executed first due to its increased precedence other wise both * and / have same precedence.
NOTE:
but remember despite same precedence / execute first than * because it comes first. Just like () is way to increase precedence, shifting position of operator is another way increase or decrease precedence of operators.The table present here. will be helpful for you in this regard.
Because double bmi = weight / length * length = weight is not same as double bmi= weight / (length * length)
In the first case, You are ending up getting the same result as you are not using any parenthesis.
/ and * have equal precedence, so weight / length * length means (weight / length) * length
Because / has higher precedence than . Hence, your code just performs divide and multiply by length which returns weight. () has higher precedence than / and hence the code inside () will execute first which results in weight multiplied by lengthlength

Converting user input from binary to decimal using Modulus Operator in Java

I am having a hard time figuring out the answer to a homework assignment for Programming 1 class. The assignment is prompt a user for input (up to 4 bits) in binary, and convert it to the decimal equivalent. Using loops, conditional statements, ParseInt, and anything other than the modulus operator and other math operators are not allowed.
I am having trouble with the mathematical aspect, I think once I understand how to use the modulus operator to answer the question I would be able to write the code for it.
I have searched and have not been able to find anything that was able to help.
You should be getting the number values of each position and add them using the power of 2 to get back the original number.
double num = 1110;
double ones = Math.floor(num % 10);
double tens = Math.floor(num/10 % 10);
double hundreds = Math.floor(num/100 % 10);
double thousands = Math.floor(num %10000 /1000);
double tenThousands = Math.floor(num / 10000 % 10);
double original = (ones * 1) +
(tens * 2) +
(hundreds * 4) +
(thousands * 8);
System.out.println(num);
System.out.println("ones: " +ones);
System.out.println("tens: " +tens);
System.out.println("hundreds: " +hundreds);
System.out.println("thousands: " + thousands);
System.out.println("original number : " + original);

Java/Pawn Maths formula returns 0

could someone explain me what am I doing wrong here, and how can I make this formula to be working? right now it returns 0 in Java and Pawn. But it works in PHP, which isnt making much sense for me.
int test = (65 / 65 / (1 + 25) * 10000);
In Java, integer division will truncate the results. For example, 5/2 will truncate 2.5 to 2.
To ensure you're using float with numeric constants, add a .0 on the end, as in:
int test = (65.0 / 65.0 / (1.0 + 25.0) * 10000.0);
Java operations for multiplication and division is left to right, so your expression is really:
(((65 / 65) / 26) * 1000) // Clarifying parenthesis
((1 / 26) * 1000)
(0 * 1000) // integer division!
0
To avoid this, you just need to ensure the operations are casted to a double.
The simple way would be just changing the first value to a double, either by:
int test = (65D / 65 / (1 + 25) * 10000);
Or
int test = (65.0 / 65 / (1 + 25) * 10000);
However, if you are refactoring your code later, you might want to change more than just the first value.

Java math operations shorthand?

I've been searching but no luck. I want to know if there is a way to perform shorthand operations with the number 1. I know how to do it with addition and subtraction:
/*
* Addition: variableName++;
* Addition: variableName +=;
* Subtraction: variableName--;
* Subtraction: variableName -=;
* Multiplication: variableName*=
* Multiplication: variableName**; ?
* Division: variableName /=;
* Division: variableName// ? impossible
* Exponent: variableName ^=;
* Exponent: variableName^^; ?
* Modulo: variableName %=;
* Modulo: variableName%%; ?
*/
What about the others? Multiplication, Division, Exponents, Modulo.
Doing division seems almost impossible.
EDIT:
I should have been more specific.
I want to know if there is another version of ++ or - - for the other operators.
The other mathematical shorthand operators which Java provides are the += (a = a + number), *=, and so on.
The complete list of operators can be obtained from here.
Try these:
Int a = 5;
a += 1;
a -= 1;
a *= 2;
a /= 2;
Are these shorthand enough?
The other operators won't actually change the number.
e.g.
x = x * 1;
x is unchanged, why would you want a shorthand that doesn't change the value of x?
There are some languages that allow you to make your own shorthand for operators but this requires a bit of research
I think there is no increment or decrement operator like for multiplication and division.Instead we can use them like below
a *= b (a = a * b)
a /= b (a = a / b) and so on..
also if we want to multiply x by 1 or divide x by 1 then also there will be no effect on the final result..so what's the need

Categories

Resources