例題分析(2)
21。
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class Qipaofa {
?public static void main(String[] args) {
??int i,j;
??int a[] = {30,1,-9,40,25};??
??int n = a.length;
??for( i = 1;i < n; i++){
???for( j = 0; j < n-1; j++)
????if(a[j] > a[j+1]){
?????int t = a[j];
?????a[j] = a[j+1];
?????a[j+1] = t;
????}
??}
??for( i = 0; i<n; i++)
???System.out.println(a[i]+"");
?}
}
22。
public class Quiz {
?public static void main(String[] args) {
?try{
??try{
???int i;
???int j=0;
???i = 1 / j;
??}catch(Exception e){
???System.out.print("a");
???throw e;
??}finally{
???System.out.print("inside");
??}
???}catch(Exception e){
????System.out.print("CaughB");
???}finally{
????System.out.print("OutSide");
???}
?}
}
23.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Reverse {
?public static void main(String[] args) {
??int i,n = 10;
??int a[] = new int[10];
??for (i = 0;i<n; i++)
??try{
???BufferedReader br = new BufferedReader(
?????new InputStreamReader(System.in));
???a[i] = Integer.parseInt(br.readLine());
??}catch(IOException e){};
??for (i = n-1; i>=0; i--)
???System.out.println(a[i]+" ");
??System.out.println();
?}
}
24.
public class Sample {
String length;
public Sample(String l){length = l;}
?public static void main(String[] args) {
??Sample s1,s2,s3;
??s1 = new Sample("aa");
??s2 = new Sample("aa");
??s3 = s2;
??long m = 21L;
??int k = 88;
??String n = "aa";
??if(s1==s2)
???System.out.println("s1==s2");
??if(s2==s3)
???System.out.println("s2==s3");
//??if(k==s1)
//??System.out.println("m==s1");
??if(s1.equals(s2))
???System.out.println("s1.equals(s2)");
??
?}
}
25.
class SplitString
{
String SplitStr;
int SplitByte;
public SplitString(String str,int bytes)
{
SplitStr=str;
SplitByte=bytes;
System.out.println("The String is:′"+SplitStr+"′;SplitBytes="+SplitByte);
}
public void SplitIt()
{
int loopCount;
loopCount=(SplitStr.length()%SplitByte==0)?(SplitStr.length()/SplitByte):(SplitStr.length()/SplitByte+1);
System.out.println("Will Split into "+loopCount);
for (int i=1;i<=loopCount ;i++ )
{
if (i==loopCount){
System.out.println(SplitStr.substring((i-1)*SplitByte,SplitStr.length()));
} else {
System.out.println(SplitStr.substring((i-1)*SplitByte,(i*SplitByte)));
}
}
}
public static void main(String[] args)
{
?SplitString ss = new SplitString("test中dd文dsaf中男大3443n中國(guó)43中國(guó)人 0ewldfls=103",4);
ss.SplitIt();
}
}
26.
public class StringText {
?/**
? * @param args
? */
?public static void main(String[] args) {
??? String a ="Gone With Wind";
??? String t= "Wind";
??? String m;
//??? m=a-t;
//??? m=a[3]+"one";
??? m=a.toUpperCase();
?}
}
27.
class Vehicle1{
?public void drive(){
??System.out.println("Vehicle1:drive");
?}
}
class Car1 extends Vehicle1{
?public void drive(){
??System.out.println("Car1:drive");
?}
}
public class Test {
?public static void main(String[] args) {
??Vehicle1 v;
??Car1 c;
??v=new Vehicle1();
??c = new Car1();
??v.drive();
??System.out.println("1");
??c.drive();
??System.out.println("2");
??v = c;//把地址c給了v
??System.out.println("3");
??v.drive();
??System.out.println("xiang shang zhuan xing ");
??v=(Vehicle1)c;
??System.out.println("no using");
??v.drive();
?}
}
28.
class Example{
?String str;
?public Example(){
??str = "example";
?}
?public Example(String s){
??str = s;
??System.out.println("aa");
//??System.out.println("asd"+m);
?}
}
class Demo extends Example{}
public class test1 {
?public static void f(){
??
??Example ex = new Example("Good");
//??Demo d = new Demo("Good");//只能新方法或者重載調(diào)用"Good"
?}
?public static void main(String[] args) {
??f();
?}
}
29.
public class Test2 {
public void aa(int i, int j){
?System.out.println("a");
}
public void Aa(int i, int j){
?System.out.println("A");
}
?public static void main(String[] args) {
??// TODO Auto-generated method stub
??Test2 t2 = new Test2();
??t2.Aa(5,3);
??t2.aa(3,1);
?}
}
30.
import java.util.Date;
class?? super1{??
?????? {??
??????????????? System.out.println("super1?? ok");??
??????? }??
??????? super1()?? {??
?????????? System.out.println("3");??
?????? }??
?? }??
???
? class?? Employee?? extends?? super1{??
???????????? private?? String?? name;??
???????????? private?? double?? salary=1500.00;??
???????????? private?? Date?? birthday;??
//???????????? public?? Employee(){??
//???????????? ?System.out.println("3");?
//???????????? }?? 這里不會(huì)被執(zhí)行。。。
??????????? public?? Employee(String?? n,Date?? DoB){??
??????????????? System.out.println("2");??
??????????????? name=n;??
??????????????? birthday=DoB;??
??????????? }??
???????????? public?? Employee(String?? n){??
???????????????? this(n,null);??
???????????????? System.out.println("4");??
???????????? }??
?? }
?? class?? Manager?? extends?? Employee{??
????? {??
?????????? System.out.println("Manager?? ok");??
?????? }?
????? int b =3;
????? private?? String?? department;??
?????? public?? Manager(String?? n,String?? d){
??? ???
???????????? super(n);
???????????? System.out.println("a");?
???????????? department=d;??
?????? }?
?? }
????
?? public?? class?? Test3{??
?????? public?? static?? void?? main(String?? args[]){??
???????????? new?? Manager("Smith","sales");??
?????? }??
?? }
31.
class testB {
??? public testB(){
???? a1();
?? }
??? public void a1() {
????? System.out.println("A-a1");
??? }
????
? }
??
? public class TestA extends testB{
??? int bb=0;
??? public TestA() {
???? bb=1000;
??? }
??? public void a1() {
???? System.out.println("bb is"+bb);
????? System.out.println("B-a1");
??? }
??? public static void main(String[] args) {
????? new TestA();
??? }
??
? }
32.
public class TestException {
?public static void f1(){
??try{
???f2();
???System.out.println("f2 complete");
??}catch(Throwable t){}
?}
?public static void f2(){
??int x= 0;
??int i = 12 / x;
??System.out.println("division by zero........");
?}
?public static void main(String[] args) {
??
?f1();
//?TestException x = new TestException();
//?x.f1();
?System.out.println("f1 complete");
?}
}
33.
public class WeiZhi {
?public static void main(String[] args) {
??String a = "abcabcbcadf";
??int n = a.indexOf("a");
??int m = a.lastIndexOf("a");
??System.out.println(n);
??System.out.println(m);
??System.out.print(a.length());
//??int k =a.length() - a.f(x);
??String k = a.substring(1,5);
??System.out.println(k);
?}
}
34.
public class Xiabiao {
?/**
? * @param args
? */
?public static void main(String[] args) {
?int i,s =0;
?int a[] = {10,20,30,40,50,60,70,80,90};
?for (i = 0; i< a.length;i++)
??if(a[i]%3 == 0) s += a[i];
?System.out.println("s="+s);
?}
}
35.
public class Xunhuan {
?public static void main(String[] args) {
??int i,j;
//??lab:
???for(i = 0;i < 6;i++){
???for(j = 5; j > 2; j--){
????if(i == j){
?????System.out.println(""+j);
//?????continue lab;
????}
???}
??}
?}
}
posted on 2006-12-24 15:44 youngturk 閱讀(230) 評(píng)論(0) 編輯 收藏 所屬分類(lèi): 個(gè)人隨筆總結(jié)