CS256: Lab 2


Due: See Moodle for the due date and time

Goals:

1) Gain a clear understanding of simple if statements.

2) More practice with reading and displaying values and the assignment statement.

3) Use the println() method to write values to the JGrasp Run I/O window.

Requirements:

1) Create a directory named lab2 and put the following work in this directory.

 

2) Write a class named TestScore to input a test score from the user then output the message “passing” if the score is above 65.0 or the message “failing” if the score is less than or equal to 65.0.  You do not have to check that the score is between 0 and 100.  Output your message to JGrasp’s Run I/O window by using System.out.println()

 

3) Write a class named IfCheck that inputs two numbers from the user.  If the first number is greater than the second number, output the first number minus the second number, otherwise output the second number minus the first number.  Next, in either case, multiply the two numbers together.  If the product is a positive number, output the message “Product is positive”, otherwise output “Product is not positive”.  Output your messages to JGrasp’s Run I/O window by using System.out.println()

 

4) Write a class named FindRange that prompts the user to enter a double, then prints

the user’s number followed by  “: is in the range “  then print one of the following ranges:

less than 60.0

less than 70.0

less than 80.0

less than 90.0

less than or equal to 100.0

greater than 100.0

The range you choose must be the lowest range into which the user’s input falls, e.g., if the user inputs 60.0, you should print:

60.0: is in the range less than 70.0

Hints: Use a nested if statement.

 

5) Write your name in a comment at the top of each file.  Make a zip file of your entire lab2 directory and submit this file on moodle

2 points Extra Credit (requires a loop)

Write a class named SquareRoot to calculate the square root of a positive number, N input from the user.  A square root can be approximated by making an initial guess of the answer.  This initial guess can be checked by squaring and comparing against N.  Obviously, the initial guess will probably be quite wrong.  A better guess can be calculated from this initial guess using the following formula:  nextguess = 0.5(lastguess+(N/lastguess)).  Use 1.0 as your initial guess.  This formula allows a previous guess at the root (lastguess) to be improved (nextguess).  Repeat this calculation until the difference between two successive guesses is less than 0.005, then print your last guess in the JGrasp Run I/O window.  Test your answers with the calculator application.  (You may not use Math.pow() to implement this problem, though you could use it to check the results your program produces.)

Approach:

1)  Create a folder named lab2 inside the CS256 folder on your F: drive:

 

2)  Create a new class named TestScore

You do not need a loop or a variable for this class, although you may declare and use a variable if you like.

 

3)  Test your class by trying at least 3 numbers and make sure one of your numbers is 65.

 

4)  Create a new class named IfCheck.

Write English comments (pseudo-code) inside your new class stating what you intend to do, e.g,

// read and store first number

// read and store second number

 

5) After completing the pseudo-code, go back and write java code underneath your comment lines.  (The above pseudo-code is not complete.)

 

6)  Test your class by trying at least 4 pair of numbers, so that every branch of your code is executed, and make sure you get the answers you expect.