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
Tuesday, November 15, 2011
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
−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
−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
******
******
******
******
******
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™:
Pasting this code into BlueJ™ will produce the same scenario as in your original blog post.
−Phoenix
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!");
}
}
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
Subscribe to:
Posts (Atom)