Download doc midterm soluton

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
COMP-202B, Winter 2009, All Sections
MIDTERM SOLUTION
Short Questions
--------------1.
a) A widening conversion occurs when a value's type goes from a type with low
precision to a type with higher precision; that is, from a type that can
express a small number of values to a type that can express a larger
number
of values. A narrowing conversion occurs when a value's type goes from a
type
with high precision to a type with lower precision; that is, from a type
that
can express a large number of value to a type that can express a smaller
number of values.
b) An variable of type int can only store integers, while a variable of type
double can store real numbers, including integers.
c) A char is a primitive type; values of type type consist of a single
character, and literals are delimited by single quotation marks. String is
a reference (object) type; String objects consist of multiple ordered
characters, and String literals are delimited by double quotation marks.
d) The = operator evaluates the expression on its right, and stores the value
in
the variable on its left; the left side of the = must be a variable, it
cannot be an expression. The == operator evaluates the expression on its
right, then evaluates the expression on its left, and compares the two. It
returns true if both sides are equal, false otherwise. Both sides of a ==
can be an expression.
e) Logical AND (represented by the && operator in Java) evaluates to true if
all
its operands evaluate to true; it evaluates to false as soon as a single
operand evaluates to false. Logical OR (represented by the || in Java)
evaluates to true if at least one of its operands evaluates to true; it
evaluates to false only when all of its operands evaluate to false.
2.
a) - Variable a gets assigned 4
- Variable b gets assigned a % 3
- The value of a is 4
- When 4 is divided by 3, the remainder is 1
- Therefore, b gets assigned 1
- Variable c gets assigned (int)1.5 * a + b
- Casting has precedence over multiplication, so only 1.5 is cast to
int
- 1.5 being cast to int yields 1
- The value of a is 4
- Multiplication has precedence over addition, so only the value of a
is multiplied by 1
- The product of 1 and 4 is 4
- The value of b is 1
- When 4 is added to 1, the sum is 5
- Therefore, b gets assigned 5
- The expression "The answer is: " + (b + c) is evaluated and displayed.
- The + is normally executed from left to right, but parentheses force
b + c is to be evaluated first
- Variable b is of type int, variable c is of type int, so the + means
addition and the result is an int
- The value of b is 1, the value of c is 5
- When 1 is added to 5, the sum is 6
- Literal "The answer is: " is of type String, the result of (b + c)
is
of type int, so the + means String concatenation (the sum of b and c
is "promoted" to String to make "6"), and the result is a String
- When String literal "The answer is: " is concatenated to String "6",
the result is "The answer is: 6"
Therefore, the code fragment displays: "The answer is: 6"
b) - The value of a is divided by 2. Variable a is of type int, 2 is of type
int, therefore, int division takes place.
- The value of a is 7
- When 7 is divided by 2, the quotient is 3 (integer division).
- The value of c is divided by 4.0. Variable c is of type int, 4.0 is of
type
double, therefore the value of c is promoted to have type double, and
floating point division takes place.
- The value of c is 2, promotion to double results in 2.0
- When 2.0 is divided by 4.0, the quotient is 0.5 (double division)
- The value of b is added to 0.5. Variable b is of type int, 0.5 is of
type
double, therefore, the value of b is promoted to have double, double
addition takes place, and the result is of type double.
- The value of b is 5, promotion to double results in 5.0
- When 5.0 is added to 0.5, the sum is 5.5
- 3 (the result of a / 2) is not equal to 3.5, so a / 2 == 3.5 evaluates
to
false
- 5.5 (the result of b + c / 4.0) is equal to 5.5, so b + c / 4.0 == 5.5
evaluates to true
- The value of d is 50, the value of e is 25, 50 > 25 therefore evaluates
to
true
- false OR (true AND true) is equivalent to false OR true, which evaluates
to
true
Therefore, the code fragment displayes: "true"
3. The program will display "5"
Notice that despite the indentation, there are no curly brackets surrounding
the
loop body. Therefore, only the line i = i + 1 is part of the loop body, which
means result gets updated only once, that is, when the loop finishes
executing.
At that time, the loop body has executed n times, so the value of i is 5. The
value of i is then multiplied by the initial value of result, 1, thus
resulting
in a final value of 5 for result.
Long Questions
-------------4.
1:
2:
3:
4:
5:
6:
7:
8:
9:
y == 4
t1 == {0, 0}
t2 == {2, 4}
t1 == {4, 4}
x == 4
t1 == {4, 8}
t2 == {4, 8}
t1 == {4, 8}
t2 == null
5.
1. Line 0 (syntactic): Missing import statement for java.util.Scanner
(Line 1 and line 3 are acceptable locations for this error)
2. Line 17 (syntactic): Variable found was not declared
(Lines 18, 20, and 25 are acceptable locations for this error)
3. Line 18 (syntactic): Variable i was not initialized before its first use
(Line 7 is also an acceptable location for this error)
4. Line 18 (semantic): Loop condition should be (i < s.length() && !found),
otherwise, the loop body will never be executed
5. Line 22 (semantic): Incrementing i should occur only if the character we
are
looking for is not at position i, otherwise the displayed index will be
off-by-one.
Programming Questions
--------------------6.
Part 1:
public class Point {
private int x;
private int y;
public Point(int myX, int myY) {
x = myX;
y = myY;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public double computeDistance(Point p) {
int deltaX;
int deltaY;
deltaX = x - p.x;
deltaY = y - p.y;
return Math.sqrt(deltaX * deltaX + deltaY * deltaY);
}
public boolean equals(Point p) {
boolean result;
if (p == null) {
result = false;
} else {
result = (x == p.x) && (y == p.y);
}
return result;
}
public String toString() {
return "(" + x + ", " + y + ")";
}
}
Part 2:
import java.util.Scanner;
public class ComputeDistance {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
Point p1;
Point p2;
int x;
int y;
System.out.println("Enter the coordinates of the point p1: ");
System.out.print("- x-coordinate: ");
x = keyboard.nextInt();
System.out.print("- y-coordinate: ");
y = keyboard.nextInt();
p1 = new Point(x, y);
System.out.println();
System.out.println("Enter the coordinates of the point p2: ");
System.out.print("- x-coordinate: ");
x = keyboard.nextInt();
System.out.print("- y-coordinate: ");
y = keyboard.nextInt();
p2 = new Point(x, y);
System.out.println();
System.out.println("p1 == " + p1);
System.out.println("p2 == " + p2);
System.out.println("The distance between p1 and p2 is: " +
p1.computeDistance(p2));
}
}
7.
Most strategies for solving this problem involve looping through the String
representing the binary number, extracting each group of 4 bits, and
figuring out which hexadecimal digit the extracted 4-bit group represents.
There are two ways of converting a group of 4-bits to the corresponding
hexadecimal digit:
- The naive way: Have 16 if-statements, one for each possible 4-bit group;
not
recommended on a midterm where time constraints are an issue
- The smart way: Convert the 4-bit group to an int representing its value in
decimal, and then converting the decimal value using 1 if-statement (if the
decimal value is less than 10, then just concatenate it to the String,
otherwise, add 'A' - 10 to it and cast to char before concatenation.
Both approaches are illustrated below:
Naive approach:
import java.util.Scanner;
public class ConvertToHexadecimalNaive {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
String binaryString;
String hexString;
String bitGroup;
String answer;
int numberBits;
int value;
int power;
char hexDigit;
do {
System.out.print("Enter a binary number: ");
binaryString = keyboard.nextLine();
hexString = "";
numberBits = binaryString.length();
for (int i = 0; i < numberBits; i = i + 4) {
bitGroup = binaryString.substring(i, i + 4);
hexDigit = ' ';
if (bitGroup.equals("0000")) {
hexDigit = '0';
}
if (bitGroup.equals("0001")) {
hexDigit = '1';
}
if (bitGroup.equals("0010")) {
hexDigit = '2';
}
if (bitGroup.equals("0011")) {
hexDigit = '3';
}
if (bitGroup.equals("0100")) {
hexDigit = '4';
}
if (bitGroup.equals("0101")) {
hexDigit = '5';
}
if (bitGroup.equals("0110")) {
hexDigit = '6';
}
if (bitGroup.equals("0111")) {
hexDigit = '7';
}
if (bitGroup.equals("1000")) {
hexDigit = '8';
}
if (bitGroup.equals("1001")) {
hexDigit = '9';
}
if (bitGroup.equals("1010")) {
hexDigit = 'A';
}
if (bitGroup.equals("1011")) {
hexDigit = 'B';
}
if (bitGroup.equals("1100")) {
hexDigit = 'C';
}
if (bitGroup.equals("1101")) {
hexDigit = 'D';
}
if (bitGroup.equals("1110")) {
hexDigit = 'E';
}
if (bitGroup.equals("1111")) {
hexDigit = 'F';
}
hexString = hexString + hexDigit;
}
System.out.println("The corresponding value in hexadecimal: " +
hexString);
System.out.println();
System.out.println("Would you like to convert more? ");
System.out.print("Enter \"y\" or \"yes\" to continue: ");
answer = keyboard.nextLine();
System.out.println();
} while (answer.equalsIgnoreCase("yes") || answer.equalsIgnoreCase("y"));
}
}
Smart approach:
import java.util.Scanner;
public class ConvertToHexadecimal {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
String binaryString;
String hexString;
String bitGroup;
String answer;
int numberBits;
int value;
int power;
do {
System.out.print("Enter a binary number: ");
binaryString = keyboard.nextLine();
hexString = "";
numberBits = binaryString.length();
for (int i = 0; i < numberBits; i = i + 4) {
bitGroup = binaryString.substring(i, i + 4);
power = 1;
value = 0;
for (int j = 3; j >= 0; j--) {
if (bitGroup.charAt(j) == '1') {
value = value + power;
}
power = power * 2;
}
if (value < 10) {
hexString = hexString + value;
} else {
hexString = hexString + (char) ('A' + value - 10);
}
}
System.out.println("The corresponding value in hexadecimal: " +
hexString);
System.out.println();
System.out.println("Would you like to convert more? ");
System.out.print("Enter \"y\" or \"yes\" to continue: ");
answer = keyboard.nextLine();
System.out.println();
} while (answer.equalsIgnoreCase("yes") || answer.equalsIgnoreCase("y"));
}
}