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:
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]: ");
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:
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
See Course Home Page.