Wednesday, February 29, 2012

Lab 15: Comparable

For this lab you will implement two classes.

An Employee class with a int ssn property and a String name property. The class implements the Comparable interface by comparing the ssn of the two employees. You can learn more about Comparable by reading the case study in page 632 of your textbook.

You will also implement a Department class which maintains an array of Employees. It implements the methods
  • add(String name, int ssn) which adds a new employee to the department. You can assume that at most 10 employees will be in a department
  • toString() returns a string of all the people in the department but sorted by ssn.

For example, the sample code below generates some random employees and them prints them out:
public static void main(String[] args) {
  Department d = new Department();
  for (int i=0; i < Math.random() * 10;i++){
   int n = (int)(Math.random() * 100);
   String name = "John" + n;
   d.add(name, n);
  }
  System.out.println(d);
 }
Here is the output of one run. Notice how they are all sorted. Every time you run it the numbers will be random but will (should) appear sorted.
John3 3
John16 16
John49 49
John61 61
John92 92
Remember to turn in both files to the dropbox.cse.sc.edu.

Tuesday, February 28, 2012

Practice Condingbat Bonus Points

Once again I will give you a bonus test point for each of the following sections you finish on codingbat.com. The sections are: Array-1, Array-2, Array-3, String-3. These will have to be done before Wed, March 14 @noon.

Remember to add jmvidal@gmail.com as your 'teacher share', click on the 'prefs' link in the top of the page to set your 'teacher share'.

Homework Solutions

Here are some of my solutions for recent homeworks.

Monday, February 27, 2012

Lab 14: Undead

Everyone is a Person. But, as you know, this set is divided into the Living and the Undead.
  • The Civilian and the Slayer are are all Living.
  • Zombie, Vampire and Ghost all belong to the Undead
For this lab you will implement all these classes and in the given inheritance hierarchy. You will also implement the appropriate methods so that when you run this main on your program:
public static void main(String[] args) {
    Zombie z1 = new Zombie("Keith Richards");
    Vampire v1 = new Vampire("Angel");
    Ghost g1 = new Ghost("Bloody Mary");
    Civilian c1 = new Civilian("Rick Grimes");
    Civilian c2 = new Civilian("Glenn");
    Slayer s1 = new Slayer("Buffy Summers");
    Person[] people = {z1, v1, g1, c1, c2, s1};
    //Tell me, are you living or undead.
    for (Person p: people){
      p.alive();
    }
    System.out.println("-----------------------------");
    //OK now, say Hi
    for (Person p: people){
      p.sayHi();
    }
    System.out.println("------------------------------");
    Civilian c3 = new Civilian("Keith Richards");
    //a Person equals any other person with the same name
    System.out.println("Is Keith's zombie equal to Keith? " + z1.equals(c3));
    //except Ghosts, they are not equal to anything,
    //not even themselves.
    System.out.println("Aye you equal to yourself? " + g1.equals(g1));
    System.out.println("Are you equal to Rick? " + g1.equals(c1));
 }
you get the following output:
I am Undead.
I am Undead.
I am Undead.
I am Living.
I am Living.
I am Living.
-----------------------------
arrgh....braaaains.....
Hi, I am Angel, a vampire.
.........
Hello, I am Rick Grimes
Hello, I am Glenn
Buffy Summers here, just saving the world, again.
------------------------------
Is Keith's zombie equal to Keith? true
Aye you equal to yourself? false
Are you equal to Rick? false
Finally, your Person, Living and Undead classes should be abstract.

You will turn in all your .java files (remember, there is one for each class) at the dropbox.cse.sc.edu.


Homework 7: Camelot

The Thing class hierarchy.
For this thy sixth homework, which does appear after the fifth but before the seventh, you shall implement the simple game of Camelot. Camelot, you see, is a silly place where peasants pathetically move about insisting they are not dead yet while brave knights honor thy King by slashing them to bits.

You will implement the classes shown in in the figure, along with a Game class which holds the board description. The specific methods that each class will implement are explained in detail in the Javadoc documents for Camelot (which you must read and follow). Notice that the documentation tells you exactly which methods and which properties you need to implement for every class. The Animated class has a protected enum Direction {N,S,E,W}; which shows up as Animated.Direction in the javadocs.

The program will have a main in the Game class which looks like this:
public static void main(String[] args) {
    Scanner keyboard = new Scanner(System.in);
    Game g = new Game();
    g.add(new Sword(3,3));
    User user = new User(1,4);
    g.add(user);
    g.add(new Knight(5,5));
    g.add(new Peasant(9,5));
    g.add(new Peasant(6,7));
    g.add(new Peasant(4,8));
    g.add(new Peasant(7,2));
    g.add(new Peasant(3,5));
    String command = "";
    do {
      System.out.println(g); //print out the game board
      System.out.print("Your move:");
      command = keyboard.next();
      user.move(command); //move the user
      g.moveAll(); //move everyone
      g.resolveConflicts(); //resolve any conflicts between those in the same row,col
    } while (user.isAlive());
 }
A sample run of the program looks like:
__________
____Y_____
__________
___S_P____
________P_
_____K____
_______P__
__P_______
__________
_____P____

Your move:south
Knight moves S
Peasant moves E
Peasant too weak to move.
Peasant too weak to move.
Peasant too weak to move.
Peasant too weak to move.
Peasant too weak to move.
__________
__________
____Y_____
___S_P____
________P_
__________
_____K_P__
__P_______
__________
______P___

Your move:west
Knight moves W
Peasant moves S
Peasant too weak to move.
Peasant moves E
Peasant too weak to move.
Peasant moves N
Peasant too weak to move.
Peasant too weak to move.
Peasant too weak to move.
______P___
__________
___Y______
___S_P__P_
__________
__________
____K___P_
__P_______
__________
__________

Your move:south
Knight moves W
Peasant moves N
Peasant too weak to move.
Peasant moves S
Peasant too weak to move.
Peasant too weak to move.
Peasant moves E
Peasant too weak to move.
Peasant moves E
Peasant too weak to move.
User picks up Sword
__________
__________
__________
___Y__P_P_
__________
__________
___K______
___P____P_
__________
______P___

Your move:east
Knight moves W
Peasant too weak to move.
Peasant too weak to move.
Peasant moves E
Peasant too weak to move.
Peasant moves W
Peasant too weak to move.
Peasant too weak to move.
__________
__________
__________
____Y_P__P
__________
__________
__K_______
__P_____P_
__________
______P___

Your move:east
Knight moves S
Peasant too weak to move.
Peasant moves S
Peasant too weak to move.
Peasant moves W
Peasant too weak to move.
Peasant too weak to move.
Peasant moves S
Peasant too weak to move.
Bloodthristy Knight kills a P
__________
__________
__________
_____Y__P_
______P___
__________
__________
__K_______
________P_
______P___

Your move:south
Knight moves W
Peasant moves W
Peasant too weak to move.
Peasant moves W
Peasant too weak to move.
Peasant moves N
Peasant too weak to move.
Peasant moves S
Peasant too weak to move.
__________
__________
________P_
__________
_____Y____
______P___
__________
_K________
_______P__
_____P____

Your move:quit
Knight moves W
Peasant moves E
Peasant too weak to move.
Peasant moves S
Peasant too weak to move.
Peasant moves S
Peasant too weak to move.
Peasant too weak to move.

Basically, the user tells the User how to move (N,S,E,W). The knights move randomly (one of N,S,E,W) on each turn. The peasants flip a coin, if heads they stay put otherwise they move randomly (one ofN,S,E,W). The world is 10 by 10 and wraps around.

The resolveconflicts method is described in the javadocs. It goes over every Thing. If there is another Thing in the same row,col position then, if the Thing is a Knight it kills (removes) any Thing else there. If it is the user then if it finds the sword there it picks it up (thus killing it) and it is it a peasant it kills it.

I recommend you implement this program in the following order:
  1. The Thing hierarchy, start at the top and work your way down. Start with the properties, then the toString() methods, then the move() methods.
  2. The Game class, its properties and constructor.
  3. Game.toString(), test it.
  4. Game.add()
  5. Game.remove()
  6. Game.thingsAt()
  7. Game.moveAll(): this should just call move() on every thing.
  8. Finally, Game.removeConflicts()
