Lab 1: Designing Functions

 

This lab introduces Metrowerks CodeWarrior environment that we will use in the labs and that you are advised to do your home works in. You will learn to write simple methods using the design recipe described in class and see some common error reports. At the end of the lab you will fill out a questionnaire and get a blackboard account for this class.

 

Part A: Getting Started

 

1.      Get acquainted with the course web site: http://www.ccs.neu.edu/home/vkp/1101-sp03

2.      Proceed to Lab Notes tab. Download Lab1.exe,extract files and save them in the folder Lab1.

3.      Find Lab1.mcp - open it

4.      Run the project - CTRL/F5.

5.      Play with the GUI by evaluating areaOfDisk several times.

6.      Test areaOfDiskTests.

7.      Save console as .txt - print it: reliable 229. Submit the print out at the end of the lab. 

 

Part B: Exploring Numbers and Arithmetic

 

  1. Use the function computeInt to determine the result of the following expressions, notice the idiosyncrasies:
    1. 27 % 4
    2. 27.5 % 4
    3. 150 / 3 * 10
    4. 150 / (3 * 10)
    5. 150 - (20 - 30) – 10
  2. Use the function computeDouble to determine the result of the following expressions:
    1. -27.6 % -4
    2. 0.0/ 0
    3. 1 / 0
    4. 10 / 3.0 * 3
    5. 10.0 / 3 * 3
    6. 10.0/3/3*3*3
    7. 1E20 /3/3/3*3*3*3.
  3. Find out whether the function areaOfDisk allows the input to be an integer.
  4. Find out what happens if you use int return type in the function areaOfDisk instead of double.

 

Part B: Writing Code

 

  1. Familiarize yourself with the design recipe located at the course web site under the Text tabCourse ReadingDesign Recipe 1.
  2. In Metrowerks, close the console and make sure you are in the Lab1.java.
  3. Type in the following functions:

 

/* compute the perimeter of a rectangle, given the width and the height */

double perimeter(double height, double width){

     return (2 * height + 2 * width);

}

 

/* test the perimeter function */

void perimeterTests(){

     testHeader("Testing the perimeter function");

 

     expected(100.0);

     actual  (perimeter(20.0, 30.0));

}

 

  1. What is the purpose, header and body of the function perimeter? Where are the tests for this function?
  2. Run the code. Make sure the function produces the expected results by testing it in the GUI.
  3. Try a couple of errors and see the messages:
    1. omit ; after return
    2. type : after return
    3. misspell one of the variable names
    4. omit one of the parentheses
  4. Add more tests.

 

Part C: Questionnaire & Using Blackboard

 

  1. Print and fill out the questionnaire.
  2. Turn in the questionnaire with the print out of the console that you printed out earlier (part A-step 7).
  3. Use the instruction sheet to sign up for Blackboard.
  4. Save all your work on a diskette and clean the hard drive.

 

 

Part D: Extras

1.  Use the function computeInt to determine the result of the following expressions. Experiment with the expression to find out what is the largest integer Java can represent.

                           i.16 * 16 * 16 * 16 * 16 * 16 * 16 * 16 * 16 * 16

2.Explore arithmetic - GUI with text input and eval button - like Interactions window

3.Design recipe for simple function

4.Primitive types: int, double, char, boolean

5.Explore relational and logical operators

6.Explore functions with if

7.Explore composition of functions