CS1011
Class Exercises
## Exercise 1: if Write a program that asks the user to enter a three letter word. The program should indicate whether the letters in the word are in alphabetical order, reverse alphabetical order, or neither. Sample interaction should look like this: <pre> Enter three letters: aim The three letters entered are in alphabetical order. Enter three letters: toe The three letters entered are in reverse alphabetical order. Enter three letters: bye The three letters entered are not in order. </pre> ## Exercise 2: switch Suppose you are making a game that needs to control a character on the screen. The following key mappings should be used: <key>h</key> - left, <key>j</key> - down, <key>k</key> - up, <key>l</key> - right. Write a program that accepts a character and prints the appropriate direction. Sample interaction should look like this: <pre> j down k up h left </pre> ## Exercise 3: Rewrite for loop as while loop Rewrite the following `for` loop as a `while` loop: ``` for(int i=1; i<10; i=i+1) { System.out.println(i + "^2 = " + i*i); } ``` ## Exercise 4: Loops Write a program that asks the user to enter a word. The program should indicate whether the letters in the word are in alphabetical order, reverse alphabetical order, or neither. Sample interaction should look like this: <pre> Enter a word: abhors The letters entered are in alphabetical order. Enter a word: soon The letters entered are in reverse alphabetical order. Enter a word: typical The letters entered are not in order. </pre> ## Exercise 5: do while loop Write a program that asks the user to enter something. If the user just hits enter without entering any characters first, the program should ask the user again (repeatedly, until the user enters something). The program should then end. Hint: use `nextLine()`. ## Exercise 6: Loops Ask the user to enter two integer values: height and width. Your program should then generate a box of <key>*</key>s that is height tall and width wide. Sample interaction may look like this: <pre> Please enter the height: 3 Please enter the width: 5 ***** ***** ***** </pre> ## Exercise 7: Loops Ask the user to enter a name and print all of the letters in the name, one per line. Include line numbers for the lines. Sample interaction may look like this: <pre> Enter a name: Chris Letters: 1. C 2. h 3. r 4. i 5. s </pre> ## Exercise 8: Loops Ask the user to enter as many positive integers as the user wants followed by a non-positive integer. The program should then display the sum of all positive values that are divisible by 3. Sample interaction may look like this: <pre> Enter a bunch of positive integers followed by a non-positive integer: 1 2 3 4 5 6 7 8 9 10 11 18 0 Answer: 36 </pre> ## Exercise 9: Loops Suppose you are making a game that needs to control a character on the screen. The following key mappings should be used: <key>h</key> - left, <key>j</key> - down, <key>k</key> - up, <key>l</key> - right. Write a program that accepts a string of characters and generates a sequence of moves. Characters other than the directional characters specified earlier should be ignored. Sample interaction should look like this: <pre> jjlllk down down right right right up </pre> ## Exercise 10: Loops Rewrite the program in the previous exercise so that the output matches the sample below. <pre> jjlllk 2 down 3 right 1 up </pre>

Friday, 21-Sep-2018 08:55:56 EDT