1.5 HW Hacks
From the instructions of the submission, it said to have the entire notebook (assuming your lesson notebook) with all code cells executed. I hope I interpreted the instructions correctly.
int a = 5, b = 2;
System.out.println(a / b);
System.out.println((double)a / b);
System.out.println((int)3.9);
System.out.println((int)(3.9 + 0.5));
2
2.5
3
4
public class CastDemo {
public static void main(String[] args) {
int a = 7, b = 2;
System.out.println(a / b); // 3 (int division)
System.out.println((double)a / b); // 3.5 (promoted to double before divide)
double d = 5.9;
int t = (int) d; // 5 (truncate toward 0)
System.out.println(t);
// Rounding to nearest int (per CED):
int rPos = (int)(d + 0.5); // 6 (non-negative rounding)
double n = -5.9;
int rNeg = (int)(n - 0.5); // -6 (negative rounding)
System.out.println(rPos + " " + rNeg);
}
}
CastDemo.main(null);
3
3.5
5
6 -6
int max = Integer.MAX_VALUE; // 2147483647
int min = Integer.MIN_VALUE; // -2147483648
System.out.println(max + 1); // overflow → -2147483648
System.out.println(min - 1); // overflow → 2147483647
-2147483648
2147483647
# Utilities to simulate Java 32-bit signed int in Python
INT_MIN = -2**31
INT_MAX = 2**31 - 1
MASK32 = 0xFFFFFFFF
def to_int32(x: int) -> int:
x &= MASK32
# convert to signed
return x if x <= INT_MAX else x - (1 << 32)
def add_int32(a: int, b: int) -> int:
return to_int32(a + b)
def sub_int32(a: int, b: int) -> int:
return to_int32(a - b)
def mul_int32(a: int, b: int) -> int:
return to_int32(a * b)
def div_int32(a: int, b: int) -> int:
# Java int division truncates toward zero
if b == 0:
raise ZeroDivisionError("division by zero")
q = int(a / b) # Python truncates toward zero for int()
return to_int32(q)
def mod_int32(a: int, b: int) -> int:
# Java remainder has same sign as dividend (a)
if b == 0:
raise ZeroDivisionError("mod by zero")
q = div_int32(a, b)
r = to_int32(a - q * b)
return r
# Demo
print("INT_MIN, INT_MAX:", INT_MIN, INT_MAX)
print("Overflow examples:")
print("MAX+1 =", add_int32(INT_MAX, 1))
print("MIN-1 =", sub_int32(INT_MIN, 1))
print("(-7)/2 =", div_int32(-7, 2), " (-7)%2 =", mod_int32(-7, 2))
print("(7)/-2 =", div_int32(7, -2), " (7)%-2 =", mod_int32(7, -2))
INT_MIN, INT_MAX: -2147483648 2147483647
Overflow examples:
MAX+1 = -2147483648
MIN-1 = 2147483647
(-7)/2 = -3 (-7)%2 = -1
(7)/-2 = -3 (7)%-2 = 1
def java_truncate(x: float) -> int:
# Casting double->int truncates toward 0
return int(x) # Python int() also truncates toward 0
def ced_round(x: float) -> int:
if x >= 0:
return int(x + 0.5)
else:
return int(x - 0.5)
tests = [5.1, 5.5, 5.9, -5.1, -5.5, -5.9, 0.49, -0.49]
for v in tests:
print(f"x={v:5} truncate={java_truncate(v):3} ced_round={ced_round(v):3}")
x= 5.1 truncate= 5 ced_round= 5
x= 5.5 truncate= 5 ced_round= 6
x= 5.9 truncate= 5 ced_round= 6
x= -5.1 truncate= -5 ced_round= -5
x= -5.5 truncate= -5 ced_round= -6
x= -5.9 truncate= -5 ced_round= -6
x= 0.49 truncate= 0 ced_round= 0
x=-0.49 truncate= 0 ced_round= 0
int a = 3, b = 2;
double x = a / b; // 1.0 (int division then widened)
double y = (double)a / b; // 1.5 (promoted before divide)
System.out.println(x);
System.out.println(y);
1.0
1.5
int a = 10, b = 4;
System.out.println(a / b);
System.out.println(a % b);
System.out.println((double)(a / b));
System.out.println((double)a / b);
2
2
2.0
2.5
double d = -2.6;
System.out.println((int)d);
System.out.println((int)(d - 0.5));
System.out.println((int)(-d + 0.5));
-2
-3
3
int x = Integer.MAX_VALUE;
int y = x + 2;
System.out.println(x);
System.out.println(y);
2147483647
-2147483647
# Q1 reasoning
a, b = 10, 4
print("a / b =", int(a / b)) # integer division result
print("a % b =", a - int(a / b) * b)
print("(double)(a / b) =", float(int(a / b)))
print("(double)a / b =", a / b) # Python uses float division here by default
a / b = 2
a % b = 2
(double)(a / b) = 2.0
(double)a / b = 2.5
// FRQ 1
public static double avgInt(int a, int b) {
return ((double)a + b) / 2.0;
}
// FRQ 2
public static double percent(int correct, int total) {
if (total == 0) return 0.0;
return 100.0 * ((double) correct) / total;
}
// FRQ 3
public static int safeMod(int a, int b) {
if (b == 0) return 0;
return a % b;
}
System.out.println(avgInt(3, 4));
System.out.println(percent(5, 10));
System.out.println(safeMod(3, 4));
3.5
50.0
3
double x = 7.9;
int y = (int) x;
System.out.println(y);
7
int a = 10;
double b = 5.5;
System.out.println(a);
System.out.println(b);
10
5.5
int num = 9 / 2;
System.out.println(num);
4
double d = 15.7;
int i = (int)(d + 0.5);
System.out.println(i);
16