Faculty of Arts & Sciences
Department of Computer Science
CMPS 200—Introduction to Programming
Exam 1 (2 hours)
Problem
1
BankLoan.java
(25%)
Write
a
program
BankLoan.java
that
calculates
the
bank
loan
(monthly
payment,
and
total
loan
to
be
paid
after
including
the
total
interest)
based
on
a
given
initial
loan
amount,
yearly
interest
rate
and
number
of
loan
years.
You
should
create
a
method,
bankLoan,
that
takes
three
parameters
(in
the
same
order):
1)
number
of
loan
years;
2)
initial
loan
amount;
3)
yearly
interest
rate,
which
corresponds
to
a
percentage.
The
formula
to
compute
the
monthly
payment
is
as
follows:
𝑖 × 𝑟
𝑚𝑜𝑛𝑡ℎ𝑙𝑦 𝑝𝑎𝑦𝑚𝑒𝑛𝑡 = !!
1− 1 + 𝑟
r:
monthly
interest
rate
n:
number
of
months
i:
initial
loan
amount
Example:
bankLoan(20,
15000.0,
10.0)
gives
the
following
output
(make
sure
to
include
the
special
characters
and
follow
the
same
exact
format):
Loan
period
in
months:
240
Annual
interest
rate:
10.0%
Initial
loan:
$15000.0
Monthly
payment:
$144.75
Total
loan
with
interest:
$34740.78
Problem
2
RandomFair.java
(30%)
Write
a
method
isRandomFair
that
takes
three
integers
a,
b
and
n
as
parameters
(where
a
<
b
and
n
>
0).
The
method
generates
n
random
integers
between
a
and
b
(both
inclusive),
and
prints
true
if
the
difference
between
(1)
the
average
of
the
random
integers,
and
(2)
the
midpoint
of
a
and
b,
is
less
than
or
equal
to
2;
it
prints
false
otherwise.
For
example,
for
a
=
3,
b
=
40,
n
=
10
and
if
the
generated
random
integers
are:
25
13
8
16
15
25
7
20
6
27,
then
the
method
should
print
the
following:
For
[3,
40]
and
n
=
10
-‐-‐
random
is
fair:
false
In
this
case,
the
average
of
the
random
integers
is
equal
to
16.2
and
the
midpoint
of
a
and
b
is
equal
to
21.5.
The
method
prints
false
since
the
difference
between
16.2
and
21.5
is
greater
than
2.
Write
a
program
RandomFair.java
that
calls
isRandomFair
method
for
a
=
1,
b
=
10
and
n
varying
from
10
to
100000
in
100
increments.
Problem
3
Bowtie.java
(45%)
Write
a
method,
bowtie,
that
takes
an
integer
height
as
a
parameter.
It
then
uses
this
parameter
to
draw
and
appropriately
size
the
figure
below.
Assume
that
the
height
is
always
an
odd
number
greater
than
5.
The
following
pictorial
illustrates
the
figure
details
when
height
is
equal
to
7:
Introduction to Programming (CMPS 200)
Write
a
program,
Bowtie.java
that
calls
the
method
for
different
values
of
height.
Sample
Run
1
(when
height
=
7)
C:>
java
Bowtie
++++++++
++++++++
+++++++++
+++++++++
++++++++++
++++++++++
++++++++++++++++++++++
++++++++++
++++++++++
+++++++++
+++++++++
++++++++
++++++++
Sample
Run
2
(when
height=11)
C:>
java
Bowtie
++++++++++++
++++++++++++
+++++++++++++
+++++++++++++
++++++++++++++
++++++++++++++
+++++++++++++++
+++++++++++++++
++++++++++++++++
++++++++++++++++
++++++++++++++++++++++++++++++++++
++++++++++++++++
++++++++++++++++
+++++++++++++++
+++++++++++++++
++++++++++++++
++++++++++++++
+++++++++++++
+++++++++++++
++++++++++++
++++++++++++
Sample
Run
3
(when
height=15)
C:>
java
Bowtie
++++++++++++++++
++++++++++++++++
+++++++++++++++++
+++++++++++++++++
++++++++++++++++++
++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
++++++++++++++++++++
++++++++++++++++++++
+++++++++++++++++++++
+++++++++++++++++++++
++++++++++++++++++++++
++++++++++++++++++++++
++++++++++++++++++++++++++++++++++++++++++++++
++++++++++++++++++++++
++++++++++++++++++++++
+++++++++++++++++++++
+++++++++++++++++++++
++++++++++++++++++++
++++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
++++++++++++++++++
++++++++++++++++++
+++++++++++++++++
+++++++++++++++++
++++++++++++++++
++++++++++++++++
Submission
Instructions
• Your
exam
submission
must
consist
of
a
single
zip
archive
named
s#_exam1_netid
that
contains
your
properly
commented
three
source
files
(.java
files)
only
(BankLoan.java,
RandomFair.java,
Bowtie.java).
No
other
files
will
be
accepted.
We
will
compile
and
run
your
programs.
Fall 2014-15 2 of 2