前言

因為計概之前是在教Java以及oop

所以今次出了含有oo concept 的題目

要求如下

應考心得

份requirement是沒有太大用途,原因是那份doc是沒有明確指出這些class之間有甚麼關係。

個人認為整份doc最重要的部份是例子截圖

其他的就請去讀懂main.java,否則你連傳入的variable究竟是object還是primite type

題目一

第一題的大意就是建立五個class

而當中的BlackTea.javaMilk.javaPearl.java理論上都是要extend Material.java。 (因為BlackTea、Milk和Pearl也是Material)

而且Material.java class內有gram (見下)

Material.java :

gram: material content (double value)

calorie: material heat (double value)

Material(double): constructer, initialize gram

calculateCalorie(): no need to do something

而這個gram理應為BlackTea.javaMilk.javaPearl.java共同具備的特性(但題目並沒有具題說明) (見下)


BlackTea.java

BlackTea(double): constructer, initialize gram, and print the message “add black tea”

calculateCalorie(): calculate calories of black tea, formula is 0.1*gram


Milk.java

Milk(double): constructer, initialize gram, and print the message “add milk”

calculateCalorie(): calculate calories of milk, formula is 4.5*gram


Pearl.java

Pearl(double): constructer, initialize gram, and print the message “add pearl”

calculateCalorie(): calculate calories of pearl, formula is 5*gram


而請注意Material.java class裏面其實還有一個variable calorie 是還未使用,而這個calorie其實就是用來儲存上方 粗斜體 的value(又是不說明的東西)

所以這是用來伏學生的嗎?

Main.java

public class Main {

	public static void main(String[] args) {
		Material list[] = {new Pearl(25), new Milk(300), new BlackTea(400)};
		Drink pearlMilkTea = new Drink("pearl milk tea");
		for(int i=0; i<list.length; i++) {
			pearlMilkTea.addMaterial(list[i]);
		}
		pearlMilkTea.calculateTotalCalorie();
		
		Material list2[] = {new Milk(100), new BlackTea(500), new Milk(100)};
		Drink milkTea = new Drink("milk tea");
		for(int i=0; i<list2.length; i++) {
			milkTea.addMaterial(list2[i]);
		}
		milkTea.calculateTotalCalorie();

	}

}

Drink.java個人認為應該要知道甚麼是static方可完成

從上方Main.java可見line 4 and line 11所儲存的是extendsMaterial.java的object的array

line 6 to 8 and line 13 to 15則是將array裏每一個object pass給Drink這個object。

個人認為這樣有兩個做法,但都要用到static 但因為本人過於懶,所以只想說自己的做法

Drink.java

public class Drink {
	static double sum = 0;
	String name;
	Material content;
	public Drink(String name) {
		this.name = name;
	}
	public void addMaterial(Material thing) {
		this.content = thing;
		this.sum += thing.calculateCalorie();
	}
	public void calculateTotalCalorie(){
		System.out.println(this.name + " heat is " + this.sum + " kcal");
		this.sum = 0;
	}
}

本人用sum將pass進來的object的Calorie先全部加總,然後在所謂的calculateTotalCalorie()sum print 出來

但因為本人在sum 用了static,即是當我不用Drink這個object時,這個sum的variable還是存在(儲存上一次修改的狀態),所以我在Drink.javaline 14this.sum set 為 0

而其他的class如下:

BlackTea.java

public class BlackTea extends Material{
	public BlackTea(double value) {
		super(value);
		this.calorie = 0.1;
		System.out.println("add black tea");
	}
	@Override
	public double calculateCalorie() {
		return this.calorie * this.gram;
	}
	
}

Milk.java

public class Milk extends Material {
	public Milk(double value) {
		super(value);
		this.calorie = 4.5;
		System.out.println("add milk");
	}
	@Override
	public double calculateCalorie() {
		return this.calorie * this.gram;
	}
}

Pearl.java

public class Pearl extends Material{
	public Pearl(double value) {
		super(value);
		this.calorie = 5;
		System.out.println("add pearl");
	}
	@Override
	public double calculateCalorie() {
		return this.calorie * this.gram;
	}
}

Material.java

public class Material {
	double gram;
	double calorie;
	public Material(double value) {
		this.gram = value;
	}
	public double calculateCalorie() {
		return 0;
	}
}

題目二

而題目二則只要看懂example即可

Main.java

import java.util.*;
import java.util.concurrent.TimeUnit;

public class Main {

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		Pikachu player1 = new Pikachu("Pikachu");
		Raichu player2 = new Raichu("Raichu");
		System.out.println("Pikachu and Raichu were born!!!");
		int player1Dice;
		int player2Dice;

