COMP 132 Practice Midterm Exam I Solution 75 points name: 1. (12 ...
COMP 132 Practice Midterm Exam I Solution 75 points name: 1. (12 ...
COMP 132 Practice Midterm Exam I Solution 75 points name: 1. (12 ...
Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
<strong>name</strong>:<br />
<strong>COMP</strong> <strong>132</strong> <strong>Practice</strong> <strong>Midterm</strong> <strong>Exam</strong> I <strong>Solution</strong><br />
<strong>75</strong> <strong>points</strong><br />
<strong>1.</strong> (<strong>12</strong> <strong>points</strong>) Find and clearly explain at least three errors in the following program. The errors could<br />
be compile time or runtime errors. Answers are at the top of the next page.<br />
public interface Tasty {<br />
int flavorLevel();<br />
boolean tastierThan(Tasty t);<br />
}<br />
public class Pizza implements Tasty {<br />
private boolean extraCheese;<br />
public Pizza(boolean ec) { extraCheese = ec; }<br />
public int flavorLevel() {<br />
if (extraCheese) { return 10; } else { return 5; }<br />
}<br />
}<br />
public boolean hasExtra() { return extraCheese; }<br />
public class Anchovy {<br />
private int salt;<br />
public Anchovy(int initSalt) { salt = initSalt; }<br />
public int flavorLevel() { return salt + 5; }<br />
public int saltiness() { return salt; }<br />
}<br />
public boolean tastierThan(Tasty t) { return salt > t.saltiness(); }<br />
public class Salad implements Tasty {<br />
private String dressing;<br />
}<br />
public Salad(String initDressing) { dressing = initDressing; }<br />
public int flavorLevel() { return dressing.length(); }<br />
public boolean tastierThan(Tasty t) { return this.flavorLevel() > t.flavorLevel(); }<br />
public static void main(String [] args) {<br />
Tasty s = new Salad("Peppercorn Ranch");<br />
Anchovy a = new Anchovy(30);<br />
System.out.println(s.tastierThan(a));<br />
Pizza p = (Pizza) s;<br />
System.out.println(p.hasExtra());<br />
}<br />
1
The Pizza class does not implement the tastierThan method, even though it implements the Tasty<br />
interface.<br />
In main, an instance of Anchovy is passed as a parameter to the tastierThan method, but Anchovy<br />
does not implement the Tasty interface.<br />
In main, an instance of Salad is cast to Pizza - this would cause a ClassCastException at runtime.<br />
2. (5 <strong>points</strong>) Was the Minesweeper lab a good example of the Model-View-Controller design pattern If<br />
so, which parts of the code were the model and which parts were the view If not, why not<br />
Yes. The model is the MineSweeperBoard class because it maintains the state of the application and<br />
does all computation. The view is the MineSweeper class because it defines the GUI for the application.<br />
3. Suppose that class <strong>Exam</strong>pleClass in package comp<strong>132</strong>.exam1 declares a field as follows:<br />
public static final int TWO = 2;<br />
State whether or not each of the following method definitions would compile (your answer for each<br />
should just be yes or no):<br />
(a) (2 <strong>points</strong>) Assume this method is in class <strong>Exam</strong>pleClass in package comp<strong>132</strong>.exam<strong>1.</strong><br />
public void printFour() { System.out.println(TWO + 2); } Yes<br />
(b) (2 <strong>points</strong>) Assume this method is in class <strong>Exam</strong>pleClass in package comp<strong>132</strong>.exam<strong>1.</strong><br />
public void addTwo() { TWO = TWO + 2; } No<br />
(c) (2 <strong>points</strong>) Assume this method is in class SampleClass in package comp<strong>132</strong>.examples.<br />
public void printTwo() { System.out.println(comp<strong>132</strong>.exam<strong>1.</strong><strong>Exam</strong>pleClass.TWO); }Yes<br />
4. (8 <strong>points</strong>) What output is displayed by the following code snippet<br />
int [] [] mat = new int [4][4];<br />
for (int i = 0; i < mat.length; i++) {<br />
for (int j = 0; j < mat[0].length; j++) {<br />
mat[i][j] = (i + 1) * (j + 1);<br />
}<br />
}<br />
int j = mat[0].length - 1;<br />
for (int i = 0; i < mat.length; i++) {<br />
System.out.println(mat[i][j]);<br />
j = j - 1;<br />
}<br />
5. (5 <strong>points</strong>) Suppose that a program crashes because a method is called when its precondition is not<br />
satisfied. Is this the fault of the programmer who wrote the method, or of the programmer who wrote<br />
the code that called the method Why<br />
The programmer who wrote the code that called the method. The precondition is a contract between the<br />
method and calling code, and a method is only guaranteed to work correctly when its precondition is<br />
satisfied. Hence, the second programmer broke the contract by calling the method when its precondition<br />
was not satisfied.<br />
4<br />
6<br />
6<br />
4<br />
2
6. (39 <strong>points</strong>) The following definition of class Employee will be used for storing information about the<br />
employees of a large company. Complete the definition of the equals method according to the given<br />
javadoc specification.<br />
public class Employee {<br />
private String <strong>name</strong>;<br />
private int yearHired;<br />
private int salary;<br />
public String getName() { return <strong>name</strong>; }<br />
public int getYearHired() { return yearHired; }<br />
public int getSalary() { return salary; }<br />
public Employee(String initName, int initYear, int initSalary) {<br />
<strong>name</strong> = initName;<br />
yearHired = initYear;<br />
salary = initSalary;<br />
}<br />
public void giveRaise(int amount) {<br />
salary = salary + amount;<br />
/** @return whether or not obj is an employee with the same <strong>name</strong>, year hired and<br />
salary as the calling object */<br />
public boolean equals(Object obj) {<br />
if (obj instanceof Employee) {<br />
Employee emp = (Employee) obj;<br />
return <strong>name</strong>.equals(emp.<strong>name</strong>) && salary == emp.salary<br />
&& yearHired == emp.yearHired;<br />
} else {<br />
return false;<br />
}<br />
}<br />
}<br />
On the next page, complete the definition of the constructor and equals method for the Manager<br />
class, which represents managers who work for the company. The constructor must initialize all fields<br />
(includingthoseinheritedfromEmployee),andyouarenotallowedtoaddanymethodstotheEmployee<br />
class.<br />
3
public class Manager extends Employee {<br />
private int numManagees; // the number of employees managed by this manager<br />
public int getNumManaged() { return numManagees; }<br />
/** initialize all fields of the manager with the specified parameters */<br />
public Manager (String initName, int initYear, int initSalary, int numManaged) {<br />
}<br />
super(initName, initYear, initSalary);<br />
numManagees = numManaged;<br />
/** @return whether or not obj is a Manager with the same <strong>name</strong>, year hired, salary<br />
and number of managees as the calling object. For full credit, do not use any<br />
of the accessors from the Employee class (getName, getYearHired, getSalary in<br />
your solution. */<br />
public boolean equals(Object obj) {<br />
if (super.equals(obj)) {<br />
return obj instanceof Manager && ((Manager) obj).numManagees == numManagees;<br />
} else {<br />
return false;<br />
}<br />
}<br />
}<br />
Finally, complete the definitions of the hireEmployee, giveEmployeesRaise and managementReport<br />
methods of the Payroll class (on the next page) according to the given javadoc specifications. Note<br />
that the signature of the hireEmployee method is not given - you must provide the parameters and<br />
return type for this method. If you need more space, you can use the back of the page as long as you<br />
indicate that you have done so.<br />
import java.util.ArrayList;<br />
public class Payroll {<br />
private ArrayList allEmployees;<br />
public Payroll() {<br />
allEmployees = new ArrayList();<br />
}<br />
4
** the hireEmployee method can add both regular employees and Managers to the<br />
payroll - define it here */<br />
public void hireEmployee(Employee e) {<br />
allEmployees.add(e);<br />
}<br />
/** increase the salary of all employees who are not managers by the<br />
specified amount */<br />
public void giveEmployeesRaise(int amount) {<br />
}<br />
for (Employee e : allEmployees) {<br />
if (!(e instanceof Manager)) {<br />
e.giveRaise(amount);<br />
}<br />
}<br />
/** print the <strong>name</strong> of each manager and the number of employees that person manages */<br />
public void managementReport() {<br />
for (Employee e : allEmployees) {<br />
if (e instanceof Manager) {<br />
System.out.print(e.getName() + ": " + ((Manager) e).getNumManaged());<br />
}<br />
}<br />
}<br />
}<br />
5