CS 245 Lecture 2 – GUIs, Clickture

Agenda

  • what ?s
  • who you are
  • program this
  • writing a slideshow application

TODO

  • Optional: review CS 145 stuff in chapters 3 and 4 in Core Java.
  • Read Core Java 6.3 through 6.4.2 and 7 through 7.2.
  • What Swing widget creates a box for editing text?
  • 1/4 sheet.

Program This

  1. Write method isValid that reports whether or not an IPAddress is valid.
  2. Write some assert statements for testing IPAddress.isValid.

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

An ideal marriage
One who never makes mistakes
One who fixes them

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *