CS代考 Objects Examples Class #2

Objects Examples Class #2
On page 2 of this sheet there are three classes, Avatar, Wizard and Adventurer. On this side is the class containing the main method and some additional static methods.
public class TestAvatars {
public static int numberActive(Avatar c1, Avatar c2,

Copyright By PowCoder代写 加微信 powcoder

Avatar c3,Avatar c4) { int number = 0;
if (c1.isActive()) number++; if (c2.isActive()) number++; if (c3.isActive()) number++; if (c4.isActive()) number++; return number;
public static String printAvatars(Avatar c1, Avatar c2, Avatar c3, Avatar c4) {
String s = “”;
s += c1 + “\n”;
s += c2 + “\n”;
s += c3 + “\n”;
s += c4 + “\n”;
return s;}
public static void main(String[] args) {
Avatar av1 = new Avatar(“Binky”);
Avatar av2 = new Avatar(“Flo”);
Wizard wiz1 = new Wizard(“Merlina”); Adventurer adv1 = new Adventurer(“Skyhawk”);
System.out.println(“The Avatars are originally:”); System.out.println(printAvatars(av1, av2, wiz1, adv1) +
int counter = 0;
while (counter < 4 & numberActive(av1, av2, wiz1, adv1) > 1)
{if (av1.isActive()) av1.castSpell(av2); if (av1.isActive()) av1.dig();
if (av2.isActive()) av2.castSpell(adv1); if (av2.isActive()) av2.dig();
if (wiz1.isActive()) wiz1.castSpell(adv1); if (wiz1.isActive()) wiz1.dig();
if (adv1.isActive()) adv1.takeAChance(); System.out.print(“The Avatars are now:\n”
counter++; }
+ printAvatars(av1, av2, wiz1, av1));

Study the Avatar class.
1. Why are there two constructors?
2. Notice the second constructor. What is the ¡°this¡± term referring to?
3. Read the castSpell method carefully. What does it do?
4. Notice that the parameter c refers to another Avatar object for which we can access the
Strength attribute directly (i.e. without using a getter/setter), even though this attribute is private. How is this possible?
Now read through the Wizard class.
5. How does it extend the Avatar class (i.e what is different between the two classes)?
6. Look carefully at the toString method. What is the reference to super allowing us to do
7. Now read through the Adventurer class. How does it extend the Avatar class?
8. Draw a class diagram for the three classes you have just looked at.
Now take a look at the main class, TestAvatars.
9. What does the numberActive method do?
10. What does the printAvatars method do? Notice that the parameters are all of type
Avatar, even if they might be of the other subtypes (Wizard or Adventurer) – how is that
11. Run through the program and see what happens. You can toss a coin to decide whether
the random number generated in the Adventurer¡¯s takeAChance method is ¡Ü 0.5 or >0.5.
12. How would you rewrite the main method so that the players randomly play one of their
available moves every round, until there is only one remaining active player?

public class Avatar { private String name;
private int strength; protected int gold;
public Avatar(String n, int s, int g) { name = n;
strength = s;
gold = g; }
public Avatar(String n) {
this(n, 8, 8); // default amount of strength and gold
// getters and setters omitted to save space – but they do exist!
public void dig() {
gold = gold + 1;
strength = strength – 1;
public void castSpell(Avatar c) { if (c.isActive()) {
c.strength = c.strength – 1;
gold = gold – 2;
strength = strength – 1;
public boolean isActive() {
if ((strength <= 0) || (gold <= 0)) return false; return true; public String toString() { return name + ", " + "strength = " + strength + ", gold = " + public class Wizard extends Avatar { private int magicStars; public Wizard(String n, int s, int g, int m) { super(n, s, g); magicStars = m;} public Wizard(String n) { super(n); magicStars = 10;} public void castSpell(Avatar c) { if (c.isActive()) { c.setStrength(c.getStrength() - 2); // does more harm setGold(getGold() - 2); magicStars = magicStars - 2; // but costs stars setStrength(getStrength() - 1); public String toString() { String s = super.toString(); return s + ", magic stars = " + magicStars; public class Adventurer extends Avatar { public Adventurer(String n, int s, int g) { super(n, s, g); public Adventurer(String n) { super(n); public void takeAChance(){ //either double or halve gold supply double random = Math.random(); if(random<0.5) { System.out.println("random number<0.5"); setGold(getGold()/2);} else {System.out.println("random number<0.5"); setGold(getGold()*2); 程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com