In 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
hi derek, too bad you switched to java, i really liked your video’s
What language did you prefer? I’m constantly planning for the future
C++ would be great, if you can do it.
Thanks.
That tutorial is coming in the games tutorials. I’ll have to cover C first though
I’d like to have it on PHP. But you’re videos were great. Thanks for sharing.
You’re very welcome 🙂 I’ll cover php in detail after I finish with Java
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!
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
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.
Sorry about that. Yes I do these tutorials out of my head so on occasion a little error slips in.
the string winningFlip in class CoinGame doesn’t change, so I moved line 5 inside startGame method.
Is that correct?
Sorry about that error
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
That is the perfect way to use this tutorial. I’m glad you enjoyed it 🙂
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
Hello Marc, I’m very happy that they are helping 🙂 Many more videos are coming.
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
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.