i have a computer science 201  assignment to be done by tonight homework + extra credit

profileabutid
cmpt_201_lab_4.htm

CMPT 201 Westminster Lab 4: Approximations Due on September 26, 2014

Objectives. The objectives of this lab are:

  • To trace a program
  • To write a program from a description
  • To recognize that floating-point values are approximations

Step 1. Record rules for tracing a program

With your pair-programming partner, review your answers to the last activity on creating a program memory trace. Write down rules for how to create a program memory trace. Be sure to address:

  • What Java syntax indicates you should create a new row to the Stack.
  • What Java syntax indicates you should update the Stack.
  • What Java syntax indicates you should add code to Heap.
  • What the arrows mean.

Step 2. Trace a program

Download ScannerDemo.java, a program from Section 2.3 in the textbook.

Compile and run the program a few times.

Then on a piece of paper, create a trace of the program, assuming the user enters the following input:

201      76543
9.99     2014 
Go Griffins
What is a Sugar Hoodie?

Be sure you understand what happens in the program. If you're not sure, read page 16 in the blue booklet or pages 97-99 in your textbook.

Show your trace to the instructor or SI before continuing to the next step.

Step 3. Use a method from Scanner to read a floating point number

Start your program in a source file called Approximation.java. In the main method, you should use the Scanner class to read a floating-point value and store it in a variable called number.

Compile and run your program. It will not print anything yet, but it should prompt you for a number.

Step 4. Make some calculations.

Next, we will perform some arithmetic. Compute 1.0 /number and store the result in a new variable reciprocal. Then multiply number and reciprocal, and store them in a variable called product.

Print all three numbers (number, reciprocal, and product).

Compile and run your program. You should run the program several times on different numbers. Does everything look as you expect?

Step 5. Add an if-statement

Now add a multibranch if-statement to your program, to check if product is less than 1, greater than 1, or equal to 1.

A three-way branch statement has the syntax:

if (boolean-expression-1)
     statement-1
else if (boolean-expression-2)
     statement-2
else
     statement-3

Inside each statement, you should print whether the product is less than, greater than, or equal to 1.

Compile and run your program. To test your program, try running the program on numbers near 2e11 and 2e-11.

Step 6. Print the difference

Finally, print the difference between 1 and the product (perform a subtraction). Be sure to print a user-friendly message with the difference.

Extra Credit. Write an explanation

Research online and write a paragraph in your own words, explaining why computers have trouble expressing certain floating-point numbers, such as 0.1. You can draw a diagram if that helps.

If you do the extra credit for a lab, be sure to include a comment in Canvas to let me know it was completed.

Lab Handin

Each member of your group should try to make only one upload to Canvas, although if you forget to upload any portion, or if you find and fix errors in your program after your submission but before the deadline, you may upload your lab again.

Only one member of your group should upload to Conversation.java to Canvas. Don't forget to document your code and use good programming style, which includes:

  • using meaningful variable names
  • using proper indentation
  • including a comment block before the program that describes what program does and includes your name and the assignment

Everyone in the group should rate their partner as a comment to the assignment in Canvas. Your partner should be rated on a scale of 5-100, based on the following questions:

  1. Did your partner read preparatory materials before coming to the scheduled lab? (1-20)
  2. Did your partner do his or her fair share of the work? (1-20)
  3. Did your partner cooperatively follow the pair programming model (rotating roles of driver and navigator)? (1-20)
  4. Did your partner make contributions to the completion of the lab assignment? (1-20)
  5. Did your partner cooperate? (1-20)
If you were completely satified with the process, you should give your partner a score of 100. If you were not satisfied with the process, please provide a breakdown of your partner's rating, and elaborate (in as much detail as you wish) on your experience.