Question: Java Programming: 1) Assume an array-based list implemented by a class that uses the fields String [] list, Int nElements; to represent the array of
Java Programming:
1) Assume an array-based list implemented by a class that uses the fields String [] list, Int nElements; to represent the array of elements, and the number of elements currently stored. Show the code for a constructor that creates a list with a default capacity of 100.
2) Assuming a Nodeclass
Class node {
Int element;
Node left, right;
Node(int el, Node left, Node right)
{
Element = el;
This.left = left
This.right = right
}
Write a method int depth(Node tree) that returns the length of the longest path that begins at the node tree and ends at any leaf of the binary tree.
3) A ternary tree is like a binary tree, except each node may have up to three successors. Write a TNode class that can be used to represent a ternary tree. Define a notion of preorder and postorder traversal for ternary trees, and write methods void postorder(TNode tree) and void preorder(TNode tree) that implements your notion of those traversals.
4) In a program you need to store the populations of 12 Countries.
a. Define two arrays that may be used in parallel to store the names and populations
b. Write the code needed to have the user input this information
c. Write a loop to display these two arrays in a meaningful way.
6) Explain what is meant by auto boxing and unboxing. When does this occur?
7) Given the following code for selection sort, give the values for the array for each pass of the algorithm for the array { 5, 15, 7, 4, 8, 18} .
public static void selectionSort(int[] array) { int startScan, index, minIndex, minValue;
for (startScan = 0; startScan < (array.length-1); startScan++)
{
minIndex = startScan;
minValue = array[startScan];
for(index = startScan + 1; index < array.length; index++) {
if (array[index] < minValue) {
minValue = array[index]; minIndex = index;
}
}
array[minIndex] = array[startScan];
array[startScan] = minValue;
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
