Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Can anyone explain the output of this code for lines h, i, and j? I dont underst

ID: 3620698 • Letter: C

Question

Can anyone explain the output of this code for lines h, i, and j?
I dont understand how to really read it.

import java.util.Scanner;

public class FigureMeOut_02{
public static void main(String[] args){
int iResult, num1 = 52, num2 = 40, num3 = 27, num4 = 4;
double fResult, val1 = 19.0, val2 = 17.28;
fResult = val1 / num4;
System.out.println(fResult); // (a)
fResult = val1 / num2;
System.out.println(fResult); // (b)
iResult = num1 / num2;
System.out.println(iResult); // (c)
fResult = (double)num1/num2;
System.out.println(fResult); // (d)
fResult = num1 / (double) num2;
System.out.println(fResult); // (e)
fResult = (double) (num1 / num2);
System.out.println(fResult); // (f)
iResult = (int) (val1 / num4);
System.out.println(fResult); // (g)
fResult = (int) (val1 / num4);
System.out.println(fResult); // (h)
fResult = (int) (val1 / num4);
System.out.println(fResult); // (i)
fResult = (int) ((double)num1 / num2);
System.out.println(fResult); // (j)
iResult = num3 % num4;
System.out.println(iResult); // (k)
iResult = num2 % num3;
System.out.println(iResult); // (l)
}
}

Explanation / Answer

Hello dedesigns, I assume that by lines "h", "i", and "j", you mean the lines with those corresponding letters in the comments. So, for (h), I believe the part you have a question about is... fResult = (int) (val1/num4); In many instances in Java, we read the code from right to left to understand it. We do that in this case, starting with the (val1/num4). First, we figure out what (val1/num4) is, then... we make it into an integer. What that code means, is that val1/num4 is stored as an int in the variable fResult, instead of as a double. You need the (int) to make fResult store (val1/num4) as an integer, because fResult is predefined as a double, so fResult will be double unless you specifically assign it to be something else, in this case, int. A similar thing is going on in (i), just a bit more complex. In (i) you have... fResult = (int) ((double)num1/num2); This means that num1 is set as a double, even though it was predefined as an int. And because num1 is a double, when you do num1/num2, you will end up with a double. That is all that is inside the right side (). On the left side, (), there is an int inside the parenthesis. So, like (h), this means that the result of ((double)num1/num2) will be stored as an integer in fResult instead of the predefined double. (j) is different than the previous two. It uses the % operator. First you have to know what the % operator is. % means modulus. What it does is show the remainder of a division operation. So, ie: 4 % 3 = 1, because when you do 4/3, you end up with a remainder of 1. note: '%' '/' and '*' all have the same precedence, meaning that they are all on the same level in order of operations. So, if you are deciding which of the three to do first if you have them next to each other without parenthesis, you go from left to right.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote