teaching machines

CS 245 Lecture 2 – GUIs, Clickture

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

Agenda

TODO

Program This

show

Code

IPAddress.java

package lecture01;

import java.util.Random;

public class IPAddress {
  private String ip;

  public IPAddress(String ip) {
    this.ip = ip;
  }

  public String toString() {
    return ip;
  }
  
  public boolean isValid() {
    
    String[] quadrants = ip.split("\\.", -1);
    
    if (quadrants.length != 4) {
      return false;
    }
    
    for (int i = 0; i < 4; ++i) {
      try {
        int quadrant = Integer.parseInt(quadrants[i]);
        if (quadrant < 0 || quadrant > 255) {
          return false;
        }
      } catch (NumberFormatException e) {
        return false;
      }
    }
    
    return true;
  }

  public boolean isLocal() {
    return ip.startsWith("192.168.");
  }
}

TestIPAddress.java

package lecture01;

import org.junit.Assert;
import org.junit.Test;

public class TestIPAddress {
  @Test
  public void testConstructor() {
    IPAddress addy = new IPAddress("1.1.1.1");
    Assert.assertEquals("toString gave incorrect ip address", "1.1.1.1", addy.toString());
  }

  @Test
  public void testIsLocal() {
    IPAddress addy = new IPAddress("1.1.1.1");
    Assert.assertFalse(addy.isLocal());
    addy = new IPAddress("192.168.0.1");
    Assert.assertTrue(addy.isLocal());
  }
  
  @Test
  public void testIsValid() {
    Assert.assertTrue(new IPAddress("1.2.3.4").isValid());
    Assert.assertTrue(new IPAddress("10.255.19.4").isValid());
    Assert.assertTrue(new IPAddress("0.0.0.0").isValid());
    Assert.assertTrue(new IPAddress("0.255.0.255").isValid());
    Assert.assertFalse(new IPAddress("0.255.0").isValid());
    Assert.assertFalse(new IPAddress("0.-255.0.10").isValid());
    Assert.assertFalse(new IPAddress("0.256.0.10").isValid());
    Assert.assertFalse(new IPAddress("0.1.0.10.30").isValid());
    Assert.assertFalse(new IPAddress("0.25/6.0.10").isValid());
    Assert.assertFalse(new IPAddress("foobag").isValid());
    Assert.assertFalse(new IPAddress("0..0.10").isValid());
    Assert.assertFalse(new IPAddress("0101030").isValid());
    Assert.assertFalse(new IPAddress("0.255.0^7.10").isValid());
    Assert.assertFalse(new IPAddress("fo.o.ba.g").isValid());
    Assert.assertFalse(new IPAddress("").isValid());
  }
}

AnnotationExample.java

package lecture02;

public class AnnotationExample {

//  @Override
  public String toString(int i) {
    return "ASdfasdfdsfas";
  }
  
  public static void main(String[] args) {
    AnnotationExample eg = new AnnotationExample();
    System.out.println(eg);
  }
}

Clickture.java

package lecture02;

import java.awt.BorderLayout;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class Clickture {
  public static void main(String[] args) throws IOException {
    JFrame frame = new JFrame("Clickture");
    
    JButton leftButton = new JButton("<");
    frame.add(leftButton, BorderLayout.WEST);
    
    JButton rightButton = new JButton(">");
    frame.add(rightButton, BorderLayout.EAST);
    
    BufferedImage image = ImageIO.read(new File("/Users/johnch/Desktop/imgs/a.jpg"));
    JLabel imageLabel = new JLabel(new ImageIcon(image));
    frame.add(imageLabel, BorderLayout.CENTER);
    
    frame.setSize(1000, 700);
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  }
}

ImageDirectoryCursor.java

package lecture02;

public class ImageDirectoryCursor {
  public ImageDirectoryCursor() {
  }
}

Haiku

show