cs101_mw113

Assignment 3

Contents

Due Dates

Part 1: Due Thursday, October 1, 2015 by 11:00 am in the Assignments link on Moodle. Bring a copy with you to class.

Parts 2-4: Due Monday, October 5, 2015 by 11:59pm in your publish/<your username>/cs101/Assignment3 folder


Submission checklist

  1. From the Students link on the left, find your username and click on the Assignment3 link
  2. Verify that you have two .html files:
    1. PrintBill.html
    2. PrintBillWithFreeShipping.html
  3. Both of the files above should show your bill generation Flash movies when clicked
  4. If you would like, you can rename one of the .html files to be index.html -- this will make the browser automatically show that file when the directory link (i.e., Assignment3) is clicked

REMEMBER: Do not edit your files after the submission, as I will judge submission time by the last modified time stamp! If you want to work further on any part, create a NEW folder named postsubmission within your cs101/Assignment3 folder and put your additional modifications within.

Resources

String API used by Unity

Goals

In this assignment, you will create a Unity application to create a bill for a purchase. Information about (1) the cost of the item, (2) the shipping zip code, and (3) the cost of shipping and handling are encoded in a purchase code, which is a String. You will learn about splitting a problem into smaller, reusable parts and how to use String functions to extract and decode this information and format it as a bill. Here is an example. Try adding the the following parameters to the Unity app below:

  • Item name: Great Expectations
  • Purchase code: 01002-ZALKJ83DJ2E3

Creating a Bill

Part 1: Planning the backend (25 points)

Part 1 Due Thursday, October 1, 2015 by 11:00 am in the Assignments link on Moodle. Bring a copy with you to class.

In this part, you will write pseudo code and design the functions that will carry out calculating and formatting the bill. We will not be programming until Part 2.

  • Write pseudo code for the following:
    To create a bill, first take an item name and its purchase code. From the purchase code, extract the cost of the item. To do this, find the index of the '-', then convert the next two characters to unicode. The item cost is the first character's unicode minus the second character's unicode. Next, extract the zip code -- the first five characters. Then extract the the shipping cost from the zip code -- the sum of the last two digits of the zip code. Compute the total bill as the item cost plus the shipping cost. Finally, format the bill to include the name of the purchased item, its cost, shipping and handling, to what zip code it is being shipped, and the total cost. Print the bill.
  • Rather than having one function to do all of this, break it up into smaller, more manageable pieces. You should be able to create at least five functions. Use what you learned from Lab 3: cupcake lab and the String reference in the API.
    • For each function in your pseudo code, make a function signature, including:
      • parameter names and types
      • the return type
      • a comment above the function stating what it does, an constraints that exist for the parameters, and what is returned.

Type your refined pseudo code (just the final refinement is fine) and function signatures in a Word document or text/rtf file -- call it Assignment3PseudoCode.doc/txt/rtf -- and upload it in the Assignments link on Moodle CS101 by Thursday morning at 11 am.

Part 2: Programming the backend (30 points)

Parts 2--4 Due Monday, October 5, 2015 by 11:59pm in your publish/<your username>/cs101/Assignment3 folder

You have refined pseudo code and a list of signatures from Part 1. Now you are going to take that pseudo code and declare and define the functions necessary to calculate and format a bill.

  • Create a new directory publish/<your username>/cs101/Assignment3.
    • Create a new Unity project in the directory you just created.
    • Attach a new script component to the Main Camera in your new scene called BillFunctions.
  • Take the comments from your pseudo code and write the corresponding lines of JavaScript code (function declarations and definitions go inside the BillFunctions.js file). Remember: I am expecting to see a comment for each line of code!
    • Add code to test your function using the parameters:
      • Item name: Great Expectations
      • Purchase code: 01002-ZALKJ83DJ2E3
    • Print the bill using the print or Debug.Log function to print to the console.

You will need to copy the intToString and stringToInt functions we used into your BillFunctions.js file:

/** 
 * This function takes a String parameter that consists 
 * only of digits (e.g., "3", "465", ...)
 * and converts it to the int value (e.g., 3, 465, ...). 
 * The int value is returned to the caller.
 **/
function stringToInt( s : String ) : int 
{
    // You should not understand this right now!
    return int.Parse(s);
}
 
/** 
 * This function takes an int parameter (e.g., 3, 465, ...)
 * and returns the String version of it (e.g., "3", "465", ...).
 * The String value is returned to the caller.
 **/
function intToString( i : int ) : String 
{
    // You should not understand this right now!
    return i.ToString();
}

Part 3: The Graphical User Interface (GUI) (25 points)

Using the skills you learned in Lab 2: conditionals, create text fields for the inputs and output and a button in your scene. There should be an input box for the item name and the purchase code. The output box should be large enough to see the entire bill.

  • Attach a new Script to the Main Camera called PrintBill.js, and create variables to represent the input and output fields
  • Add code to the PrintBill.js that will place a listener on the button.
  • Create a variable in PrintBill.js to allow you to reference the functions you've written, using something like var billFunctions : BillFunctions;. In the Unity inspector view, drag the Main Camera into this variable.
  • Add a new function in BillFunctions.js that will be called by the listener above and create a bill using the contents of the input text boxes and sets the contents of the output text box to the bill text.
  • Call this new function using the dot method billFunctions.getBill(...)
  • Publish your work (go to File->Publish)

Part 4: Free shipping (20 points)

In this part, we will see why making many small functions is so helpful. We will create a new Unity application that will be just like PrintBill, except that the shipping cost will always be free.

  • Save your PrintBill as a new Scene in your cs101/Assignment3 directory named PrintBillWithFreeShipping.
  • Add two new functions to your BillFunctions.js file:
    • One will be a new function that takes two parameters, the item name and purchase code, and creates a new bill where the cost of shipping is $0 Hint: this should look a lot like a function you wrote in Part 1.
    • The other will be similar to the function you called in PrintBill, but it will instead call the function you just made in the step above.
  • Modify your action listener call in PrintBillWithFreeShipping.js to reference the second function you made above.
  • Publish your work (go to File->Publish)

Here's an example of what it might look like:


Skin by RIL Partner