Friday, December 9, 2011

Challenge 035’s Solution

To complete Challenge 35, the following code must be pasted into BlueJ™:

/**
 * This class creates and deletes “Task” and “TaskManager” objects
 *
 * @author Phoenix Jones
 * @version 1.0
 */
public class TaskRunner
{
    // creates three “Task” objects and one “TaskManager” object
    Task task1 = new Task("Task One", "Low");
    Task task2 = new Task("Task Two", "Medium");
    Task task3 = new Task("Task Three", "High");
    TaskManager taskManager1 = new TaskManager();
    public void main()
    {
        // adds all three tasks to the “TaskManager” object
        taskManager1.addTask(task1);
        taskManager1.addTask(task2);
        taskManager1.addTask(task3);
        // prints all three tasks to the console window
        taskManager1.showTask(0);
        System.out.println("\n");
        taskManager1.showTask(1);
        System.out.println("\n");
        taskManager1.showTask(2);
        // deletes one of the tasks from the “TaskManager” object
        taskManager1.removeTask(0);
    }
}
 
Pasting this code into BlueJ™ will produce the same scenario as in your original blog post.
 
−Phoenix

Thursday, December 8, 2011

Challenge 033’s Solution

To complete Challenge 34, the following code must be pasted into BlueJ™:

import java.util.Scanner;
/**
 * Challenge 033 (400 XPs) : Aurora Scores a New Transit System
 */
public class TicketMachine {
    // instance variables
    private int price;
    private int balance;
    private int total;
    /**
     * Constructor for objects of class “TicketMachine”
     */
    public TicketMachine(int price)
    {
        // initializes instance variables
        this.price = price;
        this.balance = balance;
        this.total = total;
    }
    /**
     * Displays the price of a ticket
     */
    public void getPrice()
    {
        System.out.println("Price: " + price + "¢");
    }
    /**
     * Displays one’s current balance
     */
    public void getBalance()
    {
        System.out.println("Current Balance: " + balance + "¢");
    }
    /**
     * Allows one to insert coins to add to one’s balance
     */
    public void insertMoney()
    {
        Scanner input = new Scanner(System.in);
        while(balance < price)
        {
            System.out.println("Please insert change: ");
            int i = input.nextInt();
            System.out.print("\n");
            if((i == 1)||(i == 5)||(i == 10)||(i == 25)||(i == 50)||(i == 100))
            {
                balance = balance + i;
                System.out.println("Your current balance is: " + balance + "¢.");
            }
            else
            {
                System.out.println("ERROR: Not a real payable amount!");
            }
        }
        System.out.println("You’ve matched the price of the ticket.");
    }
    /**
     * Prints one’s ticket if their balance is high enough
     */
    public int printTicket()
    {
        if(balance >= price)
        {
            System.out.println("  ****************************  ");
            System.out.println("**   Greenman Transit Lines   **");
            System.out.println("**         Price: " + price + "¢         **");
            System.out.println("  ****************************  ");
            total = price - balance;
        }
        else
            System.out.println("Please insert " + (price - balance) + "¢");
        return total;
    }
    /**
     * Refunds one’s change once they’ve printed their ticket
     */
    public void refundBalance()
    {
        if (total <= 0)
        {
            System.out.println("Thank you for using Greenman Transit Lines!\nYour change is " + total + "¢.");
        }
        else
            System.out.println("You have been fully refunded " + balance + "¢.");
    }
}
 
Pasting this code into BlueJ™ will produce the same scenario as in your original blog post.
 
−Phoenix
 
Note 1: I completed this challenge with the aid of Zach J, Itay P, and Brian W.
Note 2: Because of the use of int values for balance, one can insert all types of U.S. coins except for the half−cent.

Monday, December 5, 2011

Challenge 034’s Solution

To complete Challenge 34, the following code must be pasted into BlueJ™’s TaskManager class: 

    /**
     * Checks for invalid index values
     *
     * @param n  integer of index where task resides in ArrayList
     */
    public boolean checkValidIndex(int n)
    {
        if (n >= 0)
            return true;
        else
            return false;
    }
 
Pasting this code into BlueJ™ will produce the same scenario as in your original blog post.
 
−Phoenix

Tuesday, November 15, 2011

Challenge 032’s Solution

In the coding, only the second number of 6144 will return true. This is because that 6144 is the only value that makes both rem and checkDigit equal the same values, being 4. Furthermore, the variable nRemaining is used to simplify the statements for d3, d2, and d1. Because nRemaining initially equals the equation “n ÷ 10”, d3 can be rewritten as “(n / 10) % 10”. nRemaining’s value is again divided by 10; thus, d2 could be rewritten as “((n / 10) / 10) % 10”. Again, nRemaining is redefined by ¹⁄10th of itself; d1 can be rewritten as “(((n / 10) / 10) / 10) % 10”. However, it is easier to write d1 as “nRemaining % 10”, which is why it is used.
 
−Phoenix

Monday, November 14, 2011

Challenge 030’s Solution

The subtle error within this code is with how the while statement is written. The coding presents no change to the variable num. Therefore, if num doesn’t equal SENTINEL, the while statement will occur, and since neither variable will change, the while statement will still be true, and repeat indefinitely. This will create lag, with the program continuing to run without progress. To correct this, one must present a command that will change the value of num at the while statement’s end.
 
−Phoenix

Wednesday, November 9, 2011

Challenge 031’s Solution

In hexadecimal, the color sequence (14,20,255) would be represented as #0E14FF. This is because hexadecimal is a base−16 numeral sequence; thus, there are six additional “digits” beyond 9 in its numeral system, represented by “A”, “B”, “C”, “D”, “E”, and “F”. In this sense, one does not use the tens place until the decimal number 16. When calculated, the decimal number 14 is equivalent to the hexadecimal number E, the decimal number 20 is equivalent to the hexadecimal number 14, and the decimal number 255 is equivalent to the hexadecimal number FF.
 
−Phoenix

Wednesday, November 2, 2011

Challenge 029’s Solution

When the code is placed into BlueJ™, the following comes out in the Terminal window:
 
******
******
******
******
******
 
This occurs because of the for command—a statement similar to the while loop. It starts by setting k as an int value of 1. It then checks if k is ≤ 5, which it is. This leads the for loop to execute its own code. This code consists of a second for loop, which sets i as an int value of 1 and operates as long as i is ≤ 6. Because i is ≤ 6, its code, which is to print “*”, is done. Then, i’s for statement adds 1 to i and repeats as long as i is ≤ 6. This causes the character “*” to be printed six times before i’s for statement ends. The end of k’s code skips to the next line. After this, k’s for statement adds 1 to k and repeats as long as k is ≤ 5. This causes the line “******” to be printed five times before k’s for statement ends.

−Phoenix

Tuesday, November 1, 2011

Challenge 028’s Solution

To complete Challenge 28, the following code must be pasted into BlueJ™:
 
import java.util.Scanner;

public class Challenge028
{
    public static void main(String[] args)
    {
        Scanner scanUserInput = new Scanner(System.in);
        System.out.print("Current Grade (%): ");
        int n = scanUserInput.nextInt();
        if (n >= 70)
            if (n >= 80)
                if (n >= 90)
                    System.out.println("Excellent!");
                else
                    System.out.println("Good!");
            else
                System.out.println("Average.");
        else
            System.out.println("Bad Times!");
    }
}
 
Pasting this code into BlueJ™ will produce the same scenario as in your original blog post.
 
−Phoenix

Monday, October 31, 2011

Challenge 027’s Solution

The following statement is colored accordingly by its constructor method (green), data type (█), variable name (█), and parameter(s) (█):
Location hotSpot = new Location(4, 7);
 
−Phoenix

Challenge 026’s Solution

The error in Karl’s code is his statement “if (n % 2 == 0)”, which really means that the action is only performed if the remainder of “n ÷ 2 = 0”. Therefore, the program will only correctly output n if n is an even, non−zero number. If n is a negative number, the output will always be what Karl wants it to be. The solution to this problem is to simply eliminate the statement in question.
 
−Phoenix

Friday, October 21, 2011

Challenge 025’s Solution

The class Person contains three instance variables: a String variable named “name”, a String variable named “gender”, and an int variable named “yearOfBirth”. The purpose of instance variables is to set values specific to individual objects.  Two additional instance variables that are appropriate for the class Person are written as “private int heightMeters” and “private int weightGrams”.
 
−Phoenix

Thursday, October 20, 2011

Challenge 024’s Solution

The statement-in-question should be replaced with the code “Person person1 = new Person(name, "female", 2000);”. This is because of how the second constructor for the object Person is set up. It states that Person objects should be defined in the following order: a String value of n, a String value of g, and an int value of year. The value n defines person1’s name, (when a name isn’t defined, one should write “name” in place of “n”). The value g defines person1’s gender, (when a gender isn’t defined, one should write “gender” in place of “g”). The value year defines person1’s year of birth, (when a year of birth isn’t defined, one should write “yearOfBirth” in place of “year”). Therefore, Lauren’s mistake was how she tried to define person1. Her person lacked a name, so she should have wrote “name” instead of skipping it altogether. She also placed the variable g after the variable year, when it should have been placed before. That is why Lauren made her mistake.
 
−Phoenix

Wednesday, October 19, 2011

Challenge 023’s Solution

Considering the A.P. GridWorld Case Study, the Bug class typically has 1 constructor. This constructor can be written as “Bug objectname = new Bug(parameters);”, where the constructor creates a new Bug object using the Bug class. This Bug’s name is written in place of the phrase “objectname”, and its various characteristics are defined in place of the phrase “parameters”. However, if one needs to create multiple Bug objects, then there are multiple constructors for the Bug class, (for example: there are 4 Bug class constructors in Challenge 22).
 
−Phoenix

Wednesday, October 12, 2011

Challenge 022’s Solution

To complete Challenge 22, the following code must be pasted into BlueJ™:

import info.gridworld.actor.*;
import info.gridworld.grid.*;
import java.awt.*;


public class Challenge022
{
    public static void main(String[] args)
    {
        ActorWorld GridWorld = new ActorWorld();


        Bug BUGGRN = new Bug(Color.GREEN);
        Bug BUGBLU = new Bug(Color.BLUE);
        Bug BUGYLW = new Bug(Color.YELLOW);
        Bug BUGMAG = new Bug(Color.MAGENTA);


        BUGGRN.setDirection(Location.NORTHWEST);
        BUGBLU.setDirection(Location.NORTHEAST);
        BUGYLW.setDirection(Location.SOUTHWEST);
        BUGMAG.setDirection(Location.SOUTHEAST);


        GridWorld.add(new Location(4, 4), BUGGRN);
        GridWorld.add(new Location(4, 5), BUGBLU);
        GridWorld.add(new Location(5, 4), BUGYLW);
        GridWorld.add(new Location(5, 5), BUGMAG);


        GridWorld.show();
    }
}


Pasting this code into BlueJ™ will produce the same scenario as in your original blog post.
 
−Phoenix

Wednesday, October 5, 2011

Challenge 021’s Solution

After the code completes, variable x equals 30, while variable y equals 90. The code starts out saying that variable x is an int value of 30, and variable y is an int value of 40. Next, it says that the code will proceed if “x ≥ 0”, which is true. Because this is true, the value for y is changed so that “y = x × 3”, which makes y now equal 90. After that, it says that the code will proceed if “y < 50”, which is false. This “if” statement doesn’t have a counterpart “else” statement, so the code segment is completed with x equaling 30 and y equaling 90.
 
−Phoenix

Challenge 020’s Solution

The answer to Challenge 20 is (B) A syntax error will occur. When all preconditions have been set, (being turning n into the int value 0 at the code’s start), BlueJ™ “cannot find symbol - variable x”, as the challenge never specified to mention x otherwise in the code.
 
However, if one does define variable x, the answer is (D) statement2, but not statement1, will be executed. This is because statement 1 can only be executed if both the statements “n ≠ 0” and “x ÷ n > 100” are true; however, n is 0, so statement 1 cannot be true. statement 2 is activated “else”, meaning that if no other “if” conditions can be met, this is performed, making statement 2 the only one to be executed.
 
−Phoenix

Tuesday, October 4, 2011

Challenge 019’s Solution

With this code, the quantity of the variable result is 12. Based on Java™’s Order of Operations, addition (+) and subtraction (−) takes place after multiplication (×), division (÷), and modulos (%). Because of this, the formula first tackles “3 × 6”, which provides the int value 18. Next is “18 ÷ 4”; however, because the two variables are written as int values, the normal answer of 4.5 is rounded down to the nearest integer, being 4. After that is the operation “4 % 3”, meaning “Remainder of 4 ÷ 3”. The function “4 ÷ 3” has a remainder of 1. Finally, since one has gone through all the multiplication, division, and modulos in the problem, the addition and subtraction is addressed, going to the operation “13 − 1”, which is 12.
 
−Phoenix

Monday, October 3, 2011

Challenge 018’s Solution

Potential Fixes 1−4 correctly output “2.6”, while Potential Fix 5 incorrectly outputs “2.0”. This is due to how each Potential Fix is written. Potential Fix 1 writes “(double)” before the number 13, making 13 into the double value 13.0. When a double value (13.0) is divided over an int value (5), the result must be a double value (2.6). Next, in Potential Fix 2, the term “(double)” is written before the number 5, making 5 into the double value 5.0; therefore, the result is the double value 2.6. Third, Potential Fix 3 writes “13.0” in place of “13”. Since 13.0 is, by itself, a double value, the result is again 2.6. After that, Potential Fix 4 does something similar, writing “5.0” in place of “5”. 5.0 is a double value; the result is 2.6. Finally, there is Potential Fix 5, which doesn’t work. While this solution does write “(double)” before the equation, it mistakenly uses extra parentheses, writing “(13 / 5)”. This causes the function 13 ÷ 5 to be performed first, but, since both numbers are written as int values, the result is 2. This number is what then gets converted into the double value 2.0, which is output’d in the end.
 
−Phoenix

Thursday, September 29, 2011

Challenge 014’s Solution

In Challenge 14, the output displays “0.0”. This is because the expression “double n = 99/100” is written with int values for the numbers 99 and 100. Because of this, the result of 99 ÷ 100 must also be an int value, and therefore equals 0. The number 0 is converted to a double value after the function is performed, which is why the statement “0.0” is displayed. To combat this problem, one can do either of two solutions: first, one can simply write the numbers 99 and 100 as double values, replacing the previous expression with “double n = 99.0/100.0”. Because of their new notation, the resulting output will be “0.99”. Second, one can convert each number into a double value within the equation itself, replacing the previous expression with “double n = (double)99/(double)100”. This makes 99 and 100 into the quantities 99.0 and 100.0, respectively, resulting (again) in the output “0.99”.
 
−Phoenix

Monday, September 26, 2011

Challenge 011’s Solution

In Challenge 11, entering the code as−is creates an error message, saying that the method goFigure cannot be referenced from a static context. When the term “static” is removed from the “output” code−segment, then one cannot run the program. To combat this problem, one can add the word “static” in front  of the coding “double goFigure(int n)”.  When this is done, the output is “2.0 12”. At first, the challenge assigns n as an integer equal to 12. The method goFigure then has n perform the function “n % 7”, which means “the remainder of ‘n ÷ 7’ ”. This number is 5, and is then used in the next statement “12 / n”. Because n is assigned an “int” value, the answer is rounded to 2. After that, n is reassigned as a “double” value, making goFigure(n) equal 2.0. In the last bit of code, the value of goFigure(n), or 2.0, is printed on the screen, followed by a space and the original value of n, being 12; thus, when run, the output is “2.0 12”.
 
−Phoenix

Wednesday, September 21, 2011

Challenge 017’s Solution

Statements A, B, C, D, and E all have an output of true. Statement A requires both !A and !(B && !C) to be true. Because A, B, and C are all false, !A is “not false”, meaning it’s true.  Also, De Morgan’s Law states that “!(B && !C)” is the same as “!B || C”, and since !B is true, Statement A is true. Statement B requires either !A, !B, or !C to be true. While unnecessary, all three are true, so Statement B is true. Statement C states “!(A || B || C)”, which is the same as !A, !B, and !C. All of these mean “true”, so Statement C is true. Statement D states “!(A && B && C)”, which also means !A, !B, or !C. This statement is the same as Statement B, which is true, so Statement B is true. Finally, Statement E requires !A or !(B || !C) to be true. !A is true, so Statement E is true. That is why all five statements have an output of true.
 
−Phoenix

Tuesday, September 20, 2011

Challenge 016’s Solution

When the code is placed into BlueJ™, the following comes out in the Terminal window:
 
\* This is not
a comment *\
 
