Mathematics 130



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.

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);

node2.setLink(node3);

(b) LLStringNode nodeList = new LLStringNode("alpha");

LLStringNode node = new LLStringNode("beta");

node.setLink(nodeList);

nodeList = node;

LLStringNode node = new LLStringNode("gamma");

node.setLink(nodeList);

nodeList = node;

[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”:

list.insert(S1);

list.insert(S2);

S2.diet(-5);

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.

[8 pts] Now, you should also demonstrate what a try-catch block might look like, around the instantiation of the object “chevy,” in the box below.

[16 pts] 5. Answer the following questions:

What is a Deep Copy in Java?

A full copy of an object, including full copies of any data members. The new, copied-to object, has NO references to the original object or its data members.

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

What is an activation record?

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 =0)

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;

}

}

Your Current Average in the

Course is: _________________

info: link:

“gamma”

public class MilesOBExcep extends Exception

{

public MilesOBExcep()

{

super();

}

public MilesOBExcep(String message)

{

super(message);

}

}

info: link:

“gamma”

info: link:

“beta”

info: link:

“alpha”

info: link:

“alpha”

node 1

node 2

node 3

node

nodelist

Jack, 190

Jenny, 105

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download