CS 145 Lecture 20 – NDeckerBurger, our first object
April 20, 2012 by Chris Johnson. Filed under cs145, lectures, spring 2012.
Agenda
- objects
- instance variables
- constructors
- methods
- static vs. instance
Code
NDeckerBurger.java
package prefinal;
public class NDeckerBurger {
private int nDecks;
private boolean hasCheese;
public static final int CALORIES_PER_DECK = 211;
public NDeckerBurger(int givenDeckCount) {
nDecks = givenDeckCount;
}
public NDeckerBurger(int givenDeckCount, boolean hasCheese) {
nDecks = givenDeckCount;
this.hasCheese = hasCheese;
}
public int getDeckCount() {
return nDecks;
}
public int getPickleCount() {
return 3 * nDecks;
}
public int getCheeseCount() {
if (hasCheese) {
return 1 * nDecks;
} else {
return 0;
}
}
public int getBunCount() {
return nDecks + 1;
}
public int getKetchupOunces() {
return nDecks;
}
public int getBaconCount() {
return 3 * nDecks;
}
public double getCalorieCount() {
return CALORIES_PER_DECK * getDeckCount() +
31 * getCheeseCount() +
10 * getPickleCount() +
27 * getBunCount() +
41 * getBaconCount() +
10 * getKetchupOunces() / 0.4;
}
}
Restaurant.java
package prefinal;
public class Restaurant {
public static void main(String[] args) {
NDeckerBurger myBreakfast = new NDeckerBurger(7);
// myBreakfast.nDecks = 7;
NDeckerBurger yourBreakfast = new NDeckerBurger(3);
// yourBreakfast.nDecks = 3;
System.out.println(myBreakfast.getDeckCount());
System.out.println(myBreakfast.getCalorieCount());
System.out.println(yourBreakfast.getCalorieCount());
System.out.println(NDeckerBurger.CALORIES_PER_DECK);
// ILL-EGAL. getDeckCount is not static.
// System.out.println(NDeckerBurger.getDeckCount());
}
}