6.092: Intro
to
J
av
a
3:
Loops,
A
rray
s
A
s
s
ignment
2
F
oo Corpor
ati
on
needs
a
pr
ogram to
c
al
cul
ate
how
muc
h
to
pay their
empl
oyees.
1.
P
ay
=
hours
w
orked x
bas
e
pay
2.
Hour
s
ov
er
40
get pai
d
1.5 the bas
e
pay
3.
The base
pay
mus
t be no l
es
s
than
$8.00
4.
Th
e number
of hour
s
mus
t
be no more than 60
Frequent
I
ssues
(I
)
T
he s
i
gnatur
e
of
the
ma
i
n
method
c
annot
be
modif
ie
d.
public s
tatic
void
m
ain(
St
ring
[]
argum
ents)
{
..
.
}
Frequent
I
ssues
(I
I
)
Re
t
ur
n
v
a
l
ues
:
i
f
y
ou de
cl
a
r
e
t
ha
t
t
he
me
t
hod
i
s not
voi
d
,
th
en
i
t h
as
to
retu
rn
so
m
eth
i
n
g
!
public st
atic
in
t
pay
(doubl
e base
Pay, i
nt hou
rs) {
if (b
asePay
< 8.0
)
re
turn
-1
;
else
if (ho
urs >
60)
re
turn
-1
;
else {
i
nt sal
ary =
0;
..
.
r
eturn
salary
}
}
Frequent
I
ssues
(
III)
Don't c
r
eate dupl
i
c
ate
v
a
ri
abl
es
w
ith
the
s
ame
name
p
ubl
ic
sta
tic in
t p
ay(
dou
ble
ba
seP
ay,
in
t h
our
s)
{
in
t
s
ala
ry
= 0
;
//
OK
…
in
t
s
ala
ry
= 0
;
/
/ s
ala
ry
alr
ead
y d
efi
ned
!!
…
do
ubl
e
s
ala
ry
= 0
;
/
/sa
lar
y a
lre
ady
de
fin
ed!
!
…
}
cla
ss
We
ekl
yP
ay {
pub
lic
s
tat
ic
vo
id
pa
y(
dou
bl
e
b
as
ePa
y,
in
t
hou
rs
) {
if (
bas
eP
ay < 8.0
) {
Sys
te
m.o
ut
.pr
in
tln
(
"
You
m
ust
b
e p
ai
d a
t
lea
st
$8
.0
0/h
ou
r"
);
} el
se if (h
ou
rs > 60) {
Sys
te
m.o
ut
.pr
in
tln
(
"
You c
an'
t wor
k mor
e tha
n 60 ho
urs a we
ek
"
);
} el
se {
int
o
ver
ti
meH
ou
rs = 0;
if (h
our
s >
4
0) {
o
ver
ti
meH
ou
rs = hou
rs
-4
0
;
h
our
s =
4
0;
}
dou
bl
e
p
ay
=
ba
seP
ay
*
ho
urs
;
pay
+
= o
ve
rti
me
Hou
rs
*
ba
seP
ay
*
1.
5;
Sys
te
m.o
ut
.pr
in
tln
(
"
Pay
t
his
e
mpl
oy
ee
$"
+ pa
y);
}
}
pub
lic
s
tat
ic
vo
id
ma
in
(
St
ri
ng
[
] arg
um
ent
s) {
pa
y(
7.5
, 35)
;
pa
y(
8.2
, 47)
;
pa
y(
10.
0,
73
);
}
}
W
hat
we hav
e
learned
so far
●
V
ariables
&
t
y
pes
●
Operat
ors
●
Ty
pe conv
ers
ions
&
casting
●
Met
hods &
paramet
ers
●
If
stat
ement
T
oday
’
s
T
opics
●
Good programm
ing
st
y
le
●
Loops
●
A
rray
s
G
ood Pr
ogr
amming
S
tyle
Good programm
ing
st
y
le
Th
e goal
of
good s
ty
l
e
i
s
to
mak
e y
our
c
ode mor
e
re
adabl
e.
By y
ou and by
others
.
Ru
le #1:
u
s
e g
ood (
m
ean
in
g
f
ul
)
n
am
es
Str
ing
a
1;
int
a2
;
dou
ble
b
;
/
/ BAD
!!
Str
ing
f
irs
tN
ame
;
//
G
O
OD
Str
ing
l
ast
Na
me;
//
G
O
OD
int
te
mp
era
tu
re;
//
G
O
OD
Ru
le
#2:
Us
e in
de
nt
at
io
n
p
ub
lic
s
tat
ic
vo
id
ma
in
(
St
ri
n
g
[]
ar
gu
men
ts
) {
in
t
x
= 5;
x = x
* x
;
if
(
x >
2
0)
{
Sys
te
m.o
ut
.pr
in
tln
(x
+
“ is gr
eat
er th
an 20
.”
);
}
do
ub
le
y =
3
.4
;
}
Ha
v
e
a
de
mo wi
t
h
no
i
nde
nt
a
t
i
on
C
trl-s
h
ift
-F
to
au
to
-
fo
rm
at th
e
fi
l
e
Ru
le
#3:
Us
e w
h
it
es
p
ac
es
P
u
t w
h
i
te
sp
a
ce
s
i
n
co
m
p
l
e
x
e
x
p
re
ssio
n
s:
// BAD!
!
double
cel=f
ahr*42
.0/(1
3.0-7
.0);
// GOOD
double
cel =
fahr
* 42.
0 / (
13.0
- 7.0
);
Ru
le
#3:
Us
e w
h
it
es
p
ac
es
P
u
t b
l
an
k
l
i
n
es
t
o i
m
p
r
ov
e
r
ead
ab
i
l
i
t
y
:
public
stati
c voi
d mai
n (St
ring[
] arg
ument
s) {
int x =
5;
x = x *
x;
if (x >
20)
{
System.
out.p
rintl
n(x +
“ is
> 20
.”);
}
double
y = 3
.4;
}
Ru
le
#4:
Do n
ot
du
pl
ic
at
e
t
es
t
s
if
(ba
se
Pay
<
8
.0)
{
..
.
}
e
lse
i
f
(h
ou
rs
> 60
) {
..
.
}
e
lse
i
f
(
ba
se
Pay
>
= 8
.0
&&
h
our
s
<=
60
) {
...
}
Ru
le
#4:
Do n
ot
du
pl
ic
at
e
t
es
t
s
if
(ba
se
Pay
<
8
.0)
{
..
.
}
e
lse
i
f
(h
ou
rs
> 60
) {
..
.
}
e
lse
i
f
(
ba
se
Pay
>
= 8
.0
&&
h
our
s
<=
60
){
..
.
}
BAD
Ru
le
#4:
Do n
ot
du
pl
ic
at
e
t
es
t
s
if
(ba
se
Pay
<
8
.0)
{
..
.
}
e
lse
i
f
(h
ou
rs
> 60
) {
..
.
}
e
lse
{
..
.
}
Good programm
ing
st
y
le
(summ
ary
)
Us
e good names
f
or v
ar
i
abl
es
and methods
Us
e indentati
on
Add w
hi
tes
pac
es
Don't dupl
i
c
ate
tes
ts
oop
s
L
oop
s
Loops
static v
oid
ma
in (
St
ring
[]
argum
ents)
{
Syst
em.out
.print
ln(
“Ru
le #1”
);
Syst
em.out
.print
ln(
“Ru
le #2”
);
Syst
em.out
.print
ln(
“Ru
le #3”
);
}
W
hat
i
f y
ou
w
ant
to do it f
or 200
Rul
es
?
Loops
Loop
operator
s
al
l
ow to
l
oop
through a
bl
oc
k
of
c
ode.
T
here
are sev
eral
l
oop operator
s
i
n J
av
a.
The
w
h
ile
operat
or
while (
condition
) {
statements
}
The
w
h
ile
operat
or
int
i=
0
;
while
(i
< 3) {
Syste
m.out.
printl
n
(
“Rul
e #“
+
i);
i = i
+1;
}
Co
unt
c
a
r
e
f
ull
y
M
a
ke
su
re
th
a
t
y
o
u
r l
o
o
p
h
a
s
a
ch
a
n
ce
to
fi
n
i
sh
.
The
fo
r
operator
for (
in
iti
ali
zat
ion
;
co
ndi
tio
n
;
u
pda
te
){
sta
tem
ent
s
}
The
fo
r
operator
for (
int
i = 0
; i <
3; i=i
+1) {
Syst
em.out
.print
ln(
“Ru
le #“
+ i);
}
No
t
e
:
i
= i+1
m
a
y be
r
e
pla
c
e
d
by
i++
S
}
B
ranching St
atement
s
br
eak
t
er
m
in
at
es
a
fo
r
or
wh
i
l
e
loop
bre
ak
;
yst
em.
out
.pr
int
ln(
“Ru
le #”
+ i
);
fo
r (
int
i=
0;
i<1
00;
i+
+)
{
i
f(i
==
50
)
r
B
ranching St
atement
s
cont
i
nue
ski
ps t
he current
i
terat
i
on
of a l
oop
and
proceeds
di
rect
l
y
to
the next
i
terat
i
on
fo
(
int i=
0; i<1
00; i+
+) {
i
f(i
==
50
)
con
tin
ue
;
S
yst
em.
out
.pr
int
ln(
“Ru
le #”
+ i
);
}
E
mbedded loops
for
(in
t i
=
0;
i <
3;
i+
+)
{
for
(i
nt
j =
2;
j
< 4
; j
++)
{
Sy
ste
m.o
ut.
pri
ntl
n (
i +
“ “
+ j
);
}
}
Sc
ope
of
the
v
ariabl
e def
i
ned i
n
the
i
ni
ti
ali
z
ati
on:
r
es
pec
ti
v
e
for
bl
oc
k
Arr
a
y
s
A
rray
s
An
arr
ay is
an i
ndex
ed
l
i
st
of
v
al
ues
.
Y
ou can
mak
e
an array of
any ty
pe
i
nt
, doubl
e
,
St
r
i
ng,
e
t
c.
.
Al
l
el
ements
of
an
ar
ray
mus
t
hav
e
the
s
ame
ty
pe.
A
rray
s
..
0
12
3
..
n-1
A
rray
s
E
x
am
ple
: double
[ ]
5.
0
2.
44
9.
01
1.
0
-9
.9
..
0
12
3
..
n-1
A
rray
s
T
h
e i
n
d
ex
star
t
s
at
zero
a
nd e
nds
a
t
l
e
ngt
h-
1
.
E
xam
p
l
e:
int[]
va
lues =
new
i
nt[5];
values[0
] = 12
;
// C
ORRECT
values[4
] = 12
;
// C
ORRECT
values[5
] = 12
;
// W
RONG!!
compi
les bu
t
// t
hrows
an Exc
eption
// a
t run-
time
H
av
e
a dem
o
w
i
t
h
r
u
n
t
i
m
e ex
c
ept
i
on
A
rray
s
An
a
rr
a
y
is
d
efined u
sin
g TYPE
[]
.
Arr
a
y
s a
r
e
j
u
st
a
no
t
h
e
r
t
y
p
e
.
int
[]
values
;
//
array
of in
t
int
[][]
values
;
//
int[]
is a
type
A
rray
s
T
o
c
r
eat
e
an
ar
r
ay
of
a g
i
v
en
s
i
z
e,
u
s
e
t
h
e op
er
at
or
new
:
int[
]
v
alu
es =
ne
w
int
[5]
;
or y
ou
m
ay
u
s
e a v
ar
i
ab
l
e
t
o s
p
ec
i
f
y t
h
e s
i
z
e:
int
siz
e =
12
;
int[
]
v
alu
es =
n
ew int
[si
ze]
;
A
rray
I
nit
ializ
at
ion
Curly
brac
es
c
an be
us
ed to
i
ni
tial
i
ze
an
arr
ay
.
It c
an
ON
L
Y
be us
ed
w
hen
y
ou
dec
l
are
the
v
ariabl
e.
int[
]
v
alu
es
= {
1
2, 24,
-2
3, 47 };
Quiz
t
ime!
Is
there an er
ror
i
n thi
s
c
ode?
int[
]
v
alu
es
= {1, 2.
5, 3, 3.5
,
4
};
A
ccessin
g
A
rra
y
s
T
o
ac
c
es
s
t
h
e el
em
e
n
t
s
of
an
ar
r
ay
,
u
s
e t
h
e
[]
op
er
at
or
:
values[i
ndex]
E
xam
p
l
e:
int[]
va
lues
= { 12
, 24,
-23, 4
7 };
values[3
] = 18
;
// {12,24
,-23,
18
}
int
x =
values
[1] +
3;
// {12,
24
,-23,1
8}
The
lengt
h
v
ariable
E
ach ar
r
a
y
has a
length
v
ari
able
built
-i
n t
hat
co
n
t
a
i
n
s
th
e l
e
n
g
th o
f
th
e
a
r
r
ay
.
int[]
va
lues =
new
int
[12
];
int
size
=
valu
es.len
gth;
/
/ 12
int[]
va
lues2 =
{1,2
,3,4,5
}
int
size
2
=
valu
es2.le
ngth;
// 5
S
t
ring array
s
A s
ide note
public s
tatic
void
m
ain (
S
tring
[
] argu
ments)
{
Syst
em.out
.print
ln(arg
uments
.lengt
h);
Syst
em.out
.print
ln(arg
uments
[0]);
Syst
em.out
.print
ln(arg
uments
[1]);
}
C
ombi
ni
ng Loops
and
Arra
y
s
Looping
t
hrough an array
E
xam
p
l
e 1
:
int[]
va
lues =
new
in
t
[5];
for
(
int
i=0;
i<val
ues.le
ngth;
i++) {
values[i
] = i;
int
y =
values
[i] *
values
[i];
System.o
ut.pri
ntln(y
);
}
Looping
t
hrough an ar
ray
E
xam
p
l
e 2
:
int[]
va
lues =
new
in
t
[5];
int
i =
0;
while
(i
< val
ues.le
ngth)
{
values[i
] = i;
int
y =
values
[i] *
values
[i];
System.o
ut.pri
ntln(y
);
i++;
}
S
umm
ary
f
or
today
1.
Pr
ogr
amming St
y
l
e
2.
Loops
3.
A
rra
ys
A
s
s
ignment
3
A
gr
oup
of
f
riends
partic
i
pate
i
n
the
B
os
ton
M
ar
athon.
F
i
nd the
bes
t perf
ormer
.
F
i
nd the
s
ec
ond
-
bes
t
perf
ormer
.
MIT OpenCourseWare
http://ocw.mit.edu
6.092 Introduction to Programming in Java
January (IAP) 2010
For information about citing these materials or our Terms of Use, visit:
http://ocw.mit.edu/terms
.