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