University of Massachusetts Amherst, Electrical and Computer Engineering ECE 122 Midterm Exam 1 Makeup Answer key March 2, 2018 Instructions: Closed book, Calculators allowed; Duration:120 minutes; Write your name and Student ID in all pages (in provided fields); Write your answer in the provided space and be sure to show all your work clearly. Notice there are 20 extra points in this exam, but the maximum grade is 100. Name: Lab Session (check box): Tu 10:00AM 11:15AM Tu 1:00PM - 2:15PM Th 10:00AM 11:15AM Th 1:00PM - 2:15PM Problem 1: (20 points) Problem 2: (20 points) Problem 3: (20 points) Problem 4: (20 points) Problem 5: (20 points) Problem 6: (20 points) Total: (100 points) 1. [20] What is the following code doing? Hint: try running the code in your head with different values of a and b and see what would be the result. public void p1method(int a, int b) int c=1; while (b>0) c*=a; b--; System.out.println(c); The code above is a method that receives two integers a and b and prints a b (a to the power of b).
2. [20] Convert the method presented below to use a while loop instead of a for loop. You should write the entire method in your answer. Hint: Which variable is your iterator or loop control variable? public int p2method(int a) int c=0; for (int i=0; i<a; i++) c+=i; return c; public int p2method(int a) int c=0; int i=0; while (i<a) c+=i; i++; return c;
3. [20 pts] In mathematics, the harmonic series is the divergent infinite series: 1 i=1 i =1+ 1 2 + 1 3 + 1 4 + 1 5 +... Write a Java method that calculates and returns the sum of the first n terms of the harmonic series: 1 i =1+1 2 + 1 3 + 1 4 + 1 5 +...+ 1 n 1 +1 n n i=1 Your method should receive n as an input parameter and return the result as double. public static double p3method(int n) double sum=0; for (int i = 1; i<=n ; i++) sum+=1.0/i; return sum;
4. [20] What is printed out on the console when you call the following function with values of 2 and 7 for a and b, respectively? public static void p4method(int a, int b) System.out.println(a/b); System.out.println((float)a/b); int i; for (i = 0 ; i<b ; i++) if (i%3 == 0) continue; System.out.println(i); System.out.println(i); switch (a) case 1: System.out.println(a+b); break; case 2: System.out.println(a-b); default: System.out.println(0); if (a>b) System.out.println(1); System.out.println(a>0); 0 0.4 1 2 4 5-3 0 true
5. [20] Given the StandardDie Class description below, which implements a Standard Die (6 faces), write a Java method that receives an integer parameter n, instantiates 2 dice and rolls both dice n times. In each iteration if face values on dice are equal, you should print Bonus!. import java.util.random; public class StandardDie private int value; private Random rand; public Die(int f) rand = new Random(); public int getvalue() return value; public int roll() value=rand.nextint(6)+1; return value; public static void p5method(int n) StandardDie d1 = new StandardDie(); StandardDie d2 = new StandardDie(); for (int i=0; i<n; i++) if (d1.roll() == d2.roll()) System.out.println("Bonus!");
6. [20 pts] Write Java code for Cat class according to the following specification: Class variables (all private) - name (String) - Cat name - feral (boolean) - Is it a feral cat or not? - scratchedlasttime (boolean) - did the cat scratch you last time play was called? Methods (all public) - Cat (constructor) receives the cat name and feral property and attributes those values to cat properties. Variable scratchedlasttime should be initially false. - getname getter method for name. - play Must return a string representing the what happens when you try to play with the cat. Feral cats will always scratch you (return ouch ). Cats that are not feral will not scratch you (return nice kitty ) if they didn t scratch last time play was called, otherwise this should just return ouch. Hint: use scratchedlasttime for that purpose. public class Cat private String name; private boolean feral; private boolean scratchedlasttime; public Cat(String n, boolean f) name = n; feral = f; scratchedlasttime = false; public String getname() return name; public String play() if (feral) return "ouch"; else if (scratchedlasttime) scratchedlasttime = false; return "nice kitty"; else scratchedlasttime = true; return "ouch";