CS100 M Spring 2002
Project 4
Due Thursday 3/28 at beginning of lecture

Goals

You will learn to write, compile and run short Java programs. Completing all the tasks in this assignment will help you learn to

Instructions

First skim and then carefully read each question entirely before starting to write algorithms/programs! Carefully read CS100M-->Java-->CodeWarrior to learn how to use CodeWarrior for writing Java programs.

Unless specified otherwise, print and submit all programs and output but do not print or submit file Keyboard.java. See also CS100M-->Homework-->Projects.

1. Calculator

Topics: arithmetic and relational operations, printing

Write a program P4Calculator.java (the class name is P4Calculator) to perform the following operations and print the results. The output should be "labeled". E.g., the output format for part (a) below may be   a: 1

  1. ((4-2)-3)+(1-(2-3))
  2. 2/5. Output a decimal result
  3. The remainder of 9/10
  4. Determine the whole number (integer) portion of the quotient 45/10
  5. Determine the whole number (integer) portion of the quotient 14.3/4.5. Hint: Use a cast.
  6. Evaluate the expression "10 is equal to 99." (The corresponding code is 100==99)
  7. Evaluate the expression "10 is not equal to 99"
  8. 300/(112). Use method Math.pow
  9. Square root of |sin(2)|
  10. e4
  11. Generate a random number in the range of 1 to 10. Use method Math.random
  12. Generate a random integer in the range of 1 to 6. Use method Math.random
Compute the answers to all the above questions in the same program.

2. Triangle

Topics: using "prepackaged" code, user input (bonus topic: output format)

Performing input and output (I/O) tasks using Java is a bit more difficult than with MATLAB. In order to facilitate students' learning of programming techniques using Java, most textbook authors provide groups of classes, packages, that perform I/O tasks.

The Keyboard class is written by Lewis and Loftus to perform the tasks of reading user input. The Keyboard class is part of a package called cs1 that comes with the Lewis and Loftus textbook. Carefully read Section 2.7 to learn how to use Keyboard class. It's quite simple and convenient, so learn it well!

The Keyboard class is included in the CD that comes with the book. It is also posted on CS100M-->Homework-->Projects. Add the file Keyboard.java to your CodeWarrior project. Read the instructions on adding files in CS100M-->Java-->CodeWarrior.

Complete Programming Project 2.10 given in Lewis and Loftus (p.105). Do not worry about printing output to three decimal places. The default format used by System.out.println is sufficient for this assignment. Name your program P4Triangle.java. Demonstrate your program using input values of 2, 3, and 5 for the lengths of the three sides of the triangle.

Bonus: Print the output to three decimal places. Use the DecimalFormat class described in Section 2.8.

3. Which quadrant?

Topic: selection (conditional) statement

Solve Question 2 of CS100M Prelim 1 (Spring 2002) using Java. The question statment is repeated below with slight modifications:

In the Cartesian coordinate system, the two-dimensional (x-y) space is separated into four quadrants (1, 2, 3, and 4) with the origin (0,0) in the center. Write a program P4Quadrants.java to prompt the user for the location of a point (the x- and y-coordinates) and identify the quadrant in which the point lies. If a point lies on a boundary between quadrants, choose a quadrant arbitrarily. If the point lies in quadrant 1 or 4, calculate the distance from the origin (0,0). The program should display the distance from the origin, if calculated, and the quadrant number. Use scalar variables only. Write concise and efficient code. Make your program general by using location and distance variables of type double.

Demonstrate that your program works properly using the following three input cases:

4. Guessing Game

Topic: selection and iteration

Complete Programming Project 3.7 given in Lewis and Loftus (p.170). Use integers in the game. Demonstrate your program with the following example run: Guess 3 times, give up, choose to play another game where you guess two times and then stop. Use scalar variables only.