Spring 2018 - CS 111 - Homework 7 p. 1 Deadline 11:59 pm on Friday, March 23, 2018 Purpose CS 111 - Homework 7 To practice with some C++ basics, including following design recipe steps for designing and writing simple C++ functions -- now using NetBeans to test and run your functions from Homework 6 -- and also designing some additional functions so you can practice using a string method and using one of your functions in another function. How to submit Submit your main.cpp file for Homework 7 on https://canvas.humboldt.edu (Remember, it is good to submit early and often!) Important notes You are still expected to follow the Design Recipe for all functions that you design/define. Remember, you will receive significant credit for the signature, purpose, header, and examples/tests portions of your functions. Typically you'll get at least half-credit for a correct signature, purpose, header, and examples/tests, even if your function body is not correct. (and, you'll lose at least half-credit if you omit these or do them poorly, even if your function body is correct). That said -- remember that, in C++: use C++ type names in the signatures of C++ functions write examples/tests as bool expressions, typically using == or <. For example, my_funct(3) == 27 abs(my_dbl(4.7) - 100.43) < 0.001...and note that these example tests are EXPRESSIONS rather than C++ statements -- do NOT end them with a semicolon! Be especially careful to include at least two examples/tests for every function, including at least one specific example/test for each "kind"/category of data, and (when there are boundaries) for boundaries between data. You can lose credit for not doing so. Be sure to indent your return statement by at least 3 spaces inside your function body! And curly braces go on their own line, lined up as shown in the posted class examples! Homework Program Setup Start NetBeans. From the File menu, select New Project.... You should see a screen that lets you select C/C++ as a Category and C/C++ Application as a Project. Do so, and click Next>.
Spring 2018 - CS 111 - Homework 7 p. 2 Type 111hw7 in the Project Name box, and use Browse... to direct the Project Location folder to your desired location. (REMEMBER: in an HSU lab, this needs to be to the U: drive or to a flash drive.) All other options should remain as they are. Then select "Finish". In the left-side window, expand the Source Files section, then double-click on main.cpp this should open an editor window with the contents of the main.cpp file. REPLACE main.cpp's current contents with the "first main.cpp template" from the CS 111 public course web site, under "References". (You can REMOVE, or just not paste in, the very first "FIRST VERSION" comment.) See the comment that has by: and last modified:? START that comment with: CS 111 - HW 7 Then put your name after by:, and today's date after last modified:. For example: /*--- CS 111 - HW 7 by: Your Name last modified: 2018-03-12 ---*/ Problem 1 In the "first main.cpp template" you pasted into your main.cpp, find the comment: /*--- PUT YOUR SIGNATURES, PURPOSES, and FUNCTION DEFINITIONS HERE ---*/ After this comment, type a blank link, and then type the comment: Problem 1 Remember function rect_perim from Homework 6, Problem 1? Type in or paste your signature, purpose, examples/tests, function header, and function body for rect_perim. Recall the function from Homework 2, Problem 2 that returns the perimeter of a rectangle. Use the design recipe to design a C++ version of this function named rect_perim. (rect_perim expects the length and width of a rectangle, and returns the perimeter of that rectangle.) Then, in the main function, edit:
Spring 2018 - CS 111 - Homework 7 p. 3...so that it says you are testing rect_perim, and includes your examples/tests for rect_perim. Make sure rect_perim runs (Run -> Run Project), and its tests pass! You should see: *** Testing rect_perim *** of rect_perim after these tests, so that you see the value those call(s) return. Problem 2 After Problem 1's rect_perim, type a blank link, and then type the comment: Problem 2 Remember function ask_abt_day from Homework 6, Problem 2? Type in or paste your signature, purpose, examples/tests, function header, and function body for ask_abt_day. Recall the fun C++ fact also noted in the Week 7 Lab Exercise: One of the benefits of the C++ string type is that you can use + to append two string instances together -- or even a string and a char*! (At least ONE of the arguments to + has to be a string instance for this to work!) Recall the function from Homework 2, Problem 3 that asks a given person how their day is going. Use the design recipe to design a C++ version of this function named ask_abt_day. (ask_abt_day expects a person's name, and returns a customized question, How's it going,...that person's name...?, asking how their day is going. As in Homework 2, optionally, you may change the exact wording of the question, as long as it includes the person's name and ends with at least one question mark.) Then, in the main function, AFTER the tests for rect_perim, paste in and edit:...so that it says you are testing ask_abt_day, and includes your examples/tests for ask_abt_day. IMPORTANT: be careful with your expected value expression in ask_abt_day's test expressions; you cannot use + between two char* expressions. This is a case where the expected value expression likely needs to be just the hoped-for resulting literal expression, without any operations. ASK ME if you have questions about this or run into problems related to this. Make sure ask_abt_day runs (Run -> Run Project), and its tests pass! You should see: *** Testing ask_abt_day ***
Spring 2018 - CS 111 - Homework 7 p. 4 of ask_abt_day after these tests, so that you see the value those call(s) return. Problem 3 After Problem 2's ask_abt_day, type a blank link, and then type the comment: Problem 3 Remember function total_feet from Homework 6, Problem 3? Type in or paste your signature, purpose, examples/tests, named constant definition (for FEET_PER_YARD), function header, and function body for total_feet. FOR FULL CREDIT, make sure that you USE the named constant FEET_PER_YARD appropriately within total_feet's function body. Recall the function from Homework 2, Problem 5 that computes the total number of feet in a given number of yards and feet. Use the design recipe to design a C++ version of this function named total_feet. (total_feet expects a number of yards and a number of feet, and returns the total number of feet overall in both of these combined.) Then, in the main function, AFTER the tests for ask_abt_day, paste in and edit:...so that it says you are testing total_feet, and includes your examples/tests for total_feet. (remember: at least ONE of these tests should have 0 as the number of yards argument, at least ONE of these tests should have a number of yards argument greater than 1, (and each of these tests should have a different value for the number of feet argument).) Make sure total_feet runs (Run -> Run Project), and its tests pass! You should see: *** Testing total_feet *** of total_feet after these tests, so that you see the value those call(s) return. Problem 4 After Problem 3's total_feet, type a blank link, and then type the comment:
Spring 2018 - CS 111 - Homework 7 p. 5 Problem 4 Remember function order_cost from Homework 6, Problem 4? Type in or paste your signature, purpose, examples/tests, named constant definitions (for COFFEE_PRICE_PER_LB, SHIP_PRICE_PER_LB, and SHIP_COST_FIXED), function header, and function body for order_cost. FOR FULL CREDIT, make sure that you USE the named constants COFFEE_PRICE_PER_LB, SHIP_PRICE_PER_LB, and SHIP_COST_FIXED appropriately within order_cost's function body. Recall the function from Homework 2, Problem 6 that computes the total price for an order of Tasty-Waking coffee. Use the design recipe to design a C++ version of this function named order_cost. (order_cost expects the number of pounds of coffee in an order, and returns the total price of that order, including shipping) Then, in the main function, AFTER the tests for total_feet, paste in and edit:...so that it says you are testing order_cost, and includes your examples/tests for order_cost. Make sure order_cost runs (Run -> Run Project), and its tests pass! You should see: *** Testing order_cost *** of order_cost after these tests, so that you see the value those call(s) return. Problem 5 After Problem 4's order_cost, type a blank link, and then type the comment: Problem 5 Remember function cels_to_fahr from Homework 6, Problem 5? Type in or paste your signature, purpose, examples/tests, function header, and function body for cels_to_fahr. Recall the function from Homework 3, Problem 2 that computes computes the equivalent Fahrenheit temperature for a given Celsius temperature. Use the design recipe to design a C++ version of this function named cels_to_fahr. (cels_to_fahr expects a temperature given in Celsius, and returns the equivalent Fahrenheit temperature)
Spring 2018 - CS 111 - Homework 7 p. 6 Then, in the main function, AFTER the tests for order_cost, paste in and edit:...so that it says you are testing cels_to_fahr, and includes your examples/tests for cels_to_fahr. Make sure cels_to_fahr runs (Run -> Run Project), and its tests pass! You should see: *** Testing cels_to_fahr *** of cels_to_fahr after these tests, so that you see the value those call(s) return. Problem 6 After Problem 5's cels_to_fahr, type a blank link, and then type the comment: Problem 6 Now for a new function! For some more practice using a string class method: Recall, from the Week 8 Lab Exercise, that the C++ string class includes a substring method named substr, that expects a starting position and a length, and returns the string starting at that position and going that many characters in the calling string instance (or until the end of that calling string instance, whichever comes first). And, interestingly, the position of the 1st character in a string is 0! That is, for: const string CHEER = "hip, Hip, HOORAY!";...this is a expression: CHEER.substr(5, 3) == "Hip" Use the design recipe to design a C++ function monogram that expects a first name, middle name, and last name, and returns an appropriate "monogram" for that name (a string combining the initials of that first, middle, and last name). For example, monogram("ava", "Marie", "DuVernay") would return "AMD", and monogram("grace", "Murray", "Hopper") would return "GMH". Remember to include a signature, purpose, function header, examples/tests, and then completed function body for monogram. (HINT: Note that, for this particular function, you should use the string class's substr method, which returns a string, instead of the string class's at method, which returns a char.) Then, in the main function, AFTER the tests for cels_to_fahr, paste in and edit:
Spring 2018 - CS 111 - Homework 7 p. 7...so that it says you are testing monogram, and includes your examples/tests for monogram. Make sure monogram runs (Run -> Run Project), and its tests pass! You should see: *** Testing monogram *** of monogram after these tests, so that you see the value those call(s) return. Problem 7 After Problem 6's monogram, type a blank link, and then type the comment: Problem 7 Recall the Week 8, Lecture 2 example function ring_area, that called circ_area in its function body to help do its desired action. For one more new function -- and a chance to call some of the functions you designed in previous problems -- imagine that you wanted a jaunty little function casual_ask that expected a person's first name, middle name, and last name, and returns a string asking about that person's day, but using their initials instead of their name. That is, this should be a expression: casual_ask("ava", "Marie", "DuVernay") == "How's it going, AMD?" (Consider: how could you use a careful combination of monogram and ask_abt_day to get the desired result? What does monogram return? What does ask_abt_day expect?) Use the design recipe to design this C++ function casual_ask. Remember to include a signature, purpose, function header, examples/tests, and then completed function body for casual_ask. FOR FULL CREDIT, make sure that you use monogram and ask_abt_day appropriately within casual_ask's function body. Then, in the main function, AFTER the tests for monogram, paste in and edit:...so that it says you are testing casual_ask, and includes your examples/tests for casual_ask. Make sure casual_ask runs (Run -> Run Project), and its tests pass! You should see: *** Testing casual_ask *** of casual_ask after these tests, so that you see the value those call(s) return.