Introduction to Software Development
GWU Computer Science
By the end of this module, you will be able to:
If you do not have your Codio course ready, use any text editor or simple IDE. Some possibilities are:
We can now better understand the static keyword.
static data is global, and is shared between all objects of a class. It's like a team name, shared by all players.
static methods are not associated with any specific object, thus none of the non-static (object) data can be directly accessed within one. These methods can access and modify comparably static data in the class.
Non-static (object) data has a separate copy for each object.
Non-static (object) methods can access the global non-static (object) data in the class. They access and modify versions of that data specific to the current object.
public class StaticExample {
private static int staticVar;
private int objectVar;
public static void staticSetVar(int val) {
staticVar = val;
}
public static int staticGetVar() {
return staticVar;
}
public void objSetVar(int val) {
objectVar = val;
}
public int objGetVar() {
return objectVar;
}
}
public class TestStaticExample {
public static void main(String[] args) {
StaticExample.staticSetVar();
System.out.println ( StaticExample.staticGetVar() );
StaticExample ob1 = new StaticExample();
ob1.objSetVar();
System.out.println ( ob1.objGetVar() );
}
}
public class StaticExample2 {
private static int staticVar;
private int objectVar;
public void objSetVars() {
objectVar = 10;
staticSetVars();
}
public int[] objGetVars() {
return new int[]{staticvar, objectVar};
}
public static void staticSetVars() {
staticVar = 10;
objSetVars();
}
public static int[] staticGetVars() {
return new int[]{staticvar, objectVar};
}
public static void main(String[] args) {
staticSetVars();
objSetVars();
StaticExample v = new StaticExample();
v.staticSetVars();
v.objSetVars();
}
}
null is a valid value for a variable of any non-primitive type. This includes:
objects
arrays
Strings
null is simply a reference to nothing
null is often used to denote "there is no valid object".
It is sometimes returned when an object is expected to denote an error.
Example: What if you try and lookup a facebook user that doesn't exist -- FBUser findUser(String first, String last)?
public class NullTest {
private int n;
public int getN() {
return n;
}
public static void main(String[] args) {
NullTest nt = null;
System.out.println(nt.getN());
}
}
FBUser u;
If a class gives the blueprint to create objects, constructors lay the initial foundation for each object.
Constructors are methods that are invoked when a new object is created that are used to initialize the data of the new object.
FBUser u = new FBUser();
new FBUser(); invokes the constructor on the newly created object.
The constructor can be overloaded just like any other method:
FBUser u = new FBUser("chester", "thecat");
What does a constructor look like? An example:
public class FBUser {
private long userId;
private String firstName, lastName;
private FBUser[] friends;
public FBUser() {
// initialize object's data here
}
public FBUser(String first, String last) {
firstName = first;
lastName = last;
}
public void setId(long id) {
userId = id;
}
public long getId() {
return userId;
}
}
Initialize the data in the constructor (Id, firstName, lastName). Then add ccode to FBUserTest.java that uses the getters in order to verify it was initialized correctly.
Where is the problem in the following code?
public class FBUser {
private long userId;
private String firstName, lastName;
private FBUser[] friends;
public FBUser(String firstName, String lastName) {
// uh oh...
}
public void setId(long id) {
userId = id;
}
public long getId() {
return userId;
}
}
public class FBUser {
private long userId;
private String firstName, lastName;
private FBUser[] friends;
public FBUser(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
public void setId(long id) {
userId = id;
}
public long getId() {
return userId;
}
}
this is, generically, a variable that references the "current" object.
If a method takes a FBUser argument, or a variable holds a FBUser, this can be used.
public class FBUser {
private long userId;
private String firstName, lastName;
private FBUser[] friends;
public FBUser() {
friends = new FBUser[10];
friends[0] = this;
}
}
void addFriend(FBUser friend)
However, add a check that will prevent a FBUser from adding itself to its own friend list. In the main, this undesirable action might be done with:
FBUser user = new FBUser();
user.addFriend(user);
void addAsFriend(FBUser otherUser)
This method adds the current FBUser as a friend in the otherUser.
Zombies...
Eat brains of humans.
Turn bit humans into zombies.
Can be killed by humans.
Humans...
Shoot zombies in the brain (so much with the brains!).
Fight other humans, and might kill them. (Why so mean?)
Are turned into zombies when bit.
Have names.
Person joe = new Person("Joe");
Person bob = new Person("Bob");
Person jane = new Person("Jane");
Person z = new Person();
System.out.println(Person.numHumans() + " humans vs. " + Person.numZombies() + " zombies.");
z.bite(bob); // poor bob
bob.bite(z); // confused zombie
Person derpbie = bob;
derpbie.bite(derpbie); // this is getting ridiculous
System.out.println(Person.numHumans() + " humans vs. " + Person.numZombies() + " zombies.");
jane.shoot(z); // humans FTW!
jane.shoot(joe); // wait...what?
jane.shoot(jane); // oh...oh no.
System.out.println(Person.numHumans() + " humans vs. " + Person.numZombies() + " zombies.");
while (true) {
derpbie.bite(derpbie); // bored zombie is bored
}
Joe, ready for action! Bob, ready for action! Jane, ready for action! ...mmmm, brains. Oh noes, a zombie! 3 humans vs. 1 zombies. Bob is gnawed on by a zombie. The zombie formerly known as Bob bites a zombie, like an idiot. The zombie formerly known as Bob is hungry...and remembers he has an arm. Nomnomnomnom. ...etc...
Why does Object-Oriented Programming (OOP) exist?
Why is it useful? Why use OOP instead of making all code and data static?
Come up with two examples where it has utility.
Design a set of classes and objects to describe cards for the card game "War". Write "prototype" classes that include the classes, data in the classes, and methods, but do not provide an implementation of the methods.
Code written for one situation should be, within reason, reusable in other situations
Writing fairly generic classes enables this.
It is important to design your classes before writing your code. I've been doing this for you in homeworks.
Take your design from the previous Exercise.
Redesign your objects so that many of them can be reused between games
Other factors to consider: