Java – War(the card came)

Posted in Examples, Programming on February 5, 2012 by Ben

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.
 

 

Comic 11 – Rough Part 2

Posted in Sketches on February 2, 2012 by Ben

Looking like this is gonna be a 3 part rough, cause I need to get started on some code here. But this is the continuation of last nights rough for what will hopefully become comic 11. Needs a lot of clean up and a few more panels if you can guess what this ones about you need to get out of my head..

 

Comic 11 Rough Part 1

Posted in Sketches on February 1, 2012 by Ben

Yeah so this might change I can’t decide if I like it or not, but this is part of the rough for comic 11.

Not doing to well this week…

Posted in News on January 31, 2012 by Ben

Not gonna be a post tonight, busy doing a web programming assignment, and trying out some of that DOTA 2 beta all these kids are talking about. I’m gonna try to get a couple extra posts up tomorrow, dunno if it’ll actually happen but regardless I haven’t given up. Just a little bogged down with work at the moment. If I don’t get some extra stuff posted tomorrow it will happen this weekend.

I have some ‘splaining to do

Posted in Sketches on January 30, 2012 by Ben

So I missed this weekend. Had a friend over friday night, family saturday night and a mad programming rush sunday night and so the sketch of the day took a back seat. So tonight I’m getting back in the game with a gesture. It was gonna be a full sketch but I’m having trouble getting it to look right. I might do some more with it or I might start working on a comic…we’ll see how much time I get to draw this week.

Blah

Posted in Sketches on January 26, 2012 by Ben

Coding all night…Here’s a random dragon..thing

 

Bugger…

Posted in Sketches on January 25, 2012 by Ben

Well I missed a day, it was bound to happen sooner or later. I had some stuff due for web programming and when I finished it I really wasn’t feeling the drawing wound up going to bed fairly early. Anyway for today’s sketch I just kept playing around with that doodle from earlier, I’m not really going for anything in particular and it probably shows. Don’t know if I’ll keep going with this one probably just gonna keep doing some gestures/posemaniacs this week as I have another project due Monday -_-

 

Gesture1

Posted in Sketches on January 23, 2012 by Ben

Today I’m just gonna post a gesture. This is gonna be a light week for the drawing. I just got a couple of projects assigned, and there’s more on the horizon, but I’m gonna try to keep going.

Fillion has a weird face…a weird gorgeous face

Posted in Sketches on January 22, 2012 by Ben

So Jason has to draw celebrities for his fancy masters degree. Naturally he chose to draw our lord and savior, Nathan Fillion. Unfortunately Nathan Fillion has what we in the business call “a weird fucking face” and Jason is having some trouble getting it just right. So being that he is a professional artist and I’m an amateur nothing, I decided to take a stab at this Fillion business…this is the result:

Java, Introduction Stuff

Posted in Computer Science, Programming on January 22, 2012 by Ben

Well, this weeks post is gonna be a quick introduction to Java. I’m doing courses in advanced object oriented programming and web programming this semester, so that’ll probably be the subject matter for the bulk of the posts the next 3 months or so. There’s some nice overlap between the two with Java Applets so I’ll start there. Before I get in to writing code I wanted to take a post to talk about some of the things that are different about Java as a programming language. I always hate it when examples and tutorials leave this shit out so here we go:

A lot of people would argue that Java is not a great language for teaching people how to program(I tend to agree). This is because Java does a lot of the low level management things for you. There are no explicit pointers in Java and no explicit memory management options. All of this is done for you. Java features automatic garbage collection which is basically just a thread running in the background that detects when objects are no longer in use and frees up the memory.

Your Java programs aren’t compiled into machine code, which means they aren’t executable without a Java Virtual Machine. Java programs compile into an intermediate step between source code and machine code called bytecode. The virtual machine reads the byte code instructions and executes them. As you might have guessed this makes Java slower than languages like C/C++.

Speaking of C/C++. If you’re familiar with the syntax then you’ll be happy to know that Java borrows a lot of it’s basic syntax from C++.

Finally, the reason you’re probably interested in Java in the first place, Java is fully object oriented. Java does contain primitive data types but that’s the end of it, pretty much everything else is an object. Every program you write is a class, though it doesn’t necessarily make use of all the properties of an object.

…and that’s what you need to know before we start writing code. So for the next post I’m going to be doing some Java Applets. Here’s a link to a few Java IDE’s. I personally use http://www.eclipse.org/http://netbeans.org/ is another good one, and finally http://www.jcreator.com/. Those are 3 of the most popular.