Skip to content

Instantly share code, notes, and snippets.

@skihero
Created February 23, 2011 07:54
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save skihero/840150 to your computer and use it in GitHub Desktop.
Save skihero/840150 to your computer and use it in GitHub Desktop.
/*
* Creating classes to learn a bit about java
*/
/**
* Super hero classes
*/
/**
* All our super heros belong to this class
*/
class Hero {
private Costume hero_cs ;
private Power hero_pwr;
private int alive ;
/* Create the hero */
Hero() {
this.hero_cs = new Costume() ;
this.hero_pwr = new Power() ;
this.alive = 1 ; // The hero lives
}
/* Change the costume here */
public void changeCostume (Costume hero_cs ) {
this.hero_cs = hero_cs ;
}
/* Change the power here */
public void changePower(Power hero_pwr) {
this.hero_pwr = hero_pwr;
}
/* Display the Stat here */
public void displayStat() {
hero_cs.display() ;
hero_pwr.display() ;
}
public void takeDamage(String severity) {
if(alive == 1 ) {
try {
hero_pwr.takeDamage(severity) ;
} catch (Exception e ) {
alive = 0 ; // KO
}
}
}
}
/**
* Our superHero Jax extends Hero
*/
class Jax extends Hero {
}
/**
* Our superHero Daxter extends Hero
*/
class Daxter extends Hero {
}
/**
* The costume our player wears
*/
class Costume {
private String color = "red" ;
private String highlights = "blue" ;
public void display() {
System.out.println("I'm wearing "
+ color
+ " and "
+ highlights
+ "highlights " );
}
/* Change the costumes here */
/* Throws TooMuchPinkException if you assign pink here */
public Costume change(String new_color , String new_highlights ) {
if(new_color == "pink"
|| new_color == "Pink" ){
throws new Exception("Too much pink " );
}
this.color = new_color ;
this.highlights = new_highlights ;
return this ;
}
}
class Power {
private int health ;
private int med_packs ;
Power() {
this.health = 100 ;
this.med_packs = 5 ;
}
Power(int init_health, int init_med_packs) {
if ( init_health > 100
|| init_med_packs > 10 ) {
/* Throw some exception */
}
}
public void display() {
System.out.println("Life "
+ health
+ "Med "
+ med_packs ) ;
}
/*
private int isAlive(){
int alive = 1 ; // Assume alive
if(health == 0 && med_packs == 0 )
alive = 0 ; // Declare dead
return alive ; // Return state
}
*/
public void takeDamage(String severity) throws Exception{
int damage ;
int effectiveDamage;
/*
switch(severity) {
case "hell" : damage = 5 ; break;
case "serious" : damage = 3 ; break;
case "minor" : damage = 2 ; break;
default: damage = 1 ;
} */
health -= damage ;
if ( health < 1 )
health += med_packs ; // use medpack
if ( health < 1 ) // if still negative
throw new Exception("dead" ) ;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment