A3-Strings and Conditions

Table of Contents


Objectives

  • Work with sequences of variables
  • Work with different types of data
  • Start declaring and using string variables
  • Start calling (invoking) string member functions
  • Start using if-statements.
  • Explore pair programming.

Academic Honesty

Read the Scholastic Honesty Policy and Assignment Integrity policies of the syllabus. Here are some clarifications for this particular assignment:

  • You are encouraged to work with one other student of this class following the rules of Pair Programming for Homework Assignments. If you choose to pair program, there is a bonus applied.
  • You may not give a copy of your code to your designated pair-programming partner if you did not develop the code together.
  • You may not show your completed code to another person or look at another person's code until you complete and submit this assignment, except for code you develop together with your pair-programming partner.
  • You may get help from people other than your pair-programming partner if you get stuck, but only if they do not show or tell you the code to type.
  • Remember that the instructor performs similarity tests on programming project submissions, and copied or plagiarized code is usually very easy to detect.

Preparation

  1. Make sure you have completed the exercises from lesson 3.
  2. Complete the Review Exercises in CodeLab 3. These exercises will help prepare you for the problem-solving programs and should be completed first.

Project Specifications

Your solutions to these project must only use techniques we have covered so far.

Programming Style

For all programs, remember to follow all the style rules we covered including:

  1. File block comments.
  2. Placement of curly braces and indentation within curly braces, including if-else-if-else Formatting.
  3. Limiting line length to about 80 characters.

    TextPad, and most other text editors, tell you both the line and column location, which lets you check the line length.

  4. No tab characters in your code.

    You can remove tab characters by either setting up TextPad correctly (see here) or by running a program named astyle (see here).

  5. Spaces before and after operators.
  6. Meaningful variable names and consistent naming style (caps vs. underbars).

Image
Image source: Ed Parrish

Project 1: Word Worksheet

Use this worksheet to improve your understanding of how to work with strings and if-statements, including:

  • String variables (lesson 3.2.3)
  • String input and output (lesson 3.2.3)
  • Concatenation (lesson 3.2.5)
  • String functions (lesson 3.2.6)
  • Comparing strings (lesson 3.3.3)

Refer to lesson 3 for more information on C++ strings and if-statements. Do not use string functions we have not covered in the lecture notes.

For this worksheet, the user enters two words. You write code to display the computations as specified below.

Project Specifications
  1. Start by downloading the worksheet: wordwork.cpp.

    Keep the same filename and add to the existing code to complete the project. Leave the existing code unchanged, except for comments as instructed.

  2. You must name the source code file that you turn in wordwork.cpp and include all your code in this single file.

    Be careful of the spelling, including capitalization, as you will lose points for a misspelled name. Naming is important in programming.

  3. Add your name and the date to the file comment block at the top of the file.
  4. User input is already coded into the worksheet.

    Do not add any other input commands or change the input order.

  5. Complete each of the following string manipulation problems and code your solutions into the worksheet where indicated by the comments. See the Example Run to verify the correctness of each computation.
    1. Measure the length of both words entered by the user and save the length in the related variable. Then use an if-statement to compare the two word lengths and print the word that is the longest.
    2. Concatenate the words and a literal string to produce an output showing the second followed by ", " followed by first. Assign the result of the concatenation operations to variable msg.
    3. Concatenate the first 2 letters of the variable first and the last 3 letters of variable second. Assign the result of the concatenation operations to variable extract.
    4. Remove the first 2 and last 1 character of both words and join them using concatenation. Assign concatenated parts to the variable middles.
    5. Compare the first letter of both words to see which one is the largest (latest in the alphabet). Print the largest letter.
  6. Example Run: The input prompts and outputs of the program must look like the following for full credit, including the same order of input and wording of the output. For the input shown you must get the same output. However, the output must change properly if the inputs are different.
    *** Word Worksheet ***
    Enter two words separated by a space, and press the Enter key
    For example: Hello World!<Enter>: Hello World!
    
    You entered: first = "Hello" and second = "World!"
    Parenthesis below show correct results for example words.
    
    *** Measuring word length ***
    First word length (5): 5
    Second word length (6): 6
    Longest word (World!): World!
    
    *** Concatenating strings ***
    Concatenated (World!, Hello): World!, Hello
    
    *** Extracting substrings ***
    Extracted (Held!): Held!
    
    *** Extracting the middle of strings ***
    Middles (llrld): llrld
    
    *** Comparing first letter of both words ***
    Largest first letter (W): W
    

    In the above example run, the user entered the words shown in italics (for emphasis) to produce the output. Your program does NOT print the characters in italics, nor does the user input appear in italics.

  7. After displaying the output, exit the program.
  8. Submit the source code file wordwork.cpp with the rest of the assignment as described in Deliverables.

