If you check this regularly you’ll know that things got a bit busy for me this week. I haven’t had a lot of time to prepare a solid tutorial or notes. So for this week as I sit here contemplating whether or not I’ve consumed enough Chinese hot sauce to kill myself and watching the super bowl I’m going to post and analyze one of my assignments that was due last week. It was written quickly, and is very simple with a basic interface and no real error handling, but I have checked my code and fixed mistakes that were pointed out to me and it’s a nice little example of some of the basics of object oriented programming in Java.
Last week I was assigned to create a program to play the card game of War. To simplify the program a bit we were told not to handle ties by redrawing but instead just to randomly select a winner of the tie. The program has 4 classes. A Card class, a CardGroup class, a CardTest class, and Assign1 which is the main class for the game.
I certainly can’t claim that the way I wrote this was the best or most efficient but I feel like it does a decent enough job of demonstrating some of the basic concepts. You can find the compiled .class files and the source code .java files here: War
If you’re new to object oriented programming there’s some things in here you’ll want to take note of:
(1) What is a class? Well in my own informal definition a class is basically a collection of methods(functions, procedures etc) and variables used to encapsulate data. In this example the Card class can be Instantiated as an Object that represents a playing card. When you instantiate a class you declare an Instance of it, usually that instance has it’s own copies of the variables and contains it’s own data. For example Card1, and Card2 may contain different data but they are both objects of the card class. Confusing? Yeah it is a bit. With practice you can see why this whole technique of object oriented programming is beneficial in a lot of cases and preferable to a functional approach.
(2) Constructors what is a constructor? You can wiki it, in another of my informal definitions constructors are basically the methods that initialize your object. They can be Overloaded which is the term for creating multiple methods with the same name and different parameters and the appropriate method is selected by the compiler depending on what parameters are given. Here are some examples of the constructors from my Card class:
public Card(){
//Constructor with no parameters
faceValue = '0';
suit = '0';
}
public Card(char face, char suitValue){
//Constructor with 2 parameters
faceValue = face;
suit = suitValue;
}
public Card(Card copyCard){
//Copy Constructor
faceValue = copyCard.faceValue;
suit = copyCard.suit;
}
(3) Getters & Setters basically these are your methods that get and set the instance variables. The reason you need to create methods to do this is because the instance variables in this case are private. That means they can only be accessed from within the class. In order for the programmer to access these variables he needs to do it through the methods that you have defined. You can probably see some benefits to this, mostly to make sure the programmer actually means to be accessing these variables. Getters and Setters are so standard that some languages like Objective-C will create them for you. Here’s an example of Getters and Setters within the Card class:
public char getFaceValue(){
//Returns the face value
return faceValue;
}
public char getSuit(){
//Returns the suit
return suit;
}
public void setFaceValue(char f){
//Sets the facevalue of this card
faceValue = f;
}
public void setSuit(char s){
//Sets the suit of this card
suit = s;
}
That’s really all I have for you this week. The game does have a problem where it never ends if I ever come back to this game(which is unlikely) I’d go ahead and make it play the game correctly, I suspect the problem is caused by the way ties are handled.






