CS 145 Lecture 21 – Our own objects
Agenda
- command-line arguments
- writing an NDeckerBurger class
- writing a class:
- Figure out what it needs to do. (Methods.)
- Figure out what its persistent state is. (Instance variables.)
- Figure out how to initialize the state. (Constructor.)
- midterm 2
Code
CommandLineArguments.java
package lecture; public class CommandLineArguments { public static void main(String[] args) { // for (int i = 0; i < args.length; ++i) { // System.out.println(args[i]); // } System.out.println(args.length); for (String arg : args) { System.out.println(arg); } } }
NDeckerBurger.java
package lecture; public class NDeckerBurger { // Instance variables private int nDecks; // Constructor public NDeckerBurger(int givenDeckCount) { nDecks = givenDeckCount; } // Methods public int getCheeseCount() { return nDecks; } public int getBaconCount() { return 4 * nDecks; } public int getDeckCount() { return nDecks; } public int getBunCount() { return nDecks + 1; } public double getKetchupOunces() { return 2.4 * nDecks; } public double getCalorieCount() { return 41 * getBaconCount() + 31 * getCheeseCount() + 211 * getDeckCount() + 27 * getBunCount() + 10 * getKetchupOunces() / 0.4; } public void fuse(NDeckerBurger other) { nDecks = nDecks + other.nDecks; } public static void printBoo() { System.out.println("boo"); } public static void main(String[] args) { NDeckerBurger breakfast = new NDeckerBurger(32 + 10); NDeckerBurger wifesBreakfast = new NDeckerBurger(14); breakfast.fuse(wifesBreakfast); System.out.println(breakfast.getDeckCount()); System.out.println(breakfast.getCalorieCount()); NDeckerBurger.printBoo(); } }
TODO
- Complete homework 3.
- Read chapter 8.
Haiku
Monogamy
Args see the world
But the ladies want stable
So now I’m instance
Args see the world
But the ladies want stable
So now I’m instance