OOP help in understanding
For a Reddit post asking about OOP. Rewrote large chunks in order to show OOP's usefulness
Posted in Java, on January 3, 2013 at 17:01
Posted in Java, on January 3, 2013 at 17:01
package project2;
import java.util.Scanner;
/*
For this snippet of code, the Unit is the base class for Dragon and Blob. Since the Dragon and Blob share some methods (isDead and takeDamage), you put these into the Unit class.
Unit myDragon = new Dragon();
Unit myBlob = new Blob();
myDragon.takeDamage(100);
myBlob.takeDamage(250);
*/
public class OOPExample {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("You have come across a dragon. What do you want to do? (fight or run)");
Unit enemy = new Dragon();
Unit player = new Player();
runInteraction(scan, player, enemy);
//Now, lets say the player now comes across a blob! Oh no!!
//But the player hasn't gotten a chance to rest.. this will be interesting
enemy = new Blob();
runInteraction(scan, player, enemy);
}
private static void runInteraction(Scanner scan, Unit player, Unit enemy){
String userAction;
while(!enemy.isDead() && !player.isDead()){
userAction = scan.nextLine();
if(userAction.equals("run")){
System.out.println("You have run away from the dragon.");
break;
}
enemy.takeDamage(player.attack());
if(enemy.isDead()){
System.out.println("You have defeated the dragon!");
break;
}
//Take advantage of the abstract method!
System.out.println(enemy.getUnitName() + " HP: " + enemy.health);
//Now the enemy attacks the player
player.takeDamage(enemy.attack());
if(player.isDead()){
System.out.println("You have been defeated!");
break;
}
System.out.println("Your HP: " + player.health);
System.out.println("What do you want to do? (fight or run)");
}
}
}
abstract class Unit {
protected int health; // Health of the Unit
protected int maxDamage; //Tells the maximum damage the Unit can inflict
public Unit(int health, int maxDamage) {
this.health = health; // Set the Health of the unit
this.maxDamage = maxDamage;
}
//This requires all classes that extend Unit to include this method
public abstract String getUnitName();
//Returns the attack value
public int attack(){
return (int) (Math.random() * maxDamage);
}
// Apply damage to this unit
public void takeDamage(int damage) {
this.health -= damage;
}
// Check to see if it's dead!
public boolean isDead() {
return this.health <= 0;
}
}
// Now, for the OOP!
class Dragon extends Unit {
private boolean flying;
public Dragon() {
super(100, 50); // Give the Dragon 250 health
flying = true;
}
public boolean isFlying() {
return flying;
}
public String getUnitName(){
return "Dragon";
}
}
class Blob extends Unit {
public Blob() {
super(500, 10); // Give the Blob 500 health!
}
public String getUnitName(){
return "Blob";
}
}
class Player extends Unit{
public Player(){
super(400, 30); //Gives the player 400 health
}
public String getUnitName(){
return "Player";
}
}
Share this code
Use the link below to share the code:
http://www.codesend.com/view/0506ba483b86f2ad2ed13c8ab43e80a2/
HTML
<a href="http://www.codesend.com/view/0506ba483b86f2ad2ed13c8ab43e80a2/">OOP help in understanding</a>
BBCode
[url=http://www.codesend.com/view/0506ba483b86f2ad2ed13c8ab43e80a2/]OOP help in understanding[/url]
© 2010 CodeSend.com - send code quick and easy
Syntax highlighting by Alex Gorbatchev
Syntax highlighting by Alex Gorbatchev
