Friday, May 11, 2012

Challenge 067’s Solution

In Part I of Challenge 67, option A best describes the method. The program starts by assigning the int variable n as the length of the String list items. The int variable m is created, equaling n − 1. Then, a for statement is used to make m equal to the element number of the greatest value of items. After that, a String variable named temp is used to switch the values of elements m and n − 1. This process is repeated with the value of n decreased by 1 until n = 1, resulting in the list being sorted in alphabetical order. Because this process best describes Sequential Search, option A is correct.

In Part II of Challenge 67, after two iterations of the while loop, the order of names will be: {“Boris”, “Alice”, “Claire”, “Dan”, “Evan”}. This is because of names’s original order: {“Dan”, “Alice”, “Claire”, “Evan”, “Boris”}. The first iteration of the while statement will compare element 4 (“Boris”) with the others and switch it with the largest of the elements before it: element 3 (“Evan”). The second iteration of the while statement will compare element 3 (“Boris”) with the others and again switch it with the largest of the elements before it: element 0 (“Dan”). This will result in the order of names being: {“Boris”, “Alice”, “Claire”, “Dan”, “Evan”}.

−Phoenix

Monday, May 7, 2012

Challenge 066’s Solution

Of the five options, the most efficient would be option B: to use a Selection Sort that terminates after 5 iterations. Option A offers to use a complete Selection Sort. A Selection Sort starts by getting an array list of size n. It searches each element in this list, and places the largest at position n, then repeats for an array list of size n − 1 until the array has a size of 1. If performed, all 2,000 cows would be ordered. Since we only need the 5 heaviest cows, option A isn’t the most efficient. Option B offers to use Selection Sort, but only for five executions. If performed, the top 5 heaviest cows would be ordered, while the remaining cows would not. Since there is no unnecessary sorting, option B is the most efficient. Option C offers to use a complete Insertion Sort. An Insertion Sort starts by getting an array list and an int variable n of size 1. The value of the element n + 1 is saved separately. Then, among the first n elements of the array, the saved value is inserted in numeric order, and values greater than it are pushed one element right. This repeats with n + 1 until n is the size of the array. If performed, all 2,000 cows would be ordered. Since we only need the 5 heaviest cows, option C isn’t the most efficient. Option D offers to use Insertion Sort, but only for five executions. If performed, only the first 5 cows who were weighed would be ordered, while the remaining cows would not. Since the first 5 cows ordered may not necessarily be the 5 heaviest, option D is inefficient. Finally, option E offers to use a complete Mergesort. A Mergesort starts by splitting the array into two equal halves. Each half is sorted separately, then combined into one sorted whole. If performed, all 2,000 cows would be ordered. Since we only need the 5 heaviest cows, option E isn’t the most efficient.
 
−Phoenix

Thursday, May 3, 2012

Challenge 065’s Solution

A binary search of 1,000 elements would be, on average, 50× faster than a sequential search. A sequential search checks each element of an array list until a matching element is found. The process can be represented with the equation S(n) = n ∕ 2, where n represents the number of elements in the array list and S(n) represents the average number of sequential comparisons it will take. Thus, an array of 1,000 elements would take, on average, 500 comparisons to target. A binary search sorts the array list in ascending order, then checks the median value of the list. If the value of the element being searched for is less than the median, then the procedure is repeated with the left half of the array; else, the procedure is repeated with the right half of the array. This process can be represented with the equation B(n) = log₂(n), where n represents the number of elements in the array list and B(n) represents the average number of binary comparisons it will take. Thus, an array of 1,000 elements would take, on average, about 10 comparisons to target. Therefore, a binary search of 1,000 elements would be, on average, 50× faster than a sequential search.
 
−Phoenix

Tuesday, May 1, 2012

Challenge 064’s Solution

An input of “la-la-la!” will produce an output of “ala-a” in the Terminal window. The original variable str has a value of “la-la-la!” with a length of 9. First, it checks if its length is greater than or equal to 3. Next, the substring method gets the length and divides it by 3, which is 3. Thus, all elements of str that are after element 0 (inclusive) up to the third element are run through the method again via recursion. This second str has a value of “la-” and a length of 3. Running this str through the first substring method creates an str with a length less than 3, so its recursion doesn’t matter. Next, the second str is run through a substring that gets all elements after element 1 (inclusive) up to the second element and prints them on the screen. This produces an output of “a”. After this, str has its first 2 elements removed and runs again through the method; however, this str’s length is less than 3, so nothing happens. The original str, with a length of 9, now ascends to the next line of code. This str is run through a substring that gets all elements after element 3 (inclusive) up to the sixth element and prints them on the screen. This produces an output of “la-”. After this, the original str has its first 6 elements removed and runs again through the method. This third str has a value of “la!” and a length of 3. Like the second str, running this str through the first substring method creates an str with a length less than 3, so its recursion doesn’t matter. The third str is run through a substring method that gets all elements after element 1 (inclusive) up to the second element and prints them on the screen. This produces an output of “a”. Finally, str has its first 2 elements removed and runs again through the method; however, this str’s length is less than 3, so nothing happens. In total, this creates an output of “ala-a”.
 
−Phoenix

Thursday, April 26, 2012

Challenge 063’s Solution

None of the three segments will work properly. Each segment has an int[] array a with a length of n. The int value k is equivalent to n. Each statement contains, at one point, the command “a[k] = ”. However, a starts at the 0th element, and thus, doesn’t have a kth element. This will throw an ArrayListOutOfBoundsException; thus, none of the three segments will work properly.
 
−Phoenix

Wednesday, April 25, 2012

Challenge 062’s Solution

