teaching machines

CS 245 Lecture 4 – Interfaces

January 30, 2014 by . Filed under cs245, lectures, spring 2014.

Agenda

TODO

Program This

public interface Conjugator {
  public String getYo(String verb);
  public String getTu(String verb);
  public String getUsted(String verb);
  public String getNosotros(String verb);
}

Write a class ArConjugator, IrConjugator, or ErConjugator that implements the Conjugator interface and transforms the verb according to the rules exemplified in the conjugations of following regular verbs:

verb yo tu usted nosotros
hablar hablo hablas habla hablamos
comer como comes come comemos
vivir vivo vives vive vivimos

Code

Verb.java

package lecture04;

public class Verb {
  private String infinitive;
  private Conjugator conjugator;

  public Verb(String infinitive,
              Conjugator conjugator) {
    this.infinitive = infinitive;
    this.conjugator = conjugator;
  }

  public String getInfinitive() {
    return infinitive;
  }

  public Conjugator getConjugator() {
    return conjugator;
  }
}

Conjugator.java

package lecture04;

public interface Conjugator {
  public String getYo(String verb);
  public String getTu(String verb);
  public String getUsted(String verb);
  public String getNosotros(String verb);
}

ArErConjugator.java

package lecture04;

public class ArErConjugator implements Conjugator {
  public String getYo(String verb) {
    return verb.substring(0, verb.length() - 2) + "o";
  }

  public String getTu(String verb) {
    return verb.substring(0, verb.length() - 1) + "s";
  }

  public String getUsted(String verb) {
    return verb.substring(0, verb.length() - 1);
  }

  public String getNosotros(String verb) {
    return verb.substring(0, verb.length() - 1) + "mos";
  }
}

IrConjugator.java

package lecture04;

public class IrConjugator implements Conjugator {
  public String getYo(String verb) {
    return verb.substring(0, verb.length() - 2) + "o";
  }

  public String getTu(String verb) {
    return verb.substring(0, verb.length() - 2) + "es";
  }

  public String getUsted(String verb) {
    return verb.substring(0, verb.length() - 2) + "e";
  }

  public String getNosotros(String verb) {
    return verb.substring(0, verb.length() - 1) + "mos";
  }
}

Verber.java

package lecture04;

import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.ArrayList;

public class Verber {
  private ArrayList<Verb> verbs;
  
  public Verber() {
    verbs = new ArrayList<Verb>();
  }
  
  public void add(Verb verb) {
    verbs.add(verb);
  }
  
  public void showTable(String path) throws FileNotFoundException {
    PrintWriter out = new PrintWriter(path);
    for (Verb verb : verbs) {
      Conjugator c = verb.getConjugator();
      out.printf("%s,%s,%s,%s,%s%n",
                        verb.getInfinitive(),
                        c.getYo(verb.getInfinitive()),
                        c.getTu(verb.getInfinitive()),
                        c.getUsted(verb.getInfinitive()),
                        c.getNosotros(verb.getInfinitive()));
    }
    out.close();
  }
}

Main.java

package lecture04;

import java.io.FileNotFoundException;

public class Main {
  public static void main(String[] args) throws FileNotFoundException {
    Verber verber = new Verber();
    
    Conjugator arErConjugator = new ArErConjugator();
    Conjugator irConjugator = new IrConjugator();

    verber.add(new Verb("abrir", irConjugator));
    verber.add(new Verb("aprender", arErConjugator));
    verber.add(new Verb("bailar", arErConjugator));
    verber.add(new Verb("caminar", arErConjugator));
    verber.add(new Verb("cantar", arErConjugator));
    verber.add(new Verb("comprender", arErConjugator));
    verber.add(new Verb("escribir", irConjugator));
    verber.add(new Verb("leer", arErConjugator));
    verber.add(new Verb("recibir", irConjugator));
    
    verber.showTable("/Users/johnch/Desktop/spanish.csv");
  }
}

TextEater.java

package lecture04;

import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.util.Scanner;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JTextArea;

public class TextEater {
  public static void main(String[] args) {
    final JFrame frame = new JFrame("TextEater");

    JMenuBar menuBar = new JMenuBar();
    frame.setJMenuBar(menuBar);

    JMenu fileMenu = new JMenu("File");
    menuBar.add(fileMenu);

    JMenuItem openMenuItem = new JMenuItem("Open");
    fileMenu.add(openMenuItem);

    final JTextArea textArea = new JTextArea();
    frame.add(textArea, BorderLayout.CENTER);

    textArea.setFont(new Font(Font.MONOSPACED, Font.PLAIN, 18));

    openMenuItem.addActionListener(new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent arg0) {
        try {
          JFileChooser fileChooser = new JFileChooser();
          int status = fileChooser.showOpenDialog(frame);
          if (status == JFileChooser.APPROVE_OPTION) {
            File selectedFile = fileChooser.getSelectedFile();
            Scanner in = new Scanner(selectedFile);
            while (in.hasNextLine()) {
              textArea.append(in.nextLine() + "\n");
            }
            in.close();
          }
        } catch (IOException e) {
          throw new RuntimeException(e);
        }
      }
    });

    frame.setSize(512, 512);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
  }
}

Haiku

A bleak dawn ahead
My code does nothing but wait
Execution comes