CS1: Lecture 31 – A First Object
Dear students,
Today we move on to our last personality: the Computer as a Creator. Up till this point, code and data have been separate entities in our code. Now, we will marry them. We’ll organize code and the data that it operates on into a single being: an object.
Objects are models of some coherent notion, like a Monster
or a Window
or a List
or a Meeting
or a Book
. These models have state (data) and behaviors (methods). The state does not belong to just one method; it belongs to all an object’s methods and lives as long as the object. The object can be made to do things through its behaviors. Sometimes these behaviors just share a view of the object’s state (getters or accessors), and sometimes these behaviors edit the object’s state (setters or mutators).
Code-wise, we still use the class
construct to create an object in Java. But there are two big differences between the code we’ve been writing and the code we’re about to write:
- We do not qualify methods of an object with the keyword static, which we’ll talk about in more detail later.
- We declare the state of an object outside all methods, marking each variable as private. Technically, state does not have to be private, but doing otherwise is considered poor design. The more you open up the inner workings of an object, the harder it will be to improve and maintain the object.
The big deal about objects is their organizational power. They let us model an entity, a being from the domain of the problem we are trying to solve in language close to the problem we are trying to solve. And the first problem we’ll try to solve is one inspired by a part-time job I had in college. Derek and I had to create a website for McGraw-Hill to help college students track what they ate and analyze the results. So, let’s model an NDeckerBurger
.
NDeckerBurger
What’s the state of an NDeckerBurger
? The number of decks or patties. We will declare a single piece of state, an instance variable, to hold the number of decks. We will make it private:
public class NDeckerBurger {
private int ndecks;
}
Do we need other state? Perhaps we could add state for the number of pickles, bacon strips, cheese slices and so on. But really, these could just be derived properties. We can determine their number from ndecks
with a little arithmetic. We will add some getters to compute their amounts. Let’s also add a method to count up the calories in a NDeckerBurger
, using these numbers that I pulled from the internet some years ago:
- Patty: 211 calories per patty
- Bun: 27 calories per bun
- Cheese: 31 calories per slice
- Ketchup: 10 calories per 0.4 ounces
- Pickles: 1 calorie per slice
- Bacon: 41 calories per strip
But how we create an NDeckerBurger
? We need a special form of method called a constructor that gets called when a burger is born:
public class NDeckerBurger {
private int ndecks;
public NDeckerBurger(int givenDeckCount) {
ndecks = givenDeckCount;
}
}
Let’s also add some setters: a setPattyCount
method and a merge
method that unites two burgers.
The primary thing to get used to when writing code for an object is that not all the data needs to come from parameters. As long as a method is not static
, it also has access to any instance variables in the class.
TODO
Here’s your TODO to complete before we meet again:
- Midterm 2 is on Monday, November 25. The structure will be the same as before. During the exam, you may consult one page of hand-written notes that you have made. The topics that we’ve been discussing since the last exam include conditional statements, loops, arrays, and
ArrayList
. So that I may have time to grade and that you may rest, we will not have lab next week on November 26. - CS 145, your lab is posted.
- We will conduct peer reviews for homework 4 in lab this week.
See you next class!
P.S. It’s time for a haiku!
My cat has 1 tail
18 claws, 100 coins
1 up, and 10 lives
P.P.S. Here’s the code we wrote together…
Breakfast.java
package lecture1118.cs145;
public class Breakfast {
public static void main(String[] args) {
NDeckerBurger breakfast = new NDeckerBurger(4);
NDeckerBurger secondBreakfast = new NDeckerBurger(3);
breakfast.merge(secondBreakfast);
System.out.println(breakfast.getCalorieCount());
}
}
FileWritingForMM.java
package lecture1118.cs145;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
public class FileWritingForMM {
public static void main(String[] args) throws FileNotFoundException {
File file = new File("/Users/johnch/Desktop/favorite.txt");
int favoriteNumber = 13;
PrintWriter out = new PrintWriter(file);
out.println(favoriteNumber);
out.close();
}
}
NDeckerBurger.java
package lecture1118.cs145;
public class NDeckerBurger {
private int nDecks;
public NDeckerBurger(int givenDeckCount) {
nDecks = givenDeckCount;
}
public NDeckerBurger() {
this(1);
}
public int getCheeseSliceCount() {
return nDecks;
}
public int getBaconCount() {
return nDecks * 2;
}
public int getLettuce() {
return 10;
}
public double getKetchup() {
return 0.5 * nDecks;
}
public int getBunCount() {
return nDecks + 1;
}
public int getPickleCount() {
return 7 * nDecks;
}
public void merge(NDeckerBurger that) {
this.nDecks += that.nDecks;
}
public double getCalorieCount() {
return nDecks * 211 +
this.getBunCount() * 27 +
this.getCheeseSliceCount() * 31 +
this.getPickleCount() * 1 +
this.getLettuce() * 5 +
this.getBaconCount() * 41 +
this.getKetchup() * 10 / 0.4;
}
}