CS 145 Lecture 34 – Hello, Objects

Agenda

  • what ?s
  • EventQueue in homework 6
  • exceptions in practice
  • marrying data and code
  • the three parts of a class
  • NDeckerBurger

TODO

  • Sync and pull to get some updates to the homework 6 SpecChecker and homework 7 specification.
  • No lab on Monday.

Code

FileTest.java

package lecture1121;

import java.io.FileNotFoundException;
import java.io.PrintWriter;

public class FileTest {
  public static void main(String[] args) throws FileNotFoundException {
    PrintWriter out = new PrintWriter("blarp");
    out.println("asdfasdfjaklsdfjads");
    out.close();
  }
}

Eventism.java

package lecture1121;

import java.awt.EventQueue;
import java.lang.reflect.InvocationTargetException;
import javax.swing.JOptionPane;

public class Eventism {
  public static void main(String[] args) throws InvocationTargetException, InterruptedException {
    EventQueue.invokeAndWait(new Runnable() {
      @Override
      public void run() {
        JOptionPane.showMessageDialog(null, "Hello");
      }
    });
  }
}

Circle.java

package lecture1121;

public class Circle {
  private double radius;
  
  public Circle(double radiusGivenAtBirth) {
    radius = radiusGivenAtBirth;
  }
  
  public double getArea() {
    return Math.PI * radius * radius;
  }
  
  public double getDiameter() {
    return 2 * radius;
  }
  
  public double getCircumference() {
    return Math.PI * getDiameter();
  }
}

CircleWorld.java

package lecture1121;

public class CircleWorld {
  public static void main(String[] args) {
    Circle c1 = new Circle(5);
    System.out.println(c1.getArea());
    Circle c2 = new Circle(1);
    System.out.println(c2.getArea());
  }
}

NDeckerBurger.java

package lecture1121;

public class NDeckerBurger {
  private int nDecks;
  
  public NDeckerBurger(int givenDeckCount) {
    if (givenDeckCount <= 0) {
      throw new IllegalArgumentException("Bad patty count");
    }
    
    nDecks = givenDeckCount;
  }

  public int getDeckCount() {
    return nDecks;
  }
  
  public int getCheeseSliceCount() {
    return nDecks * nDecks; 
  }
  
  public int getBaconCardinality() {
    return nDecks * 2;
  }
  
  public int getBunCount() {
    return nDecks + 1;
  }
  
  public double getKetchupInOunces() {
    return nDecks / 3.0;
  }
  
  public double getCalories() {
    return 211 * getDeckCount() +
           31 * getCheeseSliceCount() + 
           41 * getBaconCardinality() + 
           27 * getBunCount() +
           10 / 0.4 * getKetchupInOunces();
  }
}

BurgerTime.java

package lecture1121;

public class BurgerTime {
  public static void main(String[] args) {
    NDeckerBurger breakfast = new NDeckerBurger(4);
    System.out.println(breakfast.getCalories());
  }
}

Haiku

on the separation of concerns:
Who moves the clock’s hands?
It does. Thank goodness it does
I don’t have the time

Comments

Leave a Reply

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