Professor Charles Xavier is having a tough time remembering all the different kinds of X-Men, their power and weakness. With the improvement of cerebro his mind can reach anywhere on earth. He decided to build a database of X- men so that he can find them quickly. You will be helping the great professor in this homework.
Edit: The constructor now takes an enemy list instead of just one enemy.
You’ll need to create two classes to store this information. First you’ll need to create the Xman class. It will need the following methods.
public Xman(String name, String xname, String power, String weakness, List<String> enemy, Date birthDate, String gender, String DNASequence)
The constructor will need to set the appropriate instance variables.
public String getName()
This method should return the xname and name concatenated like xname(name).
public String getPower()
Return the power
public String getWeakness()
Return the weakness.
public List<Xman> getEnemy()
Return the enemy Xmen.
public Date getBirthDate()
Return the date of birth. You will need to import the package java.util.date
public String getGender()
Return the gender.
public String getDNASequence()
Return the DNA sequence (if you decide to use print statements for debugging, be aware that this is a really long string).
public boolean equals(Object o)
You need override the equals() method, such that two Xman with the same DNA sequence are considered equal.
Because there are lots of X-men, we need a fast way look them up. We’re going to use the hash tables discussed in class to create a way of looking up records by both DNA sequence and name. Make the class XDatabase which will contain two arrays of lists:
List<Xman>[] dnaSeq;
List<Xman>[] nameIndex;
Having a list at each index of the arrays will allow us to handle collisions, by simply adding multiple Records to that spot. You can use linked list or array list whatever you like. (This can be yours/java's default LinkedList /ArrayList)
Java’s base class Object, has the method hashCode(), which returns an int based on the value of that object. It’s important to note that this can return a negative integer, so when you hash it, you’ll need to take the absolute value. So we can figure out the position a record belongs in thednaSeq array by:
int i = Math.abs(xman.getDNASequence().hashCode()) % dnaSeq.length;
Because we’re going to have a large number of X-men in our database, we need to be conscious of memory usage. This means that we won’t create a list at an index, until we store something at that index.Finally, this is the list of methods you will need to implement in your XDatabase class:
Now, instead of using java's hash code, write your own hash function. It will take a string and generate a hash code from that. Test all the methods above.
(个人意见,仅供参考,如有错误,还望指出)
所有方法已进行简单测试。
Xman类:
import java.util.*; public class Xman { private String name; private String xname; private String power; private String weakness; private List<String> enemy; private Date birthDate; private String gender; private String DNASequence; public Xman(String name, String xname, String power, String weakness,List<String> enemy, Date birthDate, String gender, String DNASequence) { this.name = name; this.xname = xname; this.power = power; this.weakness = weakness; this.enemy = enemy; this.birthDate = birthDate; this.gender = gender; this.DNASequence = DNASequence; } public String getName() { return name; } public String getXname() { return xname; } public String getPower() { return power; } public String getWeakness() { return weakness; } public String getGender() { return gender; } public List<String> getEnemy() { return enemy; } public Date getBirthDate() { return birthDate; } public String getDNASequence() { return DNASequence; } public int hashCode() { int result = 1; if((DNASequence == null)) { result = 0; } else { result = Math.abs(DNASequence.hashCode()) % DNASequence.length(); } return result; } public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; Xman other = (Xman) obj; if (DNASequence == null) { if (other.DNASequence != null) return false; } else if (!DNASequence.equals(other.DNASequence)) return false; return true; } }
XDatabase类:
import java.util.*; public class XDatabase { private List<Xman>[] dnaSeq; private List<Xman>[] nameIndex; private int num;// Xman的数量 private int length;//数组长度 //length是数组的长度,可能某个元素为null //num值Xman的数量,相当于length去掉null的数量之后的个数 /* * 构造函数,传入size,为List分配内存,初始化num、和length; */ public XDatabase(int size) { dnaSeq = new ArrayList[size]; nameIndex = new ArrayList[size]; for(int i=0;i<size;i++) { dnaSeq[i] = new ArrayList(); nameIndex[i] = new ArrayList(); } num = 0; length = 0; } /* * 添加Xman */ public Xman add(Xman xman) { int pos=-1;//为了减少内存的浪费,设置pos用来检查是否有已分配内存但未使用的位置。 for (int i = 0; i < length; i++) {//遍历检查dnaSeq中是否已存在此xman,若已存在,则更新,否则直接添加。 if(dnaSeq[i].size()==0) { pos=i; } if (dnaSeq[i].contains(xman)) { Xman prexman = dnaSeq[i].get(0); dnaSeq[i].clear(); dnaSeq[i].add(xman); nameIndex[i].clear(); nameIndex[i].add(xman); return prexman; } } if(pos==-1) { nameIndex[length].add(xman); dnaSeq[length++].add(xman); num++; } else { nameIndex[pos].add(xman); dnaSeq[pos].add(xman); num++; } return null; } /* * 根据DNA序列查找Xman */ public Xman getByDNASequence(String dnaSeqence) { for (int i = 0; i < length; i++) { if (dnaSeq[i].size() == 0)//如果这个位置没有xman,直接跳过 continue; if (dnaSeq[i].get(0).getDNASequence().equals(dnaSeqence)) {//比较DNA序列 return dnaSeq[i].get(0); } } return null; } /* * 根据name查找Xman,可能有多个相同name的Xman,所以存在List<Xman>中 */ public List<Xman> getByName(String name) { List<Xman> xmans = new ArrayList<Xman>();//分配内存 for (int i = 0; i < length; i++) { if (nameIndex[i].size() == 0) continue; if (nameIndex[i].get(0).getName().equals(name)) {//姓名相同,就存入xmans中 xmans.add(nameIndex[i].get(0)); } } return xmans; } /* * 根据DNA序列删除Xman */ public Xman delete(String dnaSeq) { for (int i = 0; i < length; i++) { if (this.dnaSeq[i].size() == 0) continue; if (this.dnaSeq[i].get(0).getDNASequence().equals(dnaSeq)) {//DNA序列相同则进行删除操作 Xman xman = this.dnaSeq[i].get(0); this.dnaSeq[i].clear(); this.nameIndex[i].clear(); num--;//这里就是num和length的区别,num可能会减少,但length只能增加 return xman; } } return null; } public int size() { return num; } /* * 根据DNA查找friends */ public List<Xman> friends(String dnaSeq) { Xman xmanB = getByDNASequence(dnaSeq);//待查找的Xman List<Xman> xfriends = new ArrayList<Xman>(); if(xmanB==null) { return xfriends; } for (int i = 0; i < length; i++) { if (this.dnaSeq[i].size() == 0) continue; Xman xmanA = this.dnaSeq[i].get(0);//待检查的Xman //如果A不在B的敌人列表中 if (xmanB.getEnemy().contains(xmanA.getDNASequence()) == false) { //A在B的敌人中的至少一个的敌人列表中 for (int j = 0; j < xmanB.getEnemy().size(); j++) { Xman xmanC = getByDNASequence(xmanB.getEnemy().get(j)); if(xmanC==null) { continue; } if (xmanC.getEnemy().contains(xmanA.getDNASequence())) { xfriends.add(xmanA); break; } } } } return xfriends; } /* * 根据name查找partner */ public List<Xman> partner(String name){ List<Xman> xmanBs = getByName(name); List<Xman> xpartners = new ArrayList<Xman>(); for(int k=0;k<xmanBs.size();k++) {//待检查的Xman Xman xmanB = xmanBs.get(k); for (int i = 0; i < length; i++) { if (this.dnaSeq[i].size() == 0) continue; Xman xmanA = this.dnaSeq[i].get(0); //A不在B的敌人列表中 if (xmanB.getEnemy().contains(xmanA.getDNASequence()) == false) { boolean is=true; //A在B的所有敌人的敌人列表中时 for (int j = 0; j < xmanB.getEnemy().size(); j++) { Xman xmanC = getByDNASequence(xmanB.getEnemy().get(j)); if (xmanC.getEnemy().contains(xmanA.getDNASequence()) == false) { is=false; break; } } if(is==true) xpartners.add(xmanA); } } } return xpartners; } /* * 根据name查找arch rival */ public List<Xman> archrival(String name) { List<Xman> xmanBs = getByName(name); List<Xman> xarchrivals = new ArrayList<Xman>(); for(int k=0;k<xmanBs.size();k++) { Xman xmanB = xmanBs.get(k); for (int i = 0; i < length; i++) { if (this.dnaSeq[i].size() == 0) continue; Xman xmanA = this.dnaSeq[i].get(0); //A在B的敌人列表中 if (xmanB.getEnemy().contains(xmanA.getDNASequence()) == false) { boolean is=true; List<Xman> bxfriends = friends(xmanB.getDNASequence()); //A在B的所有朋友的敌人列表中 for (int j = 0; j < bxfriends.size(); j++) { Xman xmanC = bxfriends.get(j); if (xmanC.getEnemy().contains(xmanA.getDNASequence())==false) { is=false; break; } } if(is==true) xarchrivals.add(xmanA); } } } return xarchrivals; } }