|       | Lecture 12 - slide 10 : 30 | 
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
// THE VIEW CLASS
public class DivModPresenter extends Applet {
   private DivModCalculator modelObject;
   private DivModListener listener;
   private Label title;
   private TextField dividend, divisor;
   private Label quotient,rest;
   public DivModPresenter(){
     modelObject = new DivModCalculator (this);
     listener = new DivModListener (modelObject, this);
     title = new Label ("Div Mod Calculator");
     dividend = new TextField ("0",5);
     divisor = new TextField ("0",5);
     quotient = new Label ("Kvotient:");
     rest = new Label ("Rest:") ;
    }
   public void init() {
     dividend.addActionListener (listener);
     divisor.addActionListener (listener);
     setLayout(new GridLayout(6,1));
     add(title);
     add(divisor);  add(dividend); 
     add(quotient); add(rest); 
     resize (200,200);
   }
   // Extract and return the dividend from the view.
   public int getDividend(){
     String strResult = dividend.getText();
     return Integer.parseInt(strResult);
   }
   // Extract and return the divisor from the view.
   public int getDivisor(){
     String strResult = divisor.getText();
     return Integer.parseInt(strResult);
   }
   // Show the quotient and the rest as text labels in the view.
   public void showResult(IntPair quotientRest){
     quotient.setText("Kvotient: " + Integer.toString(quotientRest.firstInt,10));
     rest.setText("Rest: " + Integer.toString(quotientRest.secondInt,10));
   }
  public static void main(String[] args){
     Frame f = new Frame("Color adjuster");
     f.addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent e) {
             System.exit(0);
           }
       });
     DivModPresenter presenter =  new DivModPresenter();
     presenter.init();
     f.add("Center", presenter);
     f.pack();
     f.setSize(new Dimension(500,500));
     f.setVisible(true);
 } // end main
} // end DivModPresenter
class IntPair {
  public int firstInt, secondInt;
  public IntPair(int firstInt, int secondInt){
    this.firstInt = firstInt;
    this.secondInt = secondInt;
  }
}
// THE MODEL CLASS
class DivModCalculator {
  private DivModPresenter viewObject;
  IntPair inputMemory, outputMemory;
  public DivModCalculator(DivModPresenter viewObject) {
    this.viewObject = viewObject;
    inputMemory = null; outputMemory = null;
  }
   
  private IntPair divMod(int x, int y){
    // require x > 0 && y > 0 ;
    int rest = x;
    int quotient = 0;
    
    while (rest >= y) {
      // invariant rest + quotient * y = x;
      rest = rest - y;
      quotient = quotient + 1;
    }
    return (new IntPair(quotient,rest));
    // ensure  rest + quotient * y = x && rest < y && rest >= 0 ;
  } // end div
  public void doCalculate(int input1, int input2){
    inputMemory = new IntPair(input1, input2);
    if (input1 > 0 && input2 > 0)
      outputMemory = divMod(input1, input2);
    else
      outputMemory = new IntPair(-1, -1);
    // update the view - in an easy but primitive way:
    viewObject.showResult(outputMemory);
  }
} // end DivModCalculator
// THE CONTROLLER CLASS
class DivModListener implements ActionListener{
  private DivModCalculator modelObject;
  private DivModPresenter viewObject;
  public DivModListener(DivModCalculator modelObject, DivModPresenter viewObject){
    this.modelObject = modelObject;
    this.viewObject = viewObject;
  }
  public void actionPerformed(ActionEvent action){
    modelObject.doCalculate(viewObject.getDivisor(), viewObject.getDividend());
  }
} // end DivModListener