1 |
e01 |
CS56 w18 |
Name: | ||
---|---|---|
(as it would appear on official course roster) | ||
Umail address: | @umail.ucsb.edu |
EXAM: e01: Midterm 1
ready? | date | points |
---|---|---|
true | Mon 02/12 11:00AM |
You may not collaborate on this exam with anyone. If you need to use the restroom, you must leave your cell phone with the exam proctor before leaving the room.
- Write your name at the top of this page AND EVERY ODD NUMBERED PAGE.
- Double check that you turned in ALL pages; look for "End of Exam" on the last page.
- This exam is closed book, closed notes, closed mouth, cell phone off.
- You are permitted one sheet of paper (max size 8.5x11") on which to write notes.
- This sheet will be collected with the exam, and might not be returned.
- Please write your name on your notes sheet.
-
For this question, you need page 1 of handout A with code for these files:
Beverage.java
,Edible.java
,Food.java
,FreeCandy.java
andProduct.java
. These are classes used by a grocery store known as “Trader Bobs”.Some of these methods will compile and run, while others will not.
Indicate, for each method, whether it compiles or not, and if it does compile, the output when invoked. in context of the classes on handout A and assuming the methods appear inside this class:
public class TraderBobs { // methods appear here }
Will it compile? Output when called (only if it compiles) ☐ Yes
☐ No- (4 pts)
public static void TB01 () { Edible o = new Beverage(89,"Diet Coke",0,12.0); System.out.println("o: " + o.getFluidOunces()); }
-
(4 pts)
public static void TB02 () { Edible t = new FreeCandy(30); System.out.println("t: " + t.getPrice()); }
- (4 pts)
-
Continued from previous problem…
Some of these methods will compile and run, while others will not.
Indicate, for each method, whether it compiles or not, and if it does compile, the output when invoked. in context of the classes on handout A and assuming the methods appear inside this class:
public class TraderBobs { // methods appear here }
-
(4 pts)
public static void TB03 () { Beverage m = new Beverage(99,"Coke",150,12.0); System.out.println("m: " + m.getCalories()); }
-
(4 pts)
public static void TB04 () { FreeCandy s = new FreeCandy(25); System.out.println("s: " + s.getName()); }
-
(4 pts)
public static void TB05 () { Edible q = new Edible(149,"Snickers",245,1.56); System.out.println("q: " + q.getCalories()); }
-
(4 pts)
public static void TB06 () { Food r = new Food(99,"Almonds",100,0.63); System.out.println("r: " + r.getName()); }
-
(4 pts)
public static void TB07 () { Edible p = new Food(249,"Kind Bar",200,1.4); System.out.println("p: " + p.getCalories()); }
-
-
Continued from previous problem…
Some of these methods will compile and run, while others will not.
Indicate, for each method, whether it compiles or not, and if it does compile, the output when invoked. in context of the classes on handout A and assuming the methods appear inside this class:
public class TraderBobs { // methods appear here }
-
(4 pts)
public static void TB08 () { Beverage n = new Edible(199,"Gummi Bears",520,5); System.out.println("n: " + n.getPrice()); }
-
(4 pts)
public static void TB09 () { Product u = new Product(299,"Ziploc Bags"); System.out.println("u: " + u.getName()); }
-
(4 pts)
public static void TB10 () { Product v = new FreeCandy(30); System.out.println("v: " + v.getCalories()); }
-
-
(10 pts) For this question, and all the remaining ones on this exam, you should refer to page 2 of handout B which shows a short program called
AllFactors.java
, along with it’s output. As you can see, this program outputs a list of the prime factors of the integers from 1 to 50.You should also refer to page 2 of handout A which shows the
Factors
class; this class is used by theAllFactors
class. To help you understand what the methods of theFactor
class are supposed to do, handout B also has some code for JUnit tests for these classes, along with some explanatory comments. You may assume that all of the test cases pass; this has been verified.- Please note that NOT ALL OF THE CODE for the
Factors
is shown. - Missing code is indicated with with blanks (_____).
Similar to the
Polynomial
class from lab02, the classFactors
extendsjava.util.ArrayList
. A reference forArrayList
appears on page 1 of handout B.In this problem, you’ll be asked to fill in some of the missing code for this class. Please read the instructions carefully, and also read ALL of the code and test cases before asking any questions. The answers lie in the code.
The constructor for
Factors
takes a plain old Javaint []
as a parameter, and puts all of the elements into theArrayList<Integer>
for the Factors object.This method is incomplete. You might not use all of the blanks! (that is, the correct answer might leave some of them unused.) Since answers and approaches may vary, if you need to write more lines of code, fit them in as needed; the intended answer uses no more blanks than the ones provided.
public Factors(int [] nums) { ___________________________________________ for (int i: nums) { ______________________________________ } ___________________________________________ }
- Please note that NOT ALL OF THE CODE for the
-
(10 pts) Refer to the previous problem for an explanation of the context of this code. Fill in the missing line.
It can be solved with one line but there are multiple possible approaches. If your solution requires two or three lines, fill in those lines above and below the blank as needed.
/** add factor to end of list */ public void addFactor(int f) { ________________________________________ }
-
(10 pts) Refer to the previous problem for an explanation of the context of this code. Fill in the missing line.
It can be solved with one line but there are multiple possible approaches. If your solution requires two or three lines, fill in those lines above and below the blank as needed.
/** replace last factor with the value i */ public void replaceLastFactor(int i) { _______________________________ }
-
(10 pts) Refer to the previous problem for an explanation of the context of this code. Fill in the missing line.
It can be solved with one line but there are multiple possible approaches. If your solution requires two or three lines, fill in those lines above and below the blank as needed.
public void addNextFactor() { // set to the last element in the ArrayList int lastElement = ____________________________________ // in special case where last element is 4, need <= lastElement/2 not < 2 for (int i=2; i<=lastElement/2; i++) { if (lastElement % i == 0) { this.replaceLastFactor(i); this.addFactor(lastElement/i); return; } } }
-
(10 pts) Note that there is no code for an overridden
toString
method for theFactors
class. And yet the sample output does not show this, like we might expect for a class with no overridden toString in the source code.i: 1 factors: Factors@ab341245 i: 2 factors: Factors@ab3094fc i: 3 factors: Factors@ab34d284 i: 4 factors: Factors@ab3410fe i: 5 factors: Factors@ab3fca51 ... etc.
Explain why we get the output that we do, i.e.
i: 1 factors: [1] i: 2 factors: [2] i: 3 factors: [3] i: 4 factors: [2, 2] i: 5 factors: [5] ... etc.
- (10 pts) Explain the role of "targets" in an ant `build.xml` file. Include only enough detail that at a job interview, the interveiwer would be confident that you know what you are talking about.