Spring Semester 2003


Practical One Makeup: The Pool of Eligible Problems
This list will be final by Thursday, April 10 11pm (tonight).

It will be constantly updated and adjusted in the meantime.

Please solve the following problems:

  1. Big Java Problem 6.17

    This was done in class on Tuesday, April 8.

    It also appears in Lecture Notes Thirteen.

  2. Big Java Problem 6.16

    A solution for the Game of Nim appears in Lecture Notes Fourteen.

  3. Big Java Problem 6.14
    class ExpApproximator {
      double term = 1, sum = 1; 
      int n = 0; 
      double x; 
      ExpApproximator(double x) { this.x = x; } 
      boolean keepGoing() {
        if (Math.abs(term) < 0.00001) return false; 
        return true; 
      }
      void calculate() {
        n += 1; 
        term = term * x / n;     
        sum += term; 
        this.report(); 
      }
      void report() { System.out.println(sum); }
      public static void main(String[] args) {
        ConsoleReader c = new ConsoleReader(System.in); 
        System.out.print("What's the exponent? "); 
        double exp = c.readDouble(); 
        ExpApproximator a = new ExpApproximator(exp); 
        while (a.keepGoing()) {
          a.calculate(); 
        }
        a.report(); 
        System.out.println("Proof: " + Math.pow(Math.E, exp));
      }
    }
  4. Big Java Problem 6.13

    Here's a solution worked out Monday night:

    class Newton_Raphson {
      public static void main(String[] args) {
        ConsoleReader c = new ConsoleReader(System.in); 
        System.out.print("What's the number? "); 
        double a = c.readDouble(); 
        System.out.print("What's the order of the root? "); 
        double n = c.readDouble(); 
    
        double xOld = 1; 
    
        while (Math.abs(a - Math.pow(xOld, n)) > 0.01) {
          xOld = xOld - (Math.pow(xOld, n) - a) / 
                        (n * Math.pow(xOld, n - 1)); 
          System.out.println(xOld); 
        }
        System.out.println("Proof: " + Math.pow(xOld, n)); 
      }
    }
  5. Big Java Problem 6.12
  6. Big Java Problem 6.11
  7. Big Java Problem 6.10

    Check out the Wizard defined in these notes.

    Here's a simplified version of it:

    class Wizard {
        int countFactors(int number) {
    	int count = 0; 
            for (int i = 2; number > 1; i++) {
    	    while (number % i == 0 ) {
    		count += 1; 
                    System.out.println(i);
    		number = number / i;
    	    }
    	}
    	return count; 
        } 
    
    
        public static void main(String[] args) {
          Wizard a = new Wizard(); 
          a.countFactors(Integer.parseInt(args[0])); 
        }
    }
  8. Big Java Problem 6.8

    Here's a solution to this problem:

    import java.applet.*; 
    import java.awt.*; 
    import javax.swing.JOptionPane;
    
    /* 
        <applet code="One.class" width=300 height=300> 
    
        </applet> 
     */
    
    public class One extends Applet {
      int number; 
    
      Circle[] circles; 
    
      public void init() {
        this.number = Integer.parseInt(
                        JOptionPane.showInputDialog("How many? ")); 
        this.circles = new Circle[number]; 
        for (int i = 0; i < circles.length; i++) 
          circles[i] = new Circle(
                         (int)(Math.random() * 200 + 50), 
                         (int)(Math.random() * 200 + 50), 
                         (int)(Math.random() * 25 + 25), 
                         new Color((float)Math.random(), 
                                   (float)Math.random(), 
                                   (float)Math.random()), 
                         new Color((float)Math.random(), 
                                   (float)Math.random(), 
                                   (float)Math.random())
                       ); 
      }
      public void paint(Graphics g) {
        for (int i = 0; i < circles.length; i++) 
          this.circles[i].show(g); 
      }
    }
    
    class Circle {
      int x, y; 
      int radius; 
      Color color; 
      Color border; 
      Circle(int x, int y, int r, Color c, Color b) {
        this.x = x; this.y = y; 
        this.radius = r; 
        this.color = c; this.border = b; 
      }
      void show(Graphics g) {
        g.setColor(color); 
        g.fillOval(x - radius, y - radius, 2 * radius, 2 * radius); 
        g.setColor(border); 
        g.drawOval(x - radius, y - radius, 2 * radius, 2 * radius); 
      } 
    }
  9. Big Java Problem 6.7

    Here's a solution to this problem:

    import java.util.*; 
    
    class Average {
      public static void main(String[] args) {
        Calculator calc = new Calculator(); 
        ConsoleReader c = new ConsoleReader(System.in); 
        System.out.print("Type the numbers: "); 
        String line = c.readLine();
        calc.process(line); 
      }
    }
    
    class Calculator {
      void process(String line) {
        StringTokenizer st = new StringTokenizer(line); 
        double sum = 0, sumSq = 0, n = 0; 
        while (st.hasMoreTokens()) {
          int x_i = Integer.parseInt(st.nextToken()); 
          sum += x_i; 
          sumSq += x_i * x_i; 
          n += 1; 
        }
        System.out.println(n + " numbers."); 
        System.out.println("Average is: " + sum / n); 
        System.out.println("Std. deviation: " + 
          Math.sqrt( (sumSq - sum * sum /  n) / (n - 1))); 
      }
    }
  10. Big Java Problem 6.5

    The basic for loop is given in Lecture Notes Thirteen.

    for (int i = 3; i <= n; i++) {    
      fNew = fOld + fOlder; 
      fOlder = fOld; 
      fOld = fNew; 
    } 
  11. Big Java Problems 6.3 and 6.4

    This also appears in Lecture Notes Thirteen.

  12. Big Java Problem 6.2
  13. Big Java Problem 6.1
  14. Big Java Problem 5.16

    This problem is listed here as problem 14.

    A solution to it could be found in this document.

    It's called Fourteen.java and both links are off Lecture Notes Ten.

  15. Big Java Problem 5.14
  16. Big Java Problem 5.12

    This is Problem 10 in this set of programs.

    Solutions (Ten.java) are posted here (off Lecture Notes Ten).

  17. Big Java Problem 5.9
  18. Big Java Problem 5.8

    Here's a more general solution:

    class GradeConverter {
      String letter(double number) {
        double[] values =  {  0,  0.7,   1,  1.3,  1.7,   2,  2.3,  2.7,  3,   3.3,  3.7,  4}; 
        String[] letters = {"F", "D-", "D", "D+", "C-", "C", "C+", "B-", "B", "B+", "A-","A"}; 
        for (int i = 0; i < values.length; i++) {
          if (values[i] > number) {
             double average = (values[i] + values[i-1]) / 2; 
             if (number >= average) return letters[i];
             else return letters[i-1]; 
          } 
        }
        return "I'm sorry!... Not a valid input."; 
      }
      public static void main(String[] args) {
        GradeConverter a = new GradeConverter(); 
        for (int i = 0; i < 10; i++) {
          double number = Math.random() * 4; 
          System.out.println(number + " = " + a.letter(number)); 
        }
    
        double number = 3.85; 
        System.out.println(number + " = " + a.letter(number)); 
    
    
      }
    }
    But also see 5.7 below.
  19. Big Java Problem 5.7

    Both problems appear in here.

    They are called Seven and Eight.

  20. Big Java Chapter 4 Section 4.9 (IntersectionApplet.java)
  21. Big Java Chapter 4 Section 4.8 (ColorApplet.java)
  22. Big Java Chapter 4 Section 4.7 (CarApplet.java)
  23. Big Java Problem P4.15
  24. Big Java Problem 3.14
  25. Big Java Problem 3.12

    That's reverse in Lab Three but here's more general solution(s):

    class DigitExtractor {
      int theNumber; 
      int index;  
      String theCopy;
      DigitExtractor (int aNumber) { 
        this.theNumber = aNumber; 
        this.theCopy = this.theNumber + ""; 
        this.index = this.theCopy.length();
      }
      int nextDigit() {
        this.index -= 1; 
        return theCopy.charAt(this.index) - '0'; 
      }
      boolean hasMoreDigits() {
        if (index > 0) return true;
        else return false; 
      }
    
      public static void main(String[] args) {
        DigitExtractor d = new DigitExtractor(123456789); 
        while (d.hasMoreDigits()) {
          System.out.println(d.nextDigit()); 
        }
      }
    }
    
    class DigitExtractorTwo {
      int theNumber; 
      DigitExtractorTwo (int aNumber) { 
        this.theNumber = aNumber; 
      }
      int nextDigit() {
        int digit = this.theNumber % 10; 
        this.theNumber = this.theNumber / 10; 
        return digit;
      }
      boolean hasMoreDigits() {
        if (this.theNumber > 0) return true;
        else return false; 
      }
    
      public static void main(String[] args) {
        DigitExtractorTwo d = new DigitExtractorTwo(987654321); 
        while (d.hasMoreDigits()) {
          System.out.println(d.nextDigit()); 
        }
      }
    }

    A similar approach is used in Lecture Notes Thirteen (Four.java).

  26. Big Java Problem 3.11

    A very similar problem is listed here with number 6.

    A solution to this problem could be found in this document.

    Both are links listed under Lab Notes Two (Six.java)

  27. Big Java Problem 2.15
  28. Big Java Problem 2.14
  29. Big Java Problem 2.13
  30. Big Java Problem 2.12
  31. Big Java Problem 2.11

    This, essentially, is your Lab Four.

  32. Big Java Problem 2.10
  33. Big Java Problems 2.8-2.9
  34. Big Java Chapter 13 Section 13.2 (see Purse.java)
  35. Check page 331 in Section 8.1 for RootApproximator.java
  36. Big Java Problem P13.10
  37. Lab Nine
  38. Lab Eight
  39. Big Java Problem P17.12 (see Lecture Notes Sixteen at the end)
  40. Fraction (Lecture Notes Fifteen)

    Enough said.

  41. Homework Four Report
  42. Homework Four Tigger

    Here's a solution:

    class Tigger {
        String x, y; 
        Tigger(int x, int y) {
            this.x = x + ""; 
            this.y = y + ""; 
        }
        void bounce() {
            int a = calculate(x), 
                b = calculate(y); 
            this.x = a + "";
            this.y = b + ""; 
    
        }
        int calculate(String a) {
            int sum = 0; 
            for (int i = 0; i < a.length(); i++) {
                sum += (a.charAt(i) - '0') * (a.charAt(i) - '0'); 
            } 
            return sum; 
        }
        String report() {
            String 
                x = "   " + this.x, 
                y = "   " + this.y; 
            return 
                "Tigger just bounced to (" + 
                x.substring(x.length() - 3) + ", " 
                + y.substring(y.length() - 3) + ")"; 
        }
    } 
  43. Homework Four Paper-Scissors-Rock
    class One {
        public static void main(String[] args) {
    
    	Player bonaparte, wellington;
    
    	bonaparte = new Player(); 
    	wellington = new Player(); 
    
    	System.out.println("Let the game begin!"); 
    
    	bonaparte.makeGuess(); 
    	wellington.makeGuess(); 
    
            System.out.println("The guesses have been made: "); 
    
    	System.out.println("  Bonaparte has chosen .... " + bonaparte.report());
    	System.out.println("  Wellington has chosen ... " + wellington.report()); 
    
    	if (bonaparte.strongerThan(wellington)) 
    	    System.out.println("Bonaparte wins!"); 
            else if (wellington.strongerThan(bonaparte))
    	    System.out.println("Wellington wins!"); 
    	else System.out.println("It's a draw...");     }
    }
    
    class Player {
        String guess; 
        String makeGuess() {
    	int value = (int) (Math.random() * 3); 
            if (value == 0) this.guess = "paper"; 
            if (value == 1) this.guess = "rock"; 
            if (value == 2) this.guess = "scissors"; 
    	return this.guess; 
        }
        boolean strongerThan(Player other) {
    	if (this.guess.equals("paper")    && other.guess.equals("rock"    ) || 
    	    this.guess.equals("rock" )    && other.guess.equals("scissors") ||
    	    this.guess.equals("scissors") && other.guess.equals("paper")) 
    	    return true; 
    	else return false; 
        }
        String report() {
    	return guess; 
        }
    }
  44. Homework Four Line
    class One {
        public static void main(String[] args) {
    	Line a = new Line(new Point(0, 0), new Point (1, 1)); 
    	System.out.println(a.length()); 
        }
    }
    
    class Line {
        Point a, b; 
        Line(Point a, Point b) {
    	this.a = a; 
    	this.b = b; 
        }
        double length() {
    	return (this.a).distanceTo(this.b); 
        }
    }
    
    class Point {
        double x, y;
        Point(double x, double y) {
    	this.x = x; this.y = y; 
        }
        double distanceTo(Point other) {
    	double 
    	    dX = this.x - other.x, 
    	    dY = this.y - other.y; 
    	return Math.sqrt(dX * dX + dY * dY); 
        }
    }
  45. Homework Four Oracle
    class Oracle {
        ConsoleReader c = new ConsoleReader(System.in); 
        void takeCall() {
    	System.out.println("Oracle> Hi, ask me any question..."); 
    	String question = c.readLine(); 
    	System.out.println("Oracle> That's a tough one... any words of wisdom that would apply to this?");
            this.advice = this.c.readLine(); 
    	System.out.println("Oracle> Nice. Back to your question I'd say: ");
    	System.out.println("***(" + this.answer + ")***"); 
            this.answer = this.advice; 
        }
        String answer = "The answer, my friend, is in the blowing of the wind."; 
        String advice = " Man gave names to all the animals, in the beginning... ";
    }
    
  46. Homework Four Elevator
    class One {
        public static void main(String[] args) {
    	Elevator e = new Elevator(20); 
    	e.up(26); 
    	e.down(14); 
            e.up(10); 
    	e.down(30); 
            e.up(e.currentFloor() + 3); 
        }
    }
    
    class Elevator {
        int floor; 
        Elevator(int floor) {
    	this.floor = floor; 
        }  
        void up(int to) {
    	if (this.floor >= to) {
    	    System.out.println("Sorry, from floor " + this.floor + 
    			       " we can't go up to floor " + to); 
    	} else {
    	    System.out.println("Elevator going up (" + 
    			       this.floor +
    			       " --> " + to + ")"); 
    	    for (int i = this.floor; i <= to; i++) {
    		this.floor = i; 
    		this.report(); 
    	    }
    	    System.out.println("Elevator now on floor: " + this.floor); 
    	}
        }
        void down(int to) {
    	if (this.floor <= to) {
    	    System.out.println("Sorry, from floor " + this.floor + 
    			       " we can't go down to floor " + to); 
    	} else {
    	    System.out.println("Elevator going down: (" + 
    			       this.floor +
    			       " --> " + to + ")"); 
    	    for (int i = this.floor; i >= to; i--) {
    		this.floor = i; 
    		this.report(); 
    	    }
    	    System.out.println("Elevator now on floor: " + this.floor); 
    	}
        }
        void report() {
    	System.out.println("   The elevator is now on floor " + this.floor); 
        }
        int currentFloor() { return this.floor; } 
    }
That's it.

Best of luck in preparing the exam!

Let me know if you need help.

I will be posting the text for each of these problems here soon.


Last updated: Apr 10 (10:40pm,) 2003 by Adrian German for A201