Last night during the Help Session we worked on

    (a) Homework Seven 

    (b) Homework Eight 

  When is the next exam? 

  Answer: two weeks from now. 

  What is on the next exam?

  Answer: problems like the ones in Homework Seven (mainly). 

  It's safer to say problems like those in Chapter Eight and Nine. 

  I am really interested in design, modeling. 

  Next assignment is: The Face

  Wed we may give you help with the eyes. 

  class Face {
    Eye a, b; 
    Nose n; 
    Mouth m; 
    Face(Eye left, Eye right, Nose nose, Mouth mouth) { 
      this.a = left; 
      // ...
    }
  }

  class Eye {

  } 

  class Nose {

  }

  class Mouth {
  
  }

  We are focused on modeling. 

  Could you please model a Fraction for me? 

  In general we have three steps:

  (a) instance variables 

  (b) constructors 

  (c) instance methods 

  Let's see some Fractions in action:

  Microsoft Windows [Version 6.1.7601]
  Copyright (c) 2009 Microsoft Corporation.  All rights reserved.

  C:\Users\dgerman\Desktop>javac Fraction.java

  C:\Users\dgerman\Desktop>java Fraction
  Test of operations:
    Add: 2/3 + (-2/3) = 0
    Sub: 2/3 - (-2/3) = 4/3
    Mul: 2/3 * (-2/3) = (-4/9)
    Div: 2/3 / (-2/3) = (-1)
  Test of predicates:
    1. Does 2/3 equal (-2/3)?  The answer is: false
    2. Is (-4) an integer? The answer is: true
    3. Does 0 equal 0? The answer is: true
    4. Is 5/8 greater than 2/3? The answer is: false
 
  C:\Users\dgerman\Desktop>

  So let's get started:


import java.math.BigDecimal;   

class Example {
  public static void main(String[] args) {
    double a = 4.35;
    int b = 100; 
    double result = a * b;
    System.out.println( a + " * " + b + " equals... " + a * b ); 
    // ----------------------
    BigDecimal m, n, q; 
    m = new BigDecimal(4.35); // 2 / 3 * 1.0 
    n = new BigDecimal(100); 
    q = m.multiply(n); 
    System.out.println( m + " * " + n + " = " + q ); 

    m = new BigDecimal("4.35"); 
    q = m.multiply(n); 

    System.out.println( m + " * " + n + " = " + q ); 
  }
}

Next we write Fraction:

public class Fraction /* extends java.lang.Object */ {
  int x, y;

  public Fraction() {
    this.x = 1;
    this.y = 1;
  }

  public Fraction( int x, int y) {
    this.x = x;
    this.y = y;
  }

  public String toString() {
    return x + "/" + y; 
  }
  public Fraction add( Fraction q) {
    return new Fraction(this.x * q.y + this.y * q.x, this.y * q.y);
  }

  
  public static void main(String[] args) {
    Fraction a, b;
    a = new Fraction();
    a = new Fraction( 2, 3 ); 
    System.out.println( a );
    Fraction c = new Fraction(1, 2);
    Fraction d = new Fraction(1, 2); 
    Fraction result = c.add(d); 
    System.out.println( c + " + " + d + " = " + result ); 
  }
}

To be continued.

--