Computer Science 15-111 (Sections A & B), Spring 2007

Homework #2 (part 2)

  Due:  Mon 22-Jan-2007, 8pm (online submisson).

 

Note:  instructions for submitting this assignment will be discussed in class on Monday, hence the extension to 8pm that day.  Late assignments are still due by Tuesday at 5pm.

 

Write the following Java programs.  Be sure to use good style, including a good user interface which never throws exceptions (even for “illegal” input!).  Provide meaningful prompts for input, and concise output explaining your program’s results.  Also, be sure your variables have meaningful names whenever possible.

 

Although you would ordinarily use methods for these problems, we have not reviewed them yet (that’s Monday’s lecture!), so they are not required for this assignment.  For each program, you can place all your code (just this once!) inside a main method.

 

1.      Write IsTriangle.java, a program that reads in 3 integers and outputs whether they can form the sides of a triangle.  This is true if each pair of sides sums to greater than the third side.

 

2.      Write IsRightTriangle.java, a program that reads in three integers and outputs whether or not they can form the sides of a right triangle.  That is, if they are a Pythagorean Triple satisfying a2 + b2 = c2.  Note that your program must work regardless of the order in which the values are entered.

 

3.      Write IsPrime.java, a program that reads in an integer and outputs whether or not that number is prime.  Recall that a prime number has no divisors besides itself and 1.  The smallest prime number is 2.  Note that if a number is not prime, then it must have a divisor greater than 1 and less than or equal to its own square root (why?).  You must use this optimization for full credit.

 

4.   Write IsPrimeWithSentinel.java, a modification of Primes.java that repeatedly reads in integers, outputting whether or not they are prime, until a special value (the “sentinel”) is entered.  In this case, the sentinel is 0 (zero), at which point your program should halt.

 

5.   Write IsPerfect.java, which repeatedly reads in positive integers up until the sentinel 0 (zero), and outputs whether or not they are perfect.  A number is perfect if it is the sum of its own proper divisors (excluding itself).  For example, the proper divisors of 6 are 1, 2, and 3, and 6 = 1 + 2 + 3, so 6 is perfect.  28 is the next perfect number.

 

6.   Write NthPerfect.java, which reads in a positive integer N and prints out the Nth perfect number.  So if the input is 1, your program should output 6.  If the input is 2, your program should output 28.  And so on.  Don’t worry about overflow.

 

7.      Leonard Euler, one of the greatest math minds ever, proved the following:
             π2 = 6 (1/12 + 1/22 + 1/32 + 1/42 + 1/52 + ... )
Astonishing!  To demonstrate this, write a program PiFromEuler.java which reads in a positive integer k and uses Euler's formula to approximate pi with the first k terms of this sum.  Be sure to use doubles as appropriate, and to multiply by 6 and then take the square root (since Euler's formula finds pi squared, not pi).