/****************************************************************************/ public class Problem1 { public static int sumFactors(int x) { int sumFactor = 0; int count = x; while(count > 0) { if(x % count == 0) sumFactor += count; count--; } /* also, for(int count = x; count>0; count--) if(x % count == 0) sumFactor += count; */ return sumFactor; } public static int max(int x, int y) { if (x >= y) return x; return y; } public static void main(String[] args) { TokenReader in = new TokenReader(System.in); System.out.print("Enter an integer: "); int newnum = in.readInt(); int oldnum = newnum; int maxsum = 0; while (newnum >= 1 && newnum <= 1000) { maxsum = max(maxsum,sumFactors(newnum)); System.out.println("Sum: "+sumFactors(newnum)); System.out.print("Enter an integer: "); oldnum = newnum; newnum = in.readInt(); // get next input -- reprompt if decreases: while (newnum <= oldnum && newnum >= 1) { System.out.println("Your integers must increase!"); System.out.print("Enter an integer: "); newnum = in.readInt(); } } if (oldnum == newnum) // also, if (maxsum==0) System.out.println("No sum!"); else System.out.println("Max sum: "+maxsum); } } // Class Problem1 /****************************************************************************/ public class Problem2 { public static void main(String[] args) { // create clones Clone a = new Clone("argo",70,null); Clone b = new Clone("brur",40,a); Clone c = new Clone("chag",10,b); Clone d = new Clone("drym", 1,c); Clone.showLineage(a); Clone.showLineage(b); Clone.showLineage(c); Clone.showLineage(d); } } class Clone { // Instance variables: private String name; // name private int age; // age private Clone parent; // parent, if any // Create a new youngest child of parent with $name$, $age$, // possible $parent$ (which may be $null$) Clone(String name, int age, Clone parent) { this.name = name; this.age = age; this.parent = parent; } // Print a description of the current Clone: name, age, name of parent // If a parent does not exist, indicate by printing "no parent": public String toString() { String parentLabel = "parent: "; if (parent == null) parentLabel += "none"; else parentLabel += parent.name; return "Clone " + name + ": age: " + age + ", " + parentLabel; } // Describe ancestors for input Clone $c$: // Print out public static void showLineage(Clone c) { System.out.println("Lineage of Clone "+c.name+":"); Clone top = c.parent; while(top != null) { System.out.println("-> "+top); top = top.parent; } if (c.parent == null) System.out.println("no ancestors"); } } // Class Clone /****************************************************************************/ public class Problem3 { public static void main(String[] args) { // create 2 rowers: Rower r1 = new Rower(); Rower r2 = new Rower(); // create boat: Boat b = new Boat(); // time data: final int START=0, STOP = 3600; int time; int oldtime = START; for (time = START; time <= STOP; time++) { // rower1 and rower2 row // update new boat position: b.moveBoat(r1.rowDist(oldtime, time) + r2.rowDist(oldtime,time)); // update time: oldtime = time; } // report final position of boat System.out.println(b.getPosition()); } } class Rower { private double eff; // efficiency private double rate; // dist per sec (m/s) private final double MIN = 0.75; // min distance can travel (m) private final double MAX = 1.50; // max distance can travel (m) public Rower() { rate = Math.random()*(MAX-MIN)+MIN; // give rate as 1.25 <= rate < 2.25 } private void changeEfficiency(int time) { eff = (-time*time/3240000.0) + (time/900.0); } // return distance rower manages to row boat public double rowDist(int time1, int time2) { int deltaTime = Math.abs(time2-time1); // not necessarily on prelim changeEfficiency(time2); return eff*rate*deltaTime; // (%)*(dist/time)*(time)->(% dist) } } // Class Rower class Boat { private double pos; // initial pos is zero Boat() { } public void moveBoat(double change) { pos += change; } public double getPosition() { return pos; } } /****************************************************************************/