java相关
〖摘要:〗
使用XDoclet简化hibernate配置文件
这只是一个简单的事例,用来展示如何运用XDclote来为hibernate生成配置文件.是XDoclote之旅的开端
更多信息可从http://xdoclet.sf.net/上获取。
XDoclet面向属性编程
目录结构c:\eclipse\workspace\hibernate\src\hb\Cat.java
c:\eclipse\workspace\hibernate\src\example.java
c:\eclipse\workspace\hibernate\src\hibernate.cfg.xml
c:\eclipse\workspace\hibernate\src\xdoclet-lib\放置你所要的所有的包
c:\eclipse\workspace\hibernate\build.xml
相关hibernate.cfg.xml请参看蜗牛乐园的java分类的 如何让猫活起来 这篇文章。
xdoclet.jar , xdoclet-hibernate-module.jar, xdoclet-xdoclet-module.jar, xdoclet-xjavadoc.jar
这四个jar文件是为了运行hibernatedoclet必备的。
下面举例具体说明,如何运用XDoclet来解决配置文件变化和重构升级带来的困难。
Cat.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
<class
name="hb.Cat"
table="cat"
dynamic-update="false"
dynamic-insert="false"
>
<id
name="id"
column="CAT_ID"
type="java.lang.String"
unsaved-value="null"
>
<generator class="uuid.hex">
</generator>
</id>
<property
name="name"
type="java.lang.String"
update="true"
insert="true"
column="NAME"
/>
<!--
To add non XDoclet property mappings, create a file named
hibernate-properties-Cat.xml
containing the additional properties and place it in your merge dir.
-->
</class>
</hibernate-mapping>
这样一个简单的配置文件
下面让我们来看,如何运用XDoclet来自动生成他。
Cat.java是它的类,具体请注意其中加入的标识符。
package hb;
/**
* @hibernate.class table="cat"
*/
public class Cat {
private String id;
private String name;
public Cat() {
}
/**
* @hibernate.id column="CAT_ID" generator-class="uuid.hex" unsaved-value="null"
*/
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
/**
* @hibernate.property column="NAME"
*/
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String toString() {
String strCat = new
继续阅读其余的 7924 字
使用XDoclet简化hibernate配置文件
这只是一个简单的事例,用来展示如何运用XDclote来为hibernate生成配置文件.是XDoclote之旅的开端
更多信息可从http://xdoclet.sf.net/上获取。
XDoclet面向属性编程
目录结构c:\eclipse\workspace\hibernate\src\hb\Cat.java
c:\eclipse\workspace\hibernate\src\example.java
c:\eclipse\workspace\hibernate\src\hibernate.cfg.xml
c:\eclipse\workspace\hibernate\src\xdoclet-lib\放置你所要的所有的包
c:\eclipse\workspace\hibernate\build.xml
相关hibernate.cfg.xml请参看蜗牛乐园的java分类的 如何让猫活起来 这篇文章。
xdoclet.jar , xdoclet-hibernate-module.jar, xdoclet-xdoclet-module.jar, xdoclet-xjavadoc.jar
这四个jar文件是为了运行hibernatedoclet必备的。
下面举例具体说明,如何运用XDoclet来解决配置文件变化和重构升级带来的困难。
Cat.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
<class
name="hb.Cat"
table="cat"
dynamic-update="false"
dynamic-insert="false"
>
<id
name="id"
column="CAT_ID"
type="java.lang.String"
unsaved-value="null"
>
<generator class="uuid.hex">
</generator>
</id>
<property
name="name"
type="java.lang.String"
update="true"
insert="true"
column="NAME"
/>
<!--
To add non XDoclet property mappings, create a file named
hibernate-properties-Cat.xml
containing the additional properties and place it in your merge dir.
-->
</class>
</hibernate-mapping>
这样一个简单的配置文件
下面让我们来看,如何运用XDoclet来自动生成他。
Cat.java是它的类,具体请注意其中加入的标识符。
package hb;
/**
* @hibernate.class table="cat"
*/
public class Cat {
private String id;
private String name;
public Cat() {
}
/**
* @hibernate.id column="CAT_ID" generator-class="uuid.hex" unsaved-value="null"
*/
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
/**
* @hibernate.property column="NAME"
*/
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String toString() {
String strCat = new
继续阅读其余的 7924 字
air_tuyh
2005-05-14 21:10:42
阅读:3013
评论:0
引用:0
〖摘要:〗
lucene建立搜索
使用lucene来完成全文搜索需要两个步骤:
1 建立索引
2 构建查询并运行他们
lucene API可以和很容易的完成这个任务
下面我们来介绍索引的建立和搜索:
一:建立一个Cat实例的索引。
1:需要一个lucene的IndexWriter类的实例,它是lucene索引能力的入口,
同时它允许可查询数据被写到一个索引中。
public class CatSearcher{
String indexDir = "index"; // directory storing index files
private IndexWriter openIndexWriter() throws IOException {
Analyzer analyzer = new StandardAnalyzer();
return new IndexWriter(indexDir, analyzer, false);
}
}
2:构建索引后,建立包含Cat字段的文档,这个文件被索引。
public class CatSearcher{
//...
private Document buildDocument(Cat contact) {
Document document= new Document();
document.add(Field.Keyword("id",
String.valueOf(contact.getId())));
document.add(Field.Text("Name", contact.getName()));
return document;
}
<---------------->
在文件中有四种文件类型被引用:Field.Text, Field.UnIndexed, Field.Keyword
, Field.UnStored,使用的类型取决于字段的内容。
<---------------->
3.建立索引最后一步是向它添加文件
public class CatSearcher{
//...
private void index(Cat contact) throws IOException {
IndexWriter writer = openIndexWriter();
Document document = buildDocument(contact);
writer.addDocument(document);
writer.optimize();
writer.close();
}
批量添加要搜索的Document的时候,在批量末尾调用optimize()更有效。
optimize():优化磁盘中的索引以便进行更有效的检索。
二:搜索文件
public class CatSearcher{
//...
public Hits search(String fieldname, String criteria)
throws ParseException, IOException {
// open IndexSearcher
IndexSearcher searcher = new IndexSearcher(indexDir);
try {
Query query = buildQuery(fieldname, criteria);
Hits hits = searcher.search(query);
return hits;
} finally {
// searcher.close();
}
}
其中Query对象由bulideQuery()生成
public class CatSearcher{
//...
private Query buildQuery(String fieldName, String criteria)
throws ParseException {
Analyzer analyzer = new CustomAnalyzer();
QueryParser parser = new QueryParser(fieldName, analyzer);
return parser.parse(criteria);
}
<--------------------->
说了这么多,还是让我们来看看如何使用这些类进行搜索:
CatSearcher searcher = new CatSearcher();
//perform search
Hits hits = searcher.search("Name", "Bar or fred");
if (hits.length() == 0) {
// no results found
继续阅读其余的 2721 字
lucene建立搜索
使用lucene来完成全文搜索需要两个步骤:
1 建立索引
2 构建查询并运行他们
lucene API可以和很容易的完成这个任务
下面我们来介绍索引的建立和搜索:
一:建立一个Cat实例的索引。
1:需要一个lucene的IndexWriter类的实例,它是lucene索引能力的入口,
同时它允许可查询数据被写到一个索引中。
public class CatSearcher{
String indexDir = "index"; // directory storing index files
private IndexWriter openIndexWriter() throws IOException {
Analyzer analyzer = new StandardAnalyzer();
return new IndexWriter(indexDir, analyzer, false);
}
}
2:构建索引后,建立包含Cat字段的文档,这个文件被索引。
public class CatSearcher{
//...
private Document buildDocument(Cat contact) {
Document document= new Document();
document.add(Field.Keyword("id",
String.valueOf(contact.getId())));
document.add(Field.Text("Name", contact.getName()));
return document;
}
<---------------->
在文件中有四种文件类型被引用:Field.Text, Field.UnIndexed, Field.Keyword
, Field.UnStored,使用的类型取决于字段的内容。
<---------------->
3.建立索引最后一步是向它添加文件
public class CatSearcher{
//...
private void index(Cat contact) throws IOException {
IndexWriter writer = openIndexWriter();
Document document = buildDocument(contact);
writer.addDocument(document);
writer.optimize();
writer.close();
}
批量添加要搜索的Document的时候,在批量末尾调用optimize()更有效。
optimize():优化磁盘中的索引以便进行更有效的检索。
二:搜索文件
public class CatSearcher{
//...
public Hits search(String fieldname, String criteria)
throws ParseException, IOException {
// open IndexSearcher
IndexSearcher searcher = new IndexSearcher(indexDir);
try {
Query query = buildQuery(fieldname, criteria);
Hits hits = searcher.search(query);
return hits;
} finally {
// searcher.close();
}
}
其中Query对象由bulideQuery()生成
public class CatSearcher{
//...
private Query buildQuery(String fieldName, String criteria)
throws ParseException {
Analyzer analyzer = new CustomAnalyzer();
QueryParser parser = new QueryParser(fieldName, analyzer);
return parser.parse(criteria);
}
<--------------------->
说了这么多,还是让我们来看看如何使用这些类进行搜索:
CatSearcher searcher = new CatSearcher();
//perform search
Hits hits = searcher.search("Name", "Bar or fred");
if (hits.length() == 0) {
// no results found
继续阅读其余的 2721 字
air_tuyh
2005-05-14 21:07:56
阅读:2320
评论:0
引用:0
〖摘要:〗
hibernate如何让猫活起来
下面让我们用事例来阐述:
代码是从idiot那引入的,算是借花献佛了。
把数据库驱动放入C:\tomcat\common\lib\
修改C:\TOMCAT/conf/server.xml,在其中添加:
<Context path="/hibernate" docBase="hibernate">
<Resource name="jdbc/hibernate" scope="Shareable" type="javax.sql.DataSource"/>
<ResourceParams name="jdbc/hibernate">
<parameter>
<name>factory</name>
<value>org.apache.commons.dbcp.BasicDataSourceFactory</value>
</parameter>
<!-- DBCP database connection settings -->
<parameter>
<name>url</name>
<value>jdbc:mysql://localhost:3306/hibernate?useUnicode=true</value>
</parameter>
<parameter>
<name>driverClassName</name><value>org.gjt.mm.mysql.Driver</value>
</parameter>
<parameter>
<name>username</name>
<value>root</value>
</parameter>
<parameter>
<name>password</name>
<value>abc</value>
</parameter>
<!-- DBCP connection pooling options -->
<parameter>
<name>maxWait</name>
<value>3000</value>
</parameter>
<parameter>
<name>maxIdle</name>
<value>100</value>
</parameter>
<parameter>
<name>maxActive</name>
<value>10</value>
</parameter>
</ResourceParams>
</Context>
别忘了建立一张表,在mysql数据库,hibernate 库中
目录结构:c:\tomcat\webapps\hibernate\WEB-INF\classes\hb\Cat.class
c:\tomcat\webapps\hibernate\WEB-INF\classes\hb\HibernateUtil.class
c:\tomcat\webapps\hibernate\WEB-INF\classes\hb\Cat.hbm.xml
c:\tomcat\webapps\hibernate\WEB-INF\classes\hibernate.cfg.xml
c:\tomcat\webapps\hibernate\WEB-INF\lib\放置你所要的所有的包
c:\tomcat\webapps\hibernate\hb\addCat.jsp
c:\tomcat\webapps\hibernate\hb\getCat.jsp
<--------------------------------------------------------------->
Cat.java
package hb;
public class Cat {
private String id;
private String name;
public Cat() {
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String toString() {
String strCat = new StringBuffer()
.append(this.getId()).append(", ")
.append(this.getName()).append(", ")
.toString();
return strCat;
}
}
<--------------------------------------------------------->
HibernateUtil.java
package hb;
import net.sf.hibernate.*;
import net.sf.hibernate.cfg.*;
public class HibernateUtil {
private static 继续阅读其余的 9734 字
hibernate如何让猫活起来
下面让我们用事例来阐述:
代码是从idiot那引入的,算是借花献佛了。
把数据库驱动放入C:\tomcat\common\lib\
修改C:\TOMCAT/conf/server.xml,在其中添加:
<Context path="/hibernate" docBase="hibernate">
<Resource name="jdbc/hibernate" scope="Shareable" type="javax.sql.DataSource"/>
<ResourceParams name="jdbc/hibernate">
<parameter>
<name>factory</name>
<value>org.apache.commons.dbcp.BasicDataSourceFactory</value>
</parameter>
<!-- DBCP database connection settings -->
<parameter>
<name>url</name>
<value>jdbc:mysql://localhost:3306/hibernate?useUnicode=true</value>
</parameter>
<parameter>
<name>driverClassName</name><value>org.gjt.mm.mysql.Driver</value>
</parameter>
<parameter>
<name>username</name>
<value>root</value>
</parameter>
<parameter>
<name>password</name>
<value>abc</value>
</parameter>
<!-- DBCP connection pooling options -->
<parameter>
<name>maxWait</name>
<value>3000</value>
</parameter>
<parameter>
<name>maxIdle</name>
<value>100</value>
</parameter>
<parameter>
<name>maxActive</name>
<value>10</value>
</parameter>
</ResourceParams>
</Context>
别忘了建立一张表,在mysql数据库,hibernate 库中
目录结构:c:\tomcat\webapps\hibernate\WEB-INF\classes\hb\Cat.class
c:\tomcat\webapps\hibernate\WEB-INF\classes\hb\HibernateUtil.class
c:\tomcat\webapps\hibernate\WEB-INF\classes\hb\Cat.hbm.xml
c:\tomcat\webapps\hibernate\WEB-INF\classes\hibernate.cfg.xml
c:\tomcat\webapps\hibernate\WEB-INF\lib\放置你所要的所有的包
c:\tomcat\webapps\hibernate\hb\addCat.jsp
c:\tomcat\webapps\hibernate\hb\getCat.jsp
<--------------------------------------------------------------->
Cat.java
package hb;
public class Cat {
private String id;
private String name;
public Cat() {
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String toString() {
String strCat = new StringBuffer()
.append(this.getId()).append(", ")
.append(this.getName()).append(", ")
.toString();
return strCat;
}
}
<--------------------------------------------------------->
HibernateUtil.java
package hb;
import net.sf.hibernate.*;
import net.sf.hibernate.cfg.*;
public class HibernateUtil {
private static 继续阅读其余的 9734 字
air_tuyh
2005-05-11 21:36:48
阅读:1570
评论:0
引用:0
模拟人工生态代码中的count类没有给出,
补充为:
public class Count {
public void click() { value = value + 1 ; }
public int get() { return value; }
public void set(int x) { value = x; }
public String toString()
{ return String.valueOf(value); }
private int value;
}
air_tuyh
2005-04-29 18:35:26
阅读:792
评论:0
引用:0
〖摘要:〗
以前也贴过这些代码,由于world.java的代码,所以没有给出顶层类,
这次给出了predatorprey.java
但是整个程序还存在一个问题,world.java中的 eden()函数(文中有红色**号标记的部分),
在编译的时候给出错误 :不匹配的类型 imcompatible types
//PredatorPrey.java - top level class
class PredatorPrey {
public static void main(String[] args) {
World odd = new World(10), even = new World(10);
int i, cycles = 10;
even.eden(10); //generate initial World
System.out.println(even); //print initial state
for (i = 0; i < cycles; i++) {
System.out.println("Cycle = " + i + "\n\n");
if (i % 2 == 1) {
even.update(odd);
System.out.println(even);
}
else {
odd.update(even);
System.out.println(odd);
}
}
}
}
<------------------------------------------------------------------->
//World.java - square grid of life form cells
import tio.*;
class World {
World(int n) {
size = n; cells = new Living[n][n];
for (int i = 0; i < size; i++)
for (int j = 0; j < size; j++)
cells<i>[j] =new Empty(i,j);
}
public void clearNeighborCounts() {
Fox.neighborCount.set(0);
Rabbit.neighborCount.set(0);
Grass.neighborCount.set(0);
Empty.neighborCount.set(0);
}
public void eden(int size){
for (int i = 0; i < size; i++)
for (int j = 0; j < size; j++) {
char c = (char)Console.in.readChar();
while (c != 'F' && c != 'R'&& c != 'G'&& c != '.')
c = (char)Console.in.readChar();
**
cells<i> [j] = c;
}
}
public String toString() {
String t = "";
for (int i = 0; i < cells.length; i++)
for (int j = 0; j < cells<i>.length; j++) {
if ( (j + 1) % 10 == 0)
t = t + "\n" + cells<i>[j];
else
t = t + cells<i>[j];
}
return t;
}
public void update(World oldWorld) {
//borders are taboo
for (int i = 1; i < size - 1; i++)
for (int j = 1; j < size - 1; j++)
cells<i>[j] =
oldWorld.cells<i>[j].next(oldWorld);
}
Living[][] cells;
private int size ; //set in constructor继续阅读其余的 8176 字
以前也贴过这些代码,由于world.java的代码,所以没有给出顶层类,
这次给出了predatorprey.java
但是整个程序还存在一个问题,world.java中的 eden()函数(文中有红色**号标记的部分),
在编译的时候给出错误 :不匹配的类型 imcompatible types
//PredatorPrey.java - top level class
class PredatorPrey {
public static void main(String[] args) {
World odd = new World(10), even = new World(10);
int i, cycles = 10;
even.eden(10); //generate initial World
System.out.println(even); //print initial state
for (i = 0; i < cycles; i++) {
System.out.println("Cycle = " + i + "\n\n");
if (i % 2 == 1) {
even.update(odd);
System.out.println(even);
}
else {
odd.update(even);
System.out.println(odd);
}
}
}
}
<------------------------------------------------------------------->
//World.java - square grid of life form cells
import tio.*;
class World {
World(int n) {
size = n; cells = new Living[n][n];
for (int i = 0; i < size; i++)
for (int j = 0; j < size; j++)
cells<i>[j] =new Empty(i,j);
}
public void clearNeighborCounts() {
Fox.neighborCount.set(0);
Rabbit.neighborCount.set(0);
Grass.neighborCount.set(0);
Empty.neighborCount.set(0);
}
public void eden(int size){
for (int i = 0; i < size; i++)
for (int j = 0; j < size; j++) {
char c = (char)Console.in.readChar();
while (c != 'F' && c != 'R'&& c != 'G'&& c != '.')
c = (char)Console.in.readChar();
**
cells<i> [j] = c;
}
}
public String toString() {
String t = "";
for (int i = 0; i < cells.length; i++)
for (int j = 0; j < cells<i>.length; j++) {
if ( (j + 1) % 10 == 0)
t = t + "\n" + cells<i>[j];
else
t = t + cells<i>[j];
}
return t;
}
public void update(World oldWorld) {
//borders are taboo
for (int i = 1; i < size - 1; i++)
for (int j = 1; j < size - 1; j++)
cells<i>[j] =
oldWorld.cells<i>[j].next(oldWorld);
}
Living[][] cells;
private int size ; //set in constructor继续阅读其余的 8176 字
air_tuyh
2005-04-28 19:16:13
阅读:851
评论:0
引用:0
〖摘要:〗
这是一个简化java中输入输出的包。平时自己只是用到其中的少数的几个,
把它贴出来,希望能给学习java的人带来方便。
Console.java
package tio;
import java.io.*;
/**
* The class <code>Console</code> is a convenience class.
* It contains a static variable <code>in</code> that is
* initialized to refer to a ReadInput object, reading
* from the standard input stream System.in.
* It also contains a static variable <code>out</code>
* that is initialized to refer to a FormattedWriter,
* writing to the output stream System.out.
*/
public class Console {
public final static ReadInput in =
new ReadInput(new InputStreamReader(System.in));
public final static FormattedWriter out =
new FormattedWriter(System.out);
}
<------------------------------------------------------------------->
ReadException.java
package tio;
import java.io.*;
/**
* The class <code>ReadException</code> is used to convert
* java.io.IOExceptions into a subtype of
* RuntimeException. By doing this, users of ReadInput
* methods do not need to use throws declarations,
* simplifying beginning programs. Subtypes of
* RuntimeException do not need to be declared using
* a throws clause.
*
* @author C. E. McDowell
* @version 1.1, Released for Java By Dissection
*
*/
public class ReadException extends RuntimeException {
/**
* Constructs a ReadException object with no
* specific message.
*/
public ReadException() {
super();
}
/**
* Constructs a ReadException object with the
* specified message.
*
* @param message the error message
*/
public ReadException(String message) {
super(message);
}
}
<-------------------------------------------------------------------->
PrintFileWriter.java
package tio;
import java.io.*;
/**
* The class <code>PrintFileWriter</code> is a
* convenience class. It adds one constructor to its
* parent class, PrintWriter. This new constructor
* takes the name of a file.
* <p>
* <code>new PrintFileWriter(fileName)</code> is the
* same as <code>
* new PrintWriter(new FileWriter(fileName))
* </code>.
*/
public class PrintFileWriter extends PrintWriter {
public PrintFileWriter(String filename)
throws IOException
{
super(new FileWriter(filename));
}
}
<-------------------------------------------------------------------->
ReadInput.java
package tio;
import java.io.*;
/**
* The class <code>ReadInput</code> contains methods that
* allow for simple input of numbers, strings and
* characters from a text stream.
*
* @author C. E. McDowell
* @version 1.1, release for Java By Dissection
*/
public class ReadInput {
/**
* Constructs a ReadInput object for reading from any
* Reader object.
*
* @param input the Reader text stream to read
继续阅读其余的 35603 字
这是一个简化java中输入输出的包。平时自己只是用到其中的少数的几个,
把它贴出来,希望能给学习java的人带来方便。
Console.java
package tio;
import java.io.*;
/**
* The class <code>Console</code> is a convenience class.
* It contains a static variable <code>in</code> that is
* initialized to refer to a ReadInput object, reading
* from the standard input stream System.in.
* It also contains a static variable <code>out</code>
* that is initialized to refer to a FormattedWriter,
* writing to the output stream System.out.
*/
public class Console {
public final static ReadInput in =
new ReadInput(new InputStreamReader(System.in));
public final static FormattedWriter out =
new FormattedWriter(System.out);
}
<------------------------------------------------------------------->
ReadException.java
package tio;
import java.io.*;
/**
* The class <code>ReadException</code> is used to convert
* java.io.IOExceptions into a subtype of
* RuntimeException. By doing this, users of ReadInput
* methods do not need to use throws declarations,
* simplifying beginning programs. Subtypes of
* RuntimeException do not need to be declared using
* a throws clause.
*
* @author C. E. McDowell
* @version 1.1, Released for Java By Dissection
*
*/
public class ReadException extends RuntimeException {
/**
* Constructs a ReadException object with no
* specific message.
*/
public ReadException() {
super();
}
/**
* Constructs a ReadException object with the
* specified message.
*
* @param message the error message
*/
public ReadException(String message) {
super(message);
}
}
<-------------------------------------------------------------------->
PrintFileWriter.java
package tio;
import java.io.*;
/**
* The class <code>PrintFileWriter</code> is a
* convenience class. It adds one constructor to its
* parent class, PrintWriter. This new constructor
* takes the name of a file.
* <p>
* <code>new PrintFileWriter(fileName)</code> is the
* same as <code>
* new PrintWriter(new FileWriter(fileName))
* </code>.
*/
public class PrintFileWriter extends PrintWriter {
public PrintFileWriter(String filename)
throws IOException
{
super(new FileWriter(filename));
}
}
<-------------------------------------------------------------------->
ReadInput.java
package tio;
import java.io.*;
/**
* The class <code>ReadInput</code> contains methods that
* allow for simple input of numbers, strings and
* characters from a text stream.
*
* @author C. E. McDowell
* @version 1.1, release for Java By Dissection
*/
public class ReadInput {
/**
* Constructs a ReadInput object for reading from any
* Reader object.
*
* @param input the Reader text stream to read
继续阅读其余的 35603 字
air_tuyh
2005-04-27 21:28:09
阅读:1891
评论:2
引用:0
〖摘要:〗
这是个很简单的游戏,也许你以前还玩过呢。最近发现自己的电子词典上有一些
小游戏,所以对这方面挺感兴趣的。而且我觉得一些有意义的程序可以引发你对
学习这种语言的兴趣。
游戏规则:共21颗石子,由电脑和人交替拿石子,一次允许拿1到3个石子,谁最
后把石子拿完,谁胜出。
想想看,玩这个游戏的时候,用什么方法可以立于不败之地哦?
TwentyOnePickup.java
import tio.*;
public class TwentyOnePickup {
public static void main(String[] args) {
printInstructions();
int numberOfStones = 21;
boolean playerMovedLast = false;
while (numberOfStones > 0) {
numberOfStones = playerMove(numberOfStones);
playerMovedLast = true;
if (numberOfStones > 0){
numberOfStones = computerMove(numberOfStones);
playerMovedLast = false;
}
}
if (playerMovedLast)
System.out.println("Congratulations, you won.");
else
System.out.println("Better luck next time.");
}
static void printInstructions() {
System.out.println(
"The object of this game is to remove the last"
+ " stone.\n"
+ "There are 21 stones in the pile to start"
+ " with.\n"
+ "You may remove from 1 to 3 stones on each"
+ " move.\n"
+ "Good Luck!");
}
static int playerMove(int numberOfStones) {
int move = getUserMove(numberOfStones);
numberOfStones = numberOfStones - move;
System.out.println("There are " + numberOfStones
+ " stones remaining.");
return numberOfStones;
}
static int computerMove(int numberOfStones) {
int move;
if (numberOfStones <=3) {
move = numberOfStones; /* remove the rest */
}
else {
move = numberOfStones%4;
if (move == 0) move = 1;
}
numberOfStones = numberOfStones - move;
System.out.println("The computer removes " + move
+ " stones leaving " + numberOfStones + ".");
return numberOfStones;
}
static int getUserMove(int numberOfStones) {
System.out.println("Your move - how many stones"
+ " do you wish to remove?");
int move = Console.in.readInt();
while (move > numberOfStones || move < 1 ||
move > 3) {
if (numberOfStones >= 3)
System.out.println("Sorry," +
" you can only remove 1 to 3 stones.");
else
System.out.println("Sorry, you can only "
继续阅读其余的 570 字
这是个很简单的游戏,也许你以前还玩过呢。最近发现自己的电子词典上有一些
小游戏,所以对这方面挺感兴趣的。而且我觉得一些有意义的程序可以引发你对
学习这种语言的兴趣。
游戏规则:共21颗石子,由电脑和人交替拿石子,一次允许拿1到3个石子,谁最
后把石子拿完,谁胜出。
想想看,玩这个游戏的时候,用什么方法可以立于不败之地哦?
TwentyOnePickup.java
import tio.*;
public class TwentyOnePickup {
public static void main(String[] args) {
printInstructions();
int numberOfStones = 21;
boolean playerMovedLast = false;
while (numberOfStones > 0) {
numberOfStones = playerMove(numberOfStones);
playerMovedLast = true;
if (numberOfStones > 0){
numberOfStones = computerMove(numberOfStones);
playerMovedLast = false;
}
}
if (playerMovedLast)
System.out.println("Congratulations, you won.");
else
System.out.println("Better luck next time.");
}
static void printInstructions() {
System.out.println(
"The object of this game is to remove the last"
+ " stone.\n"
+ "There are 21 stones in the pile to start"
+ " with.\n"
+ "You may remove from 1 to 3 stones on each"
+ " move.\n"
+ "Good Luck!");
}
static int playerMove(int numberOfStones) {
int move = getUserMove(numberOfStones);
numberOfStones = numberOfStones - move;
System.out.println("There are " + numberOfStones
+ " stones remaining.");
return numberOfStones;
}
static int computerMove(int numberOfStones) {
int move;
if (numberOfStones <=3) {
move = numberOfStones; /* remove the rest */
}
else {
move = numberOfStones%4;
if (move == 0) move = 1;
}
numberOfStones = numberOfStones - move;
System.out.println("The computer removes " + move
+ " stones leaving " + numberOfStones + ".");
return numberOfStones;
}
static int getUserMove(int numberOfStones) {
System.out.println("Your move - how many stones"
+ " do you wish to remove?");
int move = Console.in.readInt();
while (move > numberOfStones || move < 1 ||
move > 3) {
if (numberOfStones >= 3)
System.out.println("Sorry," +
" you can only remove 1 to 3 stones.");
else
System.out.println("Sorry, you can only "
继续阅读其余的 570 字
air_tuyh
2005-04-27 21:26:39
阅读:769
评论:0
引用:0
第一次成功的通过了一个会话bean,远程调用.
代码参看蜗牛乐园中java分类相关文章.
代码参看蜗牛乐园中java分类相关文章.
air_tuyh
2005-04-10 18:57:35
阅读:802
评论:0
引用:0
〖摘要:〗
<?xml version="1.0" encoding="UTF-8"?>
<project default="_generation_" name="Packaging Generator">
<target name="_generation_" depends="N10004"/>
<target name="N10004" description="node.jar">
<jar destfile="node.jar" manifest="c:/eclipse/workspace/NodeIDE/src/META-INF/MANIFEST.MF">
<zipfileset dir="c:/eclipse/workspace/NodeIDE/src/META-INF" prefix="META-INF">
<include name="ejb-jar.xml"/>
</zipfileset>
<zipfileset dir="c:/eclipse/workspace/NodeIDE/src/META-INF" prefix="META-INF">
<include name="jboss.xml"/>
</zipfileset>
<zipfileset dir="c:/eclipse/workspace/NodeIDE/src/META-INF" prefix="META-INF">
<include name="jbosscmp-jdbc.xml"/>
</zipfileset>
<zipfileset dir="c:/eclipse/workspace/NodeIDE/bin" includes="**/*.class"/>
</jar>
</target>
</project>
<?xml version="1.0" encoding="UTF-8"?>
<project default="_generation_" name="XDoclet Generator">
<path id="xdoclet.classpath"><pathelement location="c:/eclipse/workspace/NodeIDE/bin"/>
<pathelement location="c:/eclipse/workspace/NodeIDE/lib/commons-httpclient.jar"/>
<pathelement location="c:/eclipse/workspace/NodeIDE/lib/commons-logging.jar"/>
<pathelement location="c:/eclipse/workspace/NodeIDE/lib/javax.servlet.jar"/>
<pathelement location="c:/eclipse/workspace/NodeIDE/lib/jbossall-client.jar"/>
<pathelement location="c:/eclipse/workspace/NodeIDE/lib/jboss-client.jar"/>
<pathelement location="c:/eclipse/workspace/NodeIDE/lib/jboss-j2ee.jar"/>
<pathelement location="c:/eclipse/workspace/NodeIDE/lib/jnp-client.jar"/>
<pathelement location="c:/eclipse/workspace/NodeIDE/lib/junit.jar"/>
<pathelement location="c:/eclipse/workspace/NodeIDE/lib/xercesImpl.jar"/>
<pathelement location="c:/eclipse/workspace/NodeIDE/lib/xmlParserAPIs.jar"/>
<fileset dir="c:/eclipse/plugins/org.jboss.ide.eclipse.xdoclet.core_1.4.1/">
<include name="*.jar"/>
</fileset>
</path>
<target
继续阅读其余的 2063 字
<?xml version="1.0" encoding="UTF-8"?>
<project default="_generation_" name="Packaging Generator">
<target name="_generation_" depends="N10004"/>
<target name="N10004" description="node.jar">
<jar destfile="node.jar" manifest="c:/eclipse/workspace/NodeIDE/src/META-INF/MANIFEST.MF">
<zipfileset dir="c:/eclipse/workspace/NodeIDE/src/META-INF" prefix="META-INF">
<include name="ejb-jar.xml"/>
</zipfileset>
<zipfileset dir="c:/eclipse/workspace/NodeIDE/src/META-INF" prefix="META-INF">
<include name="jboss.xml"/>
</zipfileset>
<zipfileset dir="c:/eclipse/workspace/NodeIDE/src/META-INF" prefix="META-INF">
<include name="jbosscmp-jdbc.xml"/>
</zipfileset>
<zipfileset dir="c:/eclipse/workspace/NodeIDE/bin" includes="**/*.class"/>
</jar>
</target>
</project>
<?xml version="1.0" encoding="UTF-8"?>
<project default="_generation_" name="XDoclet Generator">
<path id="xdoclet.classpath"><pathelement location="c:/eclipse/workspace/NodeIDE/bin"/>
<pathelement location="c:/eclipse/workspace/NodeIDE/lib/commons-httpclient.jar"/>
<pathelement location="c:/eclipse/workspace/NodeIDE/lib/commons-logging.jar"/>
<pathelement location="c:/eclipse/workspace/NodeIDE/lib/javax.servlet.jar"/>
<pathelement location="c:/eclipse/workspace/NodeIDE/lib/jbossall-client.jar"/>
<pathelement location="c:/eclipse/workspace/NodeIDE/lib/jboss-client.jar"/>
<pathelement location="c:/eclipse/workspace/NodeIDE/lib/jboss-j2ee.jar"/>
<pathelement location="c:/eclipse/workspace/NodeIDE/lib/jnp-client.jar"/>
<pathelement location="c:/eclipse/workspace/NodeIDE/lib/junit.jar"/>
<pathelement location="c:/eclipse/workspace/NodeIDE/lib/xercesImpl.jar"/>
<pathelement location="c:/eclipse/workspace/NodeIDE/lib/xmlParserAPIs.jar"/>
<fileset dir="c:/eclipse/plugins/org.jboss.ide.eclipse.xdoclet.core_1.4.1/">
<include name="*.jar"/>
</fileset>
</path>
<target
继续阅读其余的 2063 字
air_tuyh
2005-04-10 18:54:25
阅读:698
评论:0
引用:0
〖摘要:〗
/*
* Generated by XDoclet - Do not edit!
*/
package com.liuyang.ejb.cmp.node;
/**
* Remote interface for Node.
* @author liuyang
*/
public interface Node
extends javax.ejb.EJBObject
{
public void setTitle( java.lang.String title )
throws java.rmi.RemoteException;
public java.lang.String getTitle( )
throws java.rmi.RemoteException;
public void setText( java.lang.String text )
throws java.rmi.RemoteException;
public java.lang.String getText( )
throws java.rmi.RemoteException;
}
/*
* Created on 2003-7-21
*/
package com.liuyang.ejb.cmp.node;
import java.rmi.RemoteException;
import javax.ejb.CreateException;
import javax.ejb.EJBException;
import javax.ejb.EntityBean;
import javax.ejb.EntityContext;
import javax.ejb.RemoveException;
/**
* @author liuyang
* @ejb.bean description="NodeBean"
* display-name="NodeBean"
* jndi-name="cmp/node"
* name="Node"
* primkey-field="title"
* schema="node"
* type="CMP"
* view-type="remote"
* cmp-version = "2.x"
*
*
* @ejb.finder signature="java.util.Collection findAll()"
* query = ""
*
* @jboss.query signature ="java.util.Collection findAll()"
* query = "SELECT OBJECT(g) FROM node g"
*
* @ejb.finder query=""
* signature="java.util.Collection findByTitle(java.lang.String title)"
*
* @jboss.declared-sql signature = "java.util.Collection findByTitle(java.lang.String title)"
* where = "title = {0}"
*
*
*/
public abstract class NodeBean implements EntityBean {
/**
* @ejb.interface-method
* view-type = "remote"
*/
public abstract void setTitle(String title);
/**
* @return
*
* @ejb.pk-field
*
* @ejb.persistent-field
*
* @ejb.interface-method
* view-type = "remote"
*
*/
public abstract String getTitle();
/**
* @ejb.interface-method
* view-type = "remote"
*/
public abstract void setText(String text);
/**
* @return
*
* @ejb.pk-field
*
* @ejb.persistent-field
*
* @ejb.interface-method
* view-type = "remote"
*
*/
public abstract String getText();
/**
* @param title
* @param text
* @return
* @throws CreateException
* @ejb.create-method
*
*/
public String ejbCreate(String title,String text) throws CreateException
{
setTitle(title);
setText(text);
return title;
}继续阅读其余的 9093 字
/*
* Generated by XDoclet - Do not edit!
*/
package com.liuyang.ejb.cmp.node;
/**
* Remote interface for Node.
* @author liuyang
*/
public interface Node
extends javax.ejb.EJBObject
{
public void setTitle( java.lang.String title )
throws java.rmi.RemoteException;
public java.lang.String getTitle( )
throws java.rmi.RemoteException;
public void setText( java.lang.String text )
throws java.rmi.RemoteException;
public java.lang.String getText( )
throws java.rmi.RemoteException;
}
/*
* Created on 2003-7-21
*/
package com.liuyang.ejb.cmp.node;
import java.rmi.RemoteException;
import javax.ejb.CreateException;
import javax.ejb.EJBException;
import javax.ejb.EntityBean;
import javax.ejb.EntityContext;
import javax.ejb.RemoveException;
/**
* @author liuyang
* @ejb.bean description="NodeBean"
* display-name="NodeBean"
* jndi-name="cmp/node"
* name="Node"
* primkey-field="title"
* schema="node"
* type="CMP"
* view-type="remote"
* cmp-version = "2.x"
*
*
* @ejb.finder signature="java.util.Collection findAll()"
* query = ""
*
* @jboss.query signature ="java.util.Collection findAll()"
* query = "SELECT OBJECT(g) FROM node g"
*
* @ejb.finder query=""
* signature="java.util.Collection findByTitle(java.lang.String title)"
*
* @jboss.declared-sql signature = "java.util.Collection findByTitle(java.lang.String title)"
* where = "title = {0}"
*
*
*/
public abstract class NodeBean implements EntityBean {
/**
* @ejb.interface-method
* view-type = "remote"
*/
public abstract void setTitle(String title);
/**
* @return
*
* @ejb.pk-field
*
* @ejb.persistent-field
*
* @ejb.interface-method
* view-type = "remote"
*
*/
public abstract String getTitle();
/**
* @ejb.interface-method
* view-type = "remote"
*/
public abstract void setText(String text);
/**
* @return
*
* @ejb.pk-field
*
* @ejb.persistent-field
*
* @ejb.interface-method
* view-type = "remote"
*
*/
public abstract String getText();
/**
* @param title
* @param text
* @return
* @throws CreateException
* @ejb.create-method
*
*/
public String ejbCreate(String title,String text) throws CreateException
{
setTitle(title);
setText(text);
return title;
}继续阅读其余的 9093 字
air_tuyh
2005-04-10 18:52:31
阅读:694
评论:0
引用:0
