Chapter 4
Using Classes

  1. Use the Scanner class to write a program to read a phrase on a single line (using nextLine) and then print the string after replacing every "o" with "xx".

INPUT: How now brown cow

OUTPUT:

Hxxw nxxw brxxwn cxxw

2. Write a program that reads a 10-digit telephone number, such as 9876543210, and formats the number as (987) 654-3210 for printing. Try reading 9876543210 into an integer. What happens and why? Then use the String type to complete the program.

3. The earliest mention of Hog Latin was in an article published in 1866. It referred to the addition of letters to a word to form a language used by young boys and is similar to Pig Latin today. Use the following simplified Pig Latin rules to translate a single word and to print the result.

If a word starts with a consonant cluster or consonant move the consonant or consonant cluster from the initial location of that particular word to the last position of the name. Add a suffix the suffix “-ay” to the end of the word.

  1. Duck becomes uckd+ay to form the pig Altin word uckday

  2. Move become ovem+ay to form ovemay

  3. Scratch becomes atchscr+ay to form atchsray

To create a Pig Latin word from a word starting with a vowel, add the suffix “-ay” at the end of the original word. For instance,

  1. Oval becomes ovalay

  2. Ultimate become ultimateay

  3. Odd becomes odday

4. The Caesar cipher for secret messages is named after Julius Caesar, who, according to Suetonius, used it with a shift of three (A becoming D when encrypting, and D becoming A when decrypting) to protect messages of military significance.

Encryption is calculated by using modular arithmetic to first transform the letters into numbers, according to the scheme, A → 0, B → 1, ..., Z → 25. Encryption of a letter x by a shift n can be described mathematically as, Encrypt(x)=(x+n) mod 26.

Write a program to read n and a word and then to output the encrypted word.
Input: 4 zoo
Output: dss

5. The java.awt.Rectangle class defines a Rectangle object with its upper-left corner defined by x,y and having an integer width and height. Read two sets of 4 integers to create two rectangles then print out their intersection and union rectangles.
Input:
40 40 60 30 50 50 80 40
Output:

java.awt.Rectangle[x=50,y=50,width=50,height=20]

java.awt.Rectangle[x=40,y=40,width=90,height=50]