Homework Assignments for CSC 175
Spring 2007
Homework 1 (Due Date 1/19/07):
Homework 2 (Due Date 1/29/07):
1. (25 points) Categorize the following as Java keyword, valid identifier, or invalid identifier:
2. (25 points) Given the following declarations:
int A = 1;
int A1;
double D = 1.0;
double D1;
int B = 1;
int C = 1;
What are the results of the following Java expressions?
a. A1 = 32/10;
b. A1 = 25 % 6 + 3 * 3 – 1;
c. A1 = 25 % (6 + 3) * 3 – 1;
d. A1 %= 7/A + 6;
e. A1 = 5.0 / D;
f. D1 = 6.0 / D;
g. D1 = 44/10;
h. D1 -= 6.3 * 3 + B++;
i. D1 += 6.3 * 3 + (++C);
j. D1 = 10 % 20;
3. (25 points) Evaluate the following:
a. int i = ‘1’;
b. int j = ‘1’ + ‘2’;
c. int k = ‘a’;
d. char c = 90;
e. char c = 10 + 50;
f. int l = ‘1’ + 2;
g. char c = “b”;
h. int m = 1 + ‘2’ + 3;
i. char d = 200;
j. int n = ‘9’ + ‘8’;
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, problem 2.4 and page 64, problem 2.6.
Homework 3 (Due Date 2/14/07):
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 = 2 and y = 1?
b. What is output if x = 3 and y = 2?
c. What is output if x = 0 and y = 3?
d. What is output if x = 2 and y = 2?
e. What is output if x = 2 and y = 5?
if (a == 1)
x += 1;
else if (a == 2)
x += 5;
else if (a == 4)
x += 10;
else if (a == 5)
x += 15;
3. (25 points) Assume x and y are int type. Which are correct Java expressions?
a. x > y > 0
b. x = y && y
c. x /= y
d. x or y
e. x and y
f. (x != 0) || (x = 0)
g. (x & y) | (x == y)
h. (x != y) && (y == x)
i. (y = x) && (x = y)
j. (x || true)
4. (25 points) Assume that x is 1, show the results of the following Boolean expressions:
a. (true) && (3 > 4)
b. !(x > 0) && (x > 0)
c. (x > 0) || (x < 0)
d. (x != 0) || (x == 0)
e. (x >= 0) || (x < 0)
f. (x != 1) == !(x == 1)
g. (x == 1) == true
h. (x < 1) && false || (x > 1)
i. (x >= 1) == false || true
j. (x <= 0) && (x != 5) || false
5. (100 points) Programming Problems: Textbook page 94, problems 3.9 and 3.12.
Homework 4 (Due Date 2/26/07):
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(“*****”);
}
}
}
3. (25 points) Show the output of the following program.
public
class Test {
public static void main (String[] args)
{
for (int i = 1; i < 5; i++) {
int j
= 0;
while
(j < i) {
System.out.println(j
+ " ");
j++;
}
}
}
}
4. (25 points) Show the output of the following program:
public
class Test {
public static void main (String[] args)
{
int i =
1;
do {
int num = 1;
for (int j = 1; j <= i; j++) {
System.out.print(num
+ "G");
num += 2;
}
System.out.println();
i++;
}
while (i <= 5);
}
}
5. (100 points) Programming Problem: Textbook page 124, problem 4.24
Homework 5 (Due Date 3/21/07):
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 HW5P2 {
public static void main (String [] args)
{
int i =
0;
while (i <= 4) {
xMethod(i);
i++;
}
System.out.println("i
is " + i);
}
public static void xMethod (int i) {
do {
if (i % 3 != 0)
System.out.print (i + "
");
i--;
}
while (i >= 1);
System.out.println();
}
}
a. Math.sqrt(25)
b. Math.pow(8 3)
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.9
b. Textbook page 166, problem 5.17
Homework 6 (Due Date 3/30/07):
int x = 100;
int [] numbers = new int [x];
x = 200;
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};
int[] array = new int[5][6];
int[] x = {1, 2};
array[0] = x;
System.out.println(“array[0][1] is “ + array[0][1]);
Homework 7 (Due Date 4/18/07):
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 4/27/07):
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