StarWars.java Questions
Instructions
- With the left mouse button pressed, drag the mouse
across the contents of the program listing that is shown
below. This will select the program listing. Then
choose Copy from the Edit menu of your web
browser to copy the program listing to the clipboard.
- Start your text editor and create a new file called
StarWars.java. Choose Paste to paste the contents of
the program listing from the clipboard into the new file.
- Save the file to disk and begin answering the
questions that are shown below.
- When you need to look at the model answer, click here.
- Click here to go back to the
tutorials menu.
Program listing
|
class XWing
{
private int shields;
private int weapon;
private boolean dead;
public XWing()
{
shields = 1000;
weapon = 10;
}
public int getWeapon()
{
return weapon;
}
public boolean isDead()
{
return dead;
}
public void hit(int damage)
{
shields = shields - damage;
if (shields < 0)
{
System.out.println("Boom!!!");
dead = true;
}
}
}
class TieFighter {
private int shields;
private int weapon;
private boolean dead;
public TieFighter()
{
shields = 500;
weapon = 20;
}
public int getWeapon()
{
return weapon;
}
public boolean isDead()
{
return dead;
}
public void hit(int damage)
{
shields = shields - damage;
if (shields < 0)
{
System.out.println("Boom!!!");
dead = true;
}
}
}
class StarWars {
private void duel(XWing x, TieFighter t)
{
for (;;)
{
x.hit(t.getWeapon());
if (x.isDead())
{
System.out.println("X-Wing is dead");
break;
}
t.hit(x.getWeapon());
if (t.isDead())
{
System.out.println("Tie Fighter is dead");
break;
}
}
}
private void battle(XWing[] good, TieFighter[] evil)
{
int g = 0;
int e = 0;
int goodDeaths = 0;
int evilDeaths = 0;
while (g < good.length && e < evil.length)
{
System.out.println("battling X-Wing #" + g +
" versus Tie Fighter #" + e);
duel(good[g],evil[e]);
if (good[g].isDead())
{
g++;
goodDeaths++;
}
if (evil[e].isDead())
{
e++;
evilDeaths++;
}
}
int finalGood = good.length - goodDeaths;
int finalEvil = evil.length - evilDeaths;
System.out.println();
System.out.println("Battle Report:\t\tX-Wings\t\tTie Fighters");
System.out.println("------------------------------------------------------");
System.out.println();
System.out.println("Initial ships:\t\t" + good.length + "\t\t" + evil.length);
System.out.println();
System.out.println("Killed ships:\t\t" + goodDeaths + "\t\t" + evilDeaths);
System.out.println();
System.out.println("Final ships:\t\t" + finalGood + "\t\t" + finalEvil);
System.out.println();
if (finalGood > finalEvil)
{
System.out.println("The rebel alliance is victorious!");
}
else
{
System.out.println("The dark side has conquered!");
}
System.out.println();
}
public void doStuff()
{
XWing[] goodies = new XWing[3];
for (int i=0; i<goodies.length; i++)
{
goodies[i] = new XWing();
}
TieFighter[] baddies = new TieFighter[3];
for (int i=0; i<baddies.length; i++)
{
baddies[i] = new TieFighter();
}
battle(goodies,baddies);
}
public static void main(String[] args)
{
StarWars me = new StarWars();
me.doStuff();
}
}