CS 201, Lab Assignment 8

Total Page:16

File Type:pdf, Size:1020Kb

CS 201, Lab Assignment 8

1

CS 201, Lab Assignment 8

3 problems, 5 points apiece, 15 points total. All of the problems require that you write a class containing a static method and a program to test it. Problems 1 and 2 build on each other, so you only have to hand in the finished product of problem 2 to cover both 1 and 2. There are no other kinds of questions on this assignment, so altogether what you should be handing in is printouts of a test program and class for each of problems 2 and 3. You should also be handing in a printout of one of the pages of Web documentation that you created as part of your solution to problem 2. As usual, solutions that you can use as a check on your work are provided on the course Web page.

1. This problem is simply programming exercise P7.5 from the end of chapter 7 in the book. This problem makes use of the Point2D.Double class in Java, which has come up in previous assignments . If you need to refresh your memory on it you can hunt around in the appendices of the book or you can go directly to the java API documentation online in order to find out the information you need.

Note that even though the assignment uses geometric points, you’re not writing a graphical applet like you did for chapter 4. You need to write a class and a static method that will return a numeric value. You have to import the Point2D.Double class. Remember that the import statement only has to show the containing class. It will look like this: import java.awt.geom.Point2D;

However, when you declare objects of this class in your program you have to use the full class name, namely: Point2D.Double

If you look at the documentation, you'll find methods like getX() and getY() which apply to any kind of point. You will need to use these methods to “take apart” the points and get their coordinates in the method in order to solve the problem. You also need to recall the distance formula and be able to use the square root method in the Math class. The formula for the distance d between arbitrary points (x1, y1) and (x2, y2) in the Cartesian plane is: d = sqrt((x1 – x2) * (x1 – x2) + (y1 – y2 ) * (y1 – y2))

To complete this part of the assignment you need to: Create a class to contain the static method; write the code for the static method which takes in 2 points as the explicit parameters and returns the distance between them; write a program that tests the method by using it; and run the program to get output. The program should prompt the user for the x and y coordinates of the first point, construct that point; prompt the user for the x and y coordinates of the second point, construct that point; and then call the static method with the two points as parameters, printing out the return value of the call, which 2 should be the distance between the points. To do a nice test you can choose values for the coordinates so that the points would be the non-right angle corners of a 3, 4, 5 right triangle. For example, if the points were (0, 0) and (3, 4), the distance between them should turn out to be exactly 5.

2. Use the javadoc utility to create the web page style documentation of the class and static method you created in Part 1. The javadoc utility is explained in the book. If you look at the class RecursionAndComments.java that comes before this assignment on the course Web page, you will find that the use of comments to create documentation is illustrated in the class, and that the comments contain a summary of the steps to follow in order to create the documentation. There is also a link to the documentation that results when the javadoc utility is run on the class.

3. For this problem you need to write a test program and class where the class contains a method that recursively implements the divide and average algorithm for finding the square root of a number. Complete, step-by-step specifications are given below.

The program should: a. Import the class MySquareRootClass containing the static method which you will write. b. Import the ConsoleReader class. c. This class should be named TestSquareRoot. d. You will need a main() method. e. You will need to construct a console object. f. You will need 3 double variables, mynumber, firstguess, and answer. g. You need to prompt the user to enter the number they’d like to find the square root of. h. You need to take in a value for mynumber. i. You need to prompt the user to enter their first guess for the square root. In order to work correctly, the guess will have to be between 0 and the number you’re trying to find the square root of. j. You need to take in a value for firstguess. k. You need to call the method mySquareRoot(), passing in mynumber and firstguess as parameters, and capturing the return value in answer. l. You need to print out the answer. m. It would also be nice to print out the square of the answer to see how close it was to the truth.

Continued on the next page. 3

The class should: a. Import the java.lang.Math class. b. This class should be named MySquareRootClass. c. In the class there will be a single method named mySquareRoot(), which is public, static, and returns a double. d. The method should take 2 parameters, a double called tofindrootof, and a double called possibleroot. e. Inside the method 2 more double variables should be declared, quotient and average. f. You need to find the quotient by dividing tofindrootof by possibleroot. g. If the absolute value of the difference between possibleroot and quotient is less than .1, you should return possibleroot. h. Otherwise you should calculate average as the sum of possibleroot and quotient divided by 2. i. Then you should recursively call the method mySquareRoot with tfindrootof and average as the input parameters.

Here is a simple illustration of how this algorithm works. Suppose you want to find the square root of 24 and your first guess is 3.

24 / 3 = 8

8 – 3 = 5, which is > .1

(8 + 3) / 2 = 5.5

Now repeat:

24 / 5.5 = 4.36…

5.5 – 4.36 = 1.136…, which is > .1

(5.5 + 4.36) / 2 = 4.93

Now repeat:

24 / 4.93 = 4.87

4.93 – 4.87 = .06

That means you don’t have to go any further. 4.93 is close enough. For what it’s worth, 4.93 * 4.93 = 24.3, which is pretty close. If you wanted to get a more accurate result, you would change the value you’re checking against from .1 to something smaller.

Recommended publications