public class Expressions { private void literals(){ // finite precision: System.out.print("LITERALS\nFinite precision: (1.0 - 0.1 - 0.1 - 0.1 - 0.1 - 0.1)"); if((1.0 - 0.1 - 0.1 - 0.1 - 0.1 - 0.1) == 0.5) System.out.println(" == 0.5"); else System.out.println(" != 0.5"); System.out.println("Finite precision: (1.0 - 0.1 - 0.1 - 0.1 - 0.1 - 0.1 == " + (1.0 - 0.1 - 0.1 - 0.1 - 0.1 - 0.1) ); System.out.println(); // float / doubles float f1 = 0.5F, f2 = 0.1e5F; double d1 = .1, d2 = 2d, d3 = 3.0D, d4 = 3; System.out.println("f1: 0.5F: " + f1 + ", f2: 0.1e5F: " + f2); System.out.println("d1: .1: " + d1 + ", d2: 2d: " + d2 + ", d3: 3.0D: " + d3 + ", d4: 3: " + d4); System.out.println(); } private void formatting(){ double dblTotal = 123456.789; System.out.println("FORMATTING"); System.out.printf("1: dblTotal is: $%,.2f%n", dblTotal); // %, comma notation, %n new line System.out.printf("2: dblTotal is: $%,.2f", dblTotal); // no new line System.out.printf("3: dblTotal is: %.2f%n", dblTotal); // new line System.out.println(); } private void unaBin(){ // expressions: binary * / + - int i3 = 2 - 3 + 5, i4 = 2 - (3 + 5); int i5 = 4 / 2 * 5, i6 = 4 / 2 / 2, i7 = 4 / ( 2 / 2); System.out.println("EXPRESSIONS"); System.out.println("i3: 2 - 3 + 5: " + i3 + ", i4: 2 - (3 + 5): " + i4); System.out.println("i5: 4 / 2 * 5: " + i5 + ", i6: 4 / 2 / 2: " + i6 + "," + " i7: 4 / ( 2 / 2): " + i7); int i8 = 2*3+4, i9 = 2*(3+4), i10 = 2+3*4, i11 = (2+3)*4; System.out.println("i8: 2*3+4: " + i8 + ", i9: 2*(3+4): " + i9); System.out.println("i10: 2+3*4: " + i10 + ", i11: (2+3)*4: " + i11); int i12 = 2/3+4, i13 = 2/(3+4), i14 = 2/3/4; //, i15 = 2/(3/4); System.out.println("i12: 2/3+4: " + i12 + ", i13: 2/(3+4): " + i13 + ", i14: 2/3/4: " + i14 ); // + "i15: 2/(3/4): " + i15); System.out.println(); // expressions: unary and binary - int u1 = 2 - -1, u2 = 2 - -(2 - -0), u3 = u1 - -u2; System.out.println("u1: 1 - -1: " + u1 + ", u2: 1 - -(2 - -0): " + u2 + ", u3: u1 - -u2:" + u3); System.out.println(); } private void divMod(){ System.out.println("DIVMOD\ninteger modular arithmetic, p = p/q * q + p%q"); System.out.println("remainder is negative only if dividend is negative"); //System.out.println(" 1 / 0: "+1/0+", 1 % 0: "+1%0); //System.out.println(" 1 % 0: "+1%0); System.out.println("integer division rounds towards 0 (truncates): 3/2 = 1, -3/2 = -1"); System.out.println(" 7 / 3: "+7/3+", 7 % 3: "+7%3); System.out.println("-7 / 3:"+ -7/3 +", -7 % 3: "+ -7%3); System.out.println(" 7 /-3:"+ 7 / -3+", 7 % -3: "+7 % -3); System.out.println("-7 /-3: "+ -7 / -3+", -7 % -3: "+-7 % -3); System.out.println(); System.out.println("p = p/q * q + p%q does not apply for float modular"); System.out.println("7.5 / 3: " + 7.5 / 3 + ", 7.5 % 3: "+ 7.5 % 3); System.out.println("-7.5 / 3: " + -7.5 / 3 + ", -7.5 % 3: "+ -7.5 % 3); System.out.println(); // quotient, remainder int seconds = 4000; int hours = seconds / 3600; int remainingSeconds = seconds % 3600; int minutes = remainingSeconds / 60; remainingSeconds = remainingSeconds % 60; System.out.println(seconds + " seconds = " + hours + " hour, " + minutes + " minutes and " + remainingSeconds + " seconds = " + (hours*3600+minutes*60+remainingSeconds) + " seconds"); System.out.println(); } private void incrDecr(){ // increment, decrement operators int i = 10; System.out.println("INCRDECR\n10*i++ : "+ 10*i++); i--; System.out.println("10 * ++i : "+ 10* ++i); System.out.println(); } private void casting(){ System.out.println("CASTING"); //Watch it: there is no byte or short arithmetic in JVM byte b = 10; // b = b+b; // compile time error: typeof b+b = int, operation done with int + b = (byte)(b+b); short s = 100; // s=s+s; same, fix: s=(short)(s+s); // careful: int or float / int k=5; double h = 1 + k / 2; System.out.println("double h = 1 + 5/2 = " + h); h = 1.0 + k/2; System.out.println("double h = 1.0 + 5/2 = " + h); h = 1.0 + k/2.0; System.out.println("double h = 1.0 + 5/2.0 = " + h); h = 1.0 + (double)k/2; System.out.println("double h = 1.0 + (double)5/2 = " + h); System.out.println(); int trunc_i =(int)3.9; System.out.println("trunc_i = (int)3.9 = " + trunc_i); System.out.println(); //int div = 5.0/2; //what is wrong? } public static void main(String[] args) { Expressions e = new Expressions(); e.literals(); //e.formatting(); //e.unaBin(); //e.divMod(); //e.incrDecr(); //e.casting(); } }