codetoad.com
  ASP Shopping CartForum & BBS
  - all for $20 from CodeToad Plus!
  
  Home || ASP | ASP.Net | C++/C# | DHTML | HTML | Java | Javascript | Perl | VB | XML || CodeToad Plus! || Forums || RAM 
Search Site:
Search Forums:
  Inheritance  sidewayson2wheels at 10:08 on Tuesday, February 14, 2006
 

I need to create the following 4 programs but don't know where to start.

Java Programming 2006
Course Code: ELE-201
Coursework 5
Inheritance
1. Create a superclass called Monster.java.
Every monster should have a name.
This class define two methods called attack and move.
i. The generic monster attack method should return a random int between 1 and 5. It
should print to the console "NAME who is of class CLASSTYPE attacks
generically - x points damage", where NAME is the name of the monster,
CLASSTYPE is class of the monster (this.getClass()) and x is the random int.
ii. The generic move method should be defined as follows:
public void move(int direction){
switch(direction){
case 0:
System.out.println(this.name + " moving 1 step NORTH.");
break;
case 1:
System.out.println(this.name + " moving 1 step EAST.");
break;
case 2:
System.out.println(this.name + " moving 1 step SOUTH.");
break;
default:
System.out.println(this.name + " moving 1 step WEST.");
break;
}
}
2. A dragon is a type of monster that attacks by breathing fire or clawing (generic attack). A dragon will
attack by breathing fire 30% of the time and generically the rest of the time*. When it attacks by
breathing fire, it does 1 to 50 points of damage (and prints to the console a statement, like the generic
attack, that indicates name, class type, attack type and damage done).
* Remember you access a superclass with the super keyword.
Name your program Dragon.java
3. A troll is a kind of monster. A trolls can't be named Shirley or Sheila. If the user attempts to misname a
troll your program should print an error message and name the troll John.
Name your program Troll.java

Can someone please start me off in the right direction.

  Re: Inheritance  crwood at 20:00 on Wednesday, February 15, 2006
 

Every monster should have a name.
This is an instance/member variable in the Monster class.
i. The generic monster attack method should return a random int between 1 and 5. It
should print to the console "NAME who is of class CLASSTYPE attacks
generically - x points damage", where NAME is the name of the monster


public int attack() {
// get random int
// print given statement
// return the int
}

ii. The generic move method should be defined as follows:
A freebie: copy and paste.
A start:

import java.util.Random;
import javax.swing.JOptionPane;

public class MonsterTest {
public static void main(String[] args) {
String name = JOptionPane.showInputDialog(null, "enter a name");
Dragon dragon = new Dragon(name);
name = JOptionPane.showInputDialog(null, "enter a name");
Troll troll = new Troll(name);
int n = dragon.attack();
troll.move(n);
n = troll.attack();
dragon.move(n);
}
}

class Monster {
String name;
Random seed;
final int MAX = 5;

protected Monster(String name) {
this.name = name;
seed = new Random();
}

public int attack() {
int x = seed.nextInt(MAX + 1);
System.out.println(name + " who is of class " +
this.getClass() + " attacks generically - " +
x + " points damage");
return x;
}

public void move(int direction) {
switch(direction) {
case 0:
System.out.println(this.name + " moving 1 step NORTH.");
break;
case 1:
System.out.println(this.name + " moving 1 step EAST.");
break;
case 2:
System.out.println(this.name + " moving 1 step SOUTH.");
break;
default:
System.out.println(this.name + " moving 1 step WEST.");
//break;
}
}
}

class Dragon extends Monster {
public Dragon(String name) {
super(name);
}
}

class Troll extends Monster {
public Troll(String name) {
super(name);
}
}









CodeToad Experts

Can't find the answer?
Our Site experts are answering questions for free in the CodeToad forums








Recent Forum Threads
•  Digital.Canal.Software.V2013
•  Altair_HyperWorks_12.0.1_Win64
•  Global.mapper.V15.0
•  ESI.PAM-RTM.V2010.0
•  KISSsoft.03.2013E.SP5
•  Technical.Toolboxes.Pipeline.Toolbox.2013.Enterprise.v15.0.0
•  Simprotek.Simprosys.V3.0
•  Ricardo.SABR.V6.0p1
•  PSIM.V9.2


Recent Articles
ASP GetTempName
Decode and Encode UTF-8
ASP GetFile
ASP FolderExists
ASP FileExists
ASP OpenTextFile
ASP FilesystemObject
ASP CreateFolder
ASP CreateTextFile
Javascript Get Selected Text


© Copyright codetoad.com 2001-2013