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