HOMEWORD Reference - W2LV

Content requirements

( Building on the previous assignment )

  • Added Wolf, Bear, Dragon and other animals that can be killed by warriors to gain experience points. It is recommended that their experience points be 8, 20, and 100 in order
  • You should make sure that the XP you can get from killing each animal is defined by the specific animal object, not in the warrior class

Knowledge points or precautions

  • Variables (attributes) are defined in appropriate classes, such as killing a wolf to get 8 experience points, this value should be provided by the wolf object, not by the bear or dragon, let alone by the warrior class
  • Inheritance, parent and child classes

The following is a reference implementation, executed from the Run class

class Warrior {

	private int exp;// Unless there is a good reason, member variables must be declared private
	private int level;

	Warrior() {
		// Recommended practice, initialize each member variable reasonably in the constructor
		exp = 0;
		level = 1;
	}

	void hunt(Animal animal) {
		int expFromRabbit = animal.getExp();// Experience gained from killing a rabbit
		int newestExp = exp + expFromRabbit;// The latest experience value value (note that the exp variable has not been written)
		int newestLevel = expToLevel(newestExp);// The latest level value (note that the level variable has not been written)

		exp = newestExp;
		if (newestLevel > level) {
			// updated
			level = newestLevel;
			System.out.println("Warrior upgrade, level" + level);
		}
	}

	int expToLevel(int exp) {
		int lv = 1;
		int e = 10;

		while (exp >= e) {
			lv = lv + 1;
			int v = lv;
			int x = 1;
			while (v > 1) {
				v = v - 1;
				x = x * 2;
			}
			e = (x * 2 - 1) * 10;
		}
		return lv;
	}

	int getExp() {
		return exp;
	}
}

 

class Animal {
	protected int exp;

	int getExp() {
		return exp;
	}
}

 

class Rabbit extends Animal {
	Rabbit() {
		exp = 3;
	}
}

 

class Wolf extends Animal {
	public Wolf() {
		exp = 8;
	}
}

 

class Bear extends Animal {
	public Bear() {
		exp = 20;
	}
}

 

class Dragon extends Animal {
	public Dragon() {
		exp = 100;
	}
}

 

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

class Run {
	private static final BufferedReader R = new BufferedReader(new InputStreamReader(System.in));

	public static void main(String[] args) throws IOException, InterruptedException {
		Warrior w = new Warrior();
		// Enter q or Q to exit the program, enter anything else and the warrior will hunt a rabbit
		while (!"q".equalsIgnoreCase(R.readLine())) {
			w.hunt(new Rabbit());
			System.out.println("The warrior hunted a rabbit, the current experience value" + w.getExp());
			w.hunt(new Wolf());
			System.out.println("The warrior hunted a wolf, the current experience value" + w.getExp());
			w.hunt(new Bear());
			System.out.println("The warrior hunted a bear, the current experience value" + w.getExp());
			w.hunt(new Dragon());
			System.out.println("The warrior hunted a dragon, the current experience value" + w.getExp());
		}

	}
}

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326183042&siteId=291194637