Correct values of i:
i
= 2i
= 2i
= 2Because the '/' operator performs integer division when operating on integer variables; any remainder after the division is discarded. Thus, since 12 = (2 * 5) + 2, the remainder of 2 is ignored and the answer is 2.
Correct values of i:
i
= 1i
= 56i
= -70Because the additional parentheses in 3.3 change the order of evaluation. In 3.2, the calculation is: 11 times 7 = 77; 21 minus 42 = -21; -21 plus 77 = 56. In 3.3 the calculation is: 21 minus 42 = -21; -21 plus 11 = -10; -10 times 7 = -70.
The largest and smallest int
values are 2147483647 and
-2147483648 respectively. Any program that prints these numbers is
correct.
The largest and smallest long
values are
9223372036854775807 and -9223372036854775808 respectively. The
corresponding values for a short
are 32767 and -32768.
Any programs that print these numbers are correct.
One possible solution:
1 import java.io.*; 2 3 class Prog24 { 4 // Read two integers and do some arithmetic on them 5 6 public static void main(String[] args) throws NumberFormatException, IOException { 7 int i, j; 8 String num; 9 BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); 10 11 System.out.print("Please enter an integer: "); 12 num = in.readLine(); 13 i = Integer.parseInt(num); 14 System.out.print("Please enter another integer: "); 15 num = in.readLine(); 16 j = Integer.parseInt(num); 17 System.out.println("i + j = " + (i + j)); 18 System.out.println("i - j = " + (i - j)); 19 System.out.println("i * j = " + (i * j)); 20 System.out.println("i / j = " + (i / j)); 21 System.out.println("i % j = " + (i % j)); 22 } 23 }
One possible solution:
1 import java.io.*; 2 3 class Prog24 { 4 // Read two long integers and do some arithmetic on them 5 6 public static void main(String[] args) throws NumberFormatException, IOException { 7 long i, j; 8 String num; 9 BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); 10 11 System.out.print("Please enter a long integer: "); 12 num = in.readLine(); 13 i = Long.parseLong(num); 14 System.out.print("Please enter another long integer: "); 15 num = in.readLine(); 16 j = Long.parseLong(num); 17 System.out.println("i + j = " + (i + j)); 18 System.out.println("i - j = " + (i - j)); 19 System.out.println("i * j = " + (i * j)); 20 System.out.println("i / j = " + (i / j)); 21 System.out.println("i % j = " + (i % j)); 22 } 23 }