/* Program for Java arithmetic and type conversions See arithmetic.java for the trace values, and run the program yourself to see what happens. */ public class arith { public static void main(String[] args) { int num1, num2 = 6; float real1 = 12.45f; double bigreal, d; num1 = 10; num1 = num2 + 23 - num1 * 3; // num1 = 10; num1 = num2 + (23 - num1) * 3; // d = real1; // implicit conversion bigreal = -234; // implicit conversion num2 = '*'; // implicit conversion //NOT: num1 = 4.7; num1 = (int) 4.7; // explicit conversion, typecast // num1 is now 4 (truncates) num2 = (int) Math.round(4.302940); // num1 = (int) Math.floor(2345.86759); // System.out.println("num1 is " + num1); num2 = (int) Math.ceil(4.3540495); // System.out.println("sqrt of 26 is" + Math.sqrt(26)); System.out.println(num1); System.out.println(num1 + num2); System.out.println(num1 + ' ' + num2); System.out.println(num1 + " " + num2); System.out.println("num1 is " + num1 + num2); System.out.println(num1 + num2 + "num1 is "); System.out.println("sum is " + (num1 + num2)); num1 = 3; bigreal = 20.34; d = bigreal / 2 + 14 * num1 / 5; // d = bigreal / 2 + 14 * num1 / 5.0; // num2 = 5; d = bigreal / 2 + 14 * (double) num1 / num2; // num2 = 42 % 5; // num1 = 7 % 23; // bigreal = 10 / 3; // bigreal = 10 / 3.0; // System.out.println(d + " " + num2 + " " + bigreal); System.out.printf("%.1f %5d %15.8f\n", d, num2, bigreal); System.out.println("end"); // shortcuts int num = 6; double result; num = num * 2; // num *= 2; // num = num + 10; // num += 10; // num = num + 1; // num += 1; // num++; // ++num; // num = 6; result = 10 + num++; System.out.println(result + " " + num); num = 6; result = 10 + ++num; System.out.println(result + " " + num); num--; int a = 9; num = a % 2 - 2 % a ; System.out.println(num); } } /* OUTPUT RESULTS: */