Review

advertisement
Midterm Review
CSCI 1227: Programming and Problem Solving in Java
Dr. Tami Meredith, Spring 2014
There is nothing to submit today or that you must show the lab instructor. This exercise is intended to
prepare you for the midterm exam and to provide you with some questions to practice your
programming skills. If you can solve these questions you should pass the midterm without any problems.
A. Vowelicious
Write a program that prompts a user to enter a sentence and then displays, as an integer, the number of
vowels (i.e., a, e, i, o, u) in that sentence. Here is a sample output:
Enter a sentence:
I love computer programming! :-)
Your sentence had 9 vowels.
B. De-Vowellated
Now, modify your program from Part A to print out the sentence with all the vowels removed. Here is a
sample output:
Enter a sentence:
I think computer programming is better than sex!
Without vowels your sentence is:
thnk cmptr prgrmmng s bttr thn sx!
C. Fun with Factors
Write a program that asks the user to enter an integer and then prints all the factors of that integer.
Here is a sample output:
Enter a positive integer: 30
The factors of 30 are: 1, 2, 3, 5, 6, 10, 15, 30
D. Seeing it sdrawkcaB
Write a program that asks a user to enter a sentence and then print out the sentence with each word
reversed. Here is a sample output:
Enter a sentence:
Nothing is more fun than writing programs in Java.
gnihtoN si erom nuf naht gnitirw smargorp ni .avaJ
Notes:
Here is some source code that reads a sentence from a user and then prints it out:
import java.util.Scanner;
public class echo
{
public static void main (String[] args)
{
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter a sentence:");
String sentence = keyboard.nextLine();
System.out.println("Your sentence is:");
System.out.println(sentence);
} // end main()
} // end class echo
1. In the above code, you could split sentence into an array of words using the following code:
String[] words = sentence.split("[ ]+");
2. The modulus operator, %, generates the remainder of a division. The remainder is 0 if something is
evenly divisible (e.g., 12 % 4 is 0, 12 % 5 is 2 so 12 is not evenly divisible by 5).
3. On the exam, you might not get any source code given to you. Thus, your "cheat sheet" should
probably have the basic program template, as well as the code needed to obtain Strings or Integers from
the user.
4. Experiment and try stuff! Explore and program as much as you can.
5. THINK, THINK, THINK! You will do 10 times better on the exam (and any programming task) if you
spend a few minutes and think about each problem, before starting to code anything! You will have
plenty of time in the exam. All programs will easily be solvable in about a dozen lines of code and you
won't be asked to write more than 2 full programs and a few methods at most!
Download