Programming and Computer Science in Java:

See Course Home Page.

**Due
Date: Wed, Mar 19, 2003**

For this assignment, you will need to use the readString, readDouble, and readInt code found here.

For each of the following questions, anything which your
programs *compute* must be computed within a new method, and not directly
inside your main method.

**Topic: Math Operators and Methods**

The following code demonstrates use of various Math operators and methods:

class MyClass { public static void main(String[] args) { double d, e; int i, x, y; // Casting a double into an integer d = 1.987; i = (int) d; // truncates d, so i = 1 System.out.println("d = " + d + ", casted to an int i = " + i); // Casting an integer into a double. System.out.println("3/2 = " + (3/2)); System.out.println("(double)(3/2) = " + (double)(3/2)); System.out.println("(double)(3)/2 = " + (double)(3)/2); // now demonstrate integer operators and methods System.out.println(); System.out.println("Now demonstrating integer operators and methods."); while (true) { System.out.print("Enter two integers [0's to exit]: "); x = readInt(); y = readInt(); if ((x == 0) && (y == 0)) { break; } System.out.println("Math.abs(" + x + ") = " + Math.abs(x)); System.out.println(x + " % " + y + " = " + (x % y)); System.out.println("Math.max(" + x + "," + y + ") = " + Math.max(x,y)); System.out.println("Math.min(" + x + "," + y + ") = " + Math.min(x,y)); } // now demonstrate double operators and methods System.out.println(); System.out.println("Now demonstrating double operators and methods."); System.out.println("Math.PI = " + Math.PI); System.out.println("Math.E = " + Math.E); while (true) { System.out.print("Enter two doubles [0's to exit]: "); d = readDouble(); e = readDouble(); if ((d == 0) && (e == 0)) { break; } System.out.println("Math.abs(" + d + ") = " + Math.abs(d)); System.out.println("Math.round(" + d + ") = " + Math.round(d)); System.out.println("Math.sqrt(" + Math.abs(d) + ") = " + Math.sqrt(Math.abs(d))); System.out.println("Math.exp(" + d + ") = " + Math.exp(d)); System.out.println("Math.log(" + Math.abs(d) + ") = " + Math.log(Math.abs(d))); System.out.println("Math.sin(" + d + ") = " + Math.sin(d)); System.out.println("Math.cos(" + d + ") = " + Math.cos(d)); System.out.println("Math.tan(" + d + ") = " + Math.tan(d)); System.out.println("Math.max(" + d + "," + e + ") = " + Math.max(d,e)); System.out.println("Math.min(" + d + "," + e + ") = " + Math.min(d,e)); System.out.println("Math.pow(" + d + "," + e + ") = " + Math.pow(d,e)); } // Finally, demonstrate 10 random numbers: System.out.println("Here are 10 random numbers:"); i = 0; while (i < 10) { System.out.println(Math.random()); i = i + 1; } } // readString(), readInt(), and readDouble() }

What you must understand here:

*Casting*a double into an integer.*Casting*an integer into a double.- The modulus (%) operator: (x % y) is the
*remainder*when x is divided by y. - Math.abs(x) takes the
*absolute value*of x (works for integers or doubles) - Math.max(a,b) takes the
*maximum*of a and b (works for integers or doubles) - Math.min(a,b) takes the
*minimum*of a and b (works for integers or doubles) - Math.PI is the constant PI (3.14159265358...)
- Math.E is the constant E (2.718281828459...)
- Math.round(d)
*rounds*d to the nearest integer - Math.sqrt(d) takes the
*square root*of d - Math.pow(c,d) raises c to the d
^{th}power (that is, it equals c^{d}) - Math.exp(d) raises
*e*(2.718...) to the d^{th}power (that is, it equals e^{d}) - Math.log(d) takes the
*natural logarithm (base e -- 2.718...)*of d - Math.sin(d) takes the
*sine*of d, where d is in*radians*- To convert degrees to radians: (2 * Math.PI * (degrees / 360))

- Math.cos(d) takes the
*cosine*of d, where d is in*radians* - Math.tan(d) takes the
*tangent*of d, where d is in*radians* - Math.random() returns a random double in [0.0,1.0]

**Problems: Math Functions**

**Question 1**: Area of a Circle

Write a Java program which reads in a double which is the radius of a circle,
and prints out the area of that circle.

__ Question 2: Radius of a Circle
__Write a Java program which reads in a double which is the area of a circle,
and prints out the radius of that circle.

__ Question 3: An Improved Prime Test
__Write a Java program which reads in an integer N, and prints out whether or
not N is prime. However, you must use the modulus operator (%) to test if
a number is evenly divisible by another. Also, you must only test for
division against the

__ Question 4: Analyzing the Improved Prime Test
__Here, we compare the run times of our improved prime test against our
original prime test. To do this, make a table (on a piece of paper) with
three columns -- N, Seconds for Test 1, and Seconds for Test 2. In the
first column, place different values of N, say 100, 1000, 10k, 100k. In
the second and third columns, place the corresponing number of seconds the
appropriate programs required on that input. Plot these values on a
graph. Make some conclusions about how well or poorly your improved prime
test runs, trying to be as specific as possible.

__ Question 5: Point in Unit Circle
__Read in two doubles, x and y, and print out whether the point (x,y) falls
within the unit circle. Note that this is true if and only if x

**Topic: String Variables**

The following code demonstrates basic use of String variables:

class MyClass { public static void main(String[] args) { String s; int i; while (true) { System.out.print("Enter a string ['done' to quit]: "); s = readString(); // note that we use s.equals("done"), not (s == "done") if (s.equals("done")) { break; } System.out.println("You entered: " + s); // Demonstrate string concatentation with "+" s = "Wahoo:" + s; System.out.println("Now s = " + s); // Demonstrate finding the length of s System.out.println("Length of s: " + s.length()); // Demonstrate finding individual characters within s System.out.println("Demonstrating how to access each character in s:"); i = 0; while (i < s.length()) { System.out.print(" s.charAt(" + i + ") = " + s.charAt(i)); if (s.charAt(i) == 'h') { System.out.print(" [ this is an 'h' ] "); } System.out.println(); i = i + 1; } } } // Must include following in order to use console in main() public static TextReader console = new TextReader(System.in); }

What you must understand here:

- How to declare String variables
- How to assign values into String variables
- How to use println to print out String variable values
- String concatentation operator: +
- String comparisons: s.equals("done") and not (s == "done")
- How to find String length using s.length()
- How to read a String from user input (readWord() not readString())
- How to access an individual character in the string: s.charAt(i)
- This is 0-based, so the first character is s.charAt(0)
- This returns a
*character*which is*not*a string - You can test if it is a particular character as follows:

s.charAt(i) == 'h'

Note that 'h' uses*single-quotes*, since it is the*character*h and*not the string "h"* - You can compare two characters in the string directly, so to test if
the first and second characters are the same, you may test:

s.charAt(0) == s.charAt(1)

**Problems: String Variables**

**Question 6:** Longest Name

Write a Java program which reads in a positive integer N followed by N last
names. Your program should print out the longest last name (in the case of
a tie, print out the first one entered).

__ Problem 7: Palindrome
__Write a Java program which repeatedly reads in a word, quitting when the
user enters "done", and prints out whether or not that word is a
palindrome. That is, whether it is the same backwards as forwards.
Some palindromes: "mom", "dad", "madam",
"detartrated", as well as nonsensical words such as "foof",
"abccba", and so forth.

**Problems: More Problems**

**Question 8:** Factorial

Write a Java program which repeatedly reads in a positive integer n (quitting
when n <= 0), and prints out n factorial, where n! = n * (n-1) * (n-2) * ...
This time, though, compute the factorial using doubles rather than
integers. Compare how the two versions (integers versus doubles) work (and
fail) for very large numbers, and explain the different behaviors.

__ Question 9: Perimeter of a Regular Polygon
__ Read in the name of a regular polygon and a side length, and output the
perimeter of that polygon. The polygon will be a triangle, square, pentagon, hexagon, heptagon, or octagon.
For example, if the input is

**Question 10:** Infinite Exponentiation, Finite Answer

Read in a positive double y and output the positive double x such that the infinite exponentiation of x equals y, that is: x^(x^(x^(x^(x^(x^(x^(x^(x...)))...) = y.

__ Question 11: Hamming Distances
__Read in an integer N followed by a word W followed by N more words of the same length as W. Output the word after W which is "closest" by Hamming Distance to W -- that is, the one which differs by the fewest characters from W.

Sample Input: 4 port pond tart post cork

Sample Output: post

Good luck!

DK

See Course Home Page.