1. 程式人生 > >Java實驗 容器試題 第六題

Java實驗 容器試題 第六題

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:

public XDatabase(int size) 

The constructor will initialize your two arrays to be of the specified   size. You will also need to keep track of the total number of items in your   database, so you should initialize a size variable to 0 in here too.

public Xman add(Xman xman) 

This method will take in a Xman, and add it both to the dnaSeq and   nameIndex arrays in the appropriate places. If a record with the same   DNASequence already exists in the database, it should get replaced, and the   previous record should be returned. Otherwise return null.

public Xman getByDNASequence(String dnaSeqence) 

This method will search for a Xman with the specified dnaSequence using   your dnaSeq list. It should return null if no record with that dnaSequence   exists.

public List<Xman> getByName(String name) 

This method will search for a Xman based on name, using the nameIndex.   Because it is possible for multiple Xman to have the same name, it will   return of list of Xman with matching names. If no records match, an empty   list should be returned.

public Xman delete(String dnaSeq) 

This method should delete the Xman with the matching dnaSeq from both   indexes, and return that record. If no record is found, it should return   null.

public int size() 

This method should return the number of Xman stored in the database.

public List<Xman> friends(String dnaSeq)

Given a dnaSeq of an Xman, find   his/her friends. An Xman A is a friend of another Xman B,   if and only if A is not in B's enemy List   and A is in the enemy list of at least one of the enemys   of B. If no friends are found, return empty list.

public List<Xman> partner(String name)

Given the name of a Xman, find his/her partner. An Xman A is   the partner of another Xman B, if and only if A is   not in B's enemy List and A is in the enemy list   of all enemies of B. If no partners are found, return empty list.   If multiple Xman are found by the same name, concatenate all their partners   in the list.

public List<Xman> archrival(String name)

Given the name of a Xman, find his/her arch rival. An Xman A is   the arch rival of another Xman B, if and only if A is   in the enemy list of Band A is in the enemy list   of all friends of B. If no arch rivals are found, return empty   list. If multiple Xman are found by the same name, concatenate all their arch   rivals in the list.

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.

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 xname+name;
	}
	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)) {
			return 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;
	}
}
import java.util.*;
public class XDatabase {
	private List<Xman> []dnaSeq;
	private List<Xman>[]nameindex;
	private int len;
	private int cnt;
	@SuppressWarnings("unchecked")
	public XDatabase(int size) {
		this.len=0;
		this.cnt=0;
		dnaSeq=new ArrayList[size];
		nameindex=new ArrayList[size];
		for(int i=0;i<size;i++) {
			dnaSeq[i]=new ArrayList<Xman>();
			nameindex[i]=new ArrayList<Xman>();
		}
	}
	public Xman add(Xman xman) {
		int temp=-10;
		for(int i=0;i<len;i++) {
			if(dnaSeq[temp].contains(xman)) {
				Xman t=dnaSeq[temp].get(0);
				dnaSeq[temp].clear();
				dnaSeq[temp].add(xman);
				nameindex[temp].clear();
				nameindex[temp].add(xman);
				return t;
			}
			if(dnaSeq[i].size()==0) {
				temp=i;
			}
		}
		if(temp==-10) {
			dnaSeq[len].add(xman);
			nameindex[len].add(xman);
			cnt++;
			len++;
		}
		else {
			dnaSeq[temp].add(xman);
			nameindex[temp].add(xman);
			cnt++;
		}
		return null;
	}
	public Xman getByDNASequence(String dnaSeqence) {
		for(int i=0;i<len;i++) {
			if(dnaSeq[i].get(0).getDNASequence().equals(dnaSeqence)) {
				return dnaSeq[i].get(0);
			}
		}
		return null;
	}
	public List<Xman> getByName(String name) {
		List<Xman> temp=new ArrayList<Xman>();
		for(int i=0;i<len;i++) {
			if(nameindex[i].get(0).getName().equals(name)) {
				temp.add(nameindex[i].get(0));
			}
		}
		return temp;
	}
	public Xman delete(String dnaSeq) {
		for(int i=0;i<len;i++) {
			if(this.dnaSeq[i].get(0).getDNASequence().equals(dnaSeq)) {
				Xman xman=this.dnaSeq[i].get(0);
				this.dnaSeq[i].clear();
				this.nameindex[i].clear();
				cnt--;
				return xman;
			}
		}
		return null;
	}
	public int size() {
		return cnt;
	}
	public List<Xman> friends(String dnaSeq){
		List<Xman> friends=new ArrayList<Xman>();
		Xman xmanB=this.getByDNASequence(dnaSeq);
		if(xmanB==null)return friends;
		for(int i=0;i<len;i++) {
			Xman xmanA=this.dnaSeq[i].get(0);
			if(xmanB.getEnemy().contains(xmanA.getDNASequence())==false) {
				for(int j=0;j<xmanB.getEnemy().size();j++) {
					Xman xmanC=this.getByDNASequence(xmanB.getEnemy().get(j));
					if(xmanC.getEnemy().contains(xmanA.getDNASequence()));
					friends.add(xmanA);
					break;
				}
			}
		}
		return friends;
	}
	public List<Xman> partner(String name){
		List<Xman> partners=new ArrayList<Xman>();
		List<Xman> xmanBs=this.getByName(name);
		if(xmanBs.isEmpty())return partners;
		for(int i=0;i<xmanBs.size();i++) {
			Xman xmanB=xmanBs.get(i);
			for(int j=0;j<len;j++) {
				Xman xmanA=this.dnaSeq[i].get(0);
				if(xmanB.getEnemy().contains(xmanA.getDNASequence())==false) {
					boolean f=true;
					for(int k=0;k<xmanB.getEnemy().size();k++) {
						Xman xmanC=this.getByDNASequence(xmanB.getEnemy().get(k));
						if(xmanC.getEnemy().contains(xmanA.getDNASequence())==false) {
							f=false;
							break;
						}
					}
					if(f==true) {
						partners.add(xmanA);
					}
				}
			}
		}
		return partners;
	}
	public List<Xman> archrival(String name){
		List<Xman> archrivals=new ArrayList<Xman>();
		List<Xman> xmanBs=this.getByName(name);
		if(xmanBs.isEmpty())return archrivals;
		for(int i=0;i<xmanBs.size();i++) {
			Xman xmanB=xmanBs.get(i);
			for(int j=0;j<len;j++) {
				Xman xmanA=this.dnaSeq[i].get(0);
				if(xmanB.getEnemy().contains(xmanA.getDNASequence())==false) {
					boolean f=true;
					List<Xman> bsfriends=this.friends(xmanB.getDNASequence());
					for(int k=0;k<bsfriends.size();k++) {
						Xman xmanC=bsfriends.get(k);
						if(xmanC.getEnemy().contains(xmanA.getDNASequence())==false) {
							f=false;
							break;
						}
					}
					if(f==true) {
						archrivals.add(xmanA);
					}
				}
			}
		}
		return archrivals;
	}
}