You Must Show Your Work for Full Credit
Total Page:16
File Type:pdf, Size:1020Kb
Computer Science 240 Name: KEY Section 01
EXAM 02 March 12, 2008
This exam has 4 pages, including this cover page. Please make sure you have all 4 pages.
You have 55 minutes to complete this exam.
You must show your work for full credit.
Honor Pledge I am familiar with the policy for Academic Page Possible Points Score Integrity as outlined in the Pathfinder: Number (intranet.juniata.edu/policies/pathfinder/aca dhonesty.html).
I have not discussed the contents of this 2 40 exam before it has been administered to me, and I will not discuss the contents of this exam before it is administered to someone 3 32 else.
I understand that failure to comply with this 4 28 agreement will constitute cheating and subject me to a charge of academic dishonesty.
Total 100 ______Signature
______Date
Your Current Average in the
Course is: ______1. For the following questions, consider the StringLog, StringLogInterface, and LLStringNode classes presented in Chapter 2. [4 pts] Is the following statement valid? Explain. StringLogInterface myLog = new StringLogInterface(); Can’t instantiate an Interface. Need something like: StringLogInterface myLog = new LinkedStringLog();
[6 pts each] For each of the following sequences, complete the figure of the node and link structure. (a) LLStringNode node1 = new LLStringNode("alpha"); LLStringNode node2 = new LLStringNode("beta"); LLStringNode node3 = new LLStringNode("gamma"); node1.setLink(node3); node 1 node 2 node 3 node2.setLink(node3); info: link: info: link: info: link: “alpha” “beta” “gamma”
(b) LLStringNode nodeList = new LLStringNode("alpha"); LLStringNode node = new LLStringNode("beta"); node.setLink(nodeList); nodeList = node; LLStringNode node = new LLStringNode("gamma"); node nodelist node.setLink(nodeList); nodeList = node; info: link: info: link: info: link: “alpha” “beta” “gamma”
[16 pts] 2. Fill in the following table (you should assume integer arithmetic when evaluating numerically):
Infix Prefix Expression Evaluated Numerically
2 + 3 * 5 OR 2 + ( 3 * 5 ) + 2 * 3 5 17
8 + 2 / 3 + 8 / 2 3 8
2 + 3 * ( 8 – 6 ) + 4 + + 2 * 3 - 8 6 4 12
( ( 4*3 ) – 5 ) / 2 OR (4*3-5) / 2 / – * 4 3 5 2 3
[8 pts] 3. Consider a sorted list called list . Two objects are to be stored in list , S1 and S2 . Each object holds a name and a “key” variable weight . The objects can be modified with the diet method, which changes the weight field of the object.
Modify the two diagrams to show the (a) by-copy (b) by-reference final state, after the following code is run. In (a) the list is “by-copy” and in (b) the list is “by-reference”: S1 Jack, 190 S1 Jack, 190
list.insert(S1); list.insert(S2); S2 Jenny, 100 S2 Jenny, 100 S2.diet(-5); Jack, 190 list list Jenny, 105 4. Consider the Car class below, which was also presented in Exam 01. [8 pts] Modify the Car class, using the MilesOBExcep exception, so that it prints out the message “Invalid initial miles” if a Car object is instantiated with less than 0 miles. Make the appropriate modifications directly to the Car class, as well as the MilesOBExcep class. public class MilesOBExcep extends public class Car Exception { { protected double miles; protected String color; public MilesOBExcep() { public Car ( double newMiles, String newColor ) super(); throws MilesOBExcep } { public MilesOBExcep(String message) { if (newMiles>=0) super(message); } miles = newMiles;
} else throw new MilesOBExcep("Invalid initial miles"); color = newColor; } public Car ( ) { color = "blue"; miles = 0; } public double getMiles ( ) { return miles; } public String getColor ( ) { return color; } public void changeMiles ( double newMiles ) { miles = newMiles; } public void changeColor ( String newColor ) { color = newColor; } } [8 pts] Now, you should also demonstrate what a try-catch block might look like, around the instantiation of the // put a try-catch block in here, object “chevy,” in the box below. // around the the instantiation [16 pts] 5. Answer the following questions: // of the chevy object below What is a Deep Copy in Java? A full copy of an object, including full copies of any data try { members. The new, copied-to object, has NO references to the Car chevy = new Car( original object or its data members. Integer.parseInt(args[0]), Integer.parseInt(args[1]) Give a closed-formula (NOT just using “sigma” notation) for the
); series: 1 + 2 + 3 + … + (n-1) + n } n*(n+1)/2 = n2 / 2 + n / 2 catch (MilesOBExcep MOB) { What is an activation record? System.out.println (MOB.getMessage()); return; } Space used at run-time to store information about a method call, including parameters, local variables, and return addresses.
What is a self-referential object? An object that includes an instance variable with a reference to an object of the same class. 6. Some historians attribute the first mathematical algorithm to Euclid, for the calculation of the “greatest common divisor,” or GCD of two integers. For example, the GCD of 20 and 16 is 4, and it is calculated recursively as:
GCD(20,16) = GCD(16,4) = GCD(4,0) = 4
In pseudo-code, the definition of the GCD, is:
if b == 0 then GCD( a , b ) = a otherwise GCD(a,b) = GCD( b , a%b )
Recall that the modulus operator, %, gives the remainder in integer division. For example, 20 % 16 is 4.
[16 pts] Complete the recursive Java method below to implement the calculation of the greatest common divisor.
public static int GCD( int a , int b) {
if ( b == 0) return a; else return GCD(b,a%b);
}
[12 pts] 7. The following code segment is a count-controlled loop going from 1 through 5. At each iteration, the loop counter is either printed or put on a stack, depending on the boolean result returned by the method random (assume that random randomly returns either true or false). At the end of the loop, the items on the stack are removed and printed. Because of the logical properties of a stack, this code segment cannot print certain sequences of the values of the loop counter.
for ( count = 1; count <= 5; count++) { if (random()) System.out.println(count); else stack.push(count); }
while (!stack.isEmpty()) { number = stack.top(); stack.pop(); System.out.println(number); }
Characterized by an increasing sequence (printed during the count up), followed by a decreasing sequence (printed during the “pop” loop) of the remaining integers.
Now, consider each of the following output sequences. Which of them could be generated by the above code segment (the output on separate lines, via println, was not shown for space considerations)?
(Circle one) True or False 1 3 5 2 4 Is a possible output .
(Circle one) True or False 1 3 5 4 2 Is a possible output .
(Circle one) True or False 1 2 3 4 5 Is a possible output .
(Circle one) True or False 5 4 3 2 1 Is a possible output .