題目
l 名為Alien的class用來表示一隻怪物
l 名為AlienPack的class用來表示一整隊的怪物和他們造成的傷害
l 將Alien的變數進行imformation
hiding
l 利用繼承創造新的Alien type class,原本Alien裡的Type(↓↓)就可以刪掉了
(也就是創建名為
SNAKE_ALIEN
OGRE_ALIEN
MARSHMALLOW_MAN_ALIEN的class)
Alien type class內需含有
1.getAlienType()-----------(例如回傳SNAKE_ALIEN反正就是要知道她是Snake還是蝦毀的)
2.getAlienTypeName()-----(每隻Alien都有名字,把她return出來)
public static final int SNAKE_ALIEN = 0;
public static final int OGRE_ALIEN = 1;
public static final int MARSHMALLOW_MAN_ALIEN = 2;
(也就是創建名為
SNAKE_ALIEN
OGRE_ALIEN
MARSHMALLOW_MAN_ALIEN的class)
Alien type class內需含有
1.getAlienType()-----------(例如回傳SNAKE_ALIEN反正就是要知道她是Snake還是蝦毀的)
2.getAlienTypeName()-----(每隻Alien都有名字,把她return出來)
l 最後,藉由getDamage(所有Alien的子類別都有)重新撰寫calculateDamage() (AlienPack裡面) 在寫一個main method測試code的正確性
import java.util.Scanner;
class Alien
{
public static final int SNAKE_ALIEN
= 0;
public static final int OGRE_ALIEN
= 1;
public static final int MARSHMALLOW_MAN_ALIEN = 2;
public int type;
public int health;
public String name;
public Alien(int type, int health, String name)
{
this.type = type;
this.health = health;
this.name = name;
}
}
class AlienPack
{
private Alien[] aliens;
public AlienPack(int numAliens)
{
aliens = new Alien[numAliens];
}
public void addAlien(Alien newAlien, int index)
{
aliens[index] = newAlien;
}
public Alien[] getAliens()
{
return aliens;
}
public int calculateDamage()
{
int damage = 0;
for(int i=0; i < aliens.length ;i++)
{
if(aliens[i].type==Alien.SNAKE_ALIEN)
{
damage +=10;
}
else if(aliens[i].type==Alien.OGRE_ALIEN)
{
damage +=6;
}
else if(aliens[i].type==Alien.MARSHMALLOW_MAN_ALIEN)
{
damage +=1;
}
}
return damage;
}
}
作業四-Q6
題目
l 創造一個叫vehicle的class,內含
1.
製造商manufacturer的name(type string)
2.
汽缸cylinder的數量(type int)
3.
擁有者owner (type Person 而Person class將在底下給你)
l 創造一個叫Truck的class,是繼承自vehicle
class,並且額外加上,
1.
負載能力load capacity 單位為頓tons,且必須包含小數(type
double)
2.
拖拉能力towing capacity單位為磅pounds(type int)
l 合理的為你的class補充建構子、accessor(就是getxxx的method)、
mutator(setxxx的method),適當的定義equals和tostring
l 寫一個程式測試你全部的method
public class Person
{
private String name;
public Person()
{
}
public Person(String theName)
{
}
public Person (Person theObject)
{
}
public String getName()
{
}
public void setName(String theName)
{
}
public String tostring()
{
}
public boolean equals(Object other)
{
}
}
沒有留言:
張貼留言