i have a computer science 201  assignment to be done by tonight homework + extra credit

profileabutid
cmpt_201_lab_3.htm

CMPT 201 Westminster Lab 3: Conversation Due on September 19, 2014

Objectives. The objectives of this lab are:

  • To call methods from the String class and from the Scanner class.
  • To write an if-else statement.
  • To write a simple while-loop (for extra credit).
Today you will writing a new program that will use the Scanner and String classes to interact with the user. Your program should:
  1. Ask the user for his or her name.
  2. Using the user's name, ask the user for a sentence with the word "I hate" in it.
  3. Error check the user's sentence.
  4. Sometimes agree with the user (I also hate) and sometimes disagree (I love).
You should not assume the sentence begins with "I hate".

You can read about the Scanner class here and the String class here. However these two links may present too many methods! Your blue booklet lists the most important methods for this lab.

Here are three examples of the program running. The blue represents the user's input:

First Example

Hi - what's your name? John John, please enter a sentence with the words "I hate" in it. I hate Mondays. I love Mondays.

Second Example

Hi - what's your name? Alice Alice, please enter a sentence with the words "I hate" in it. I hate Monday. I also hate Monday.

Third Example

Hi - what's your name? adam adam, please enter a sentence with the words "I hate" in it. Today I hate Mondays. Today I love Mondays.

Step 1. Write pseudocode for this program

With your pair-programming partner, write a list of the steps you will need to complete this program. It does not have to be detailed yet, but at least come up with a list of the steps your program will need to take. Don't be concerned if you don't know how you will perform each step.

Your steps should resemble the steps given in the textbook for the Vending Machine Case Study:

  1. Read the amount into the variable amount.
  2. originalAmount = amount;
  3. Set the variable quarters equal to the maximum number of quarters in amount.
  4. And so on...
Step 2. Use a method from Scanner to read the user's name

Start your program in a source file called Conversation.java. In the main method, you should ask the user for his or her name, and use the Scanner class for reading input from the user.

Compile and run your program.

Step 3. Ask for a sentence with "I hate" in it.

Next, prompt the user (by name) for a sentence with "I hate" in it. Use a Scanner method to read the sentence. (You've used Scanner in both Homework 1 and Homework 2.) You can assume the user will enter a correct sentence.

Compile and run your program.

Step 4. Design an algorithm to change the word "I hate" to "I love"

You and your partner should agree on an algorithm on how to print out a new sentence, where the first "I hate" in the user's sentence is replaced by "I love", using methods from the String class. When you have agreed on an algorithm, show it to me or the SI.

You should not assume the sentence begins with "I hate", nor should you assume that the word "hate" only appears once in your sentence, although your program should only modify the first "I hate".

Also come up with 2-3 harder sentences to try on your program (test cases), besides the three I have given you at the top of this lab.

Step 5. Change the word "I hate" to "I love"

Now implement your algorithm in your main method. If your program does not work correctly on all your test cases, debug your program.

Extra Credit 1. Improve on your code

Now modify your code so it is NOT case-sensitive. In other words, your program should work regardless of what the user capitalizes. ("I hAtE green beans" should still convert to "I love green beans".)

Compile and test your code.

Extra Credit 2. Sometimes you should agree with your user

Add an if-else-statement to your program. If the user's hate sentence has an odd number of characters, you should disagree with the user. Otherwise, you should agree with the user, by adding the word "also" between the words I and hate.

You should agree on a design before you start modiyfing your code.

Note - you are modifying your code from steps 4-6, not cutting and pasting. At the end of this step, your program should only ask for one sentence.

Extra Credit 3. Add a while-loop to error check

What if the user enters a sentence without the words "I hate"? Try this. You will probably get unexpected results.

Add an if-statement to your program to warn the user if they did not follow instructions. Your program should then ask for another "I hate" sentence. For now, don't worry about if the user enters TWO bad sentences!

Note: you should be using an if-statement without an else here. The else statement is optional in an if-statement. You can use one, or you can leave it out. In step 7 you used an if with an else, and in this step, you are using an if-statement alone.

Compile and test your program. Make sure it still works if the user enters a correct sentence. Then restart your program and make sure it works if the user enters one incorrect sentence (followed by a correct sentence).

After your program works, you can improve on it by switching your while-loop to an if-statement. A while-loop is similar to an if-statement, except it repeats until the condition returns false. It uses the same syntax as an if-statement without an else.

Use a loop to your program to warn the user they did not follow instructions. Change your if-statement to a while-loop (replace "if" with "while"). Your program should now ask for another "I hate" sentence, until a valid sentence has been entered.

Compile and test your program.

If you do the extra credit for a lab, be sure to include a comment in Canvas to let me know it was completed.

Lab Handin

Each member of your group should try to make only one upload to Canvas, although if you forget to upload any portion, or if you find and fix errors in your program after your submission but before the deadline, you may upload your lab again.

Only one member of your group should upload to Conversation.java to Canvas. Don't forget to document your code and use good programming style, which includes:

  • using meaningful variable names
  • using proper indentation
  • including a comment block before the program that describes what program does and includes your name and the assignment

Everyone in the group should rate their partner as a comment to the assignment in Canvas. Your partner should be rated on a scale of 5-100, based on the following questions:

  1. Did your partner read preparatory materials before coming to the scheduled lab? (1-20)
  2. Did your partner do his or her fair share of the work? (1-20)
  3. Did your partner cooperatively follow the pair programming model (rotating roles of driver and navigator)? (1-20)
  4. Did your partner make contributions to the completion of the lab assignment? (1-20)
  5. Did your partner cooperate? (1-20)
If you were completely satified with the process, you should give your partner a score of 100. If you were not satisfied with the process, please provide a breakdown of your partner's rating, and elaborate (in as much detail as you wish) on your experience.