CSC 297.Java 2003F, Static vs. Object Methods Admin: * Hmmm ... where is Alex? Sam leaves obnoxious message on answering machine. * Demos of calculators * Sam wants to see loops. * Sam wants to see better output. * Discussion of second Bishop assignment * Assignment: Do that assignment Topics: * Object model, reviewed * Object fields, revisited * Object methods, revisited * Static methods, revisited * Choosing a method type Bishop, Assignment 2: Find Prime Factors Program: PrimeFactors Purpose: Compute the prime factors of an input number. Input: An integer, val. Output: All the prime factors of val (no duplicates), p1, p2, ... pn Preconditions: val > 1 [verified] Postconditions: No two factors are the same, if i != j, pi != pk All the pis are prime. If val is prime, print a message that it is prime. If val <= 1, print a friendly error message. The pi's are all the factors. val = p1^x1 * p2^xn * ... pn^xn for some exponents x1 ... xn. PReflections: How do we know if a number is prime? No built-in procedure. A number is prime if it can't be divided by any other number. Try all the primes smaller than the number. Or ... Try all the numbers smaller than the number. Note that we can stop at the square root of val. PStrategy: Find the square root of val. limit = Math.sqrt(val) For each whole number, num, between 2 and limit try to divide val by num Scheme: (if (zero? (modulo val num)) ...) Java: if ((val % num) == 0) ... "Keep" num if it evenly divides val (print) "Keep" the result of the division (update) PCode: Your goal for Friday