Java程序辅导

C C++ Java Python Processing编程在线培训 程序编写 软件开发 视频讲解

客服在线QQ:2653320439 微信:ittutor Email:itutor@qq.com
wx: cjtutor
QQ: 2653320439
COS 170 : Lab 5 - word morph COS 170 : Lab 5 - word morph game Objectives In this lab you will start with a skeleton of a simple game, and fill in the parts of the game using the new java constructs that you are learning: switch, characters, strings, and string operations. Preparation Set up a new eclipse project for each lab (detailed instructions) Copy and paste file WordMorph.java into your project folder. Exercise 1: (1 point) - using a switch statement and String .length() We are going to be building a simple but challenging word game. Run the WordMorph.java program. It doesn't do much yet except print instructions and ask the user to type in a word. You are going to fill out this skeleton program step by step. In the code you will find comments where the answers for each exercise will go. So far the starting code just prints instructions and asks the user to type in a word. In Exercise 1 you will use a switch statement to print different messages based on the length of the word that the user entered. Copy and paste the code below into the matching place in the program and then fill in the blanks with code to make it work. Your message for the default case should include the length of their word, just like all of the other cases.         // Exercise 1: calculate the word's length         // and use a switch statement to print a message about the difficulty         int len = _______________;         switch (len) {         case 1: System.out.println("There aren't enough 1 letter words"); break;         case 2: System.out.println("It is easy to get stuck with 2 letter words"); break;         case 3: System.out.println("3 letter words are a good place to start"); break;         case 4: System.out.println("4 letter words are the most fun"); break;         default: _____________________________________________________________;         } Test it out and make sure all the cases work before continuing. Note: When you run the program, you need to click on the console window before typing your input. Many time while writing this lab, I kept forgetting to do that and typed spurious characters into my program. Don't bother putting each of the little exercises into your solutions document. At the end you will just copy the entire final program. Exercise 2: (1 point) - using String .equals() The starting code now has a loop. (I promise we will learn about loops soon.) Inside the loop the program asks the user to type 2 characters: an old character and a new replacement for that character. These are both scanned into the string "change". One possibility is that they typed "quit". In Exercise 2 you are going to check for this, and if they did type "quit", the code below will print a message and then end the program using System.exit();             // Exercise 2: check if they typed quit             if (_______________) {                 System.out.println("This is a hard game. Thanks for playing.");                 System.exit(1);             } Test it out and make sure typing "quit" works. Exercise 3: (1 point) - check that they entered 2 characters Programs that accept user input often do a lot of input validation, and that is the case with this program. Now check that they entered 2 characters.                    // Exercise 3: check that they entered 2 characters             if (________________) {                 System.out.println("Input should be 2 characters. Exiting.");                 System.exit(1);             } Test it out and make sure it works. Exercise 4: (1 point) - using .charAt() Now grab those 2 characters from the change string and put them into character variables.             // Exercise 4: get the two characters into char variables             char oldChar = ________________;             char newChar = ________________;             System.out.println("replacing "+oldChar + " with " + newChar); That println() statement is just for testing. After you have confirmed that this step works, you can comment out or delete that statement. The way we are writing this program is called incremental development. Testing and making sure that each little piece of code works as you develop your program in small steps. I did not originally develop this program in linear order like you are, but I thought that linear order would be easier for you in doing this lab. Exercise 5: (1 point) - using .indexOf(char) Check that the old character that they are replacing is actually in the word. To check that a string contains a character, you can use .indexOf(char). It returns the index of the first match or -1 if the character is not found. It may not seem like we need to store the result in a variable, but the value will be useful in the next step.             // Exercise 5: check that the word has the old char             int i1 = ____________________;             if (____________) {                 System.out.println("Old character not found. Exiting.");                 System.exit(1);             } Test it out and make sure it is working before continuing. Exercise 6: (1 point) - using .indexOf(char, startingPosition) We don't want to replace 2 characters, so make sure that the character being replaced doesn't occur a second time. You can use version of .indexOf() that takes 2 arguments (the character to find, and the index to start looking at).             // Exercise 6: check that the old character only appeared once             int i2 = ______________________;             if (________) {                 System.out.println("Old character found more than once. Exiting.");                 System.exit(1);             } Test it out and make sure it is working before continuing. Exercise 7: (1 point) - using .replace(oldChar, newChar); Finally we are ready to replace that character! Remember that Strings are immutable. We don't really replace a character in a string, but instead create a new string, and then keep that new string instead of the original.            // Exercise 7: replace the character in the word             word = __________________________________; Lets hope that worked. The next step will be a good test for this step. Exercise 8: (1 point) - printing the next line To finish off the loop body, print the loop index i which will be counting off the words as we go, along with the new word and a colon to prompt for the next change. Use a tab character like in the printing of the initial word to make it all line up neatly.  Your programs output should look like:      word  : dk 1    work  : wf 2    fork  : kt 3    fort  : fp 4    port  : ... How many words can you get? Exercise 9: (1 point) - congratulate a winner At the end of the game, after the loops run 10 times, the loop will exit and continue on with any code after the loop. For exercise nine print a congratulatory message.         // Exercise 9: print a congratulatory message         System.out.println(________________"); Now you need to do ten words to test that out! Exercise 10: (1 point) - play until you win Play the game until you succeed in getting 10 words in a row. Copy your output to your solutions document along with the complete code of your program. YOU ARE DONE! Save your solution document and your program. Show your game to a friend and see if they like it.