Image
Image source: sandara

Project 2: What's Your Dragon Name?

A dragon is a legendary creature, typically scaled or fire-spewing and with serpentine, reptilian or avian traits, that features in the myths of many cultures around world. Dragons are well known in both Chinese and European folk lore as powerful and fearful creatures. [1]

Some people believe they have an inner dragon, which protects them but also needs to be tamed or trained. In this project, we give a name to your inner dragon.

Project Specifications
  1. Write a program that gives the user a "dragon name."
  2. Name the source code file dragon.cpp and include all your code in this single file.

    Be careful of the spelling, including capitalization, as you will lose points for a misspelled name. Naming is important in programming.

  3. Ask the user for the following inputs (and no other input) in this order, as shown in the Example Run below:
    1. first name
    2. last name
    3. mother's first name
    4. father's first name
  4. Use the following algorithm to generate the "dragon name":
    The first two letters of your last name
    + last two letters of your first name
    + a space
    + the first two letters of your mother's name
    + the last letter of your father's name
    
  5. Display the result to the user with the following format:
    Your "dragon name" is: "<dragon name>"
    

    Make sure to include the double-quote marks (") in the output for full credit.

  6. Example Run: The input prompts and outputs of the program must look like the following for full credit, including the same order of input and wording of the output. For the input shown you must get the same output. However, the output must change properly if the inputs are different.
    Want to learn your "dragon name?"
    Please enter your first and last name: Bruce Hartman
    Please enter your mother's first name: Sylvia
    Please enter your father's first name: Ron
    Your "dragon name" is: "Hace Syn"
    

    In the above example run, the user entered the values shown in italics (for emphasis) to produce the output. Your program does NOT print the characters in italics, nor does the user input appear in italics.

  7. After displaying the output, exit the program.
  8. Submit the source code file dragon.cpp with the rest of the assignment as described in Deliverables.
Hints:
  • Use an escape character \" to print double quote marks.
References and More Information
  1. Dragon: from Wikipedia

Image
Image source

Project 3: Car Chatbot

For this programming assignment, we are going to write a basic chatbot program. For fun, try having a conversation with an online chatbot here or here. The chatbot we are going to design wants to know a few things about you, such as:

  1. What is your name?
  2. Where are you from?
  3. What is your favorite number?
  4. What is your dream car?
  5. How much does your dream car cost?
Project Specifications
  1. Write a chatbot program that interacts with the user as described in the Interaction section below.
  2. Name the source code file carbot.cpp and include all your code in this single file.

    Be careful of the spelling, including capitalization, as you will lose points for a misspelled name. Naming is important in programming.

  3. Interaction: Interact with the user with the following inputs in the order given (and no other input) and responses as follows:
    1. Open the conversation with the name of your bot and find out the name of the user (string). Reply with a greeting that displays the user's name, like "It's so nice to meet you <user's name>!".
    2. Find out where the user is from (string). Reply with a positive statement that says the location, like "<location> sounds like a nice place to be from!".
    3. Find out the user's favorite number (integer). Reply with a value that relates the user's favorite number to your favorite number, like "Your favorite number (<user's favorite number>) is 1.5 times as big as my favorite number, which is (7).
    4. Find out the user's dream car (string). Reply with a comment that expresses interest in the car and states the car name, like "Wow, I've always wanted a <car> too!"
    5. Find out how much the dream car costs (double). Then start a conversation about the cost of the car:
      1. A comment that restates the cost, like "Wow, <cost> is spendy."
      2. Find out how much of a down payment the user can afford, like "How much of a down payment can you afford?".
      3. Use an if-statement to test if the down payment is greater than or equal to the cost. If so, then exit the program with a reply like "Awesome! It must be nice to be rich!"
      4. Otherwise, find out how many years the user would want to take out a loan to pay for <car> (integer)
      5. Find out what annual interest rate (percent) the user expects to get for <car> (double)
      6. Reply with the expected monthly payment and total amount of money the user would pay for <car> (car price + interest), like "Your monthly payment for the <car> is <monthly payment>, which is a total of <total car cost>!.
    6. Say goodbye to <user's name>.

    Make sure to restate the input where indicated by the word "reply".

  4. Assume the user enters only valid data.
  5. For calculating monthly payments, use the following formula:

    `mPymt = {r(P)} / {1 - (1 + r)^{-n}}`
    Where:

    • `r` is the monthly interest rate
    • `P` is the cost of the car
    • `n` is the number of monthly payments

    Calculate `r` by converting the interest rate the user enters to a decimal (divide by 100) and then dividing by 12 (12 months in a year).

  6. Numbers may not display with exactly two decimal places, which is fine. Display the output using the default formatting and precision for the numbers - do NOT add any formatting statements to the code.
  7. Example Runs: The input prompts and outputs of the program must look like the following for full credit, including the same order of input and wording of the output. For the input shown you must get the same output. However, the output must change properly if the inputs are different.
    My name is Mombot.
    What is your name? Ed
    It's nice to meet you Ed.
    Where are you from? Santa_Cruz
    Santa_Cruz sounds like a nice place to be from!
    What is your favorite number? 42
    Your number (42) is 6 times as big as my favorite number, which is (7).
    What is your dream car? Tesla_Model_3
    Wow, I've always wanted a Tesla_Model_3 too.
    How much does a Tesla_Model_3 cost? 35000
    Wow, 35000 is spendy.
    How much of a down payment can you afford? 100
    With a down payment of $100 you will need a loan.
    How many months would do you need for the loan? 60
    For a 60 months loan, what is the annual interest rate you can get (%)? 3.11
    If you bought the Tesla_Model_3 with a down payment of 100,
      you would have a monthly payment of $628.815.
    Over 60 months that is a total of $37728.9!
    After you graduate and get a job as a software engineer,
      maybe you will buy your dream car.
    Well, gotta go study now. Goodbye Ed.
    
    My name is Mombot.
    What is your name? Richie$Rich
    It's nice to meet you Richie$Rich.
    Where are you from? Chicago
    Chicago sounds like a nice place to be from!
    What is your favorite number? 1000000
    Your number (1000000) is 142857 times as big as my favorite number, which is (7).
    What is your dream car? Tesla_Roadster
    Wow, I've always wanted a Tesla_Roadster too.
    How much does a Tesla_Roadster cost? 250000
    Wow, 250000 is spendy.
    How much of a down payment can you afford? 250000
    Awesome! It must be nice to be rich!
    Since rich people just got a huge tax break you can buy your dream car anytime!
    Well, gotta go study now. Goodbye Richie$Rich.
    

    In the above example runs, the user entered the values shown in italics (for emphasis) to produce the output. Your program does NOT print the characters in italics, nor does the user input appear in italics.

    Note: Your output does not have to exactly match mine above. It needs to be correct, but you are free to give your chatbot his/her own personality. Make sure the inputs are in the same order and that you reply to the data entered by the user as shown. Otherwise, have fun with it!

  8. After displaying the output, exit the program.
  9. Submit the source code file carbot.cpp with the rest of the assignment as described in Deliverables.
Hints:
  • Verify your loan calculation using a car loan calculator like this one.

Extra Credit

The following are worth extra credit points:

  1. Complete the assignment using pair programming with the same person for all three projects. (2 points)
  2. Add an additional interesting and useful string manipulation like those done in wordwork.cpp making use of both input variables and a string function that we have covered. (2 points: 1 for completing with a string function and 1 for interest or usefulness.)
    1. Submit the source code with the extra string manipulation in a file named xcwordwork.cpp.
    2. Describe the purpose of the string manipulation in your README.txt file.
    3. Include the data input section of the original wordwork.cpp file.
    4. Follow the output format of the previous problems by:
      1. Printing a title for the problem surrounded by asterisks (*) like, "*** Comparing last letter of both words ***".
      2. On the next line print a label for the problem like, "Largest last letter",
      3. followed by the expected value inside parenthesis when entering "Hello World!" like, "(o)",
      4. followed by a colon and the output of the string manipulation like, ": o".

      Example Extra Credit Output
      *** Extra Credit Word Worksheet ***
      Enter two words separated by a space, and press the Enter key
      For example: Hello World!: Hello World!
      
      You entered: first = "Hello" and second = "World!"
      Parenthesis below show correct results for example words.
      
      *** Comparing last letter of both words ***
      Largest last letter (o): o
      

Make certain that your README.txt file describes any extra credit completed.

Tutorial Lab

In preparation for next weeks lessons, complete the following:

  1. Read the assigned reading in the textbook
  2. Complete the Tutorial Exercises in CodeLab 3 before the specified due date.

    Refer to the assigned reading for the next lesson to help you understand the problems. Also, you can use the online lecture notes for more information as the notes become available. You can look at solutions if you miss your first few attempts and are stuck by clicking the "Solution" tab.

Grading Criteria

The instructor will evaluate your assignment using the following criteria. Thus you should check your assignment against these criteria to maximize your score.

Each criteria represents a specific achievement of your assignment and has a scoring guide. The scoring guide explains the possible scores you can receive. Some scoring guides have a list of indicators. These indicators are a sign of meeting, or a symptom of not meeting, the specific criterion. Note that a single indicator may not always be reliable or appropriate in a given context. However, as a group, they show the condition of meeting the criterion.

For information on grading policies, including interpretation of scores, see the syllabus.

Lesson Exercises

  • 2: All lesson exercises attempted and turned in
  • 1: Some lesson exercises completed and turned in
  • 0: No lesson exercises completed or turned in

Projects (x3)

  • 5: Demonstrates mastery of the program
    • Applies concepts from the lessons appropriately
    • Meets all specifications (see above)
    • Runs to completion with no abnormal error conditions
    • Generates correct output given correct input
    • Correct file name
  • 4: Has most of the functionality expected of the program
    • Demonstrates some techniques from the lesson
    • Attempts to meet all but one of the specifications (see above)
    • Implementation seems more complicated than necessary.
    • May have one minor error
  • 3: Has some of the functionality expected of the program
    • Demonstrates some techniques from the lesson
    • Attempts to meet at least 1/2 of the specifications (see above)
    • Implementation seems excessively complicated.
    • May have 2-3 minor errors
  • 2: Serious functional problems but shows some effort and understanding
    • Attempts to meet less than 1/2 of the of the specifications (see above)
    • Has a major error or many minor errors
    • Implementation seems very convoluted
    • Demonstrates few techniques from the lesson
  • 1: Does not compile or wrong file turned in
  • 0: Not turned in or uses techniques not covered

Project's Style

  • 3: Code is well-documented including:
  • 2: Code has a minor documentation error
  • 1: Code has some documentation errors
  • 0: No apparent attempt to follow documentation standards or write documentation comments

CodeLab Exercises

  • Number CodeLab completed correctly / number exercises * 8 and rounded up to the nearest integer.

README.txt File

  • 2: README.txt file submitted following the instructions
  • 1: README.txt file submitted but some information was missing
  • 0: No README.txt file submitted

Total possible: 30, plus extra credit

Deliverables

Students submit some homework as they work on it like CodeLab. However, students must submit other homework in Canvas following the link to A3-Strings and Conditions. Include the following items when submitting to Canvas:

  1. README.txt file prepared by following the instructions for submitting homework.
  2. All the exercise files from Lesson 3 including:
    • nameapp.cpp
    • selection.cpp
    • students.txt
  3. wordwork.cpp
  4. dragon.cpp
  5. carbot.cpp
  6. Optionally xcwordwork.cpp (extra credit)

Note: Make certain your programs compile before you turn them in. When a program does not compile then it does not function either. For all programming projects, you should expect little or no credit if your program does not compile and run. For more information see the Grading Criteria.

You must submit all the files needed to complete your assignment. Your assignment must work as submitted. Remember to test and double check your files before submitting them. If you make a mistake, you can resubmit up to the deadline. If you resubmit, you must include all your assignment files in the last submission as Canvas hides prior submissions.

Last Updated: February 10 2018 @19:56:05