Programming and Computer Science in Java:
Homework #9:  Math methods, String variables
David Kosbie, 2002-2003

Due Date:  Wed, Mar 19, 2003

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]: ");
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]: ");
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;
}
}

}```

What you must understand here:

1. Casting a double into an integer.
2. Casting an integer into a double.
3. The modulus (%) operator:  (x % y) is the remainder when x is divided by y.
4. Math.abs(x) takes the absolute value of x (works for integers or doubles)
5. Math.max(a,b) takes the maximum of a and b (works for integers or doubles)
6. Math.min(a,b) takes the minimum of a and b (works for integers or doubles)
7. Math.PI is the constant PI (3.14159265358...)
8. Math.E is the constant E (2.718281828459...)
9. Math.round(d) rounds d to the nearest integer
10. Math.sqrt(d) takes the square root of d
11. Math.pow(c,d) raises c to the dth power (that is, it equals cd)
12. Math.exp(d) raises e (2.718...) to the dth power (that is, it equals ed)
13. Math.log(d) takes the natural logarithm (base e -- 2.718...) of d
14. Math.sin(d) takes the sine of d, where d is in radians
1. To convert degrees to radians:  (2 * Math.PI * (degrees / 360))
15. Math.cos(d) takes the cosine of d, where d is in radians
16. Math.tan(d) takes the tangent of d, where d is in radians
17. 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 odd numbers (though you must test for division against 2 to handle all the even numbers).  Finally, you must only test for division up to the first integer larger than the square root of N.

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 x2 + y2 <= 1.

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]: ");
// 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()
}```

What you must understand here:

1. How to declare String variables
2. How to assign values into String variables
3. How to use println to print out String variable values
4. String concatentation operator:  +
5. String comparisons:  s.equals("done") and not (s == "done")
6. How to find String length using s.length()
8. How to access an individual character in the string:  s.charAt(i)
1. This is 0-based, so the first character is s.charAt(0)
2. This returns a character which is not a string
3. 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"
4. 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 pentagon 19 your program should output that the perimeter is 95 (which is 19 * 5).

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