This homework is due on Monday, March 12 @noon. Camelot, it is a silly place.


Interfaces and Abstract Classes

The last two topic from Chapter 8, which is the last Chapter we will cover before Test 2, are Interfaces and Abstract classes. We will cover these on lecture Monday and the rest of the lectures until the test we will be doing practice exercises.








Also, I just found this set of Java tutorials which are very nicely done, and cover a lot of the same material we are covering.

Friday, February 24, 2012

Some Lab Solutions

Lingxi reminds me that it is time I post my solutions to the labs, so here they are.

I don't have a solution for lab 11. If you have a good solution to it, feel free to post a link to it in the comments.

More Array Examples

Below is an example of a more complicated program using arrays; I write a short program to print out a row from Pascal's triangle. As I mention in the video, this video was inspired by a blog post on good interview questions for developers. Some employers feel that asking candidates to write this program on a whiteboard is a good way to filter out those who can code from those who cannot. I can say that this would be a great question for the final in this class (well, not anymore).


Wednesday, February 22, 2012

Lab 13: Connect Four


For this lob you will implement a simple text-based version of the Connect Four game.

The Connect Four board has 6 rows and 7 columns. Your program will display a text-based board and then ask the user to enter the column on which to drop a piece. The program will then show the new board with the piece dropped in the appropriate place. The colors (players) will alternate. You do not have to check to see if one of them has won (but, it would be very good practice for the test!).

Below is a sample interaction with the program:
0 1 2 3 4 5 6 
_ _ _ _ _ _ _ 
_ _ _ _ _ _ _ 
_ _ _ _ _ _ _ 
_ _ _ _ _ _ _ 
_ _ _ _ _ _ _ 
_ _ _ _ _ _ _ 

Enter column for B's move:0
0 1 2 3 4 5 6 
_ _ _ _ _ _ _ 
_ _ _ _ _ _ _ 
_ _ _ _ _ _ _ 
_ _ _ _ _ _ _ 
_ _ _ _ _ _ _ 
B _ _ _ _ _ _ 

Enter column for R's move:0
0 1 2 3 4 5 6 
_ _ _ _ _ _ _ 
_ _ _ _ _ _ _ 
_ _ _ _ _ _ _ 
_ _ _ _ _ _ _ 
R _ _ _ _ _ _ 
B _ _ _ _ _ _ 

Enter column for B's move:0
0 1 2 3 4 5 6 
_ _ _ _ _ _ _ 
_ _ _ _ _ _ _ 
_ _ _ _ _ _ _ 
B _ _ _ _ _ _ 
R _ _ _ _ _ _ 
B _ _ _ _ _ _ 

Enter column for R's move:1
0 1 2 3 4 5 6 
_ _ _ _ _ _ _ 
_ _ _ _ _ _ _ 
_ _ _ _ _ _ _ 
B _ _ _ _ _ _ 
R _ _ _ _ _ _ 
B R _ _ _ _ _ 

Enter column for B's move:6
0 1 2 3 4 5 6 
_ _ _ _ _ _ _ 
_ _ _ _ _ _ _ 
_ _ _ _ _ _ _ 
B _ _ _ _ _ _ 
R _ _ _ _ _ _ 
B R _ _ _ _ B 

Enter column for R's move:6
0 1 2 3 4 5 6 
_ _ _ _ _ _ _ 
_ _ _ _ _ _ _ 
_ _ _ _ _ _ _ 
B _ _ _ _ _ _ 
R _ _ _ _ _ R 
B R _ _ _ _ B 

Enter column for B's move:


You will implement a Board class which holds the state of the board using a 2-dimensional array. This class should have a toString method and a drop method which handles the dropping of a new piece on the board.

As always, turn it in on the dropbox.cse.sc.edu.



Insertion Sort

Below is a video about the insertion sort, which is different from the selection sort I talked about in class today, but not by much. They are very similar.