//Java 语言入门
//例子1:Hello world!
public class Hello{
public static void main (String args[ ]){
System.out.println("Hello World!!");
}
}
//例子2:applet
import java.applet.*;
import java.awt.*;
public class Boy extends Applet{
public void paint(Graphics g){
g.setColor(Color.red);
g.drawString("边喝着咖啡,边学Java",5,30);
g.setColor(Color.blue);
g.drawString("学得很认真",10,50);
}
} //标识符、关键字和数据类型
//例子1:unicode编码
public class Example2_1 {
public static void main (String args[ ]){
char chinaWord='你',japanWord='ぁ';
int p1=36328,p2=38358;
System.out.println("汉字\'你\'在unicode表中的顺序位置:"+(int)chinaWord);
System.out.println("日语\'ぁ\'在unicode表中的顺序位置:"+(int)japanWord);
System.out.println("unicode表中第20328位置上的字符是:"+(char)p1);
System.out.println("unicode表中第12358位置上的字符是:"+(char)p2);
}
}
//例子2:强制类型转换
public class Example2_2 {
public static void main (String args[ ]){
int c=2200;
long d=8000;
float f;
double g=123456789.123456789;
c=(int)d;
f=(float)g; //导致精度的损失.
System.out.println("c="+c);
System.out.println("d="+d);
System.out.println("f="+f); //f=1.23456792E8
System.out.println("g="+g); //g=1.2345678912345679E8
}
}//例子3
public class Example2_3{
public static void main(String args[]){
int a[]={100,200,300};
int b[]={10,11,12,13,14,15,16};
b=a;
b[0]=123456;
for(int i=0;i<a.length;i++){
System.out.println(a[i]);
}
System.out.println("数组a:"+a[0]+","+a[1]+","+a[2]); // b=a后,ab此后拥有同一个数组空间 a[0]=b[0]
System.out.println("数组b:"+b[0]+","+b[1]+","+b[2]);
System.out.println("数组b的长度:"+b.length);
}
}
//运算符、表达式和语句
//例子1:加密
class Example3_1 {
public static void main(String args[]){
char a1='十',a2='点',a3='攻',a4='台';
char secret='8';
System.out.println("原文:"+a1+a2+a3+a4);
a1=(char)(a1^secret);
a2=(char)(a2^secret);
a3=(char)(a3^secret);
a4=(char)(a4^secret);
System.out.println("密文:"+a1+a2+a3+a4);//炁迣攃亖
a1=(char)(a1^secret);
a2=(char)(a2^secret);
a3=(char)(a3^secret);
a4=(char)(a4^secret);
System.out.println("解密:"+a1+a2+a3+a4);//十点攻台
}
}
//例子2
class Example3_2 {
public static void main(String args[]){
int x,y=10;
if(((x=0)==0)||((y=20)==20)) {
System.out.println("y值是:"+y);//10?????
}
int a,b=10;
if(((a=0)==0)|((b=20)==20)) {
System.out.println("b值是:"+b);//20
}
}
}
//例子3:升序排列
public class Example3_3{
public static void main(String args[]){
int a=9,b=5,c=7,t;
if(a>b){
t=a; a=b; b=t;
}
if(a>c){
t=a; a=c; c=t;
}
if(b>c){
t=b; b=c; c=t;
}
System.out.println("a="+a+",b="+b+",c="+c);
}
}
//例子4 if...else...
public class Example3_4 {
public static void main(String args[]){
int math=65 ,english=85;
if(math>60){
System.out.println("数学及格了");
}
else{
System.out.println("数学不及格");
}
if(english>90){
System.out.println("英语是优");
}
else{
System.out.println("英语不是优");
}
System.out.println("我在学习控制语句");
}
}
//例子5 ?????
public class Example3_5 {
public static void main(String args[]){
int x=2,y=1;
switch(x+y){
case 1 :
System.out.println(x+y);
break;
case 3:
System.out.println(x+y);//3
case 0:
System.out.println(x+y);//3????
break;
default:
System.out.println("没有般配的:"+(x+y));
}
}
}
//例子6
public class Example3_6{
public static void main(String args[]){
long sum=0,a=5,item=a,n=10,i=1;
for(i=1;i<=n;i++){
sum=sum+item;
//System.out.println(sum);
item=item*10+a;
System.out.println(item);
}
System.out.println("*************************");
System.out.println(sum);
}
}
//例子7:sum求和
class Example3_7{
public static void main(String args[]){
double sum=0,a=1;
int i=1;
while(i<=20){
sum=sum+a;
System.out.println("sum="+sum);
i=i+1;
a=a*(1.0/i);
}
System.out.println("sum="+sum);
}
}
//例子8
class Example3_8{
public static void main(String args[]){
int sum=0,i,j;
for( i=1;i<=10;i++){
if(i%2==0) //计算1+3+5+7+9
continue;
sum=sum+i;
}
System.out.println("sum="+sum);
for(j=2;j<=50;j++){ //求50以内的素数
for( i=2;i<=j/2;i++){
if(j%i==0)
break;
}
if(i>j/2){
System.out.println(""+j+"是素数");
}
}
}
} //类、对象和接口
//例子1:对象声明,方法调用
public class People{
int height;
String ear;
void speak(String s){
ear="肥耳"
System.out.println(s);
}
}
class Pig{
public static void main(String args[]){
People pig=new People(); //声明对象,为对象分配内存,使用new 运算符和默认的构造方法
pig.height=170;
pig.ear="两只大耳朵";
System.out.println("身高:"+pig.height);
System.out.println(pig.ear);
pig.speak("师傅,咱们别去西天了,去故宫吧");//对象调用方法
System.out.println(pig.ear);//ear重新赋值
}
}
//例子2
class Point{
int x,y;
Point(int a,int b) {
x=a;
y=b;
}
}
public class A{
public static void main(String args[]){
Point p1,p2; //声明对象p1和p2
p1=new Point(10,10); //为对象分配内存,使用 new 和类中的构造方法
p2=new Point(23,35);
}
}
//例子4:方法调用 class 梯形{
float 上底,下底,高,面积;
梯形(float x,float y,float h){
上底=x;
下底=y;
高=h;
}
float 计算面积(){
面积=(上底+下底)*高/2.0f;
return 面积;
}
void 修改高(float height){
高=height;
}
float 获取高(){
return 高;
}
}
public class Example4_4 {
public static void main(String args[]){
梯形 laderOne=new 梯形(12.0f,3.5f,50),laderTwo=new 梯形(2.67f,3.0f,10);//还可以这样写( ⊙ o ⊙ )啊!
System.out.println("laderOne的高是:"+laderOne.获取高());
System.out.println("laderTwo的高是:"+laderTwo.获取高());
System.out.println("laderOne的面积是:"+laderOne.计算面积());
System.out.println("laderTwo的面积是:"+laderTwo.计算面积());
laderOne.修改高(10);
float h=laderOne.获取高();
laderTwo.修改高(h*2);
System.out.println("laderOne现在的高是:"+laderOne.获取高());
System.out.println("laderTwo现在的高是:"+laderTwo.获取高());
System.out.println("laderOne现在的面积是:"+laderOne.计算面积());
System.out.println("laderTwo现在的面积是:"+laderTwo.计算面积());
}
}
//例子5:传参
class People{
String face;
void setFace(String s){
face=s;
}
}
class A{
void f(int x,double y,People p){
x=x+1;
y=y+1;
p.setFace("笑脸");
System.out.println("参数x和y的值分别是:"+x+","+y);//101,101.88
System.out.println("参数对象p的face是:"+p.face); //笑脸
}
}
public class Example4_5 {
public static void main(String args[]){
int x=100;
double y=100.88;
People zhang=new People();
zhang.setFace("很严肃的样子");
System.out.println("main方法中x和y的值仍然分别是:"+x+","+y);//100,100.88
System.out.println("main方法中对象zhang的face是:"+zhang.face);//很严肃的样子
A a=new A();
a.f(x,y,zhang);
System.out.println("main方法中x和y的值仍然分别是:"+x+","+y);//100,100.88
System.out.println("main方法中对象zhang的face是:"+zhang.face);//笑脸
}
}
//例子6
class 圆 {
double 半径;
圆(double r) {
半径 = r;
}
double 计算面积() {
return 3.14 * 半径 * 半径;
}
void 修改半径(double 新半径) {
半径 = 新半径;
}
double 获取半径() {
return 半径;
}
}
class 圆锥 {
圆 底圆;
double 高; 圆锥(圆 circle, double h) {
this.底圆 = circle;
this.高 = h;
} double 计算体积() {
double volume;
volume = 底圆.计算面积() * 高 / 3.0;
return volume;
} void 修改底圆半径(double r) {
底圆.修改半径(r);
} double 获取底圆半径() {
return 底圆.获取半径();
}
}class Example4_6 {
public static void main(String args[]) {
圆 circle = new 圆(10);
圆锥 circular = new 圆锥(circle, 20);
System.out.println("圆锥底圆半径:" + circular.获取底圆半径());
System.out.println("圆锥的体积:" + circular.计算体积());
circular.修改底圆半径(100);
System.out.println("圆锥底圆半径:" + circular.获取底圆半径());
System.out.println("圆锥的体积:" + circular.计算体积());
}
}//例子7
class 梯形 {
float 上底, 高;
static float 下底; 梯形(float x, float y, float h) {
上底 = x;
下底 = y;
高 = h;
} float 获取下底() {
return 下底;
} void 修改下底(float b) {
下底 = b;
}
}class Example4_7 {
public static void main(String args[]) {
梯形 laderOne = new 梯形(3.0f, 10.0f, 20), laderTwo = new 梯形(2.0f, 3.0f, 10);
梯形.下底 = 200; // 通过类名操作类变量 类.属性
System.out.println("laderOne的下底:" + laderOne.获取下底());
System.out.println("laderTwo的下底:" + laderTwo.获取下底());
laderTwo.修改下底(60); // 通过对象操作类变量 对象.方法
System.out.println("laderOne的下底:" + laderOne.获取下底());
System.out.println("laderTwo的下底:" + laderTwo.获取下底());
}
}
class Fibi {
public static long fibinacii(int n) {
long c = 0;
if (n == 1 || n == 2)
c = 1;
else
c = fibinacii(n - 1) + fibinacii(n - 2);
return c;
}
}public class Example4_8 {
public static void main(String args[]) {
System.out.println(Fibi.fibinacii(7));// 13
}
}
//例子9
import java.applet.Applet;
import java.awt.*;
public class Example4_10 extends Applet {
Button redbutton;
public void init() {
redbutton = new Button("我是一个红色的按钮");
redbutton.setBackground(Color.red);
redbutton.setForeground(Color.white);
add(redbutton);
}
}
//例子10: 100内质数
public class PrimNumber{
public void getPrimnumber(int n){
int sum=0,i,j;
for(i=1;i<=n;i++){
for(j=2;j<=i/2;j++){
if(i%j==0)
break;
}
if(j>i/2)
System.out.print(" "+i);
}
}
public static void main(String args[]){
PrimNumber p=new PrimNumber();
p.getPrimnumber(100);
}
}
-----------------------------------------------------------
//例子12:知3边,求面积
//Trangle.java
class Trangle{
double sideA,sideB,sideC;
boolean boo;
public Trangle(double a,double b,double c){
sideA=a;
sideB=b;
sideC=c;
if(a+b>c&&a+c>b&&c+b>a){
System.out.println("我是一个三角形");
boo=true;
}
else{
System.out.println("我不是一个三角形");
boo=false;
}
}
public void 计算面积(){
if(boo){
double p=(sideA+sideB+sideC)/2.0;
double area=Math.sqrt(p*(p-sideA)*(p-sideB)*(p-sideC));
System.out.println("面积是:"+area);
}
else{
System.out.println("不是一个三角形,不能计算面积");
}
}
public void 修改三边(double a,double b,double c){
sideA=a;
sideB=b;
sideC=c;
if(a+b>c&&a+c>b&&c+b>a){
boo=true;
}
else{
boo=false;
}
}
}public class Example4_13{
public static void main(String args[]){
Trangle trangle=new Trangle(12,3,1);
trangle.计算面积();
trangle.修改三边(3,4,5);
trangle.计算面积();
}
}
------------------------------------------------------------//例子14
class Example4_14
{ private int money;
Example4_14()
{ money=2000;
}
private int getMoney()
{ return money;
}
public static void main(String args[])
{ Example4_14 exa=new Example4_14();
exa.money=3000;
int m=exa.getMoney();
System.out.println("money="+m);//3000
}
}
//例子15:继承
class Father{
private int money;
float weight,height;
String head;
void speak(String s){
System.out.println(s);
}
}
class Son extends Father{
String hand,foot;
}
public class Example4_15{
public static void main(String args[]){
Son boy=new Son();
boy.weight=1.80f;
boy.height=120f;
boy.head="一个头";
boy.hand="两只手";
boy.foot="两只脚";
boy.speak("我是谁");
System.out.println(boy.hand+boy.foot+boy.head+boy.weight+boy.height);
}
}
//例子16
class Chengji {
float f(float x,float y){
return x*y;
}
}
class Xiangjia extends Chengji{
float f(float x,float y) {
return x+y ;
}
}
public class Example4_16 {
public static void main(String args[]){
Xiangjia sum=new Xiangjia();
Chengji sum2=new Chengji();
float c=sum.f(4,6);
float d=sum2.f(4,6);
System.out.println(c);
System.out.println(d);
}
}
//例子17
class Area{
float f(float r ){
return 3.14159f*r*r;
}
float g(float x,float y){
return x+y;
}
}
class Circle extends Area{
float f(float r){
return 3.14159f*2.0f*r;
}
}
public class Example4_17{
public static void main(String args[]){
Circle yuan=new Circle();
float length=yuan.f(5.0f);
float sum=yuan.g(232.645f,418.567f);
System.out.println(length);
System.out.println(sum);
}
}//例子18
class A {
final double PI = 3.1415926;
public double getArea(final double r){
return PI * r * r;
}
}public class Example4_18 {
public static void main(String args[]){
A a = new A();
System.out.println("面积:" + a.getArea(100));
}
}//例子19
class 类人猿 {
private int n = 100;
void crySpeak(String s) {
System.out.println(s);
}
}class People extends 类人猿 {
void computer(int a, int b) {
int c = a * b;
System.out.println(c);
}
void crySpeak(String s) {
System.out.println("**" + s + "**");
}
}class Example4_19 {
public static void main(String args[]) {
类人猿 monkey = new People();
monkey.crySpeak("I love this game");
People people = (People) monkey;
people.computer(10, 10);
}
}
//例子20
class 动物 {
void cry() {
}
}class 狗 extends 动物 {
void cry() {
System.out.println("汪汪.....");
}
}class 猫 extends 动物 {
void cry() {
System.out.println("喵喵.....");
}
}class Example4_20 {
public static void main(String args[]) {
动物 dongwu;
dongwu = new 狗();
dongwu.cry();
dongwu = new 猫();
dongwu.cry();
}
}
//例子21
abstract class A {
abstract int min(int x, int y);
int max(int x, int y) {
return x > y ? x : y;
}
}class B extends A {
int min(int x, int y) {
return x < y ? x : y;
}
}public class Example4_21 {
public static void main(String args[]) {
A a;
B b = new B();
int max = b.max(12, 34);
int min = b.min(12, 34);
System.out.println("max=" + max + " min=" + min);
a = b;
max = a.max(12, 34);
System.out.println("max=" + max);
}
}例子22
abstract class 图形
{ public abstract double 求面积();
}
class 梯形 extends 图形
{ double a,b,h;
梯形(double a,double b,double h)
{ this.a=a;
this.b=b;
this.h=h;
}
public double 求面积()
{ return((1/2.0)*(a+b)*h);
}
}
class 圆形 extends 图形
{ double r;
圆形(double r)
{ this.r=r;
}
public double 求面积()
{ return(3.14*r*r);
}
}
class 堆
{ 图形 底;
double 高;
堆(图形 底,double 高)
{ this.底=底;
this.高=高;
}
void 换底(图形 底)
{ this.底=底;
}
public double 求体积()
{ return (底.求面积()*高)/3.0;
}
}
public class Example4_22
{ public static void main(String args[])
{ 堆 zui;
图形 tuxing;
tuxing=new 梯形(2.0,7.0,10.7);
System.out.println("梯形的面积"+tuxing.求面积());
zui=new 堆(tuxing,30);
System.out.println("梯形底的堆的体积"+zui.求体积());
tuxing=new 圆形(10);
System.out.println("半径是10的圆的面积"+tuxing.求面积());
zui.换底(tuxing);
System.out.println("圆形底的堆的体积"+zui.求体积());
}
}//例子23
class Student {
int number;
String name; Student() {
} Student(int number, String name) {
this.number = number;
this.name = name;
System.out.println("I am " + name + "my number is " + number);
}
}class Univer_Student extends Student {
boolean 婚否; Univer_Student(int number, String name, boolean b) {
super(number, name);
婚否 = b;
System.out.println("婚否=" + 婚否);
}
}public class Example4_23 {
public static void main(String args[]) {
Univer_Student zhang = new Univer_Student(9901, "和晓林", false);
}
}//例子24
class Sum {
int n;
float f() {
float sum = 0;
for (int i = 1; i <= n; i++)
sum = sum + i;
return sum;
}
}class Average extends Sum {
int n;
float f() {
float c;
super.n = n;
c = super.f();
return c / n;
}
float g() {
float c;
c = super.f();
return c / 2;
}
}public class Example4_24 {
public static void main(String args[]) {
Average aver = new Average();
aver.n = 100;
float result_1 = aver.f();
float result_2 = aver.g();
System.out.println("result_1=" + result_1);
System.out.println("result_2=" + result_2);
}
}
//例子25:接口
interface Computable{
int MAX=100;
int f(int x);//????? 方法 int
}
class China implements Computable{
int number;
public int f(int x){ //不要忘记public关键字
int sum=0;
for(int i=1;i<=x;i++){
sum=sum+i;
}
return sum;
}
}
class Japan implements Computable{
int number;
public int f(int x){
return 44+x;
}
}
public class Example4_25{
public static void main(String args[]){
China zhang;
Japan henlu;
zhang=new China();
henlu=new Japan();
zhang.number=991898+Computable.MAX;
henlu.number=941448+Computable.MAX;
System.out.println("number:"+zhang.number+"求和"+zhang.f(100));
System.out.println("number:"+henlu.number+"求和"+henlu.f(100));
}
}
//例子26:接口
interface 收费 {
public void 收取费用();
}
interface 调节温度 {
public void controlTemperature();
}
class 公共汽车 implements 收费 {
public void 收取费用() {
System.out.println("公共汽车:一元/张,不计算公里数");
}
}
class 出租车 implements 收费, 调节温度 {
public void 收取费用() {
System.out.println("出租车:1.60元/公里,起价3公里");
}
public void controlTemperature() {
System.out.println("安装了Hair空调");
}
}
class 电影院 implements 收费, 调节温度 {
public void 收取费用() {
System.out.println("电影院:门票,十元/张");
}
public void controlTemperature() {
System.out.println("安装了中央空调");
}
}class Example4_26 {
public static void main(String args[]) {
公共汽车 七路 = new 公共汽车();
出租车 天宇 = new 出租车();
电影院 红星 = new 电影院();
七路.收取费用();
天宇.收取费用();
红星.收取费用();
天宇.controlTemperature();
红星.controlTemperature();
}
}
//例子27
interface ShowMessage {
void 显示商标(String s);
}
class TV implements ShowMessage {
public void 显示商标(String s) {
System.out.println(s);
}
}
class PC implements ShowMessage {
public void 显示商标(String s) {
System.out.println(s);
}
}
public class Example4_27 {
public static void main(String args[]) {
ShowMessage sm;
sm = new TV();
sm.显示商标("长城牌电视机");
sm = new PC();
sm.显示商标("联想奔月5008PC机");
}
}//例子28
interface Computerable
{ public double 求面积();
}
class 梯形 implements Computerable
{ double a,b,h;
梯形(double a,double b,double h)
{ this.a=a;
this.b=b;
this.h=h;
}
public double 求面积()
{ return((1/2.0)*(a+b)*h);
}
}
class 圆形 implements Computerable
{ double r;
圆形(double r)
{ this.r=r;
}
public double 求面积()
{ return(3.14*r*r);
}
}
class 堆
{ Computerable 底;
double 高;
堆(Computerable 底,double 高)
{ this.底=底;
this.高=高;
}
void 换底(Computerable 底)
{ this.底=底;
}
public double 求体积()
{ return (底.求面积()*高)/3.0;
}
}
public class Example4_28
{ public static void main(String args[])
{ 堆 zui;
Computerable bottom;
bottom=new 梯形(2.0,7.0,10.7);
System.out.println("梯形的面积"+bottom.求面积());
zui=new 堆(bottom,30);
System.out.println("梯形底的堆的体积"+zui.求体积());
bottom=new 圆形(10);
System.out.println("半径是10的圆的面积"+bottom.求面积());
zui.换底(bottom);
System.out.println("圆形底的堆的体积"+zui.求体积());
}
}
例子29
interface SpeakHello {
void speakHello();
}class Chinese implements SpeakHello {
public void speakHello() {
System.out.println("中国人习惯问候语:你好,吃饭了吗? ");
}
}class English implements SpeakHello {
public void speakHello() {
System.out.println("英国人习惯问候语:你好,天气不错 ");
}
}class KindHello {
public void lookHello(SpeakHello hello) {
hello.speakHello();
}
}public class Example4_29 {
public static void main(String args[]) {
KindHello kindHello = new KindHello();
kindHello.lookHello(new Chinese());
kindHello.lookHello(new English());
}
}
//例子30
class China {
final String nationalAnthem = "义勇军进行曲";
Beijing beijing; China() {
beijing = new Beijing();
} String getSong() {
return nationalAnthem;
} class Beijing {
String name = "北京"; void speak() {
System.out.println("我们是" + name + " 我们的国歌是:" + getSong());
}
}
}public class Example4_30 {
public static void main(String args[]) {
China china = new China();
china.beijing.speak();
}
}//例子31 : 内部类
class Cubic {
double getCubic(int n) {
return 0;
}
}
abstract class Sqrt {
public abstract double getSqrt(int x);
}
class A {
void f(Cubic cubic) {
double result = cubic.getCubic(3);
System.out.println(result);
}
}public class Example4_31 {
public static void main(String args[]) {
A a = new A();
a.f(new Cubic() {
double getCubic(int n) {
return n * n * n;
}
});
Sqrt ss = new Sqrt() {
public double getSqrt(int x) {
return Math.sqrt(x);
}
};
double m = ss.getSqrt(5);
System.out.println(m);
}
}
例子32
interface Cubic {
double getCubic(int n);
}interface Sqrt {
public double getSqrt(int x);
}class A {
void f(Cubic cubic) {
double result = cubic.getCubic(3);
System.out.println(result);
}
}public class Example4_32 {
public static void main(String args[]) {
A a = new A();
a.f(new Cubic() {
public double getCubic(int n) {
return n * n * n;
}
});
Sqrt ss = new Sqrt() {
public double getSqrt(int x) {
return Math.sqrt(x);
}
};
double m = ss.getSqrt(5);
System.out.println(m);
}
}//例子33:异常
public class Example4_33{
public static void main(String args[ ]){
int n=0,m=0,t=555;
try{
m=Integer.parseInt("8888");
n=Integer.parseInt("abc789"); //抛出异常,后面的不再执行,t=555
t=9999;
}
catch(NumberFormatException e){
System.out.println("发生异常:"+e.getMessage());
e.printStackTrace();
n=123;
}
System.out.println("n="+n+",m="+m+",t="+t);//123, 8888, 555
}
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////例子34
class NopositiveException extends Exception {
String message; NopositiveException(int m, int n) {
message = "数字" + m + "或" + n + "不是正整数";
} public String toString() {
return message;
}
}class Computer {
public int getMaxCommonDivisor(int m, int n) throws NopositiveException {
if (n <= 0 || m <= 0) {
NopositiveException exception = new NopositiveException(m, n);
throw exception;
}
if (m < n) {
int temp = 0;
temp = m;
m = n;
n = temp;
}
int r = m % n;
while (r != 0) {
m = n;
n = r;
r = m % n;
}
return n;
}
}public class Example4_34 {
public static void main(String args[]) {
int m = 24, n = 36, result = 0;
Computer a = new Computer();
try {
result = a.getMaxCommonDivisor(m, n);
System.out.println(m + "和" + n + "的最大公约数 " + result);
m = -12;
n = 22;
result = a.getMaxCommonDivisor(m, n);
System.out.println(m + "和" + n + "的最大公约数 " + result);
} catch (NopositiveException e) {
System.out.println(e.toString());
}
}
}
例子35
import java.lang.reflect.*;class Rect {
double width, height, area; public double getArea() {
area = height * width;
return area;
}
}public class Example4_35 {
public static void main(String args[]) {
Rect rect = new Rect();
Class cs = rect.getClass();
String className = cs.getName();
Constructor[] con = cs.getDeclaredConstructors();
Field[] field = cs.getDeclaredFields();
Method[] method = cs.getDeclaredMethods();
System.out.println("类的名字:" + className);
System.out.println("类中有如下的成员变量:");
for (int i = 0; i < field.length; i++) {
System.out.println(field[i].toString());
}
System.out.println("类中有如下的方法:");
for (int i = 0; i < method.length; i++) {
System.out.println(method[i].toString());
}
System.out.println("类中有如下的构造方法:");
for (int i = 0; i < con.length; i++) {
System.out.println(con[i].toString());
}
}
}
例子36
class Rect
{ double width,height,area;
public double getArea()
{ area=height*width;
return area;
}
}
public class Example4_36
{ public static void main(String args[])
{ try{ Class cs=Class.forName("Rect");
Rect rect=(Rect)cs.newInstance();
rect.width=100;
rect.height=200;
System.out.println("rect的面积"+rect.getArea());
}
catch(Exception e){}
}
}
//例子37:大小写转换
public class Example4_37{
public static void main(String args[ ]){
char a[]={'a','b','c','D','E','F'};
for(int i=0;i<a.length;i++){
if(Character.isLowerCase(a[i])){
a[i]=Character.toUpperCase(a[i]);
}
else if(Character.isUpperCase(a[i])){
a[i]=Character.toLowerCase(a[i]);
}
System.out.print(" "+a[i]);
}
}
}
//字符串
//例子1
class Example5_1{
public static void main(String args[]){
String s1,s2;
s1=new String("we are students");
s2=new String("we are students");
System.out.println(s1.equals(s2));//true
System.out.println(s1==s2); //false
String s3,s4;
s3="how are you";
s4="how are you";
System.out.println(s3.equals(s4));//true
System.out.println(s3==s4); //true
}
}
//例子2
class Example5_2{
public static void main(String args[]){
int number=0;
String s="student;entropy;engage,english,client";
for(int k=0;k<s.length();k++){
if(s.regionMatches(k,"en",0,2)){
number++;
}
}
System.out.println(s.length());//37
System.out.println("number="+number);//5
}
}
//例子3:字符串排序
class Example5_3 {
public static void main(String args[]) {
String a[] = { "door", "apple", "Applet", "girl", "boy" };
for (int i = 0; i < a.length - 1; i++) {
for (int j = i + 1; j < a.length; j++) {
if (a[j].compareTo(a[i]) < 0) {
String temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
for (int i = 0; i < a.length; i++) {
System.out.print(" " + a[i]);
}
}
}
例子4
class Example5_4 {
public static void main(String args[]) {
String path = "c:\\myfile\\2000\\result.txt";
int index = path.lastIndexOf("\\");
String fileName = path.substring(index + 1);
String newName = fileName.replaceAll(".txt", ".java");
System.out.println(path);
System.out.println(fileName);
System.out.println(newName);
}
}例子5
public class Example5_5
{ public static void main(String args[])
{ double n,sum=0,item=0;
boolean computable=true;
for(int i=0;i<args.length;i++)
{ try{ item=Double.parseDouble(args[i]);
sum=sum+item;
}
catch(NumberFormatException e)
{ System.out.println("您键入了非数字字符:"+e);
computable=false;
}
}
if(computable)
{ n=sum/args.length;
System.out.println("平均数:"+n);
}
int number=123456;
String binaryString=Long.toBinaryString(number);
System.out.println(number+"的二进制表示:"+binaryString);
System.out.println(number+"的十六进制表示:"+Long.toString(number,16));
String str="1110110";
int p=0,m=0;
for(int i=str.length()-1;i>=0;i--)
{ char c=str.charAt(i);
int a=Integer.parseInt(""+c);
p=p+(int)(a*Math.pow(2,m));
m++;
}
System.out.println(str+"的十进制表示:"+p);
}
}
//例子6
import java.util.Date;
import java.awt.*;
public class Example5_6{
public static void main(String args[]){
Date date=new Date();
Button button=new Button("确定");
System.out.println(date.toString());
System.out.println(button.toString());
}
}//例子7
import java.util.*;
public class Example5_7
{ public static void main(String args[])
{ String s="I am Geng.X.y,she is my girlfriend";
StringTokenizer fenxi=new StringTokenizer(s," ,");
int number=fenxi.countTokens();
while(fenxi.hasMoreTokens())
{ String str=fenxi.nextToken();
System.out.println(str);
System.out.println("还剩"+fenxi.countTokens()+"个单词");
}
System.out.println("s共有单词:"+number+"个");
}
}//例子8
class Example5_8
{ public static void main(String args[])
{ char c[],d[];
String s="巴西足球队击败德国足球队";
c=new char[2];
s.getChars(5,7,c,0);
System.out.println(c);
d=new char[s.length()];
s.getChars(7,12,d,0);
s.getChars(5,7,d,5);
s.getChars(0,5,d,7);
System.out.println(d);
}
}
例子9
class Example5_9
{ public static void main(String args[])
{ String s="清华大学出版社";
char a[]=s.toCharArray();
for(int i=0;i<a.length;i++)
{ a[i]=(char)(a[i]^'t');
}
String secret=new String(a);
System.out.println("密文:"+secret);
for(int i=0;i<a.length;i++)
{ a[i]=(char)(a[i]^'t');
}
String code=new String(a);
System.out.println("原文:"+code);
}
}
例子10
public class Example5_10
{ public static void main(String args[])
{ byte d[]="你我他".getBytes();
byte c[]="ywt".getBytes();
System.out.println("数组d的长度是(一个汉字占两个字节):"+d.length);
System.out.println("数组c的长度是:"+c.length);
String s=new String(d,0,2);
System.out.println(s);
}
}
例子11
class Example5_11
{ public static void main(String args[ ])
{ StringBuffer str=new StringBuffer("62791720");
str.insert(0,"010-");
str.setCharAt(7 ,'8');
str.setCharAt(str.length()-1,'7');
System.out.println(str);
str.append("-446");
System.out.println(str);
str.reverse();
System.out.println(str);
}
}例子12
public class Example5_12
{ public static void main (String args[ ])
{
String regex="\\w{1,}@\\w{1,}\56\\w{1,}" ;
String str1="[email protected]";
String str2="li@[email protected]";
if(str1.matches(regex))
{ System.out.println(str1+"是一个Email地址");
}
else
{ System.out.println(str1+"不是一个Email地址");
}
if(str2.matches(regex))
{ System.out.println(str2+"是一个Email地址");
}
else
{ System.out.println(str2+"不是一个Email地址");
}
}
} //时间、日期和数字
例子1
import java.util.Date;
import java.text.SimpleDateFormat;
class Example6_1
{ public static void main(String args[])
{ Date nowTime=new Date();
System.out.println(nowTime);
SimpleDateFormat matter1=
new SimpleDateFormat(" 'time':yyyy年MM月dd日E 北京时间");
System.out.println(matter1.format(nowTime));
SimpleDateFormat matter2=
new SimpleDateFormat("北京时间:yyyy年MM月dd日HH时mm分ss秒");
System.out.println(matter2.format(nowTime));
Date date1=new Date(1000),date2=new Date(-1000);
System.out.println(matter2.format(date1));
System.out.println(matter2.format(date2));
System.out.println(new Date(System.currentTimeMillis()));
}
} 例子2
import java.util.*;
class Example6_2
{ public static void main(String args[])
{ Calendar calendar=Calendar.getInstance();
calendar.setTime(new Date());
String 年=String.valueOf(calendar.get(Calendar.YEAR)),
月=String.valueOf(calendar.get(Calendar.MONTH)+1),
日=String.valueOf(calendar.get(Calendar.DAY_OF_MONTH)),
星期=String.valueOf(calendar.get(Calendar.DAY_OF_WEEK)-1);
int hour=calendar.get(Calendar.HOUR_OF_DAY),
minute=calendar.get(Calendar.MINUTE),
second=calendar.get(Calendar.SECOND);
System.out.println("现在的时间是:");
System.out.println(""+年+"年"+月+"月"+日+"日 "+ "星期"+星期);
System.out.println(""+hour+"时"+minute+"分"+second+"秒");
calendar.set(1962,5,29); //将日历翻到1962年6月29日,注意5表示六月。
long time1962=calendar.getTimeInMillis();
calendar.set(2006,9,1);
long time2006=calendar.getTimeInMillis();
long 相隔天数=(time2006-time1962)/(1000*60*60*24);
System.out.println("2006年10月1日和1962年6月29日相隔"+相隔天数+"天");
}
}
例子3
import java.util.*;
class Example6_3
{ public static void main(String args[])
{ System.out.println(" 日 一 二 三 四 五 六");
Calendar 日历=Calendar.getInstance();
日历.set(2006,11,1); //将日历翻到2006年12月1日。
int 星期几=日历.get(Calendar.DAY_OF_WEEK)-1;
String a[]=new String[星期几+31];
for(int i=0;i<星期几;i++)
{ a[i]="**";
}
for(int i=星期几,n=1;i<星期几+31;i++)
{ if(n<=9)
a[i]=String.valueOf(n)+" ";
else
a[i]=String.valueOf(n) ;
n++;
}
for(int i=0;i<a.length;i++)
{ if(i%7==0)
{ System.out.println("");
}
System.out.print(" "+a[i]);
}
}
}
例子4
import java.text.NumberFormat;
class Example6_4
{ public static void main(String args[])
{ double a=Math.sqrt(5);
System.out.println("格式化前:"+a);
NumberFormat f=NumberFormat.getInstance();
f.setMaximumFractionDigits(7);
f.setMinimumIntegerDigits(3);
String s=f.format(a);
System.out.println("格式化后:"+s);
MyNumberFormat myFormat=new MyNumberFormat();
System.out.println("格式化后:"+myFormat.format(a,4));
System.out.println("得到的随机数:");
int number=8;
for(int i=1;i<=20;i++)
{ int randomNumber=(int)(Math.random()*number)+1;
System.out.print(" "+randomNumber);
if(i%10==0)
System.out.println("");
}
}
}
class MyNumberFormat
{ public String format(double a,int n)
{ String str=String.valueOf(a);
int index=str.indexOf(".");
String temp=str.substring(index+1);
int leng=0;
leng=temp.length();
int min=Math.min(leng,n);
str=str.substring(0,index+min+1);
return str;
}
}
}
例子5
import java.math.*;
public class Example6_5
{ public static void main(String args[])
{ BigInteger sum=new BigInteger("0"),
xiang=new BigInteger("1"),
ONE=new BigInteger("1"),
i=ONE,
m=new BigInteger("30");
while(i.compareTo(m)<=0)
{ sum=sum.add(xiang);
i=i.add(ONE);
xiang=xiang.multiply(i);
}
System.out.println(sum);
}
}
先谢谢啦。