This is because of what the various symbols in the statement mean. When written between quotation marks in a String[] statement, the symbol “\\” outputs a “\” in the printed string, and “\n” causes the comment to continue on the next line, similar to what the “¶” symbol represents in modern English Writing. The rest of the phrase is printed because it too is between quotation marks.
 
−Phoenix

Monday, September 19, 2011

Challenge 015’s Solution

In Challenge 015, Statement I creates an error message. This is because it first makes “x” a double value of 14.7. Next, it tries to say that “int y = x”. This cannot be so, as an “int” value cannot equal a non−integer number. To correct this, one must make “x” an “int” value by placing “(int)” before 14.7, yielding the output “14”. Next, Statement II correctly yields the output “14”. While this statement has “x” as a double value of 14.7, the statement has the term “(int)” in front of “x”, making it an integer value before it claims that it is equal to “int y”. Therefore, no correction is necessary for Statement II. Finally, Statement III also creates an error message. This is because it tries to say that “int x” is equal to a non−integer number of 14.7. This should (again) be corrected by placing “(int)” before 14.7. This allows the statement to produce the number “14.0”, as the number 14 can be a double value. In conclusion, one could state that “int” values can be “double” values, but “double” values cannot be “int” values; similar to how integer numbers can be rational numbers, while rational numbers cannot be integer numbers.
 
−Phoenix

Friday, September 16, 2011

Challenge 008’s Solution

The answer to the solution is −1. The code places a starting value of “5”. It then says that as long as the statement “value ≥ 0” is true, it will subtract 2 from the value. When executed, “5 − 2 = 3”, and “3 ≥ 0”, so this repeats. “3 − 2 = 1” and “1 ≥ 0”, so the code repeats. “1 − 2 = −1”; however, the statement “−1 ≥ 0” isn’t true, so 2 is no longer subtracted from the value. The final bit of code says to show the value, which is why the solution of −1 shows up. The “while” statement says that as long as a condition is true, something will be performed.
 
−Phoenix

Wednesday, September 14, 2011

Challenge 009’s Solution

When x = true and y = false, all three expressions are “true”. Statement I is “true” if both condition 1 and condition 2 can be “true”. Condition 1 can be “true” with “x”, and condition 2 can be “not false” with “!y”. Statement II is also true if  both condition 1 and condition 2 can be “true”. Condition 1 can be “true” with “x”, while condition 2 requires both statements to be “true”; however, “!(x && y)” is the same as “(!x || !y)”, making condition 2 “not false” with “!y”. Finally, Statement III is “true” as long as one of the two conditions is “true”. Condition 1 requires both statements to be “true”, and “(x && !y)” is “true” and “not false”. That is why all three Statements in Challenge 009 are “true”.
 
−Phoenix

Tuesday, September 13, 2011

Challenge 013’s Solution

When in the Northeast corner of the Grid World, the Bug turns at 45° intervals until it faces a direction it can move forward in; or, in this case, South. The Bug then continues to move in that direction until colliding with another wall boundary, in which it then again continues turning at 45° intervals until it can move forward again in a West direction. Because of its starting position, the Bug will continuously travel in a square−shaped pattern.
 
−Phoenix

Friday, September 9, 2011

Challenge 010’s Solution

When the code for Challenge 10 is written in BlueJ™, an answer of 2.0 pops out. Initially, this makes no sense, as the equation appears to be d = √(4 + 5) ∕ 2, which should equal 2.1213… However, the variables for 4 and 5 have “int” before them in the code, forcing the equation to act like a Greatest Integer Function, which rounds the result to the greatest integer that is less than or equal to the nearest integer. Therefore, 2.1213 is rounded down to 2. When the phrase “int” is replaced with “double”, the result is 2.1213, supporting my hypothesis.
 
−Phoenix

Thursday, September 8, 2011

Challenge 012’s Solution

Statements I, II and III all print the number “1234”. I believe that this means that the “+” sign has two different meanings then in JAVA. When there aren’t any quotation marks in the problem, JAVA works like a regular calculator, with 12 × 100 + 34 = 1234. However, when there are quotation marks, the “+” sign merely combines the quoted expression with the added number, ignoring whether or not it makes mathematical sense that “12” + 34 = 1234. Therefore, the use of quotation marks is very important when programming mathematics in JAVA.
 
−Phoenix