The Artima Developer Community
Sponsored Link

Java Answers Forum
December 14 JAVA... bijhan here

13 replies on 1 page. Most recent reply: Dec 15, 2006 4:41 AM by Vincent O'Sullivan

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 13 replies on 1 page
Sam Gold

Posts: 6
Nickname: g0dfather
Registered: Dec, 2006

December 14 JAVA... bijhan here Posted: Dec 14, 2006 12:07 PM
Reply to this message Reply
Advertisement
yo... activate your facebook account for like a couple of hours and there is a group there that a lot of the class is on... i need you to join the group there and post all the stuff there

everyone is posting there stuff there... as soon as you activate your account i can sent you an invite to the group so do it quick times

KH

bijhan


Toledo Ohio

Posts: 8
Nickname: toledoohio
Registered: Dec, 2006

Re: December 14 JAVA... bijhan here Posted: Dec 14, 2006 1:08 PM
Reply to this message Reply
ok give me 2 minutes

Sam Gold

Posts: 6
Nickname: g0dfather
Registered: Dec, 2006

Re: December 14 JAVA... bijhan here Posted: Dec 14, 2006 1:19 PM
Reply to this message Reply
Part A: Key Concepts [10 marks]


1. Explain why one should be cautious in using recursion and iteration within each other? [2 marks]



2. Why is it important that a tree be complete for an arrayList implementation?
[2 marks]





3. Explain how protected visibility can be equivalent to public visibility. [2 marks]




4. ____, which means many forms or shapes, enables the JVM to determine which method to invoke at run time? [1 mark]

0 Polymorphism.


5. Which of the following statements is correct? [1 mark]


0 A precondition is an assertion about the state of the program before a method is executed.
0 The catch clause need not follow a try block.
0 When an exception is thrown, the flow of execution is suspended and continues at the appropriate try clause.
0 You cannot use a throw statement in a lower-level method to indicate that an error condition has been detected.


6. Analyze the following pseudo code. [1 mark]

1. class Test
2. function main()
3. inner = new Inner()
4. print(inner.k)

5. class Inner
6. protected int k;

0 The program has a syntax error because the Inner class does not have a constructor and you cannot create an object from it.
0 The program has a syntax error because the Inner class is private and it cannot be accessed in the main method.
0 The program has a syntax error because k is protected in the Inner class and it cannot be accessed in the main method.
0 The program has a syntax error because the Inner class is not static and it cannot be used to create an object in the main method.

7. Implement the method hasNext() for the class Iter. [2 marks]


8. Which of the following is the inorder traversal of a binary tree? [1 mark]

0 Traverse TL, traverse TR, visit root node.
0 Visit root node, traverse TL, traverse TR.
0 Traverse TL, visit root node, traverse TR.
0 Visit root node, traverse TR, traverse TL.

9. Draw the tree that would be formed by inserting the words from the following sentence into a binary search tree “java is much like drinking coffee” [2 marks]



10. Generic methods, like generic classes, have parameters. [1 mark]

0 True.
0 False.

Part B: Exercises [25 marks]


1. Write a recursive method for the SingleLinkedList class shown below that has the following signature int countItem(<E> item). This method returns the number of times that item is located in the SingleLinkedList. You can assume that the items support an equals method. Note: The method int countItem(<E> item) is a wrapper class to the actual recursive method. [10 marks]

public class SingleLinkedList<E> {
private Node<E> head;
private int size;

public SingleLinkedList() {
head = null;
}

private class Node<E> {
private E value;
private Node next;

private Node(E obj) {
value = obj;
next = null;
}

private Node(E obj, Node next) {
value = obj;
this.next = next;
}
}
}



2. Complete the code below using an iterator to achieve the following output …
(0.0,0.0), (0.0,1.0), . . . , (0.0,4.0), . . . , (4.0,4.0). [5 marks]

List doubles = new ArrayList<Double>();
for (int c=0; c<5; c++)
doubles.add(new Double(c));

// Include code to print output by using an iterator



3. Create the UML diagram for the following code [10 marks]