		// fight
		while (player1.HP > 0 && player2.HP > 0) {
			for (int i = 1; i < 3; i++) {
				player1Dice = player1.rollDice();
				player2Dice = player2.rollDice();
				Random ran = new Random();
				int diceNumber = ran.nextInt(4) + 1; // random the 1~6 number
				if (player1Dice > player2Dice) {
					if (i == 1) {
						player1.skills(diceNumber);
						player2.attacked(player1.attack * player1.skill * diceNumber);
					} else {
						player1.attackSuccess();
						player2.attacked(player1.attack);
					}
				}

				else if (player1Dice < player2Dice) {
					if (i == 2) {
						player2.skills(diceNumber);
						if(diceNumber!=2)
						player1.attacked(player2.attack * player2.skill * diceNumber);
					} else {
						player2.attackSuccess();
						player1.attacked(player2.attack);
					}
				}
				try {
					TimeUnit.MILLISECONDS .sleep(50);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		// print the result
		if (player1.HP <= 0)
			System.out.println(player2.name + " win.");
		else if (player2.HP <= 0)
			System.out.println(player1.name + " win.");
		else
			System.out.println("Draw.");
	}

}

Pokemons.java

import java.util.Scanner;
import java.util.Random; //import for random 

public class Pokemons {

	int HP;
	int diceNumber;
	int attack;
	String name;
	int skill;

	public Pokemons(String name) {
		this.name = name;
	}

	public int rollDice() {
		//finish your code here
		Random rand = new Random();
		return 1 + rand.nextInt(6);
	}

	public void attackSuccess() {
		//finish your code here
		System.out.println("Pikachu/Raichu attack Successd, ");
	}

	public void attacked(int attackNumber) {
		//finish your code here
		this.HP -= attackNumber;
		System.out.println("Pikachu/Raichu is attacked and HP is " + this.HP);
	}

	public void skills(int ran) {
		//finish your code here
		System.out.println(" Pikachu/Raichu uses ");
	}
	
	public int getSkill() {
		return skill;
	}

	public void setSkill(int skill) {
		this.skill = skill;
	}
	public int getHP() {
		return HP;
	}

	public void setHP(int hP) {
		HP = hP;
	}

	public int getAttack() {
		return attack;
	}

	public void setAttack(int attack) {
		this.attack = attack;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

}

Pikachu.java

public class Pikachu extends Pokemons {

	public Pikachu(String name) {
		super(name);
		this.HP = 80;
		this.attack = 3;
		this.skill = 2;
		// TODO Auto-generated constructor stub
	}

	public void skills(int diceNumber) {
		// finish your code here
		int attackNum = this.attack * this.skill * diceNumber;
		String _num = String.valueOf(attackNum);
		String[] skills = {"Double slap", "Iron Tail", "Volt Tackle", "Electric shock"};
		System.out.println("Pikachu uses <" + skills[diceNumber - 1] + "> to attack, the attack number is " + _num);
	}
	@Override
	public void attackSuccess() {
		//finish your code here
		System.out.print("Pikachu attack Successd, ");
	}
	@Override
	public void attacked(int attackNumber) {
		//finish your code here
		this.HP -= attackNumber;
		System.out.println("Pikachu is attacked and HP is " + this.HP + "\n");
	}
}

Raichu.java

public class Raichu extends Pokemons {

	public Raichu(String name) {
		super(name);
		this.HP = 100;
		this.attack = 4;
		this.skill = 3;
		// TODO Auto-generated constructor stub
	}

	public void skills(int diceNumber) {
		// finish your code here
		int attackNum = this.attack * this.skill * diceNumber;
		String _num = String.valueOf(attackNum);
		String[] skills = {"Quic Attack", "Growl", "Thunder", "Thunderbolt"};
		System.out.println("Raichu uses <" + skills[diceNumber - 1] + "> to attack, the attack number is " + _num);
	}
	@Override
	public void attackSuccess() {
		//finish your code here
		System.out.print("Raichu attack Successd, ");
	}
	@Override
	public void attacked(int attackNumber) {
		//finish your code here
		this.HP -= attackNumber;
		System.out.println("Raichu is attacked and HP is " + this.HP + "\n");
	}
}

題目三

Main.java

import java.util.*;
public class Main {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);		
		int animal = (int)(Math.random()*3)+1; //random 1 ~ 3 to choose animal
		boolean animal_hungry = true; 		   // check if the animal's satiety >= 100 
		int animal_satiety = 0; 				   // animal's satiety
		
		Zoo z = null;
		
		// call the animal class
		switch(animal) {
			case 1: 
				//code here
				z = new Tiger();
				animal_satiety = z.satiety();
				break;
			case 2:
				//code here
				z = new Monkey();
				animal_satiety = z.satiety();
				break;
			case 3:
				//code here
				z = new Penguin();
				animal_satiety = z.satiety();
				break;			
			default:
				break;
		}
		 
		System.out.println(z.name() + " is hungry(" + animal_satiety + "%)");
		System.out.println(z.name() + " want to eat " + z.food() + "\n\n");
		// feed the animal
		while(animal_hungry) { 
			//code here
			System.out.println("Please enter f to feed " + z.name());
			Scanner input = new Scanner(System.in);
			if (input.nextLine().equals("f")) {
				Random rand = new Random();
				animal_satiety += 5 + rand.nextInt(6);
			}
			if (animal_satiety < 100) {
				System.out.println(z.name() + " still hungry(" + animal_satiety + "%) \n\n");
			}else {
				System.out.println(z.name() + " is happy!!");
				animal_hungry = false;
			}
		}
				
	}
		 
		 
}

Zoo.java

import java.util.*;

public interface Zoo {
	//code here
	public int satiety();
	public String food();
	public String name();
}

class Tiger implements Zoo{
	//code here
	public int satiety() {
		Random rand = new Random();
		return 50 + rand.nextInt(31);
	}
	public String food() {
		return "meat";
	}
	public String name() {
		return "Tiger";
	}
}

class Monkey implements Zoo{
	//code here
	public int satiety() {
		Random rand = new Random();
		return 50 + rand.nextInt(31);
	}
	public String food() {
		return "banana";
	}
	public String name() {
		return "Monkey";
	}
}

class Penguin implements Zoo{
	//code here
	public int satiety() {
		Random rand = new Random();
		return 50 + rand.nextInt(31);
	}
	public String food() {
		return "Fish";
	}
	public String name() {
		return "Penguin";
	}
}