cs101_mw113

Assignment 3

Contents

Due Dates

Part 1: Due Thursday, October 1, 2015 by 11:30 am via Moodle. Bring a copy with you to class.

Parts 2-4: Due Monday, October 5, 2015 by 11:59pm (Section 01) or Tuesday, October 6, 2015 by 11:59pm (Section 02) in your publish/<your username>/dev/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

From String API used by Unity, relevant instance methods:

/** 
 * Reports the zero-based index of the first occurrence of 
 * the specified string in this instance.
 **/
function IndexOf( searchValue : String ) : int
 
/** 
 * Retrieves a substring from this instance. The substring 
 * starts at a specified character position and continues to
 * the end of the string.
 **/
function Substring( startIndex : int ) : String
 
/** 
 * Retrieves a substring from this instance. The substring
 * starts at a specified character position and has a
 * specified length.
 **/
function Substring( startIndex : int, length : int ) : String

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 need to split the problem into smaller, reusable parts and use String functions to extract and decode this information and format it as a bill. Here is an example. Try adding the following values 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 before class via 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.

  • Design functions and pseudocode using the following as your descriptive level:
    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 before class.

If you want, you can use the solutions for this part as your design.

Part 2: Programming the backend (30 points)

Parts 2--4 Due Monday, October 5, 2015 by 11:59pm (section 01) or Tuesday, October 6, 2015 by 11:59pm (section 02) 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>/dev/cs101/Assignment3.
    • Create a new 2D 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 values:
      • Item name: Great Expectations
      • Purchase code: 01002-ZALKJ83DJ2E3
    • Print the bill using the print or Debug.Log function to print to the console.

What's unicode?

All characters -- letters, numbers, and symbols, like '!' -- are stored on a computer as a series of numbers. Encoding schemes provide a unique number for every character and unicode is one method of encoding characters as numbers.

To make this a little more concrete, here are a couple of characters and their corresponding unicode values:

Character Unicode
1 49
2 50
3 51
a 97
b 98
c 99
A 65
B 66
C 67
! 33
@ 64
# 35

Useful tips and functions

The newline character \n will be useful in formatting your bill. For example, the following code:

var greeting : String = "Hello,\n World!";
Debug.Log( greeting );

will print

Hello,
World!

You will find the following functions useful; copy them into your BillFunctions.js file:

/**
 * This function takes in a String parameter and returns the
 * unicode value of the first character.
 **/
function unicodeVal( s : String ) : int 
{
   // access the first character of the string and gets its value
   return s[0];
}
 
/** 
 * 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.

  • Create variables in BillFunctions.js to represent the input and output fields
  • Add code to the BillFunctions.js that will place a listener on the button.
  • 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.
  • Don't forget to use Build Settings to build your scene as Part3.

Here's an example of what it might look like -- try the following values:

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

Part 4: Free shipping (20 points)

In this part, we will see why making many small functions is so helpful. We will create a button that will print the bill, except that the shipping cost will always be free.

  • 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 created for buttonClicked(), but it will instead call the function you just made in the step above.
  • Add a new button with the text "Print bill with free shipping!" to your Canvas
    • Don't forget to declare a new variable for this button in your script and assign it the new button you just created
  • Set this button's listener to pass in the name of the second function added in this part.
  • Don't forget to use Build Settings to build your scene as Part4.

Here's an example of what it might look like -- try the following values:

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

Skin by RIL Partner