2008年10月16日
上海交通大学java视频教程共31讲
If you are interested in java, you can spend time learning about it
public class TestPrintf{
public static void main(String args[]){
System.out.printf("%+8.3f",3.14);
System.out.println();
System.out.printf("%+-8.3f\n",3.14);
System.out.printf("%08.3f\n",3.14);
System.out.printf("%(8.3f\n",-3.14);
System.out.printf("%,f\n",2356.34);
System.out.printf("%x\n",0x4a3b);
System.out.printf("%#x\n",0x4a3b);
System.out.println("------------");
System.out.printf("你好:%s,%3d岁,工资%-7.2f\n","张三",38,15000.00);
System.out.printf("你好:%1$s,%2$3d岁,%2$#x岁\n","张三",38);
System.out.printf("%3d,%#<x",38);
}
}
摘要: Integer[] a = {3,25,12,79,48};
System.out.println(a);
System.out.println(Arrays.toStrin... 阅读全文
摘要: /** *//**
* 通过这个程序,测试散列映像的存储与遍历,方法的使用
*/
import java.util.HashMap;
import java.util.Collection;
import java.util.Iterator;
import java.util.Set;
public&nb... 阅读全文
摘要: /** *//**
* 通过这个程序,测试树映像的使用,表目集合的遍历
*/
import java.util.TreeMap;
import java.util.Map;
import java.util.Iterator;
import java.util.Set;
public class&... 阅读全文
摘要: /** *//** *//** *//**
* 我们设计的学生基本类
*/
class Student
{
private String strName = "";//学生姓名
... 阅读全文
摘要: /** *//** (C) 北大青鸟APTECH.
* 版权所有
*/
/** *//**
* 本程序导入所需的类.
*/
import java.io.File;
import java.io.BufferedR... 阅读全文
摘要: /** *//** (C) 北大青鸟APTECH.
* 版权所有
*/
/** *//**
* 本程序导入所需的类.
*/
import java.io.File;
import java.io.BufferedR... 阅读全文
摘要: /** *//** (C) 北大青鸟APTECH.
* 版权所有
*/
/** *//**
* 本程序导入所需的类.
*/
import java.io.File;
import java.io.BufferedR... 阅读全文
摘要: /**//* 北大青鸟APTECH
* 版权所有
*/
/** *//**
* 这个程序演示抽象类和方法的用法.
* @版本 1.0 2005 年 5 月 20 日
* @author M... 阅读全文
/**
* 测试数组元素拷贝
*/
public class ArrayCopy
{
public static void main(String[] args)
{
ArrayCopy aCopy = new ArrayCopy();
int[] a = {1, 2, 3, 4, 5};
int[] b = {10,20,30,40,50};
aCopy.copy(a, b);
}
public void copy(int[] from, int[] to)
{
System.out.println("第一个数组中的元素");
for (int i = 0; i < from.length; i++)
{
System.out.print(" " + from[i]);//打印出数组中的每一个元素
}
System.out.println("\n");
System.out.println("第二个数组中的元素");
for (int i = 0; i < to.length; i++)
{
System.out.print(" " + to[i]);//打印出数组中的每一个元素
}
System.out.println("\n\n将第一个数组拷贝到第二个数组\n");
System.arraycopy(from, 2, to, 0, 3);
System.out.println("拷贝完成后第二个数组中的元素");
for (int i = 0; i < to.length; i++)
{
System.out.print(" " + to[i]);//打印出数组中的每一个元素
}
}
}
输出结果是3 4 5 40 50
摘要: <%@page language="java" import="java.util.*,java.sql.*,Oper.*,voo.*" pageEncoding="GBK"%>
<table border=1>
<tr>
<th>编号</th>... 阅读全文
import java.util.Date;
import java.util.Map;
import java.util.TreeMap;
class SuppressWarningsTest
{
public static void main(String[] args)
{
Map<String,Date> map=new TreeMap<String,Date>();
map.put("hello",new Date());
System.out.println(map.get("hello"));
}
}
import java.util.Date;
import java.util.Map;
import java.util.TreeMap;
class SuppressWarningsTest2
{
public static void main(String[] args)
{
Map map=new TreeMap();
//Map<String,Date> map=new TreeMap<String,Date>();
map.put("hello",new Date());
System.out.println(map.get("hello"));
}
}
import java.util.Date;
import java.util.Map;
import java.util.TreeMap;
class SuppressWarningsTest3
{
@SuppressWarnings("unchecked")
public static void main(String[] args)
{
Map map=new TreeMap();
//Map<String,Date> map=new TreeMap<String,Date>();
map.put("hello",new Date());
System.out.println(map.get("hello"));
}
}
import java.util.Date;
import java.util.Map;
import java.util.TreeMap;
class DeprecatedTest
{
public void doSomething(){
System.out.println("guoxinghua");
}
public static void main(String[] args)
{
DeprecatedTest test=new DeprecatedTest();
test.doSomething();
}
}
class SuppressWarningsTest2
{
public static void main(String[] args)
{
Map map=new TreeMap();
//Map<String,Date> map=new TreeMap<String,Date>();
map.put("hello",new Date());
System.out.println(map.get("hello"));
}
}
import java.util.Date;
import java.util.Map;
import java.util.TreeMap;
class DeprecatedTest
{
public void doSomething(){
System.out.println("guoxinghua");
}
public static void main(String[] args)
{
DeprecatedTest test=new DeprecatedTest();
test.doSomething();
}
}
class SuppressWarningsTest2
{
@SuppressWarnings({"unchecked","deprecation"})
public static void main(String[] args)
{
Map map=new TreeMap();
//Map<String,Date> map=new TreeMap<String,Date>();
map.put("hello",new Date());
System.out.println(map.get("hello"));
DeprecatedTest test=new DeprecatedTest();
test.doSomething();
}
}
摘要:
/**//*
*测试包含对象的克隆及clone方法的重写
*/
import java.util.GregorianCalendar;
import java.util.Date;
public class CloneTest{
public static void main(String[]&nbs... 阅读全文
package cn.mldn.lxh.servlet ;
import java.io.* ;
import javax.servlet.* ;
import javax.servlet.http.* ;
public class FormServlet extends HttpServlet
{
private ServletConfig config = null ;
public void init(ServletConfig config) throws ServletException
{
this.config = config ;
}
// 表示处理get请求
public void doGet(HttpServletRequest req,HttpServletResponse resp) throws IOException,ServletException
{
// System.out.println("** Servlet doGet处理提交参数 ") ;
this.doPost(req,resp) ;
}
// 处理post请求
public void doPost(HttpServletRequest req,HttpServletResponse resp) throws IOException,ServletException
{
String name = req.getParameter("uname") ;
// 取得application对象
// ServletContext app = this.getServletContext() ;
ServletContext app = this.config.getServletContext() ;
app.setAttribute("addr","www.MLDN.cn") ;
// 取得一个session对象
HttpSession session = req.getSession() ;
session.setAttribute("sname",name) ;
// System.out.println("** Servlet doPost处理提交参数 ") ;
System.out.println("name = "+name) ;
// 重定向
resp.sendRedirect("demo.jsp") ;
}
};
/**//*
<servlet>
<servlet-name>form</servlet-name>
<servlet-class>cn.mldn.lxh.servlet.FormServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>form</servlet-name>
<url-pattern>/formServlet</url-pattern>
</servlet-mapping>
*/
初始化两种方法
有参数与无参数
有参数的方法优先
多个地址可以映射到同一个SERVLET
配置初始化参数
form表单
<form action="formServlet" method="post">
用户名:<input type="text" name="uname">
<input type="submit" value="提交">
</form>
下面是处理表单的servlet
package cn.mldn.lxh.servlet ;
import java.io.* ;
import javax.servlet.* ;
import javax.servlet.http.* ;
public class InitParameterServlet extends HttpServlet
{
// 初始化
// 要取得初始化参数,必须使用以下初始化方法
public void init(ServletConfig config) throws ServletException
{
// config对象中有取得初始化参数的方法:getInitParameter("参数名称")
String ref1 = config.getInitParameter("ref1") ;
String ref2 = config.getInitParameter("ref2") ;
String dd = config.getInitParameter("DBDRIVER") ;
System.out.println("REF1 => "+ref1) ;
System.out.println("REF2 => "+ref2) ;
System.out.println("DBDRIVER => "+dd) ;
}
// 表示处理get请求
public void doGet(HttpServletRequest req,HttpServletResponse resp) throws IOException,ServletException
{
// System.out.println("** Servlet doGet处理 ") ;
}
// 处理post请求
public void doPost(HttpServletRequest req,HttpServletResponse resp) throws IOException,ServletException
{
// System.out.println("** Servlet doPost处理 ") ;
}
// 销毁
public void destroy()
{
// System.out.println("** Servlet 销毁 ") ;
}
};
/**//*
<servlet>
<servlet-name>param</servlet-name>
<servlet-class>cn.mldn.lxh.servlet.InitParameterServlet</servlet-class>
<load-on-startup>0</load-on-startup>
<init-param>
<param-name>ref1</param-name>
<param-value>MLDN</param-value>
</init-param>
<init-param>
<param-name>ref2</param-name>
<param-value>LiXingHua</param-value>
</init-param>
<init-param>
<param-name>DBDRIVER</param-name>
<param-value>oracle.jdbc.driver.OracleDriver</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>param</servlet-name>
<url-pattern>/ipar</url-pattern>
</servlet-mapping>
*/
学习资料下载
SERVLET是一种特殊的CGI
与CGI不同是多线程,性能很高
package cn.mldn.lxh.servlet ;
import java.io.* ;
// HttpServlet属于javax.servlet.http包下
// ServletException属于javax.servlet包下
import javax.servlet.* ;
// HttpServletRequest、HttpServletResponse存放在javax.servlet.http包下
import javax.servlet.http.* ;
public class SimpleServlet extends HttpServlet
{
// 表示处理get请求
public void doGet(HttpServletRequest req,HttpServletResponse resp) throws IOException,ServletException
{
PrintWriter out = resp.getWriter() ;
out.println("<HTML>") ;
out.println("<HEAD>") ;
out.println("<TITLE>THE FIRST SERVLET</TITLE>") ;
out.println("</HEAD>") ;
out.println("<BODY>") ;
out.println("<H1>Hello World!!!</H1>") ;
out.println("</BODY>") ;
out.println("</HTML>") ;
out.close() ;
}
public void doPost(HttpServletRequest req,HttpServletResponse resp) throws IOException,ServletException
{
this.doGet(request,response) ;
}
};
/**//*
<servlet>
<servlet-name>simple</servlet-name>
<servlet-class>cn.mldn.lxh.servlet.SimpleServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>simple</servlet-name>
<url-pattern>/demo</url-pattern>
</servlet-mapping>
*/
输出 Html用out.println()
很难维护
生命周期
生老病死(与人相似)
package cn.mldn.lxh.servlet ;
import java.io.* ;
import javax.servlet.* ;
import javax.servlet.http.* ;
public class LifeCycleServlet extends HttpServlet
{
// 初始化
public void init(ServletConfig config) throws ServletException
{
System.out.println("** Servlet 初始化 ") ;
}
// 表示处理get请求
public void doGet(HttpServletRequest req,HttpServletResponse resp) throws IOException,ServletException
{
System.out.println("** Servlet doGet处理 ") ;
}
// 处理post请求
public void doPost(HttpServletRequest req,HttpServletResponse resp) throws IOException,ServletException
{
System.out.println("** Servlet doPost处理 ") ;
}
// 销毁
public void destroy()
{
System.out.println("** Servlet 销毁 ") ;
}
};
/**//*
<servlet>
<servlet-name>life</servlet-name>
<servlet-class>cn.mldn.lxh.servlet.LifeCycleServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>life</servlet-name>
<url-pattern>/lc</url-pattern>
</servlet-mapping>
*/
init
doservices
destroy
创建要求
继承HttpServlet(先导入javax.servlet.*)必须在包中
在web-inf/class下
重写两个方法
doGet(req,resp)throws IOException(javax.servlet.http.*,java.io.*)
用PrintWriter输出
修改WEB.XML文件,映射SERVLET
<servlet>
<servlet-name>simple</servlet-name>
<servlet-class>cn.mldn.lxh.servlet.SimpleServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>simple</servlet-name>
<url-pattern>/demo</url-pattern>//在地址中输入的内容</servlet-mapping>
笔记下载
servlet只初始一次(在第一次使用servlet程序后,也可以在
容器启动时初始化servlet程序,通过修改web.xml文件)
<load-on-startup>1</load-on-startup>
doGet,doPost(doget是输入地址,doPost是表单请求)
destroy(服务器关闭,或者长时间不用)
注意
开发框架和配置
/**
*静态内部类的测试
*/
import java.util.Vector;
public class StaticInnerClass{
public static void main(String[] args){
Vector vec=new Vector();
Student tom=new Student("Tom","20020410");
Student jack=new Student("jack","20020411");
Studentsmith=new Student("Smith ","20020412");
Student rose=new Student("Rose","20020413");
tom.setStudentScore(456);
jack.setStudentScore(500);
smith.setStudentScore(634);
rose.setStudentScore(414);
vec.add(tom);
vec.add(jack);
vec.add(smith);
vec.add(rose);
ArrayScore.PairScore pair=ArrayScore.minMax(vec);
System.out.println("最高分数为:"+pair.getMaxScore());
System.out.println("最低的分数为:")+pair.getMinScore();
}}
class ArrayScore{
static class PairScore{
private double maxScore;
private double minScore;
public PairScore(double max,double min){
maxScore=max;
minScore=min;
}
public double getMaxScore(){
return maxScore;
}
public double getMinScore(){
return minScore;
}
}
public static PairScore minMax(Vector studentVec){
double minScore=((Student)studentVec.get(0)).getStudentScore();
double maxScore=((Student)studentVec.get(0)).getStudentScore();
for(int i=1;i<studentVec.size();i++){
double score=((Student)studentVec.get(i)).getStudentScore();
if(minScore>score)
minScore=score;
if(maxScore<score)
maxScore=score;
}
return new PairScore(maxScore,minScore);
}
}
/**
*我们设计的学生基本类
*/
class Student{
private String strName="";
private Stirng strNumber="";
private Stirng strSex="";
private String strBirthday="";
private String strSpeciality="";
private String strAddress="";
private double totalScore;//学生的总分数
public Student(String name,String number){
strName=name;
strNumber=number;
}
public String getStudentName(){
return strName;
}
public String getStudentNumber(){
return strNumber;
}
public void setStudentSex(String sex){
strSex=sex;
}
public String getStudentSex(){
return strSex;
}
public String getStudentBirthday(){
return strBirthday;
}
public void setStudentBirthday(String birthday){
strBirthday=birthday;
}
public String getStudentSpeciality(){
return strSpeciality;
}
public void setStudentSpeciality(String speciality) {
strSpeciality=speciality;
}
public String getStudentAddress(){
return strAddress;
}
public void setStudentAddress(String address){
strAddress=address;
}
public double getStudentScore(){
return totalScore;
}
public void setStudentScore(double score){
totalScore=score;
}
public String toString(){
String information ="student name="+strName+"student number="+strNumber;
if(!strSex.equals(""))
information+=",sex="+strSex;
if(!strBirthday.equals(""))
information+=",Birthday="+strBirthday;
if(!strSpeciality.equals(""))
information+=",专业="+strSpeciality;
if(!strAddress.equals(""))
information+=",Address="+strAddress;
return information;
}
}
摘要: /** *//**
* 我们设计的学生基本类
*/
class Student
{
private String strName = "";//学生姓名
private String&nb... 阅读全文
public interface Person{
String getName();
String getSex();
String getBirthday();
String getAddress();
void setAddress(String strAddress);
}
/**
*测试这个程序,测试对象类型的动态绑定
*/
public class ObjectVarTest
{
public static void main(String[] args){
B b=new B();
C.method3(b);
}
}
class A{
public void method1(){
System.out.println("this is class A method1");
}
}
class B extends A{
public void method1(){
System.out.println("this is class B method1");
public void method2(){
System.out.println("this is class B method2");
}
}
class c{
public static void method3(A a){
a.method1();
}}
}
/**
*i测试数组列表的使用
*/
import java.util.ArrayList;
public class ArrayList{
public static void main(String[] args){
ArrayList list=new ArrayList();
list.add(new Student("Tom","20020410"));
list.add(new Student("Jack","20020411"));
list.add(new Student("Rose","20020412"));
for(int i =0;i<list.size();i++)
{
System.out.println((Student)list.get(i));
}}}
class Student {
private String strName="";
private String strNumber="";
private String strSex="";
private String strBirthday="";
private String strAddress="";
public Student(String name,String number){
strName=name;
strNumber=number;
}
public String getStudentName(){
return strName;
}
public String getStudentNumber() {
return strNumber;
}
public String setStudentSex(String sex)
{
strSex=sex;
}
public String getStudentSex(){
return strSex;
}
public String getStudentBirthday(){
return strBirthday;
}
public void setStudentBirthday(String birthday){
strBirthday=birthday;
}
public String getStudentSpeciality(){
return strSpeciality;}
public void setStudentSpececiality(String speciality){
strSpeciality=speciality;
}
public void setStudentAddress(String address){
strAddress=address;
}
public String toString(){
String information="student name="+strName+",student number="+strNumber;
if(!strSex.equals(""))
information+=",sex="+strSex;
if(!strBirthday.equals(""))
information+=",birthday="+strBirthday;
if(!strSpeciality.equals(""))
information+=",专业="+strSpeciality;
if(!strAddress.equals(""))
information+=",籍贯="+strAddress;
return information;
}
}
<%@ page contentType="text/html;charset=gb2312"%>
<%@page import ="java.sql.*"%>
<%--
使用JDBC连接Oracle数据库
使用MLDN数据库
用户名:scott
密码:tiger
--%>
<%!
String DBDRIVER="oracle.jdbc.driver.OracleDriver";
String DBURL="jadb:oracle:thin:@localhost:1521:mldn";
String DBUSER="scott";
String DBPASSWORD="tiger";
Connection conn=null;
statement stmt=null;
%>
<%try{Class.forname(DBDRIVER);
conn=DriverManger.getConnection(DBURL,DBUSER,DBPASSWORD);
//create table
String sql="CREATE TABLE mldnab(name vachar(20))";
stmt=conn.createStatement();
stmt.executeUpdate(sql);
stmt.close();
conn.close();
}catch(Exception e){
out.println(e);
}
/**
*测试类构造器的调用顺序
*/
public class ConstructorTest{
public static void main(String[] args){
C c=new C("hello");
}
}
class A{
public A(){
System.out.println("this is A");
}
}
class C extends B{
public C(String str){
super(str);
System.out.println("this is C");
}
}
public class ToStringTest{
public static void main(String[] args){
System.out.println(new Student("youName","20031001"));
}
}
/**
*通过这个程序,展示字符串求取子串的方法
*/
public class SubStringTest{
public static void main(String[] args){
String str="I am a Programmer";//定义字符串
for(int i=0;i<str.length();i++){
System.out.println("这是第"+i+"个子串:"+str.subString(i));
}}}
/**
*读取键盘输入
*/
import javax.swing.JOptionPane;
public class InputTest{
public static void main(String[] args){
String strName=JOptionPane.showInputDialog("Input your name:");
String strAge=JOptionPane.showInputDialog("Input your age:");
int age=Integer.parseInt(strAge);
System.out.println("Welcome you :"+strName);
System.out.println("你还有"+(60-age)+"年可以退休了!!");
System.exit(0);
}
}
/**
*通过这个程序,测试格式器的使用方法
*/
import java.text.NumberFormat;
import java.util.locale;
public class FormateValue{
public static void main(String[] args){
double x=100000.0/3;
System.out.println("the value is :"+x);
//首先以默认地区的格式器来输出
System.out.println("接下来输出的是美国地区的格式器的结果:");
NumberFormat number=NumberFormat.getNumberInstance(Locale.US);
NumberFormat number=NumberFormat.getCurrencyInstance(Locale.US);
NumberFormat percent=NumberFormat.getPercentInstance(Locale.US);
FormatValue fm=new FormatValue();
fm.print(number,x);
fm.print(currency.x);
fm.print(percent,x);
System.out.println("接下来输出的是默认地区的格式器的结果:");
NubmerFormat nu=NumberFormat.getNumberInstance();
NubmerFormat cu=NumberFormat.getCurrencyInstance();
NubmerFormat be=NumberFormat.getPercentIntstance();
fm.print(nu,x);
fm.print(cu,x);
fm.print(pe,x);
}
public void print(NubmerFormat nu,double x){
System.out.println(nu.format(x));
}
}
/**
*查找特定的子串
*/
public class FindSubstring{
public static void main(String[] args){
String str="Welcome the body.";
System.out.println("Find the substring boy occurrence:"+str.indexOf("boy"));//查找子串
System.out.println("Find the substring by occurrence: "+str.indexOf("by"));//查找子串
System.out.println("Find the char t occurrence: "+str.indexOf('t'));//查找特定的字符
System.out.println("Test the string begin with wel.:"+str.startsWith("wel"));//是否以"wel"开始
System.out.pritnln("Test the string end with body.:"+str.endWith("boy"));//是否以"boy"结束
}
}
/*
*命令行参数的输入,从命令行输入的任何参数,对于java来讲都是用字符串处理的
*/
public class CommandInput{
public static void main(String[] args){
if(args.length==0){
System.out.println("你没有输出参数,程序退出");
}
else{
System.out.println("你一共输入了"+args.length+"个参数");
if(args[0].equals("h"))
System.out.print("hello");
if(args[0].equals("g"))
System.out.print("Bye-Bye");
for(int i=1;i<args.length;i++)
{
System.out.print(" "+args[i]);
}
}
}
}
public class CapacityTest{
public static void main(String[] args){
String buffer sb=new StringBuffer(10);
sb.append("this is a a");
System.out.println(sb.capacity());
System.out.println(sb.length());
}
}
/*
*数组排序及随机数的产生
*/
import java.util.Arrays;
import javax.swing.*
public class ArraySort{
public static void main(String[] args){
String strIn=JoptionPane.showInputDialog("请输入工共多少个彩球:");
String strOut=JOptionPane.showInputDialog("请输入需抽取多少个彩球:");
int in=Integer.parseInt(strIn);
int[] total=new int[in];//生成彩球总数数组
for(int i=0;i<in;i++){
total[i]=i+1;
}
int[] out=new int[Integer.parseInt(strOut)];
for(int i=0;i<out.length;i++){
int r=(int)Math.random()*in;//产生随机元素序列号
out[i]=total[r];
total[r]=total[in-1];//将最后一个元素移到当前位置,把取出的删除
in--;
System.out.print(" "+out[i]);}
Arrays.sort(out);
System.out.println("抽取的数字排序后是:");
for (int i=0;i<out.length;i++){
System.out.print(" "+out[i]);
}
System.exit(0);
}}
/**
*通过这个程序,我们要测试两个方面:
*1.匿名数组可以赋值与一个已经存在数组变量,不关心原来数组变量的容量
*2.数组变量的类型必须要与匿名数组的类型一致
public class ArrayTest{
public static void main(String[] args){
ArrayTest aTest=new ArrayTest();
int[] a={1,2,3};//声明一个新的数组,并赋值与一个新的数组变量
aTest.aprint(a);
a=new int[]{10,20,30,40,50};//将匿名数组赋值与存在的数组变量a
//a=new String[]{"a","b"};//将字符型匿名数组赋值与存在的数组变量a
aTest.print(a);
}
public void print(int[] array){
System.out.println(" 数组变量的长度是 "+array.length);
System.out.println(" 数组中的每一个元素是:");
for(int i=0;i<array.length;i++)
{
System.out.print(" "+array[i]);//打印出数组中的每一个元素
}System.out.println("\n*************");
}
}
/**
*测试数组元素传递方式
*/
public class ArrayPointer{
public static void main(String[] args){
ArrayPointer aPointer=new ArrayPinter();
int[] a={1,2,3,4,5};
System.out.println(打印数组a中的元素);
aPointer.print(a);
int[] b=a;
System.out.println("\n改变数组b中的第三个元素的值。\n ");
System.out.println("打印数组b中的元素。");
b[2]=a[2]+10;
aPointer.print(b);
System.out.println("再打印数组a中的元素。");
aPointer.print(b);
}
public void print(int[] array){
for(int i=0;i<array.length;i++){
System.out.println(" "+array[i]);//打印出数组中的每一个元素
}
System.out.println("\n*******************8");
}}
/**
*通过这个程序,测试数组元素默认值
*/
public class ArrayInitTest{
public static void main(String[] args)
{
ArrayInitTest aTest=new ArrayInitTest();
int[] a=new int[10];//声明一个新的整数数组,并赋值与一个新的数组变量
aTest.println(a);//输出数组元素到控制台
boolean[] c=new boolean[10];//声明一个新的布尔数组
aTest.pprintln(c);
}
public void printInt(int[] array){
System.out.println(" 整数型数组的长度是 "+array.length);
for(int i=0;i<5;i++){
array[i]=i;
}
System.out.println(" 数组中的每个元素是: ");
for(int i=0;i<array.length;i++){
System.out.println(" "+array[i]);//打印出数组中的每一个元素
}
System.out.println("\n******************************");
}
public void printString(String[] array){
System.out.println(" 字符型数组的长度是 "+array.length);
for(int i=0;i<5;i++){
array[i]="元素"+i;
}
System.out.println(" 数组中的每个元素是: ");
for(int i=0;i<array.length;i++){
System.out.println(" "+array[i]);//打印出数组中的每一个元素
}
System.out.println("\n****************************");
}
public void printBoolean(boolean[] array){
System.out.println(" 布尔型数组的长度是 "+array.legnth);
for(int i=0;i<5;i++){
if(i%2==0)
array[i]=true;
else
array[i]=false;
}
System.out.println(" 数组中的每个元素是: ");
for (int i=0;i<array.length;i++){
System.out.print(" "+array[i]);//打印出数组中的每一个元素
}
System.out.println("\n**********************************");
}}
/**
*测试数组元素的拷贝
*/
public class ArrayCopy{
public static void main(String[] args){
ArrayCopy aCopy=new ArrayCopy();
int[] a={1,2,3,4,5};
int[] b={10,20,30,40,50};
aCopy.copy(a,b);
}
public void copy(int[] from,int[] to){
System.out.println("第一个数组中的元素");
for(int i=0;i<from.length;i++){
System.out.println(" "+from[i]);
}System.out.println("\n");
System.out.println("第二个数组中的元素");
for(int i=0;i<to.length;i++){
System.out.println(" "+to[i]);//打印出数组中的每一个元素
}
System.out.println("\n\n将第一个数组拷贝到第二个数组\n");
System.arraycopy(from,2,to,0,3);
System.out.println("拷贝完成后第二个数组中的元素");
for(int i=0;i<to.length;i++){
System.out.println(" "+to[i]);//打印出数组中的每一个元素}}}
public class StudentTest3{
public static void main(String[] args){
Student tom=new Student("Tom","20020410");
tom.setStudentSex("man");
tom.setSutdentAddress("America");
System.out.println(tom.toString());
}
}
class Student{
private String strName="";
private String strNumber="";
private String sexSex="";
private String strBirthday="";
private String strSpeciality="";
private String strAddress="";
public static void main(String[] args){
Student aStudent=new Student("沙和尚","19990000");
aStudent.setStudentAddress("通天河");
System.out.println(aStudent.toString());
}
public Student (String name,String number){
strName=name;
strNumber=number;
}
public String getStudentName(){
return strName;
}
public void setStudentSex(String sex){
strSex=sex;
}
public String getStudentSex(){
return strSex;
}
public String getStudentBirthday(){
return strBirthday;
}
public void setStudentSpciality(String speciality){
strSpeciality=speciality;
}
public String getStudentAddress(){
return strAddress;
}
public void setStudentAddress(String address) {
strAddress=address;
}
public String toString(){
String information="学生姓名="+strName+",学号="+strNumber;
if(!strSex.equals("")){
information+=",性别="+strSex;
if(!strBirthday.equals(""))
information+=", 出生年月="+strBirthday;
if(!strSpeciality.equals(""))
information+=",专业="+strSpeciality;
if(!strAddress.equals(""))
information+=",籍贯="+strAddress;
return information;
}
}
}
/*
*学生类,包括学生的基本信息
*/
public class studentTest2{
public static void main(String[] args){
int i;
for(i=0;i<10;i++) {
Student tom=new Student("Tom"+i);
if(i%2==0){
tom.setStudentSex("man");
}else{
tom.setStudentSex("female");
}
tom.setStudentAddress("America");
tom.setStudentNumber();
System.out.println(tom.toString());
}}}
class Student{
private String strName="";
private int number=0;
private String strSex="";
private String strBirthday="";
private String strSpeciality="";
private String strAddress="";
private Static int nextNumber=1;
public Student(String name){
strName=name;
}
public String getStudentName(){
return strName;
}
public int getStudentNumber(){
return number;
}
public void setStudentNumber(){
number=nextNumber;
nextNumber++;
}
public void setStudentSex(String sex){
strSex=sex;
}
public String getStudentBirthday(){
return strBirthday;
}
public void setStudentBirthday(String birthday){
strBirthday=birthday;
}
public String getStudentSpeciality(){
return strSpciality;
}
public void setStudentSpeciality(String speciality){
strSpeciality=speciality;
}
public String getStudentAddress(){
return strAddress;
}
public String toString (){
String information="student name="+strName+",student number="+number;
if(!strSex.equals(""))
information +=",sex="+strSex;
if(!strBirthday.equals(""))
information+=",birthday="+strBirthday;
if(!strSpeciality.equals(""))
information+=",专业="+strSpeciality;
if(!strAddress.equals(""))
information+=",籍贯="+strAddress;
return information;
}}
/*
*学生类,包括学生的基本信息
*/
public class StudentTest1{
public static void main(){
Student1 tom=new Student1("Tom","20020410");
Student2 jack=new Student2("jack","20030911");
System.out.println(jack.toString());
System.out.println("通过公开字段,修改实例字段值。")
//tom.strName='唐僧";
jack.strName="孙悟空";
System.out.println(jack.toString());
}}
class Student1{
private String strName="";
private String strNumber="";
public Student1(String name,String number){
strName=name;
strNumber=number;
}
public String getStudentName(){
return strName;
}
public String getStudentNumber(){
return strNumber;
}
public String toString(){
return "student="+strName+",student number="+strNumber;
} }
class Student2{
public String strName="";
public String strNuber="";
public Student2(String name,String number){
strName=name;
strNumber=number;
}
public String getStudentName(){
return strName;}
public String getStudentNumber(){
return strNumber;
}
public String toString(){
return "学生姓名="+strName+",学号="+strNumber;
}
}
/*
*静态方法访问实例字段及静态字段
*/
public class StaticMethod(){
static int a=15;
static int b=2;
public static void main(String[] args)
{
System.out.println(a+"/"+b+"="+(a/b));
}
}
/*
*学生类,包括学生类的基本信息
*/
public class StudentTest{
public static void main(String[] args)
{
Student tom=new Student("Tom","20020410");
tom.setStudentSex("man");
tom.setStudentAddress("America");
System.out.println(tom.toString());
}
}
class Student{
private String strName=";
private String strNumber="";
private String strSex="";
private String strSpeciality="";
private String strAddress="";
public Student(String name,String number){
strName=name;
strNumber=number;
}
public String getStudentName(){
return strName;
}
public String getStudentNumber(){
return strNumber;
}
public String getStudentSex(){
return strStudentSex;
}
public String setStudentSex(String sex)
{
strSex=sex;
}
public String getStudentBirthday(){
return strBirthday;
}
public void setStudentBirthday(String birthday){
strBirthday=birthday;
}
public String getStudentSpeciality()
{
return strSpeciality;
}
public void setStudentSpeciality(){
strSpeciality=speciality;
}
public String getStudentAddress(){
return strAddress;
}
public void setStudentAddress(String address){
strAddress=address;
}
public String toString(){
String information ="student name="+strName+",student number ="+strNumber;
if(!strSex.equals(""))
information+=",sex="+strSex;
if(!strBirthday.equals(""))
information+=",birthday ="+strBirthday;
if(!strAddress.equals(""))
information+=",address="+strAddress;
return information;
}
}
}
/*
*学生类的重载
*/
public class StudentOverloading{
public static void main(String[] args){
Student tom=new Student("Tom");
}}
/*
*学生类,包括学生的基本信息
*/
public static void main(String[] args){
S}
class Student{
private String strName="";
private String strNumber="";
private String strSex="";
private String strBirthday="";
private String strSpciality="";
private String strAddress="";
public Student(String name,Stirng number){
strName=name;
StrNumber=number;}
public String getStudentName(){
return strName;
}
public String getStudentNumber(){
return strNumber;
}
public String setStudentSex(String sex){
srtSex=sex;
}
public String getStudentSex(){
return strSex;
}
public String getStudentBirthday(){
return strBirthday;
}
public void setStudentBirthday(String birthday){
strBirthday=birthday;}
public String getStudentSpeciality(){
teturn strSpeciality;
}
public void setStudentSpeciality(String speciality){
strSpeciality=speciality;
}
public String getStudentAddress(){
return strAddress;
}
public void set}
/*
*静态方法访问实例字段及静态字段
*/
public class StaticMethod{
static int a=15;
static int b=2;
public static void main(String[] args){
System.out.println(a+"/"+b+"="+(a/b));
}}
/*
*Math类数学函数的运用,由于Math类中的方法全部是静态的,所以可以直接利用类名调用
*/
public class MathTest{
public static void main(String[] args){
double x=4.51;
System.out.println(Math.sqrt(x));
System.out.println(Math.round(x));
}}
/*
*访问同名的实例字段 和局部变量
*/
public class InstanceTest{
private int a=10;
public static void main(String[] args){
new InstanceTest().print();
}
public void print(){
int a =15;
int b=10;
if(b==10){
System.out.println("局部变量 a="+a);
System.out.println("实例字段变量a ="+this.a);
}
}
}
/*
*对象变量的初始化
*/
import java.util.Date;
public class InitObject{
private Date today;//构建一个对象变量
public static void main(String[] args){
new InitObject().print();
}
public void print(){
System.out.println(today.toString());
}
}
/*
*包名的引用
*/
import java.util.Date;
public class ImportTest{
public static void main(String[] args){
System.out.println(new Date());
}}
/*
*日历按照格式输出
*/
import java.util.Calendar;
import java.swing.JOptionPane;
import java.text.SimpleDateFormat;
public class FormatDateOutput{
private int year;
public static void main(String[] args){
String input=JOptionPane.showInputDialog("请输入有效的年分(YYYY):");
int year1=Integer.parseInt(input);//将字符串转化为整数
FormateDateOutput out=new FormateDateOutput();
out.setYear(year1);
if(year1!=0&&input.length()==4)
{
out.FormateDateStr();
}else{
System.out.println("输入的年份无效。");
}System.exit(0);}
public void FormateDateStr(){
SimpleDateFormat formatter=(SimpleDateFormat)SimpleDateFormat.getDateInstance();
formatter.applyPattern("yyyy-MM-dd");
Calendar cal=Calendar.getInstance();
for(int i=0;i<12;i++){
cal.set(year,i,1);
int temp=cal.getActualMaxmum(Calendar.DAY_OF_MONTH);
for (int j=1;j<=temp;j++){
cal.set(year,i,j);
String str=formatter.format(cal.getTime());
System.out.println("当前的时间是:"+str);}}}
public void setYear(int year){
this.year=year;
System.out.println("the year is "+this.year);
}
public int getYear(){
return year;}}
/*
*GregroianCalendar类的设置器与访问器
*/
import java.util.*;
public class CalendarTest{
public static void main(String[] args){
new CalendarTest().print();
}
public void print(){
Calendar calendar=new GregorianCalendar();
Date TrialTime=new Date();
Calendar.setTime(trialtTime);
System.out.println("YEAR: " + calendar.get(Calendar.YEAR));
System.out.println("MONTH: " + calendar.get(Calendar.MONTH));
System.out.println("WEEK_OF_YEAR: " + calendar.get(Calendar.WEEK_OF_YEAR));
System.out.println("WEEK_OF_MONTH: " + calendar.get(Calendar.WEEK_OF_MONTH));
System.out.println("DATE: "+ calendar.get(Calendar.DATE));
System.out.println("DAY_OF_MONTH: " + calendar.get(Calendar.DAY_OF_MONTH));
System.out.println("DAY_OF_YEAR: " + calendar.get(Calendar.DAY_OF_YEAR));
System.out.println("DAY_OF_WEEK: " + calendar.get(Calendar.DAY_OF_WEEK));
System.out.println("DAY_OF_WEEK_IN_MONTH: " + calendar.get(Calendar.DAY_OF_WEEK_IN_MONTH));
System.out.println("AM_PM: " + calendar.get(Calendar.AM_PM));
System.out.println("HOUR: " + calendar.get(Calendar.HOUR));
System.out.println("HOUR_OF_DAY: " + calendar.get(Calendar.HOUR_OF_DAY));
System.out.println("MINUTE: " + calendar.get(Calendar.MINUTE));
System.out.println("SECOND: " + calendar.get(Calendar.SECOND));
System.out.println("MILLISECOND: " + calendar.get(Calendar.MILLISECOND));
System.out.println("Current Time, with hour reset to 3");
calendar.clear(Calendar.HOUR_OF_DAY); // so doesn't override
calendar.set(Calendar.HOUR, 3);
System.out.println("YEAR: " + calendar.get(Calendar.YEAR));
System.out.println("MONTH: " + calendar.get(Calendar.MONTH));
System.out.println("WEEK_OF_YEAR: " + calendar.get(Calendar.WEEK_OF_YEAR));
System.out.println("WEEK_OF_MONTH: " + calendar.get(Calendar.WEEK_OF_MONTH));
System.out.println("DATE: " + calendar.get(Calendar.DATE));
System.out.println("DAY_OF_MONTH: " + calendar.get(Calendar.DAY_OF_MONTH));
System.out.println("DAY_OF_YEAR: "+ calendar.get(Calendar.DAY_OF_YEAR));
System.out.println("DAY_OF_WEEK: " + calendar.get(Calendar.DAY_OF_WEEK));
System.out.println("DAY_OF_WEEK_IN_MONTH: " + calendar.get(Calendar.DAY_OF_WEEK_IN_MONTH));
System.out.println("AM_PM: " + calendar.get(Calendar.AM_PM));
System.out.println("HOUR: "+ calendar.get(Calendar.HOUR));
System.out.println("HOUR_OF_DAY: " + calendar.get(Calendar.HOUR_OF_DAY));
System.out.println("MINUTE: " + calendar.get(Calendar.MINUTE));
System.out.println("SECOND: " + calendar.get(Calendar.SECOND));
System.out.println("MILLISECOND: " + calendar.get(Calendar.MILLISECOND)); }}
/*
*用户输入两个数字,程序将通知你中奖的机率有多大
*/
import javax.swing.*;
import java.math.*;
public class BigIntegerTest{
public static void main(String[] args){
String input =JoptionPane.showInputDialog("请输入彩球总数:");
int n=Inter.parseInt(input);//将字符串转化为整数
/*中奖计算公式
(n*(n-1)*(n-2)*...*(n-m+1))/(1*2*...*m)
*/
BigInteger option =BigInteger.valueOf(1);
for(int i=0;i<m;i++){
option=option.multiply(BigInteger.valueOf(n-i)).divide(BigInteger.valueOf(i+1));}
System.out.println("你的中奖机率为每" + option + "次中有一次!"); System.out.println("祝你好运!"); System.exit(0); }}
import java.util.*;
public class BaoZhuang{
public static void main(String[] args)
{
Vector a=new Vector();
a.add("123");
a.add(244);
}
}
这样写在1.4运行环境下,是不能通过的
但在1.6运行环境下,却可以。