Object Oriented Design 2

Object Oriented DesignIn the previous part of my Object Oriented Design Tutorial, I showed you how to build a Use Case, Object Model, Sequence Diagram and Class Diagram from scratch.

In this tutorial, I show you how to turn those diagrams into code and a working program. This is the process a person goes through to create excellent Object Oriented Designs. The code follows the video along with the digrams from last time.

If you like topics like this, it helps to tell Google with a click here

If you know anyone interested in Object Oriented Design, feel free to share

Code from the Video

COIN.JAVA

public class Coin {
	
	private String coinOption = "";
	public String[] coinValue = {"Heads", "Tails"};
	
	Coin(){
		
		// A random value of 0 or 1 is calculated
		// The value of coinOption is set based on
		// the random index chosen from coinValue[]
		
		int randNum = (Math.random() < 0.5)?0:1;
		coinOption = coinValue[randNum];
		
	}
	
	public String getCoinOption(){ return coinOption; }

}

PLAYER.JAVA

public class Player {
	private String name = "";
	private String coinOption = "";
	
	public String[] coinValue = {"Heads", "Tails"};
	
	Player(String newName){
		name = newName;
	}
	
	public String getCoinOption(){ return coinOption; }
	
	// Set coinOption to the opposite of what is sent
	
	public void setCoinOption(String opponentFlip){
		
		coinOption = (opponentFlip == "Heads")?"Tails":"Heads";
		
	}
	
	public String getRandCoinOption(){
		
		// Get a random 0 or 1
		
		int randNum = (Math.random() < 0.5)?0:1;
		
		// Set the value based on the index chosen at random
		// for the array coinValue which will be either
		// Heads or Tails
		
		coinOption = coinValue[randNum];
		
		return coinValue[randNum];
		
	}
	
	public void didPlayerWin(String winningFlip){
		
		if(coinOption == winningFlip){
			
			System.out.println(name + " won with a flip of " + coinOption);
			
		} else {
			
			System.out.println(name + " lost with a flip of " + coinOption);
			
		}
		
	}

}

COINGAME.JAVA

public class CoinGame {
	
	Player[] players = new Player[2];
	
	Coin theCoin = new Coin();
	
	CoinGame(String player1Name, String player2Name){
		
		players[0] = new Player(player1Name);
		players[1] = new Player(player2Name);
		
	}
	
	public void startGame(){
		
		// Pick a random player to choose the face value guess
		
		int randIndex = (Math.random() < 0.5)?0:1;
		String playersPick = players[randIndex].getRandCoinOption();
		
		// Set the opponents coinOption to the opposite value
		
		int opponentsIndex = (randIndex == 0)?1:0;
		players[opponentsIndex].setCoinOption(playersPick);
		
		// Flip the coin to find the winning side
		
		String winningFlip = theCoin.getCoinOption();
		
		// See the results of the flip
		
		players[0].didPlayerWin(winningFlip);
		
		players[1].didPlayerWin(winningFlip);
		
	}

}

COINFLIPPINGGAME.JAVA

import java.util.Scanner;

public class CoinFlippingGame {

	public static void main(String[] args){
		
		// Create a coin game with the 2 players provided
		
		CoinGame theCoinGame = new CoinGame("Mark", "Tom");
		
		String usersAnswer;
		
		do {
			
			theCoinGame.startGame();
			
			System.out.println("Play Again? ");
			
			Scanner playGameAgain = new Scanner(System.in);
			
			usersAnswer = playGameAgain.nextLine();
			
		} while ((usersAnswer.startsWith("y")) || (usersAnswer.startsWith("Y")));
		
	}
	
}

Object Oriented Design Diagrams

Click on the image below a few times to see it full screen

Object Oriented Design

18 Responses to “Object Oriented Design 2”

  1. Dick Stokkel says:

    hi derek, too bad you switched to java, i really liked your video’s

  2. captiva says:

    I’d like to have it on PHP. But you’re videos were great. Thanks for sharing.

  3. Jon says:

    Great video series (as they all are)! One slight bug in the code is that it appears the coin is only flipped one time instead of once per game. (Random coin flip occurs in Coin’s constructor and a Coin object is only created once.) Still, I wish I could write virtually bug-free code off the top of my head as you can!

    • Derek Banas says:

      Thank you very much 🙂 I’ll look into that bug and thank you for pointing it out. Sometimes I focus so much on the core subject that I let little silly things slip. Sorry about that

  4. Raj says:

    The code in the video had players[0] and player[0] in CoinGame constructor instead of player[0] and player[1]. The code is correct in the hand out.

    Also the code ran correctly in video, so you may have edited it. Just an observation.

    As always awesome video.

  5. Niki says:

    I just completed this. I tried to write the codes on my own using the SD/CD before looking at yours. It was fun! <3
    Niki

  6. Marc says:

    Hello Derek,

    thank you so much for the effort of putting those videos together! I’m in the process of learning Java right now and your tutorials are making it so much easier to digest. Awesome work!

    Marc

  7. Raj says:

    Hello Mr. Banas,
    I have a question to you which I was asked in an informal interview by a professor from NASA, he asked me, “can you write a compiler that takes your C# code and compiles it” (I suppose he meant without .Net framework or JVM and JDK?) . I was clueless and so much ambressed. I am just a university student not a great developer. I want to know is it possible that I can write my own compiler for my java or C# code? if yes please recommend any books for guidance. Humble thanks

    Raj

    • Derek Banas says:

      Hello Raj,

      Yes you could, but it wouldn’t be that easy. You’d just convert the C# code into the native assembly language of the host computer that runs it. Engineering a Compiler is a pretty good book on compilers.

Leave a Reply

Your email address will not be published.

Google+