Of the three, only option 3 will work properly. Option 1 starts by checking if the first number is divisible by 2. If it is, the number is printed. The writeEven method is then run again. This process results in all of the even numbers in one’s set to be printed, but in ascending order; thus, option 1 doesn’t work. Option 2 again starts by checking if the first number is divisible by 2. However, the writeEven method is only repeated if the number is even, ending if it isn’t. This process will end as soon as an odd number is inputted; thus, option 2 will not work. Finally, option 3 starts immediately by repeating the writeEven method via recursion. With this, the last number is analyzed first. If this number is divisible by 2, it is printed. This process results in all of the even numbers in one’s set to be printed in descending order; thus, option 3 works.
 
−Phoenix

Tuesday, April 17, 2012

Challenge 061’s Solution

Entering 3 into the mystery method will produce an output of 12.  The method starts by assigning the int variable n as 3. It then subtracts n by both 1 and 3 using recursion until values < 0 are produced. This procedure, summarized in the figure below, creates six different negative values. It then returns each value as 2, then adds them all together. This results in the output of 12.
 
           3
       ┌───┴───┐
       2       0
     ┌─┴─┐   ┌─┴─┐
     1  -1  -1  -3
   ┌─┴─┐
   0  -2
 ┌─┴─┐
-1  -3
 
−Phoenix

Challenge 060’s Solution

In Challenge 60, inserting a word such as “Phoenix” into the method will create an output of “xineohP”. This is mainly due to the use of the substring command. The printString method starts by accepting a String value and assigning it to the variable s. It then gets s and performs the substring method with a parameter of 1. This means that it gets s and removes the first character from the (first) word. This altered s is then resent to the printString method via recursion, and the process is repeated until s has zero elements. At that point, the s containing one element is sent through the substring method again, but this time with the parameters 0 and 1. This tells it to print the first character in s, starting at element 0, which, in this case, is the last letter of the original String s. Finally, recursion causes this procedure to repeat until the original s is sent through the last substring method, which, in effect, prints s backwards.
 
−Phoenix

Monday, April 16, 2012

Challenge 059’s Solution

The method recur will always return the greatest int value in the list x. The program first creates an int variable t. It then checks if n, the size of x, is just 1, in which case the greatest value is the zeroth element of x. Otherwise, the method sets t as equal to the second-to-last element of x using recursion. This is repeated because of recursion until there is a t value equal to the zeroth element of x. Element 0 of x, aka t, is compared to element 1 of x. The greater of the two values is returned as the result of the last recur method, which becomes the next t. This continues until the end of the original recur method, where the greatest int value in the list x is returned.
 
−Phoenix

Thursday, April 12, 2012

Challenge 058’s Solution

To complete Challenge 58, the following code must be placed into BlueJ™:
 
public class Challenge058
{
    public void drawStars(int n)
    {
        if (n != 0)
        {
            for (int x = 0; x < n; x++)
                System.out.print("*");
            System.out.print("\n");
            drawStars(n - 1);
        }
        else
            System.out.println("No more stars for you!");
    }
}
 
−Phoenix

Wednesday, April 11, 2012

Challenge 057’s Solution

When the code is placed into BlueJ™, the following comes out in the Terminal window:
.
.
you
with
be
force
the
May
.
This is caused by recursion. The program starts by running the stackWords method and accepting the first word entered, being “May”. The String variable word is assigned the value of “May”. Because word isn’t equal to “.”, a second stackWords method is began before the first reaches its end. This second stackWords method has its own local variable word, equal to “the”. Again, “the” isn’t equal to “.”, and a third stackWords method is run before the second (or first) can end. This continues until the value “.” is assigned to a word variable. At this point, the seventh stackWords method is required to write the value of word in the Terminal window, then end. The program can then advance to the sixth stackWords method and do the same thing; however, the value of word for this stackWords is different from that of the seventh. Thus, the phrase “May the force be with you.” is repeated backwards. Finally, after the first stackWords method ends, the program does as well.
.
−Phoenix

Tuesday, April 3, 2012

Challenge 056’s Solution

To complete Challenge 56, the following codes must be placed into BlueJ™:
 
import java.util.Random;
public class Die
{
    int value;
    public Die()
    {
        int value;
    }
    public int roll()
    {
        Random rand = new Random();
        return value = rand.nextInt(6) + 1;
    }
    public int getCurrentValue()
    {
        return value;
    }
    public int compareTo(int die1, int die2)
    {
        if (die1 > die2)
            return 1;
        if (die1 < die2)
            return -1;
        else
            return 0;
    }
}
 
public class DieTester
{
    public static void main(String[] args)
    {
        Die die1 = new Die();
        Die die2 = new Die();
        for (int x = 1; x <= 100; x++)
        {
            System.out.println("Trial " + x + ":");
            int roll1 = die1.roll();
            System.out.println("Value of die1: " + roll1);
            int roll2 = die2.roll();
            System.out.println("Value of die2: " + roll2);
            int compare = die1.compareTo(roll1, roll2);
            System.out.println("compareTo Result: " + compare);
            if (compare == 1)
                System.out.println("Die 1 is larger than Die 2. \n \n");
            if (compare == -1)
                System.out.println("Die 1 is smaller than Die 2. \n \n");
            if (compare == 0)
                System.out.println("Die 1 is equal to Die 2. \n \n");
        }
    }
}
 
−Phoenix

Friday, March 16, 2012

Challenge 055’s Solution

All three statements will successfully transfer money from one account to the other. Statement I removes $50 from the BankAccount object b and transfers it to the SavingsAccount object timsSavings, leaving b with $600 and s with $1,550. Statement II removes $30 from timsSavings and transfers it to the CheckingAccount object daynasChecking, leaving timsSavings with $1,470 and daynasChecking with $2,030. Finally, statement III removes $55 from daynasChecking and transfers it to b, leaving daynasChecking with $1,970 and b with $705.

−Phoenix