Sie sind auf Seite 1von 16

NerdWars

A multi-stage program with Objects


Inheritance
• For any super class, you can define a subclass
that inherits all data fields and concrete
methods (except constructors).
– The subclass need only define a constructor, and
anything new that makes it different from the
base class
– You can create an entire hierarchy of inheritance
Stage 2 – abstract Warrior
and subclasses
• The Warrior need only define those things that are
common between all social cliques.
– name, strength and IQ data fields
– toString(), getName(), getStrength(), getIQ() methods
• Define 5 sublcasses for each of the cliques, which
need only define those things that make them
different from one another.
– constructor
– generateStats() method
– victory() method (describes how the Warrior won the fight)
Warrior
data fields: name, strength, IQ
methods: toString(), getStrength()
getIQ(), getName()

Nerd Jock Prep Thug Freak


generateStats() generateStats() generateStats() generateStats() generateStats()
victory() victory() victory() victory() victory()

Every Warrior needs to have generateStats() and victory() defined for them.
Shouldn’t they be defined in the base class Warrior as well?
Warrior
data fields: name, strength, IQ
methods: toString(), getStrength()
getIQ()

Nerd Jock Prep Thug Freak


generateStats() generateStats() generateStats() generateStats() generateStats()
victory() victory() victory() victory() victory()

Every Warrior needs to have generateStats() and victory() defined for them.
Shouldn’t they be defined in the base class Warrior as well? YES
How do you generateStats for a Warrior if you don’t know what clique they are?
Warrior
data fields: name, strength, IQ
methods: toString(), getStrength()
getIQ()

Nerd Jock Prep Thug Freak


generateStats() generateStats() generateStats() generateStats() generateStats()
victory() victory() victory() victory() victory()

Every Warrior needs to have generateStats() and victory() defined for them.
Shouldn’t they be defined in the base class Warrior as well? YES
How do you generateStats for a Warrior if you don’t know what clique they are?
I don’t know. Make them abstract for Warrior.
The Nerd subclass
public class Nerd extends Warrior
{
public Nerd(String x) the constructor for each subclass need
{ only be sent the name, and
super(x, “nerd”); pass that name and the clique on to the
} constructor for the base class Warrior
public void generateStats()
{
strength = (int)(Math.random()*61) + 20; //20-80
IQ = (int)(Math.random()*61) + 120; //120-180
}
public void victory()
{
System.out.println(name + “ dispatches their opponent with a combo Jedi mind trick
and Vulcan neck pinch!”);
}
}
public, private and protected
• For the Nerd subclass, we inherit the name,
strength and IQ data fields, but can not access
them if they are declared as private in Warrior.
so, make them protected in Warrior
public – can be accessed or modified from any
program in the same folder or project.
private – can only be accessed or modified from
within the class it is defined.
protected – can only be accessed or modified from
within the class it is defined, or any subclass of that
class (protected within the family).
Abstract methods and classes
• Abstract method: a method header with no code body.
public abstract void generateStats();
All we know about it is: the return type (if any), the name,
and any needed arguments.
• Abstract class: a class definition that contains at least one
abstract method.
– The first concrete subclass of an abstract class must define
the abstract method concretely (give it a code body).
– You can not create an instance of an abstract object
– The abstract super class contains all of the code that is
common between the concrete base classes.
abstract Warrior
data fields: name, strength, IQ
methods: toString(), getStrength()
getIQ() , getName(),
abstract void generateStats();
abstract void victory();

Nerd Jock Prep Thug Freak


generateStats() generateStats() generateStats() generateStats() generateStats()
victory() victory() victory() victory() victory()

By making Warrior abstract, with abstract methods generateStats() and victory(),


it is a guarantee that all subclasses MUST have those methods defined concretely.
Stage 2 – the driver program
• Consider that you can now do the following method:
public static void fight(Warrior x, Warrior y)
{ Since Warrior is abstract and has an abstract
if(x.getIQ() > y.getIQ() + 20) method called victory, we KNOW that for
{ whatever subclass x is, it MUST have a
x.victory(); method called void victory()
System.out.println(x.getName() + “ outsmarts “ + y.getName());
}
//code here for the other possibilities
}
Stage 2 – the driver program
• Now we need to read in from a file every
student’s name in the class and their social
clique.
– See how many lines there are in the file (size)
– Create an array of Warriors with (size/2) elements
• The file has 2 lines for each warrior with their name on
one line, followed by the clique on the next line
– Read in from the file to fill up the array of Warriors
//this method counts the number of lines in a file
public static int getFileSize(String fileName) throws IOException
{
Scanner input = new Scanner(new FileReader(fileName));
int size=0;
while (input.hasNextLine()) //while there is another line in the file
{
size++; //add to the size
input.nextLine(); //go to the next line in the file
}
input.close(); //always close the files when you are done
return size;
}
We will need this if we want to read in the contents of a file into an array
because we have to know how big the array should be to create it.
//this method will fill an array of Strings with each line from a file
public static void readFile(String[] words, String fName)throws IOException
{
Scanner input = new Scanner(new FileReader(fName));
int i=0; //index for placement in the array
String line;
while (input.hasNextLine()) //while there is another line in the file
{
line=input.nextLine(); //read in the next line and store it
words[i]= line; //add the line into the array
i++; //advance the index of the array
}
input.close();
}
This needs to be modified to read in to an array of Warriors as opposed to an
array of String.
//this method will fill an array of Warriors from a file
public static void readFile2(Warrior[]words,String fName)throws IOException
{
Scanner input = new Scanner(new FileReader(fName));
int i=0; //index for placement in the array
String name, clique;
while (input.hasNextLine()) //while there is another line in the file
{
name=input.nextLine(); //read in the name and clique
clique=input.nextLine(); //assuming there is another line
if(clique.equals(“nerd”))
words[i]= new Nerd(name); //add the Warrior into the array
else
if(clique.equals(“jock”))
words[i]= new Jock(name); //add the Warrior into the array
//else…continue for all of the other subclasses of Warrior
i++; //advance the index of the array
}
input.close();
}
Stage 2 – the driver program
• Create the array of Warriors
• Read in from the file to fill the array
• Pick two Warriors from the array at random
• Report who wins the fight
int size = getFileSize(“nwars2.txt”);
Warrior[]arena = new Warrior[size/2];
readFile2(arena, “nwars2.txt”);
//code to pick two random indexes from arena, x and y
fight(arena[x], arena[y]);

Das könnte Ihnen auch gefallen