导航:首页 > 编程语言 > id3算法java源码

id3算法java源码

发布时间:2023-04-24 13:09:56

⑴ 用java编写获取多媒体文件id3信息的Android代码

^$this->error="Nosuchfile"; if($exitonerror)$this->exitonerror(); } } functionexitonerror(){ echo($this->error); exit; } functionset_id3($title="",$author="",$album="",$year="",$comment="",$genre_id=0){ $this->error=false; $this->wfh=fopen($this->file,"a"); fseek($this->wfh,-128,SEEK_END); fwrite($this->wfh,pack("a3a30a30a30a4a30C1","TAG",$title,$author,$album,$year,$comment,$genre_id),128); fclose($this->wfh); } functionget_id3(){ $this->id3_parsed=true; fseek($this->fh,-128,SEEK_END); $line=fread($this->fh,10000); if(preg_match("/^TAG/",$line)){ $this->id3=unpack("a3tag/a30title/a30author/a30album/a4year/a30comment/C1genre_id",$line); $this->id3["genre"]=$this->id3_genres_array[$this->id3]["genre_id"]]; return(true); }else{ $this->error="noidv3tagfound"; return(false); } } //get_info()helpermethods functioncalculate_length($id3v2_tagsize=0){ $length=floor(($this->info["filesize"]-$id3v2_tagsize)/$this->info["bitrate"]*0.008); $min=floor($length/60); $min=strlen($min)==1?"0$min":$min; $sec=$length`; $sec=strlen($sec)==1?"0$sec":$sec; return("$min:$sec"); } functionget_info(){ // $this->get_id3v2header(); $second=$this->synchronize(); // echo("2ndbyte=$second<b>".decbin($second)."</b><br>"); $third=ord(fread($this->fh,1)); $fourth=ord(fread($this->fh,1)); $this->info["version_id"]=($second&16)>0?(($second&8)>0?1:2):(($second&8)>0?0:2.5); $this->info["version"]=$this->info_versions[$this->info]["version_id"]]; $this->info["layer_id"]=($second&4)>0?(($second&2)>0?1:2):(($second&2)>0?3:0); ; $this->info["layer"]=$this->info_layers[$this->info]["layer_id"]]; $this->info["protection"]=($second&1)>0?"noCRC":"CRC"; $this->info["bitrate"]=$this->info_bitrates[$this->info]["version_id"]][$this->info]["layer_id"]][($third&240)]; $this->info["sampling_rate"]=$this->info_sampling_rates[$this->info]["version_id"]][($third&12)];

⑵ 求助 weka 的ID3算法java源码

/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
/*
* Id3.java
* Copyright (C) 1999 University of Waikato, Hamilton, New Zealand
*
*/
package weka.classifiers.trees;
import weka.classifiers.Classifier;
import weka.classifiers.Sourcable;
import weka.core.Attribute;
import weka.core.Capabilities;
import weka.core.Instance;
import weka.core.Instances;
import weka.core.;
import weka.core.RevisionUtils;
import weka.core.TechnicalInformation;
import weka.core.TechnicalInformationHandler;
import weka.core.Utils;
import weka.core.Capabilities.Capability;
import weka.core.TechnicalInformation.Field;
import weka.core.TechnicalInformation.Type;
import java.util.Enumeration;
/**
<!-- globalinfo-start -->
* Class for constructing an unpruned decision tree based on the ID3 algorithm. Can only deal with nominal attributes. No missing values allowed. Empty leaves may result in unclassified instances. For more information see: <br/>
* <br/>
* R. Quinlan (1986). Inction of decision trees. Machine Learning. 1(1):81-106.
* <p/>
<!-- globalinfo-end -->
*
<!-- technical-bibtex-start -->
* BibTeX:
* <pre>
* &#64;article{Quinlan1986,
* author = {R. Quinlan},
* journal = {Machine Learning},
* number = {1},
* pages = {81-106},
* title = {Inction of decision trees},
* volume = {1},
* year = {1986}
* }
* </pre>
* <p/>
<!-- technical-bibtex-end -->
*
<!-- options-start -->
* Valid options are: <p/>
*
* <pre> -D
* If set, classifier is run in debug mode and
* may output additional info to the console</pre>
*
<!-- options-end -->
*
* @author Eibe Frank ([email protected])
* @version $Revision: 6404 $
*/
public class Id3
extends Classifier
implements TechnicalInformationHandler, Sourcable {
/** for serialization */
static final long serialVersionUID = -2693678647096322561L;
/** The node's successors. */
private Id3[] m_Successors;
/** Attribute used for splitting. */
private Attribute m_Attribute;
/** Class value if node is leaf. */
private double m_ClassValue;
/** Class distribution if node is leaf. */
private double[] m_Distribution;
/** Class attribute of dataset. */
private Attribute m_ClassAttribute;
/**
* Returns a string describing the classifier.
* @return a description suitable for the GUI.
*/
public String globalInfo() {
return "Class for constructing an unpruned decision tree based on the ID3 "
+ "algorithm. Can only deal with nominal attributes. No missing values "
+ "allowed. Empty leaves may result in unclassified instances. For more "
+ "information see: "
+ getTechnicalInformation().toString();
}
/**
* Returns an instance of a TechnicalInformation object, containing
* detailed information about the technical background of this class,
* e.g., paper reference or book this class is based on.
*
* @return the technical information about this class
*/
public TechnicalInformation getTechnicalInformation() {
TechnicalInformation result;
result = new TechnicalInformation(Type.ARTICLE);
result.setValue(Field.AUTHOR, "R. Quinlan");
result.setValue(Field.YEAR, "1986");
result.setValue(Field.TITLE, "Inction of decision trees");
result.setValue(Field.JOURNAL, "Machine Learning");
result.setValue(Field.VOLUME, "1");
result.setValue(Field.NUMBER, "1");
result.setValue(Field.PAGES, "81-106");
return result;
}
/**
* Returns default capabilities of the classifier.
*
* @return the capabilities of this classifier
*/
public Capabilities getCapabilities() {
Capabilities result = super.getCapabilities();
result.disableAll();
// attributes
result.enable(Capability.NOMINAL_ATTRIBUTES);
// class
result.enable(Capability.NOMINAL_CLASS);
result.enable(Capability.MISSING_CLASS_VALUES);
// instances
result.setMinimumNumberInstances(0);
return result;
}
/**
* Builds Id3 decision tree classifier.
*
* @param data the training data
* @exception Exception if classifier can't be built successfully
*/
public void buildClassifier(Instances data) throws Exception {
// can classifier handle the data?
getCapabilities().testWithFail(data);
// remove instances with missing class
data = new Instances(data);
data.deleteWithMissingClass();
makeTree(data);
}
/**
* Method for building an Id3 tree.
*
* @param data the training data
* @exception Exception if decision tree can't be built successfully
*/
private void makeTree(Instances data) throws Exception {
// Check if no instances have reached this node.
if (data.numInstances() == 0) {
m_Attribute = null;
m_ClassValue = Instance.missingValue();
m_Distribution = new double[data.numClasses()];
return;
}
// Compute attribute with maximum information gain.
double[] infoGains = new double[data.numAttributes()];
Enumeration attEnum = data.enumerateAttributes();
while (attEnum.hasMoreElements()) {
Attribute att = (Attribute) attEnum.nextElement();
infoGains[att.index()] = computeInfoGain(data, att);
}
m_Attribute = data.attribute(Utils.maxIndex(infoGains));
// Make leaf if information gain is zero.
// Otherwise create successors.
if (Utils.eq(infoGains[m_Attribute.index()], 0)) {
m_Attribute = null;
m_Distribution = new double[data.numClasses()];
Enumeration instEnum = data.enumerateInstances();
while (instEnum.hasMoreElements()) {
Instance inst = (Instance) instEnum.nextElement();
m_Distribution[(int) inst.classValue()]++;
}
Utils.normalize(m_Distribution);
m_ClassValue = Utils.maxIndex(m_Distribution);
m_ClassAttribute = data.classAttribute();
} else {
Instances[] splitData = splitData(data, m_Attribute);
m_Successors = new Id3[m_Attribute.numValues()];
for (int j = 0; j < m_Attribute.numValues(); j++) {
m_Successors[j] = new Id3();
m_Successors[j].makeTree(splitData[j]);
}
}
}
/**
* Classifies a given test instance using the decision tree.
*
* @param instance the instance to be classified
* @return the classification
* @throws if instance has missing values
*/
public double classifyInstance(Instance instance)
throws {
if (instance.hasMissingValue()) {
throw new ("Id3: no missing values, "
+ "please.");
}
if (m_Attribute == null) {
return m_ClassValue;
} else {
return m_Successors[(int) instance.value(m_Attribute)].
classifyInstance(instance);
}
}
/**
* Computes class distribution for instance using decision tree.
*
* @param instance the instance for which distribution is to be computed
* @return the class distribution for the given instance
* @throws if instance has missing values
*/
public double[] distributionForInstance(Instance instance)
throws {
if (instance.hasMissingValue()) {
throw new ("Id3: no missing values, "
+ "please.");
}
if (m_Attribute == null) {
return m_Distribution;
} else {
return m_Successors[(int) instance.value(m_Attribute)].
distributionForInstance(instance);
}
}
/**
* Prints the decision tree using the private toString method from below.
*
* @return a textual description of the classifier
*/
public String toString() {
if ((m_Distribution == null) && (m_Successors == null)) {
return "Id3: No model built yet.";
}
return "Id3 " + toString(0);
}
/**
* Computes information gain for an attribute.
*
* @param data the data for which info gain is to be computed
* @param att the attribute
* @return the information gain for the given attribute and data
* @throws Exception if computation fails
*/
private double computeInfoGain(Instances data, Attribute att)
throws Exception {
double infoGain = computeEntropy(data);
Instances[] splitData = splitData(data, att);
for (int j = 0; j < att.numValues(); j++) {
if (splitData[j].numInstances() > 0) {
infoGain -= ((double) splitData[j].numInstances() /
(double) data.numInstances()) *
computeEntropy(splitData[j]);
}
}
return infoGain;
}
/**
* Computes the entropy of a dataset.
*
* @param data the data for which entropy is to be computed
* @return the entropy of the data's class distribution
* @throws Exception if computation fails
*/
private double computeEntropy(Instances data) throws Exception {
double [] classCounts = new double[data.numClasses()];
Enumeration instEnum = data.enumerateInstances();
while (instEnum.hasMoreElements()) {
Instance inst = (Instance) instEnum.nextElement();
classCounts[(int) inst.classValue()]++;
}
double entropy = 0;
for (int j = 0; j < data.numClasses(); j++) {
if (classCounts[j] > 0) {
entropy -= classCounts[j] * Utils.log2(classCounts[j]);
}
}
entropy /= (double) data.numInstances();
return entropy + Utils.log2(data.numInstances());
}
/**
* Splits a dataset according to the values of a nominal attribute.
*
* @param data the data which is to be split
* @param att the attribute to be used for splitting
* @return the sets of instances proced by the split
*/
private Instances[] splitData(Instances data, Attribute att) {
Instances[] splitData = new Instances[att.numValues()];
for (int j = 0; j < att.numValues(); j++) {
splitData[j] = new Instances(data, data.numInstances());
}
Enumeration instEnum = data.enumerateInstances();
while (instEnum.hasMoreElements()) {
Instance inst = (Instance) instEnum.nextElement();
splitData[(int) inst.value(att)].add(inst);
}
for (int i = 0; i < splitData.length; i++) {
splitData[i].compactify();
}
return splitData;
}
/**
* Outputs a tree at a certain level.
*
* @param level the level at which the tree is to be printed
* @return the tree as string at the given level
*/
private String toString(int level) {
StringBuffer text = new StringBuffer();
if (m_Attribute == null) {
if (Instance.isMissingValue(m_ClassValue)) {
text.append(": null");
} else {
text.append(": " + m_ClassAttribute.value((int) m_ClassValue));
}
} else {
for (int j = 0; j < m_Attribute.numValues(); j++) {
text.append(" ");
for (int i = 0; i < level; i++) {
text.append("| ");
}
text.append(m_Attribute.name() + " = " + m_Attribute.value(j));
text.append(m_Successors[j].toString(level + 1));
}
}
return text.toString();
}
/**
* Adds this tree recursively to the buffer.
*
* @param id the unqiue id for the method
* @param buffer the buffer to add the source code to
* @return the last ID being used
* @throws Exception if something goes wrong
*/
protected int toSource(int id, StringBuffer buffer) throws Exception {
int result;
int i;
int newID;
StringBuffer[] subBuffers;
buffer.append(" ");
buffer.append(" protected static double node" + id + "(Object[] i) { ");
// leaf?
if (m_Attribute == null) {
result = id;
if (Double.isNaN(m_ClassValue)) {
buffer.append(" return Double.NaN;");
} else {
buffer.append(" return " + m_ClassValue + ";");
}
if (m_ClassAttribute != null) {
buffer.append(" // " + m_ClassAttribute.value((int) m_ClassValue));
}
buffer.append(" ");
buffer.append(" } ");
} else {
buffer.append(" checkMissing(i, " + m_Attribute.index() + "); ");
buffer.append(" // " + m_Attribute.name() + " ");
// subtree calls
subBuffers = new StringBuffer[m_Attribute.numValues()];
newID = id;
for (i = 0; i < m_Attribute.numValues(); i++) {
newID++;
buffer.append(" ");
if (i > 0) {
buffer.append("else ");
}
buffer.append("if (((String) i[" + m_Attribute.index()
+ "]).equals("" + m_Attribute.value(i) + "")) ");
buffer.append(" return node" + newID + "(i); ");
subBuffers[i] = new StringBuffer();
newID = m_Successors[i].toSource(newID, subBuffers[i]);
}
buffer.append(" else ");
buffer.append(" throw new IllegalArgumentException("Value '" + i["
+ m_Attribute.index() + "] + "' is not allowed!"); ");
buffer.append(" } ");
// output subtree code
for (i = 0; i < m_Attribute.numValues(); i++) {
buffer.append(subBuffers[i].toString());
}
subBuffers = null;
result = newID;
}
return result;
}
/**
* Returns a string that describes the classifier as source. The
* classifier will be contained in a class with the given name (there may
* be auxiliary classes),
* and will contain a method with the signature:
* <pre><code>
* public static double classify(Object[] i);
* </code></pre>
* where the array <code>i</code> contains elements that are either
* Double, String, with missing values represented as null. The generated
* code is public domain and comes with no warranty. <br/>
* Note: works only if class attribute is the last attribute in the dataset.
*
* @param className the name that should be given to the source class.
* @return the object source described by a string
* @throws Exception if the source can't be computed
*/
public String toSource(String className) throws Exception {
StringBuffer result;
int id;
result = new StringBuffer();
result.append("class " + className + " { ");
result.append(" private static void checkMissing(Object[] i, int index) { ");
result.append(" if (i[index] == null) ");
result.append(" throw new IllegalArgumentException("Null values "
+ "are not allowed!"); ");
result.append(" } ");
result.append(" public static double classify(Object[] i) { ");
id = 0;
result.append(" return node" + id + "(i); ");
result.append(" } ");
toSource(id, result);
result.append("} ");
return result.toString();
}
/**
* Returns the revision string.
*
* @return the revision
*/
public String getRevision() {
return RevisionUtils.extract("$Revision: 6404 $");
}
/**
* Main method.
*
* @param args the options for the classifier
*/
public static void main(String[] args) {
runClassifier(new Id3(), args);
}
}

⑶ java中类的使用

你这几个没有实现的功能,其实才是这个程序题的精髓,是java面向对象和类封装性的体现,你看看我给的代码:

public class PersonInfo
{
private String name;

private int age;

private String rocketId;

private String favoriteCar;

public int getAge()
{
return age;
}

public void setAge(int age)
{
this.age = age;
}

public String getFavoriteCar()
{
return favoriteCar;
}

public void setFavoriteCar(String favoriteCar)
{
this.favoriteCar = favoriteCar;
}

public String getName()
{
return name;
}

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

public String getRocketId()
{
return rocketId;
}

public void setRocketId(String rocketId)
{
this.rocketId = rocketId;
}

public void ReadNewPerson(String iName, int iAge, String iRocketId, String iFavoriteCar)
{
this.setName(iName);
this.setAge(iAge);
this.setRocketId(iRocketId);
this.setFavoriteCar(iFavoriteCar);
}

public void DisplayPerson()
{
String info = this.getName() + "\t" + this.getAge() + "\t" + this.getRocketId() + "\t" + this.getFavoriteCar();
System.out.println(info);
}
}

public class FindYoungest
{
public static void main(String args[])
{
PersonInfo p1 = new PersonInfo();
PersonInfo p2 = new PersonInfo();
PersonInfo p3 = new PersonInfo();
p1.ReadNewPerson("张三", 38, "a", "宝马");
p2.ReadNewPerson("李四", 23, "b", "奔驰");
p3.ReadNewPerson("王五", 56, "c", "中华");

PersonInfo youngestPerson;
if (p1.getAge() < p2.getAge())
{
youngestPerson = p1;
}
else
{
youngestPerson = p2;
}
if (youngestPerson.getAge() > p3.getAge())
{
youngestPerson = p3;
}
youngestPerson.DisplayPerson();
}
}

⑷ 求助含有Junit测试代码的Java源代码,用来学习Junit

下面是我做项目时的例子,希望对你有所帮助。

/*
*@author ougaoyan ,date:2008-10-19
*/
package test;

import java.util.Date;

import junit.framework.TestCase;
import app.DA.BookDA;
import app.PD.Book;

public class TestBookDA extends TestCase {

public TestBookDA(String name){
super(name);
}
// public Book(int bookID, String cip, String name, String author,String press, String category, int quantity, int reborrowable,int borrowerID,Date startDate)
public void testEditBook(){
Book book1 = new Book(1,"123456","信号","张建","某出版社","电信",1,1,1,new Date());
Book book2 = new Book(-1,"123456","信号","张建","某出版社","电信"皮简,1,1,1,new Date());
Book book3 = new Book(99,"123456","信号","张建","某出版社","电信",1,1,1,new Date());
Book book4 = new Book(1,"123456","信号","张建","某出版社","电信",1,1,1,new Date());
assertEquals(true,BookDA.editBook(book1));
assertEquals(false,BookDA.editBook(book2));
assertEquals(true,BookDA.editBook(book3));
assertEquals(false,BookDA.editBook(book4));
}
////Book(int borrowerID, Date startDate, int reBorrowable, String cip)
public void testAddBook(){
Book book1 = new Book(0,"234567","信发号","张建","亮握枣某出版社","电信",1,1,1,new Date());
Book book2 = new Book(0,"123456","信的号","张建"敬拆,"某出版社","电信",1,1,1,new Date());
Book book3 = new Book(0,"99999","信i号","张建","某出版社","电信",1,1,1,new Date());
assertEquals(true,BookDA.addBook(book1));
assertEquals(true,BookDA.addBook(book2));
assertEquals(false,BookDA.addBook(book3));
}

public static void main(String[] args) {

junit.textui.TestRunner.run(TestBookDA.class);
System.out.println(new TestBookDA("TestBookDA").countTestCases());

}
}

/*
*@author ougaoyan ,date:2008-10-19
*/
package test;

import java.util.Date;
import java.util.Vector;
import junit.framework.TestCase;
import app.DA.CipDA;
import app.PD.Cip;

public class TestCipDA extends TestCase {
public TestCipDA (String name){
super(name);
}

public void testFindBooksByName(){
String name1 = "数据库";
String name2 = "小小";
Vector vector1 = CipDA.findBooksByName(name1);
Vector vector2 = CipDA.findBooksByName(name2);
assertNotNull(vector1);
assertNull(vector2);

}
public void testFindBooksByAuthor(){
String name1 = "欧阳";
String name2 = "小小";
Vector vector1 = CipDA.findBooksByAuthor(name1);
Vector vector2 = CipDA.findBooksByAuthor(name2);
assertNotNull(vector1);
assertNull(vector2);

}
public void testFindBooksByCategory(){
String name1 = "计算机";
String name2 = "计 算 机";
String name3 = "wucimin";
Vector vector1 = CipDA.findBooksByCategory(name1);
Vector vector2 = CipDA.findBooksByCategory(name2);
Vector vector3 = CipDA.findBooksByCategory(name3);
assertNotNull(vector1);
assertNotNull(vector2);
assertNull(vector3);
}
// public void testEditCip(){

// }
//public Cip(String cip, String name, String author, String press,String category, int quantity, int reserverID,Date reservedDate)
public void testAddCip(){
Cip cip1 = new Cip("2244","新加书","新者","出版社","计算机",3,0,new Date());
//Cip cip2 =new Cip(null,"新加书","新者","出版社","计算机",3,0,new Date());
Cip cip3 =new Cip("","新加书","新者","出版社","计算机",3,0,new Date());
assertEquals(true,CipDA.addCip(cip1));
//assertEquals(false,CipDA.addCip(cip2));
assertEquals(false,CipDA.addCip(cip3));
}
public void testEditCip(){
Cip cip1 = new Cip("2244","新加书","新者","出版社","计算机",3,0,new Date());
Cip cip2 =new Cip(null,"新加书","新者","出版社","计算机",3,0,new Date());
Cip cip3 =new Cip("","新加书","新者","出版社","计算机",3,0,new Date());
assertEquals(true,CipDA.editCip(cip1));
assertEquals(false,CipDA.editCip(cip2));
assertEquals(false,CipDA.editCip(cip3));
}

public static void main(String[] args) {

junit.textui.TestRunner.run(TestCipDA.class);
System.out.println(new TestCipDA("TestCipDA").countTestCases());

}
}

/*
*@author ougaoyan ,date:2008-10-19
*/
package test;
import java.util.Date;
import java.util.Vector;

import junit.framework.TestCase;
import app.DA.LibManagerDA;
import app.PD.LibManager;
import app.PD.Student;

public class TestLibManagerDA extends TestCase {

public TestLibManagerDA(String name){
super(name);
}
public void testCheck(){
String userName1 = "1234";
String password1 = "111111";
String userName2 = "";
String password2= "";
String userName3 = "1234";
String password3 = "111";
LibManager libmanager = new LibManager(1,userName1,password1,"w");
assertEquals(libmanager.getID(), LibManagerDA.check(userName1,password1).getID());
assertEquals(libmanager.getName(), LibManagerDA.check(userName1,password1).getName());
assertEquals(null, LibManagerDA.check(userName2,password2));
assertEquals(null, LibManagerDA.check(userName3,password3));
}

public void testUpdatePwd(){
int id1 =1,id2=99;
String qpwd="234567";
assertEquals(true,LibManagerDA.updatePwd(id1, qpwd));
assertEquals(false,LibManagerDA.updatePwd(id2, qpwd));
}
public void testSearchLibManager(){
LibManager libmanager1 = new LibManager(1,"1234","111111","w");
LibManager libmanager2 = new LibManager(2,"2345","23456","x");
assertEquals(libmanager1.getID(), LibManagerDA.searchLibManager("1234").getID());
assertEquals(libmanager2.getID(), LibManagerDA.searchLibManager("2345").getID());
assertNull(LibManagerDA.searchLibManager(""));
}
public void testQuantity(){
String cip1 = "123456";
String cip2 = "234567";
assertEquals(1,LibManagerDA.getBookQuantity(cip1));
assertEquals(3,LibManagerDA.getBookQuantity(cip2));
assertEquals(-1,LibManagerDA.getBookQuantity(""));
}
public void testEditLibManager(){
LibManager libmanager1 = new LibManager(1,"1234","111111","w");
LibManager libmanager2 = new LibManager(2,"2345","23456","x");
//LibManager libmanager3 = new LibManager(90,"123456677","0","y");
LibManager libmanager4 = new LibManager(3,"123456677","0","e");
assertEquals(true, LibManagerDA.editLibManager(libmanager1));
assertEquals(true,LibManagerDA.editLibManager(libmanager2));
//assertEquals(false,LibManagerDA.editLibManager(libmanager3));
assertEquals(false,LibManagerDA.editLibManager(libmanager4));
}
public void testAddLibManager(){
LibManager libmanager1 = new LibManager(-1,"1234","0","z");
LibManager libmanager2 = new LibManager(-1,"2345","0","x");
LibManager libmanager3 = new LibManager(-1,"12hh3456677","0","y");
assertEquals(true, LibManagerDA.addLibManager(libmanager1));
assertEquals(true,LibManagerDA.addLibManager(libmanager2));
assertEquals(false,LibManagerDA.addLibManager(libmanager3));
}
public void testGetBookCip(){
int bookid1 =1;
int bookid2 =4;
int bookid3 = -1;
String exceptedcip1 = "123456";
String exceptedcip2="234567";
String exceptedcip3 = null;
assertEquals(exceptedcip1, LibManagerDA.getBookCip(bookid1));
assertEquals(exceptedcip2, LibManagerDA.getBookCip(bookid2));
assertEquals(exceptedcip3, LibManagerDA.getBookCip(bookid3));

}
public void testCheckReserved(){
String cip1 = "123456";
String cip2 = "234567";
int studentid1 = 1;
int studentid2 = 2;
int studentid3 = 99;
assertEquals(true, LibManagerDA.checkReserved(cip1,studentid1));
assertEquals(true, LibManagerDA.checkReserved(cip2,studentid2));
assertEquals(false, LibManagerDA.checkReserved(cip1,studentid2));
assertEquals(false,LibManagerDA.checkReserved(cip2,studentid1));
assertEquals(false,LibManagerDA.checkReserved(cip2,studentid3));
assertEquals(false,LibManagerDA.checkReserved(cip1,studentid3));
}

//public static boolean borrowOPeration(int quantity, int bookID, int borrowerID,int reservedNum, int borrowedNum,Date startDate)
public void testBorrowOPeration(){
int quantity1 = 1;
int bookID1 = 2;
int borrowerID1 = 3;
int reservedNum1 = 1;
int borrowedNum1 = 4;
int quantity2 = 1;
int bookID2 = 1;
int borrowerID2 = 3;
int reservedNum2 = 0;
int borrowedNum2 = 7;
assertEquals(true,LibManagerDA.borrowOperation(quantity1, bookID1, borrowerID1, reservedNum1, borrowedNum1, new Date()));
assertEquals(false,LibManagerDA.borrowOperation(quantity2, bookID2, borrowerID2, reservedNum2, borrowedNum2, new Date()));
}
//public static boolean returnOperation (int quantity, int bookID, int borrowerID, int borrowedNum, Date startDate, int reborrowable)
public void testReturnOperation(){
int quantity1 = 1;
int bookID1 = 1;
int borrowerID1 = 1;
int borrowedNum1 = 4;
int reborrowable1 = 0;
int quantity2 = 1;
int bookID2 = 2;
int borrowerID2 = 2;
int borrowedNum2 = 7;
int reborrowable2 = 0;
assertEquals(true,LibManagerDA.returnOperation(quantity1, bookID1, borrowerID1, borrowedNum1, new Date(),reborrowable1));
assertEquals(true,LibManagerDA.returnOperation(quantity2, bookID2, borrowerID2, borrowedNum2,new Date(), reborrowable2));
}
public void testGetBookQuantity(){
String cip1 = "123456";
String cip2 = "234567";
int quantity1 = 5;
int quantity2 = 3;
assertEquals(quantity1,LibManagerDA.getBookQuantity(cip1));
assertEquals(quantity2,LibManagerDA.getBookQuantity(cip2));

}
public void testFindBill(){
String userName1 = "111111";
String userName2 = "222222";
float bill1 = 0;
float bill2 = 0;
assertEquals(bill1,LibManagerDA.findBill(userName1),0.000001);
assertEquals(bill2,LibManagerDA.findBill(userName2),0.000001);
}
public void testClearBill(){
String userName1 = "111111";
String userName2 = "222222";
assertEquals(true,LibManagerDA.clearBill(userName1));
assertEquals(true,LibManagerDA.clearBill(userName2));
}
public void testSelectAllLibManager(){
Vector allLibManager = new Vector();
allLibManager = LibManagerDA.selectAllLibManager();
for(int i = 0;i <allLibManager.size();i++ ){
LibManager libManager = (LibManager)allLibManager.get(i);
System.out.println(libManager.getName());
}
}
public void testSecelctAllStudent(){
Vector allStudent = new Vector();
allStudent = LibManagerDA.secelctAllStudent();
for(int i = 0;i <allStudent.size();i++ ){
Student student = (Student)allStudent.get(i);
System.out.println(student.getName());
}
}
public void testStartTimes(){
Vector times = new Vector();
times = LibManagerDA.startTimes(0);
for(int i = 0;i < times.size();i ++)
System.out.println((Date)times.get(i));
}
public void testUpdateStuBill() {

}

public static void main(String[] args) {

junit.textui.TestRunner.run(TestLibManagerDA.class);
System.out.println(new TestLibManagerDA("dd ").countTestCases());

}

}

/*
*@author ougaoyan ,date:2008-10-19
*/
package test;

import java.util.Date;
import java.util.Vector;

import junit.framework.TestCase;
import app.DA.StudentDA;
import app.PD.Book;
import app.PD.Cip;
import app.PD.Student;

public class TestStudentDA extends TestCase {

public TestStudentDA(String name){
super(name);
}
/*public Student(int id, int borrowedNum, int reservedNum, float bill,
String userName, String password, String name, String department,
String unit, String sex) */
public void testCheck(){
Student student1 = new Student(1,0,0,0,"111111","123456","张三","软件","0602","男");
//Student student2 = new Student(1,0,0,0,"111111","123","张三","软件","0602","男");
//Student student3 = new Student(2,0,0,0,"11111","123456","张三","软件","0602","男");
assertEquals(student1.getId(),StudentDA.check("111111","123456").getId());
assertEquals(true,student1.getId()==StudentDA.check("111111","123456").getId());
assertEquals(null,StudentDA.check("111111","12356"));

//assertEquals(false,);
//assertEquals(false,);
}
public void testUpdatePwd(){
int id1 = 1;
int id2 = 2;
int id3 = -1;
String password1 = "123456";
String password2 = "234567";
String password3 = "1234555";
assertEquals(true,StudentDA.updatePwd(id1, password1));
assertEquals(true,StudentDA.updatePwd(id2, password2));
assertEquals(false,StudentDA.updatePwd(id3, password3));

}
public void testSearchStudent(){
Student student1 = new Student(1,0,0,0,"111111","123456","张三","软件","0602","男");
assertEquals(true,student1.getId()==StudentDA.searchStudent("111111").getId());
assertEquals(null,StudentDA.searchStudent("111"));
}
public void testEditStudent(){
Student student1 = new Student(1,0,0,0,"111111","123456","张三","软件","0602","女");
Student student2 = new Student(1,0,0,0,"11111333555555555556631","123456","张三","软件","0602","女");
assertEquals(true,StudentDA.editStudent(student1));
assertEquals(false,StudentDA.editStudent(student2));
}
public void testAddStudent(){
Student student1 = new Student(5,0,0,0,"111111","123456","张三","软件","0602","女");
Student student2 = new Student(1,0,0,0,"1111133331","123456","张三","软件","0602","女");
assertEquals(true,StudentDA.addStudent(student1));
//assertEquals(false,StudentDA.addStudent(student2));
}
public static void testGetBorrowBookInfor(){
int studentid1 = 1;
int studentid2 = 2;
int studentid3 = 5;
Vector vector1 = new Vector();
Vector vector2 = new Vector();
Vector vector3 = new Vector();
vector1 = StudentDA.getBorrowBookInfor(studentid1);
vector2 = StudentDA.getBorrowBookInfor(studentid2);
vector3 = StudentDA.getBorrowBookInfor(studentid3);
//System.out.println(vector1);
assertNotNull(((Book)vector1.get(0)).getName());

}
public void testGetReserveBookInfor(){
int studentid1 = 1;
Vector vector1 = new Vector();
vector1 = StudentDA.getReserveBookInfor(studentid1);
assertEquals("数据库",((Cip)vector1.get(0)).getCategory());
}
//public static boolean reserveOperation(int reserverID1, String cip1, int quantity1, int reservedNum1, Date reservedDate1 )
public void testReserveOperation(){
assertEquals(true,StudentDA.reserveOperation(1, "123456", 1, 2, new Date()));
}
//void cancelReservation(int reserverID1,String cip1, int quantity1, int reserveNum1)
public void testCancelReservation(){
assertTrue(StudentDA.cancelReservation(1, "1234567", 1, 3));
}
//public static boolean updateReborrowable(int bookID, int reborrowable1, int studentID,Date startDate1)
public void testUpdateReborrowable(){
assertTrue(StudentDA.updateReborrowable(2,0,1,new Date()));
}
public static void main(String[] args) {

//junit.textui.TestRunner.run(TestStudentDA.class);
//System.out.println(new TestStudentDA("TestStudentDA").countTestCases());
testGetBorrowBookInfor();

}

}

/*
*@author ougaoyan ,date:2008-10-19
*/
package test;
import junit.framework.Test;
import junit.framework.TestSuite;

public class AllTests {

public static Test suite() {
TestSuite suite = new TestSuite("Test for test");
suite.addTestSuite(TestLibManagerDA.class);
suite.addTestSuite(TestBookDA.class);
suite.addTestSuite(TestCipDA.class);
suite.addTestSuite(TestStudentDA.class);
//suite.addTestSuite(TestLibManagerDA.class);
//$JUnit-END$
return suite;
}

}

⑸ java 方法的调用

首先说一下,你的程序代码有很多问题,下面只是为了给你改错。如果想了解更多问题可以联系我。
你使用了“System.out.println(stu.NAME());”但是程序中NAME()方法却是“void”无返回的方法定义,建议程序这样改:

public class Student{
// 如果
public String NAME(){
String name1="P";
return name1;
}

public int ID(){
int id1=19;
return id1;
}

public static void main(String[] args){
Student stu =new Student();
Student stu1 = new Student();
stu.NAME();
stu1.ID();
System.out.println(stu.NAME());
System.out.println(stu1.ID());
}

}

//下面是一个新的类和数组的应用
package student;

/**
* 学生类
*
* @author yaoming
*/
public class Student {

/** 学生ID */
private int id;
/** 学生姓名 */
private String name;
/** 学生年龄 */
private int age;

public Student(int id, String name, int age) {
super();
this.id = id;
this.name = name;
this.age = age;
}

public static void main(String[] args) {
// 创建一个学生数组(其中存放2个对象)
Student[] students = new Student[2];
students[0] = new Student(1, "张二", 25);
students[1] = new Student(2, "李三", 23);

// 循环打印student的信息
for (int i = 0; i < students.length; i++) {
System.out.println(students[i]);
}
}

public String toString() {
return "ID: " + id + " 姓名: " + name + " 年龄:" + age;
}

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getName() {
return name;
}

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

public int getAge() {
return age;
}

public void setAge(int age) {
this.age = age;
}

}

阅读全文

与id3算法java源码相关的资料

热点内容
信捷plc编程用哪个软件 浏览:939
vba导入文件 浏览:690
更新后版本英文怎么说 浏览:267
桌面云配置文件分离 浏览:505
iphone5如何升级4g网络 浏览:5
团购是在哪个app 浏览:897
打开多个word文档图片就不能显示 浏览:855
腾讯新闻怎么切换版本 浏览:269
app安装失败用不了 浏览:326
桌面文件鼠标点开会变大变小 浏览:536
手机误删系统文件开不了机 浏览:883
微信兔子甩耳朵 浏览:998
android蓝牙传文件在哪里 浏览:354
苹果6s软解是真的吗 浏览:310
c语言代码量大 浏览:874
最新网络卫星导航如何使用 浏览:425
以下哪些文件属于图像文件 浏览:774
zycommentjs 浏览:414
确认全血细胞减少看哪些数据 浏览:265
文件有哪些要求 浏览:484

友情链接