Homework Assignments for CSC 175
Fall 2006
Homework 1 (Due Date 8/30/06):
1. (25 points) Categorize the following as Java keyword, valid identifier, or invalid identifier:
2. (25 points) Given the following declarations:
int A = 1;
double D = 1.0;
What are the results of the following Java expressions?
a. A = 23/10;
b. A = 13 % 10 + 4 * 4 – 2;
c. A = 13 % (10 + 4) * 4 – 2;
d. A %=5 / A + 5;
e. A = 3.0 / D;
f. D = 3.0 / D;
g. D = 23 / 10;
h. D -= 3.6 * 3 + A++;
i. D += 3.6 * 3 + (++A);
j. D = 5 % 10;
4. (25 points) Show the output of the following program:
public class Test {
public static void main (String[] args) {
char x = ‘A’;
char y = ‘C’’
System.out.println(++y) ;
System.out.println(y++);
System.out.println(y) ;
System.out.println(x
> y);
System.out.println(x – y);
}
}
5. (100 points) Programming Problem: Textbook page 63, problems 2.3 and 2.5.
Homework 3 (Due Date 9/22/06):
1. (25 points) For the following code segment:
if (x > 2) {
if (y > 2) {
int z = x + y;
System.out.println(“z is “ + z);
}
}
else {
System.out.println(“x is “ + x);
}
a. What is output if x = 1 and y = 2?
b. What is output if x = 2 and y = 3?
c. What is output if x = 3 and y = 0?
d. What is output if x = 3 and y = 3?
e. What is output if x = 5 and y = 2?
if (a == 5)
x += 5;
else if (a == 6)
x += 10;
else if (a == 8)
x += 15;
else if (a == 9)
x += 20;
3. (25 points) Given the following declarations:
int i = 1;
int j = 2;
int k = 0;
char c = ‘1’;
bool b;
bool b1 = true;
bool b2 = false;
What are the results of the following Java expressions?
a. int i1 = i + c;;
b. char c1 = c + 2;
c. bool b0 = b1 || b2;
d. bool b3 = (false) && (3 < 4);
e. bool b4 = !(i < 0) || (j < 0);
f. bool b5 = (i != 5) && (j == 5);
g. bool b6 = !(b1 && b2) || (i < 3);
h. bool b7 = b1 || b2 && i == 0 || j != k;
i. bool b8 = b1 && b2 && true && false || true;
j. bool b9 = i == j && j == k || i == k;
5. (100 points) Programming Problems: Textbook page 94, problems 3.3 and 3.8.
Homework 4 (Due Date 10/6/06):
int i = 1;
while(i < 15) {
if ((i++) % 2 != 0) {
System.out.println(i);
}
}
public class Test {
public static void main(String [] args) {
int i = 0;
while (i < 5) {
for(int j = 1; i > 1; j--) {
System.out.print(j + “ “);
}
System.out.println(“*****”);
}
}
}
5. (100 points) Programming Problem: Textbook page 124, problem 4.18
Homework 5 (Due Date 10/25/06):
a. Printing a calendar for a month.
b. Computing a square root.
c. Testing whether a number is even, and returning true if it is.
d. Printing a message a specified number of times.
e. Finding the corresponding upper case letter given a lowercase letter.
public class Test {
public static void main (String [] args) {
int i = 1;
while (i <= 6) {
xMethod(i, 2);
i++;
}
}
public static void xMethod (int i, int num) {
for (int j = 1; j <= i; j++) {
System.out.print(num
+ “ “);
num
+= 2;
}
System.out.println();
}
}
a. Math.sqrt(16)
b. Math.pow(3, 2)
c. Math.max(2, Math.min(3,4))
d. Math.round(math.abs(-2.5)
e. Math.ceil(2.5)
public class Test {
public static void main(String [] args) {
xMethod(5);
}
public static void xMethod (int n) {
if(n > 0) {
System.out.print(n + “ “);
xMethod(n – 1);
}
}
}
a. Textbook page 164, problem 5.8
b. Textbook page 166, problem 5.18
Homework 6 (Due Date 11/3/06):
int x = 30;
int [] numbers = new int [x];
x = 60;
System.out.println(“x is “ + x);
System.out.println(“The size of numbers is “ + numbers.lenght);
a. int i = new int(30);
b. double d[] = new double(30);
c. char[] r = new char(1..30);
d. int[i] = (3, 4, 3, 2);
e. float f[] = {2.3, 4.5, 5.6};
public class Test {
public static void main (String [] args) {
int number = 0;
int[] numbers = new int[1];
m(number, numbers);
System.out.println(“number is “ + number + “ and numbers[0] is “ + numbers[0]);
}
public static void m(int
x, int[] y) {
x = 3;
y[0] = 3;
}
}
Homework 7 (Due Date 11/22/06):
1. (25 points) What is the printout of the following code?
public class Foo {
private boolean x;
public static void main (String [] args) {
Foo foo = new Foo();
System.out.println(foo.x);
}
}
2. (25 points) Show the output of the following program.
public class Test {
public static void main(String [] args) {
Count myCount = new Count();
int times = 0;
for (int i = 0; i < 100; i++) {
increment(myCount, times);
}
System.out.println(“count is “ + myCount.count);
System.out.println(“times is “ + times);
}
public static void increment (Count c, int times) {
c.count++;
times++;
}
}
Class Count {
public int count;
Count(int c) {
count = c;
}
Count() {
count = 1;
}
}
3. (25 points) Show the output for the following program.
public static Test {
public static void main(String [] args) {
T t = new T();
swap
(t);
System.out.println(“e1 = “ + t.e1) + “ and e2 = “ + t.e2);
}
public
static void swap(T t) {
int
temp = t.e1 ;
t.e1
= t.e2 ;
t.e2
= temp ;
}
}
class T {
int
e1 = 1 ;
int
e2 = 2 ;
}
4. (25 points) What is the output of the following program?
public class Foo {
static int i = 0;
static int j = 0;
public static void main(String [] args) {
int i = 2;
int
k = 3;
{
int
j = 3;
System.out.println(“i
+ j is : + (i + j));
}
k
= i + j;
System.out.println(”k
is ” + k);
System.out.println(“j
is “ + j);
}
}
5. (100 points) Programming Problem: Textbook page 257, problem 7.4
Homework 8 (Due Date 12/1/06):
1. (25 points) Suppose that s1, s2, s3, and s4 are four strings, given as follows:
String s1 = “Welcome to Java”;
String s2 = s1;
String s3 = new String (“Welcome to Java”);
String s4 = s3.intern();
What is the result of the following expressions?
a. s1 == s2
b. s2 == s3
c. s1.equals(s2)
d. s2.equals(s3)
e. s1.charAt(3)
f. s1.indexOf(‘j’)
g. s1.lastIndesOf(“o”, 15)
h. s1.length()
i. s1.substring(5, 11)
j. s1.replaceAll(“o”, “T”)
2. (25 points) Suppose that s1 and s2 are given as follows:
StringBuffer s1 = new StringBuffer(“Java”);
StringBuffer s2 = new StringBuffer(“HTML”);
Show the results of the following expressions of s1 after each statement. Assume that the expressions are independent.
a. s1.append(“ is fun”);
b. s1.append(s2);
c. s1.insert(2, “is fun”);
d. s1.insert(1, s2);
e. s1.charAt(2);
f. s1.lenght();
g. s1.deleteCharAt(3);
h. s1.reverse();
i. s1.replace(1, 2, “Computer”);
j. s1.substring(1, 3);
3. (25 points) Given the following program:
1 import java.util.StringTokenizer
2
3 public class TestStringTokenizer {
4 public static void main(String [] args) {
5 String s = “Java is cool.”;
6 StringTokenizer tokenizer = new StringTokenizer(s, “v.”);
7
8 System.out.println(“The total number of tokens is “ + tokenizer.countTokens());
9
10
11 while (tokenizer.hasMoreTokens()) {
12 System.out.println(tokentizer.nextToken());
13 }
14 System.out.println(“Any tokens left? “ + tokenizer.countTokens());
15 }
16 }
a. What is the output?
b. What would be the output if Line 6 is replaced by the following code?
StringTokenizer tokenizer = new StringTokenizer(s, “v.”, true);
4. (25 points) Suppose that s1 and s2 are two stings. Which of the following statements or expressions are incorrect?
a. String s = new String(“new string”);
b. String s3 = s1 + s2;
c. Strings3 = s1 – s2;
d. s1 == s2;
e. s1 >= s2;
f. s1.compareTo(s2);
g. int i = s1.length();
h. char c = s1(0);
i. char c = s1.charAt(s1.lenght());
j. int i = s1.lenght() - s2.length();
5. (100 points) Programming Problem: Textbook page 297, problem 8.3