A. Introduction

Other than variables, an expression might also needs numeric values such as "3.1415", "2.71828" to compute. These numeric values are called "literals". Sometimes literals maybe fractions or power of tens. How would you write fractions or exponents? See if you can figure out from the table below.

B. Arithmetic equations are made of variables, literals and -- operators

You always need operators to compute. +, -, * are straight forward. But why 1729/10 = 172? what is 1729%10? See the table below:

C. Arithmetic equations also need math functions:

More than often, computations need to use math functions such as square root or trig functions. How would you do that?

D. Try

class java3 {

public static void main(String[] args) {

int n=17;

int m=18;

double a=n / 10 + n % 10;

double b= n % 2 + m % 2;

double c=(m + n) / 2;

double d=(m + n) / 2.0;

double e=(int) (0.5 * (m + n));

double f= (int) Math.round(0.5 * (m + n));

System.out.println( "a="+a);

System.out.println ("b="+b);

System.out.println("c="+c);

System.out.println("d="+d);

System.out.println("e="+e);

System.out.println ("f="+f);

}

} a=8.0 b=1.0 c=17.0 d=17.5 e=17.0 f=18.0

a.

n / 10 + n % 10

= 17 / 10 + 17 % 10

= 1 + 17 % 10 // 17/10=1 because 17 and 10 are integers, and return integer responses

= 1 + 7

= 8

b.

n % 2 + m % 2

=17 % 2 + 18 % 2

=1 + 0

=1

c.

(m + n) / 2

= (18 + 17) / 2

= 35 / 2

= 17 // 35/2=17 because 35 and 2 are integers

d.

(m + n) / 2.0

= (18 + 17) / 2.0

= 35 / 2.0

= 17.5

e.

(int) (0.5 * (m + n))

= (int) (0.5 * (18 + 17))

= (int) 17.5

= 17

f.

(int) Math.round(0.5 * (m + n))

= (int) Math.round(0.5 * (18 + 17))

= (int) Math.round(17.5)

= (int) 18 // Math.round(17.5) = 18 for some reason

= 18

int n=17;

int m=18;

int g=n/10+n%10;

int h=n % 2 + m % 2;

int f=(m + n) / 2;

int d=(m + n)/2;

int r=(int) (0.5 * (m + n));

int t=(int) Math.round(0.5 * (m + n));

System.out.println(g);

System.out.println(h);

System.out.println(f);

System.out.println(d);

System.out.println(r);

System.out.println(t);

8

1

17

17

17

18

int n = 17;

int m = 18;

System.out.println("a=" + (n / 10 + n % 10));

System.out.println("b=" + (n % 2 + m % 2));

System.out.println("c=" + ((m + n) / 2));

System.out.println("d=" + ((m + n) / 2.0));

System.out.println("e=" + ((int) (0.5 * (m + n))));

System.out.println("f=" + ((int) Math.round(0.5 * (m + n))));

a=8 b=1 c=17 d=17.5 e=17 f=18