Lecture Summary for 211 on 6/25/03 -- O'Hare

 

Review Questions

  1. Write a line of Java code that converts the String s to the int n.

  2. Write a line of Java code that converts the String s to the double x.

  3. Write a line of Java code that converts the double x to the int n.

  4. List as many methods as you can from these classes:
    1. String
    2. Applet
    3. Graphics
  5. Write a complete Java main method that inputs two ints in input dialogs and prints their maximum.

  6. Input your distance from the city center as the number of blocks East or West from the center and the number of blocks North or South from the center. Compute the distance "as the crow flies" from the city center. Hint: use the Pythagorian Theorem.

  7. Identify these parts of this for loop: initialization, condition, iteration.
    for(i = 5; i <= 25; i += 2)
        System.out.println(i * i);

  8. What is the major difference between a while loop and a for statement?

  9. What is the major difference between a while loop and an if statement?

  10. True or False: A while loop must execute at least once.

  11. What is a reference variable?

 

 

Practice Problems

What is wrong with these while loops? Some of them might have more than one thing wrong.

// Problem 1
i = 0;
while(i <= 100)
{
    System.out.print(i + " ");
    i *= 2;
}

// Problem 2
i = 1;
while(i <= 100)
{
    System.out.print(i + " ");
    i *= 1;
}

// Problem 3
i = 1;
while(i <= 100);
{
    System.out.print(i);
    i *= 1;
}

// Problem 4
// In this loop, start high and end low.
i = 128;
while i < 1
{
    System.out.print(i + " ")
    i /= 2;
}

 

Sequential Processing

 

Practice Problems using Sequential Processing

Write a Java application that inputs a list of items in input dialogs. The user clicks on Cancel to terminate the list. Here are the answers.

  1. Input a list of ints and output the minimum value.

  2. Input a list of strings and output the average length of the strings.

  3. Input a list of names, gender, and salary. Output the average salary of men and of women.

  4. Input a list of ints. Output the largest even int.

  5. Input a list of names and salaries. Output the names of the persons with the largest and second largest salary.

 

The Currency Project

 

String Processing Problems

For each of the following problems, input a string s. Then

  1. Count and output the number of vowels.
    Hint: convert the string to upper case first so you only need to check 'A', 'E', 'I', 'O', and 'U'.

  2. Count and output the number of upper case letters.
    Hint: A character is upper case if it is greater than or equal to 'A' and less than or equal to 'Z'.

  3. Output true if the string contains at least one digit, false otherwise.
    Hint: A character is a digit if it is greater or equal to '0' and less than or equal to '9'.

  4. Output true if every character in the string is an asterisk, false otherwise.

  5. Output true if the string contains at least one x.

  6. Output true if the string begins and ends with the same character. Consider the possibility that the length of the string is zero.

More Hints:
Here are the answers to the String Processing Problems.

 

More Practice Problems

  1. Input a string and a letter. Output "Yes" if the string begins with the letter and "No" otherwise.

  2. Input a string consisting of digits. Output the sum of the digits.

  3. Input an int. Output "Prime" if the int is a prime number. Output "Composite" if the int is not a prime number.

  4. Rewrite the main method of the VendingMachine project using a nested for loop.

    Here are the answers.

 

Work Problems from Practice Midterm Exams