public class Customer {
ArrayList accounts = new ArrayList();

public Customer() {
Account defaultAccount = new SettlementAccount ();
accounts.add(defaultAccount);
}
}

public abstract class Account {
public abstract void deposit(float amount);
public abstract void withdraw(float amount);
}



public class SettlementAccount extends Account {
private float balance = 0;
private float limit = 0;
private float debt = 0;
float availableFunds() {
return (balance + limit - debt); }
public void deposit(float amount) {
balance = balance + amount; }
public void withdraw(float amount) {
if (amount > this.availableFunds()) {
throw new InsufficientFundsException();
}
balance = balance - amount; }
}



4. For the pseudo code below, what is the output? Make certain to use the line numbers in the pseudo code as reference to any traces. [5 marks]

1. main throws Exception
2. print ("m: -1-")
3. A (3)
4. print ("m: -2-")
5.
6. function A (integer i) throws Exception
7. if (i <= 0)
8. throw Exception ("0 or negative value")
9. else
10. print ("a(",i,"): -1-")
11. A (i-1)
12. print ("a(",i,"): -2-");




5. Show for the steps of sorting the array below using the Heap sort algorithm. For each step list the number of comparisons and exchanges. [7 marks]






6. Show an adjacency list and adjacency matrix representation of a graph for the map shown below from the block defined by Spadina, Grange, Grange Park, and Stephanie. Comment on which representation has better storage efficiency for this example. [8 marks]




Use the following abbreviations for the streets. Do not consider any streets not listed below.

Sp – Spadina, Hu – Huron, GrPl – Grange Pl, Be – Beverly, McC – McCaul, GrA – Grange Ave, Su – Sullivan, St – Stephanie, Jo – John St.

Street intersections can be represented by combining the street abbreviations with a dash. For example the intersection of Spadina and Grange Ave. is Sp-GrA. Consider street intersections as nodes. Use the following index mapping to create your graph.

1. Sp-GrA 2. Sp-Su 3. Hu-GrA 4. Hu-Su 5. GrPl-GrA
6. GrPl-Su 7. Su-Be 8. Be-St 9. St-Jo 10. GrA-Be

One way streets are represented by the blue arrows. The transition in the one way direction for Grange Ave. and Sullivan occurs at Huron St. The transition in the one way direction for Huron St. occurs at Grange Ave.

Toledo Ohio

Posts: 8
Nickname: toledoohio
Registered: Dec, 2006

Re: December 14 JAVA... bijhan here Posted: Dec 14, 2006 1:44 PM
Reply to this message Reply
1. Since recursion refer to calling the function within itself, one should be catuious when using the two within eahc other, cuz the function could up end up in an endless loop of calling itself over and over again, and then the program would jus stay frozen forever.

Toledo Ohio

Posts: 8
Nickname: toledoohio
Registered: Dec, 2006

Re: December 14 JAVA... bijhan here Posted: Dec 14, 2006 1:58 PM
Reply to this message Reply
4) . Polymorphism which means many forms or shapes, enables the JVM to determine which method to invoke at run time?

Sam Gold

Posts: 6
Nickname: g0dfather
Registered: Dec, 2006

Re: December 14 JAVA... bijhan here Posted: Dec 14, 2006 2:08 PM
Reply to this message Reply
do the programming questions... fuck the small short answers

Toledo Ohio

Posts: 8
Nickname: toledoohio
Registered: Dec, 2006

Re: December 14 JAVA... bijhan here Posted: Dec 14, 2006 2:34 PM
Reply to this message Reply
ok

5. Which of the following statements is correct? [1 mark]


0 A precondition is an assertion about the state of the program before a method is executed.

0 The catch clause need not follow a try block.

(C) When an exception is thrown, the flow of execution is suspended and continues at the appropriate try clause. (CORRECT)

0 You cannot use a throw statement in a lower-level method to indicate that an error condition has been detected.

Toledo Ohio

Posts: 8
Nickname: toledoohio
Registered: Dec, 2006

