We start with a minute paper challenge based on today's reading assignment. 

Design a class Fraction. 

A Fraction is a pair of two integers called num and den. 

Your class should support the following code fragment:

  Fraction a, b;
  a = new Fraction(1, 2);
  b = new Fraction(1, 2); 
  Fraction c = a.add(b); 
  System.out.println( c.num + "/" + c.den ); // what will this be like?

Answer:

public class Fraction {
   
}

This works as follows:

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> new Fraction()
Fraction@2aa853a8
> Fraction a = new Fraction()
> a
Fraction@6efb570d


Here's a bit of progress:

public class Fraction {
  private int num, den; 
  public String show() {
    return this.num + "/" + this.den;  
  }
  public static void main(String[] args) {
    Fraction a = new Fraction();
    a.num = 3; 
    a.den = 5; 
    System.out.println( a.show() ); 
  }
}

I need a custom constructor. 

public class Fraction {
  private int num, den; 
  public Fraction(int num, int den) {
    this.num = num; 
    this.den = den;
  }
  public String show() {
    return this.num + "/" + this.den;  
  }
  public static void main(String[] args) {
    Fraction a = new Fraction(3, 5);
    System.out.println( a.show() ); 
  }
}

We define add and we're done. 

public class Fraction {
  private int num, den; 
  public Fraction(int num, int den) {
    this.num = num; 
    this.den = den;
  }
  public Fraction add(Fraction other) {
    int num = this.den * other.num + other.den * this.num; 
    int den = this.den * other.den; 
    return new Fraction( num, den ); 
  }
  public String show() {
    return this.num + "/" + this.den;  
  }
  public static void main(String[] args) {
    Fraction a = new Fraction(3, 5);
    System.out.println( a.show() ); 
    Fraction b = new Fraction(1, 2); 
    Fraction c = new Fraction(1, 2); 
    Fraction d = b.add(c); 
    System.out.println( b.show() + " + " + c.show() + " = " + d.show() ); 
  }
}

Now this gives you an idea of what Chapter 3 is about. 

public class Fraction {
  private int num, den; 
  public Fraction(int num, int den) {
    this.num = num; 
    this.den = den;
  }
  public void simplify() {
    // for you to write it 
  }
  public Fraction add(Fraction other) {
    int num = this.den * other.num + other.den * this.num; 
    int den = this.den * other.den; 
    return new Fraction( num, den ); 
  }
  public String show() {
    return this.num + "/" + this.den;  
  }
  public static void main(String[] args) {
    Fraction a = new Fraction(3, 5);
    System.out.println( a.show() ); 
    Fraction b = new Fraction(1, 2); 
    Fraction c = new Fraction(1, 2); 
    Fraction d = b.add(c); 
    System.out.println( b.show() + " + " + c.show() + " = " + d.show() ); 
  }
}

Now I start discussing the lab:

import javax.swing.JFrame;
import javax.swing.JComponent; 
import java.awt.Graphics;

public class Penguin extends JComponent {
  int count; 
  public void paintComponent(Graphics g) {
    this.count += 1; 
    System.out.println( "I am drawing ... " + this.count ); 
    g.drawOval(100, 100, 200, 200); 
    
  }
  public static void main(String[] args) {
     JFrame a = new JFrame(); 
     a.setVisible(true);
     a.setSize(500, 500); 
     Penguin p = new Penguin();
     a.add(p); 
  }
}

Think about it. 

import javax.swing.JFrame;
import javax.swing.JComponent; 
import java.awt.Graphics;
import java.awt.Color; 

public class Penguin extends JComponent {
  int count; 
  public void paintComponent(Graphics g) {
    // this.count += 1; 
    // System.out.println( "I am drawing ... " + this.count ); 
    // g.drawOval(100, 100, 200, 200); 
    // default color is Color.BLACK 
    g.drawOval(  0,   0, 500, 500); // circle in the background (not necessary) [ 0] 
    g.setColor(Color.WHITE); 
    g.fillRect(  0,   0, 500, 500); // background (see marks by circle)         [ 0]     
    g.setColor(Color.BLACK); 
    int left = 5;
    g.fillOval( 80-left,  50, 365, 400); // body                                [ 1] 
    g.setColor(new Color(220, 220, 220)); 
    g.setColor(new Color(240, 200, 40)); // some gold 
    g.fillOval( 85-left, 390, 175,  80); // right foot (left)                   [ 4]
    g.setColor(new Color(255, 255, 255)); 
    g.fillOval(264-left,  94, 107,  79); // left eye (right)                    [ 7] 
    g.setColor(new Color(240, 200, 40)); // gold as feet
    g.fillArc (188-left, 173, 150, 150, 55,  65); // bill, beak                 [10] 
    
  }
  public static void main(String[] args) {
    JFrame a = new JFrame(); 
    a.setVisible(true);
    a.setSize(500, 500); 
    Penguin p = new Penguin();
    a.add(p); 
  }
}

So take the rest from the lab assignment.

--