Chapter2Elementary Programming
1.Valid identifiers:applet,Applet,$4,apps,x,y,
radius
Invalid identifiers:a++,--a,4#R,#44,class,public, int
Keywords:
class,public,int
2.double miles=100;
final double KILOMETERS_PER_MILE= 1.609;
double kilometers=KILOMETERS_PER_MILE*miles;
System.out.println(kilometers);
The value of kilometers is160.9.
3.There are three benefits of using constants:(1)
you don’t have to repeatedly type the same value;(2)
the value can be changed in a single location,if
necessary;(3)the program is easy to read.
final int SIZE=20;
4.
a=46/9;=>a=5
a=46%9+4*4-2;=>a=1+16–2=15
a=45+43%5*(23*3%2);=>a=45+3*(1)=48
a%=3/a+3;=>a%=3+3;a%=6=>a=a%6=1;
d=4+d*d+4;=>4+ 1.0+4=9.0
d+= 1.5*3+(++a);=>d+= 4.5+2;d+= 6.5;=>d=7.5
d-= 1.5*3+a++;=>d-= 4.5+1;=>d=1– 5.5=-4.5
5.
2
2
-4
-4
1
6.(2+100)%7= 4.So it is Thursday.