Re: December 14 JAVA... bijhan here Posted: Dec 14, 2006 2:47 PM
Reply to this message Reply
for the heap sort question........do they want code or pseudo code, like word answer??

here's code:

public class HeapSort {

static int comparisons=0, exchanges=0;

public static <T extends Comparable<T>> void sort(T[] table) {
buildHeap(table);
shrinkHeap(table);
}

private static <T extends Comparable<T>> void buildHeap(T[] table) {
int n = 1;
// Invariant: table[0 . . . n - 1] is a heap.
while (n < table.length) {
n++; // Add a new item to the heap and reheap.
int child = n - 1;
int parent = (child - 1) / 2; // Find parent.
while (parent >= 0 && table[parent].compareTo(table[child]) < 0) {

swap(table, parent, child);
child = parent;
parent = (child - 1) / 2;
}
}
}



private static <T extends Comparable<T>> void shrinkHeap(T[] table) {
int n = table.length;
// Invariant: table[0 . . . n - 1] forms a heap.
// table[n . . . table.length - 1] is sorted.
while (n > 0) {
n--;

swap(table, 0, n);
// table[1 . . . n - 1] form a heap.
// table[n . . . table.length - 1] is sorted.
int parent = 0;
while (true) {
int leftChild = 2 * parent + 1;
if (leftChild >= n) {
break; // No more children.
}
int rightChild = leftChild + 1;
// Find the larger of the two children.
int maxChild = leftChild;
if (rightChild < n // There is a right child.
&& table[leftChild].compareTo(table[rightChild]) < 0) {
maxChild = rightChild;
comparisons++;
}
// If the parent is smaller than the larger child,
if (table[parent].compareTo(table[maxChild]) < 0) {
// Swap the parent and child.


comparisons++;
swap(table, parent, maxChild);
// Continue at the child level.
parent = maxChild;
} else { // Heap property is restored.
break; // Exit the loop.
}
}
}
}

private static <T extends Comparable<T>> void swap(T[] table,
int i, int j) {

exchanges++;
T temp = table;
table = table[j];
table[j] = temp;
}
}

Sam Gold

Posts: 6
Nickname: g0dfather
Registered: Dec, 2006

Re: December 14 JAVA... bijhan here Posted: Dec 14, 2006 2:53 PM
Reply to this message Reply
need q 2, 3, 4, or 6 from part B of the exam

Sam Gold

Posts: 6
Nickname: g0dfather
Registered: Dec, 2006

Re: December 14 JAVA... bijhan here Posted: Dec 14, 2006 3:03 PM
Reply to this message Reply
don’t need 4 anymore

Toledo Ohio

Posts: 8
Nickname: toledoohio
Registered: Dec, 2006

Re: December 14 JAVA... bijhan here Posted: Dec 14, 2006 3:14 PM
Reply to this message Reply
question 4:

pseudo code wuestion

output will be: 0
or negative value
m : -1-
a(3): -1-a(2): -1- a(2): -1- 0 or negative value m: -2-

Toledo Ohio

Posts: 8
Nickname: toledoohio
Registered: Dec, 2006

Re: December 14 JAVA... bijhan here Posted: Dec 14, 2006 3:15 PM
Reply to this message Reply
question 4:

pseudo code wuestion

output will be:
0 or negative value
m : -1-
a(3): -1-a(2): -1- a(2): -1- 0 or negative value m: -2-


{{i m not sure about the spacing though}}

Toledo Ohio

Posts: 8
Nickname: toledoohio
Registered: Dec, 2006

Re: December 14 JAVA... bijhan here Posted: Dec 14, 2006 3:20 PM
Reply to this message Reply
10 is true

Vincent O'Sullivan

Posts: 724
Nickname: vincent
Registered: Nov, 2002

Re: December 14 JAVA... bijhan here Posted: Dec 15, 2006 4:41 AM
Reply to this message Reply
Toledo. Why are you helping this idiot to cheat?

Flat View: This topic has 13 replies on 1 page
Topic: December 14 JAVA... bijhan here Previous Topic   Next Topic Topic: Test

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use