Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 3

CSE - Assignment 4 (Hour 7 and Hour 8)

Hour 7

Read the chapter named HOUR 7 about the conditional statements if, else and switch. Study
the Java programming examples in this chapter.

Now answer the following questions:

1. Why is the following line not a Java statement?

if ( account < 0 )

2. What is the difference between the operator " = " and the operator " = = "?

3. What keyboard characters are used to create a block of Java statements?

4. What action is caused by a break statement in a switch statement?

5. Assume that you are writing a Java program that gives the grade A to students who
have 90 - 100 points, grade B to students who have 80-89 points, grade C to students
who have 70-79 points, etc. Is it better to use a switch statement or a series of if-else
statements?

6. Is the ternary operator difficult to understand?

7. Study line 6 in the ClockTalk program in Listing 7.3 (page 105). What is the name of
the object created in this line and what type does it have?

8. Study lines 7 through 11 in the ClockTalk program in Listing 7.3 (page 105). What is
the name of the method used to gives values to the variables minute, hour, day, month,
and year?

Now create and debug the following two Java programs:

Create a Java program named Confucius that reads in a string value from the DOS command
line and then uses that string as a search key in the following three quotes:

1. I hear and I forget. I see and I remember. I do and I understand.


2. Respect yourself and others will respect you.
3. It does not matter how slowly you go so long as you do not stop.

If the string is found in one or more of the quotes, then the quote(s) is printed out. Use the
indexOf method from Assignment 3 in a conditional statement to see if there is a hit on the
keyword.
Here is an example run of the program:

java Confucius and


1. I hear and I forget. I see and I remember. I do and I understand.
2. Respect yourself and others will respect you.

java Confucius you


2. Respect yourself and others will respect you.
3. It does not matter how slowly you go so long as you do not stop.

The second program is a modification of the Confucius program. The program works just like
before but if the search key is not found then the string "Search key not found." is printed.

Example run:

java Confucius2 Douglas


Search key not found.

java Confucius2 u
1. I hear and I forget. I see and I remember. I do and I understand.
2. Respect yourself and others will respect you.
3. It does not matter how slowly you go so long as you do not stop.

Hour 8

Read the chapter named HOUR 8 about loops, also known as iterations. Answer the following
questions.

9. What are the three types of loops in the Java language?

10. Which one of these three types of loops is always repeated at least once?

11. Is it a good idea to avoid exiting a loop early using the break statement?

12. The for-loop has three sections: initialization, conditional and change sections. The
variable that is initialized is referred to as the counter. Is it a good idea to change the
value of the counter inside the loop instead of in the change section?
Compare the following Java program named RepeatWithoutBreak.java with the Repeat.java
program in Listing 8.2 on page 122 of Cadenheads book. Two boolean variables have been
added to determine when the next minute and second have been reached. Note that there is no
longer a break statement or a counter value that never reaches one million in the loop.

import java.util.*;

class RepeatWithoutBreak {
public static void main(String[] arguments) {
String sentence = "Avoid break statements in loops.";
int count = 0;
boolean nextMinuteReached;
boolean nextSecondReached;
Calendar start = Calendar.getInstance();
start.roll(Calendar.MINUTE, true);
int nextMinute = start.get(Calendar.MINUTE);
int nextSecond = start.get(Calendar.SECOND);
do
{
System.out.println(sentence);
count++;
GregorianCalendar now = new GregorianCalendar();
nextMinuteReached = ( now.get(Calendar.MINUTE ) == nextMinute );
nextSecondReached = ( now.get(Calendar.SECOND) == nextSecond );
}
while ( ! (nextMinuteReached & nextSecondReached) );
System.out.println("\nI wrote the sentence " + count
+ " times.");
System.out.println("I have learned my lesson.");
}
}

Add changes to the program RepeatWithoutBreak so that the program terminates after 15
seconds instead of after one minute. The source code is available from Blackboard under
Assignment 4.

You might also like