Header

divider
Links
• Home
• Contact Me
• 
Aeries Portal
• Royal High School
• Simi Valley USD

divider

AP Statistics

• Course Calendar
• Chapter Notes
• Practice Tests
• Statistics Links

divider
AP Comp-Sci
 Course Content
• Programs
• Practice Tests
• Comp-Sci Links

divider

CP Statistics
• Course Calendar
• Chapter Notes
• Practice Tests
• Statistics Links


divider

Algebra
• Table of Contents
Geometry
• Table of Contents

Math Power
• Table of Contents

AP Computer Science Unit 4 Programs
Data Structures: Arrays, Strings and ArrayLists

 

WORKING WITH THE STRING CLASS


(1)   Write a program that asks for a single word, then displays that word backwards.

     Sample Output

     input:   stressed
     output:   desserts

(2)  Write a program to accept a word, then display the word as shown in the following example. This requires the use of nested loops.

Input:        Royal                                         

Output:    
R
                                                   
RO                         
ROY                        
ROYA                       
ROYAL

Note: the output requires that the input string be changed to all caps for output.

(3) Write a program that will allow the entry of a name consisting of a first and last name (with a space separating the two). The input is to be made into a single string. The program should then output the results in the form last, first. See the example below:

Input:        Jerry Seinfeld                           

Output:       Seinfeld, Jerry

 

(4) Write a program to put a given word into Pig Latin. There are many forms of this made up language, but here are the rules we will use:

  • If the word begins with a vowel, put way on the end of it. Example: apple becomes appleway.
  • If the word begins with a consonant, move all leading consonants to the end and put ay after it. Examples: night becomes ightnay and scram becomes amscray.
  • A special case: Words with qu at the beginning require that the u goes to the end along with the q. Examples: quit becomes itquay, square becomes aresquay.

Use the above examples to test your program. All of them need to work. Assume that the input will be all lowercase letters and that vowels will be only a, e, i, o and u (no y).

(5) The Coder
Convert a sentence into a code using a “letter shift” method.

For example, if the sentence is “Hey there, buddy!” and the shift is 7, the sentence will be changed by converting every letter to the letter 7 after (with wraparound). Note: the shift value should be an integer between 1 and 25 (error-check the input).

So the program requires the user to enter a sentence and a shift value, then outputs the coded message.

Test Data:
“Hey there, buddy!” with a code of 7 is encoded as “Olf aolyl, ibkkf!”

 

(6) The Decoder
Decode a coded sentence into plain English using the reverse of the “letter shift” method.

Test Data:
“Olf aolyl, ibkkf!” with a shift of 7 decodes as “Hey there, buddy!”

Here are some others:

Coded

Shift

Decoded

Lw’v Iulgdb!

3

Wait for it…

Pa kag yuee Wmdqx?

12

Wait for it…

 

(7) The String Math Calculator
Accept a simple arithmetic expression into a string (involving 2 1-digit integers). The program analyzes the string, performs the operation, and then displays the result (see below). As long as it works for these examples, it is considered a success. There are many other more complicated possibilities, but this is all we will consider for now.

Test Data

Input (note, no spaces)

Output

4+2

4+2=6

4-2

4-2=2

4*2

4*2=8

4/2

4/2=2

 

(8) UPC Check Digit Scanner


upc_sample

An example of a Universal Product Code (UPC)

The last digit of the UPC code is called a check digit. This digit lets the scanner determine if it scanned the number correctly or not. Here is how the check digit is calculated for the other 11 digits, using the code 811138000202 from the example shown above:

811138000202 (odd position digits in BLACK, even in RED and check digit in BLUE)

1. Add together the value of all of the digits in odd positions (digits 1, 3, 5, 7, 9 and 11).
8 + 1 + 3 + 0 + 0 + 0 = 12

2. Multiply that number by 3.
12 * 3 = 36

3. Add together the value of all of the digits in even positions (digits 2, 4, 6, 8 and 10).
1 + 1 + 8 + 0 + 2 = 12

4. Add this sum to the value in step 2.
36 + 12 = 48

5. Take the number in Step 4. To create the check digit, determine the number that, when added to the number in step 4, is a multiple of 10.

48 + 2 = 50   
The check digit is therefore 2.

Each time the scanner scans an item, it performs this calculation. If the check digit it calculates is different from the check digit it reads, the scanner knows that something went wrong and the item needs to be rescanned.

Write a program that read UPC’s from the file upcfile.txt in the txtfiles folder. It should read, check for correctness and display all UPC’s that fail the check digit test described above.

These are the ones that should fail (and thus be output to the screen).

993728472798
516069254923
339977717632
418114501927
951733717280

 

(9) Write a program to analyze the words in a sentence. This program should accept a sentence into a string, and then determine the number of words in that sentence, the average word length and the length of the longest word. Hint: a word is detected when a space or punctuation mark is encountered. For example, if the sentence is “The quick silver fox jumped over the lazy brown dog.”, the output of the program will be:

Number of Words: 10
Average Word Length: 4.2
Longest Word: 6

 

(10) Write a program that will accept both a sentence and a single letter into separate variables.  The program should then display all words in the sentence that contain the given letter. See the following examples:

Input:

Sentence: Where are they now?

Letter: e

Output:

Where
are
they

Input:

Sentence: I went to the store.

Letter: o

Output:

to
store

If there is no occurrence of the letter in the sentence, display a message stating so.

 

(11) Write a program that will accept 3 words into 3 separate strings, and then display the words in alphabetical order, one line at a time. Use the compareTo method See the example below.

Input words: Royal, Simi, Santa

Output:

ROYAL
SANTA
SIMI

Note that all words must be capitalized to do this.

 


Home  •  About Me  •  Aeries Portal  •  Contact Me
© DanShuster.com