CS 120 – Fundamentals of Computing I - Rose

Printed 7/28/2017
CSSE 120 Final Exam, Spring term, 2002-2003
page 1
CSSE 120 – Fundamentals of Computing I
Final Exam Written Part
Spring term 2002-2003
Your name: ___________________ Section: ___________ Campus Mailbox: ________
Problem
Points
available
1
5
2
5
3
10
4
10
5
5
Computer
65
Total
100
Your score
Printed 7/28/2017
CSSE 120 Final Exam, Spring term, 2002-2003
page 2
1) (5 points) Consider the following code which contains compile-time errors. Fix them.
public class Dog {
private String name;
private Thread t;
public Dog() {
t = new Thread(this);
t.start();
}
public Dog(String name) {
this.name = name;
}
public void start() {
bark();
}
public void bark() {
System.out.println(“Woof woof”);
}
}
2) (5 points) Consider the constructors from problem (1). Why can we have more than
one constructor?
Printed 7/28/2017
CSSE 120 Final Exam, Spring term, 2002-2003
page 3
3) (10 points) Given the Dog class from problem (1), consider the following code
snippet. This code is incorrect. Explain why. Then explain three ways in which you
can fix the problem, evaluating the quality of each solution in terms of good
programming practice.
Dog d = new Dog();
d.name = “Fifi”;
4) (10 points) Write a method that finds the largest number in an array of integers. Pass
the array to the method. This is the only parameter that your method should accept.
The method returns the largest number.
5) (5 points) What happens when you call the repaint() method from within the
paintComponent() method?
Printed 7/28/2017
CSSE 120 Final Exam, Spring term, 2002-2003
page 4
Final Exam: Computer Part
Instructions:

Copy the entire folder (by browsing through MyComputer)
G:\cs\cs120\exams\finalexam
to anywhere on your hard drive. It contains a JCreator project as well as the electronic
version of this exam.
 Be absolutely sure that you copy it to your C: drive. Do not try to access it on the
G: drive.
 Compile and run the project. It should produce the image below (the electronic
version of the exam contains color images).

The problems on the following pages will guide you through a series of modifications
to the project.

The problems build on each other, so implement and test each one before
attempting the next one.

Read all of the problems before starting the first one.

If you don’t understand what is required for a problem, ask your instructor.

All code you write should compile correctly, but because of time constraints, no
documentation is required and efficiency is not considered a part of your grade.

If you have any compile-time errors that you cannot resolve, you may ONCE ask for
help from your instructor. After that, you are on your own.

If you cannot get rid of compile-time errors for one (or more) of the parts of this
problem, write down your answer to that part (or parts) of the problem on the back of
the test and remove from the project whatever seems to be causing the compile-time
errors.

Turn in your project electronically by copying your entire folder to your turnin folder
at:
G:\cs\cs120\turnin\YourSection\YourUsername\FinalExam\



Please do NOT create a CVS module.
Ask your instructor for help if you have any trouble doing the copying.
Time permitting, your instructor or an assistant will grade your project when you are
done.
Printed 7/28/2017
CSSE 120 Final Exam, Spring term, 2002-2003
page 5
1) Complete the sequence of problems on the following pages. Read all of the problems
before you start working.
a) (5 points) Modify your program so that it displays four squares, as shown in the
image below:
i) One empty red square,
ii) One red square containing a red checker,
iii) One red square containing a black checker, and
iv) One empty black square.
b) (15 points) Modify your program so that clicking on a square selects that square,
and clicking on it again deselects it. Selected squares should be displayed with
white backgrounds. For example, clicking once on each of the four squares
should result in the image below.
c) (5 points) Modify your program so that only squares containing checkers can be
selected.
This problem continues on the next page.
Printed 7/28/2017
CSSE 120 Final Exam, Spring term, 2002-2003
page 6
d) (15 points) Modify your program so that when a square is selected, clicking on
another square deselects it, whether or not the second square contains a checker.
If the second square does contain a checker, then it should be selected. For
example, in the state shown in the first figure below, clicking on the indicated
square would result in the state shown in the second figure below.
e) (5 points) Modify your program so that it displays an 8x8 matrix of red squares,
each of which contains a black checker and retains all of the functionality from
the previous steps. Note: you must use arrays properly to receive credit for this
problem.
f) (10 points) Modify your program so that the colors of the squares alternate
between red and black, and checkers are only placed in the red squares, as shown
in the image below. Note that the upper left hand square should be red.
g) (10 points) Modify your program so that it places red and black checkers in the
appropriate starting locations, as shown in the image below.
This problem continues on the next page.
Printed 7/28/2017
CSSE 120 Final Exam, Spring term, 2002-2003
page 7
h) (Bonus 10 points) Modify your program so that when a square is selected,
clicking on an empty red square moves the checker to that square (whether or not
this would be a legal move in the game of checkers). All other behavior should
remain unchanged. For example, in the state shown in the first figure below,
clicking on the indicated square would result in the state shown in the second
figure below.