黑馬程序員_Java基礎[23]_練習題

---------- android培訓 java培訓 、期待與您交流! ----------


//                              【【【【練習題】】】】

//注:按照java規範書寫程序代碼,如果你認爲程序有錯誤,請支出,並說明程序錯誤原因

//=============================================================================
/*1
寫出程序結果:【a】修正異常拋出後,正確結果是:bcd。
 */
public class D_Exp_test2 {
    
    public static void func() throws Exception //對應【a2】後可編譯
    {
            try{
                throw new Exception();//錯誤【a1】     
            }
            finally{
                System.out.println("B");
            }
        }    

    public static void main(String[] args) {
        
        try{
            func();//【a3】異常拋出
            System.out.println("A");
        }
        catch(Exception e){// 【a4】接收異常
            System.out.println("C");
        }    
        System.out.println("D");
    }
}
//=============================================================================
/*2
寫出結果:
        Test
        Demo
        Test
 */
class Test{
    Test(){
        System.out.println("Test");
    }

    public void func() {
        // TODO Auto-generated method stub
        
    }
}
class Demo extends Test{
    Demo(){
        super();
        System.out.println("Demo1");
    }
    public static void main(String []args){
        new Demo();
        new Test();
    }
}

//=============================================================================
/*3.
 *寫出程序結果:
 *編譯異常 :A中未定義func()
 *
 *(在非靜態時,多態編譯看左邊,運行看右邊,a調用了func()方法,明顯A中是沒有的
 *想要編譯通過那麼在接口中還需要定義:String func();)
 */
interface A{
}
class B implements A{
    public String func(){
        return "func";
    }
}
class Demo1{
    public static void main(String []args){
        A a=new B();//這個叫接口對象指向子類對象
        //System.out.println(a.func());//後期註釋,爲了下面的題通過
    }
}



//=============================================================================
/*4
 * 寫出結果:AB
 * 設計到覆蓋問題,而循環題中,首先執行a部分,打印,然後執行到b部分,打印,
 *         然後返回一個false,這個時候循環結束(條件式中,必須是真纔行)
 */
class Fu{
    boolean show(char a){
        System.out.println(a);
        return true;
    }
}

class Demo2 extends Fu{
    public static void main(String []args){
        int i=0;
        Fu f=new Demo2();
        Demo2 d=new Demo2();
        
        for(f.show('A');f.show('B')&&(i<2);f.show('C')){
            i++;
            d.show('D');
        }
    }
    boolean show(char a){
        System.out.println(a);
        return false;
    }
}

//=============================================================================
/*5
 * 寫出程序結果: 編譯出錯
 * A1接口中沒有定義test()方法。
 */
interface A1{}

class B1 implements A1{
    public String test(){
        return "yes";
    }
}

class Demo3{
     static A1 get(){
        return new B1();
    }
    public static void main(String []ages){
        A1 a=get();
        /*指向一個get()方法,get()方法中又返回的結果是一個B1子類對象,
        而輸出的時候又想輸出父類對象中的方法,但是父類中沒有,所以出錯
        */
        //System.out.println(a.test());//爲了下面的題能繼續,我們後期手動註釋掉該行
    }
}


//=============================================================================

/*6
 * 寫出程序結果:
         *   B
         *   C
         *   5
 */
class Super{
    int i=0;
    public Super(String a){
        System.out.println("A");
        i=1;
    }
    public Super(){
        System.out.println("B");
        i+=2;//i=0+2
    }
}

class Demo4 extends Super{
    
    public Demo4(String a){
        //這裏有一句默認super(); 調用空參數構造函數
        System.out.println("C");
        i=5;
        /*
         * new Demo4("A");//的時候進入到該方法
         * 而該方法中第一行默認有一句super(); 方法。
         */
    }
    public static void main(String []args){
        int i=4;
        Super d=new Demo4("A");
        
        System.out.println(d.i);
    }
}


//=============================================================================

/*7
 * 補齊代碼:調用兩個函數,要求匿名內部類
 */
interface Inter{
    void show(int a,int b);
    void func();
}
class demo5{
    public static void main (String []args)
    {
        //補齊代碼
        Inter in=new Inter(){
            public void show(int a,int b){
                System.out.println("老巫婆的真實年紀"+a+b);
            }
            public void  func(){
                System.out.println("老巫婆變變變魔鬼身材美女");
            }
        };//.show(3,8);//匿名構造函數搞定,不過卻只能調用一個函數,所以,申明一個變量來接收
        in.show(12, 7);
        in.func();
    }
}


//=============================================================================
/*8
 * 寫出結果:編譯出錯:【非靜內部態類】中不可以定義靜態成員,
 *             除非是帶final修飾的靜態類如【a】
 */
class TD{
    int y=6;
    
    class Inner{
        //static int y=3;//  原題
        static final int y=3;//【a】修正後
        void show(){
            System.out.println();
        }
    }
}
class TC{
    public static void main(String []args){
         TD.Inner ti=new TD().new Inner();
         ti.show();
    }
}



//=============================================================================
/*9
 * 選擇題,寫出錯誤答案錯誤原因,並用單行註釋的方式
 */
class Demo6{
    int show(int a, int b){return 0;}
}
//下面那些函數可以存在與Demo6的子類中。
/*
A、public  int show (int a , int b){return 0;}//可以,重載父類方法
B、private int show (int a , int b){return 0;}//私有的,不可以,權限不夠
C、private int show (int a , long b){return 0;}//可以,和父類不是一個行數,沒有覆蓋,相當於重載
D、public  short show (int a , int b){return 0;}//不可以,因爲該函數不可以和給定函數出現在同一類中,或者子父類中。
E、static  int show (int a , int b){return 0;}// 靜態方法只能調用靜態 不可以
 */

//=============================================================================
/*10、寫出this關鍵字的含義,final有那些特點。
 * 答:
 * this代表本類對象,那個對象調用this所在函數,this就代表那個函數。就是所有對象的引用。
 *
 * final

 * 1、修飾變量(成員變量,靜態變量,局部變量)
 * 2、修飾的類不可以被覆蓋。
 * 3、修飾的函數不可以被覆蓋
 * 4、修飾的變量是一個常量,只能賦值一次
 * 5、內部類只能訪問局部當中的final變量
 *
 *   static final修飾的成員變量可成爲全局常量
 *  
 */

//=============================================================================
/*寫出程序結果:
 * 4---5------shouFu------ZiFu
 * 成員變量不存在覆蓋,隨着類中,所以只看左邊,f.show();z.show();  覆蓋所以
 */
class Fu1{
    int num=4;
    void show(){
        System.out.println("shouFu");
    }
}

class Zi1 extends Fu1{
    int num=5;
    void show(){
        System.out.println("ZiFu");
    }
}

class T{
    public static void main(String []args){
        Fu1 f=new Zi1();
        Zi1 z=new Zi1();
        System.out.println(f.num);
        System.out.println(z.num);
        f.show();
        z.show();//非靜態多態編譯看左,運行看右。
    }
}

//=============================================================================
/*12
 * 補齊代碼
 */
interface A2{
    void show();
}
interface B2{
    void add(int a ,int b);
}
class C2  implements A2,B2{
    //根據主函數需求:補齊代碼
    private int num;
    //purivate a,b;//方法二
    public void show(){
        System.out.println(num);
        //System.out.println(a+b);//方法二
    }
    public void add(int a ,int b){
        num=a+b;
        //this.a=a;//方法二
        //this.b=b;//方法二
    }
}

class D2{
    public static void main(String []args){
        C2  c  =new  C2();
        c.add(4,2);
        c.show();//通過該函數打印以上兩個數的和
    }
}
//=============================================================================
/*13
 * 寫出程序結果:BCD
 * try裏調用showExce();的時候拋出異常,程序跳入catch。
 */
class Demo7{
    public static void main(String []args){
        try{
            showExce();
            System.out.println("A");
        }
        catch(Exception e){
            System.out.println("B");
        }
        finally{
            System.out.println("C");
        }
        System.out.println("D");
    }
    public static void showExce() throws Exception{
        throw new Exception();
    }//把拋出異常封裝到方法當中
}

//=============================================================================
/*14
 * 寫出程序結果:編譯失敗,因爲父類中缺少空參數構造函數或者子類通過super();指定父類中的構造函數
 */
class Super2{
    int i=0;
    Super2(){}//後期添加,看題時請刪除該行。
    public Super2(String s){
        i=1;
    }
}
class Demo8 extends Super2{
    public Demo8(String s){
        //super();默認進入 空參數構造函數,然後返回子構造函數,所以i=2
        i=2;
    }
    public static void main(String []args){
        Demo8 d =new Demo8("yes");
        System.out.println(d.i);
        
    }
    
}

//=============================================================================
/*15
 * 寫出程序結果:編譯失敗
 * 因爲子父類中的get方法沒有覆蓋,但是,子類調用的時候不能明確返回的值是什麼類型
 * 所以這樣的函數不可以存在 於字符類中
 *
 */
class Super3{
    public int get(){
        return 4;
        }
}
class Demo9 extends Super3{
    //public long get(){return 5;}//該行出錯,後期註釋。
    public static void main(String []agrs)
    {
        Super3 s=new Demo9();
        System.out.println(s.get());
    }
}
//=============================================================================
/*16
 * 寫出程序結果:編譯失敗[A]執行不到。
 * throw 都是結束語句,後面是不可以跟語句的(return,continue都是結束語句)
 * 如果去掉該語句,結果是:B,d
 */
class Demo10{
    public static void func(){
        try{
            throw new Exception();
            //System.out.println("A");//後期註釋
        }
        catch(Exception e){
            System.out.println("B");
        }
    }
        public static void main(String []args){
            try{
                func();
            }
            catch(Exception e){
                System.out.println("C");
            }
            System.out.println("D");
        }    
}
//=============================================================================
//17  選擇題:   A  、D
class Demo11{
    public void func(){
        //位置1
        new Inner3();
    }
    class Inner3{}
    public static void main(String []args){
        Demo11 d=new Demo11();
        //位置2
        
    }
}
/*
    A、在位置1寫: new Inner3();//Yes
    B、在位置2寫: new Inner3();//不可以,以newi主函數是靜態的,想調用Inner3必須被static修飾
    C、在位置2寫: new d.Inner3();//格式錯誤    
    D、在位置2寫: new Demo11.Inner3();//錯誤,因爲Inner3不是靜態的。
     */

//=============================================================================
/*18
 * 寫出程序結果:編譯失敗
 * 多個catch時,父類的catch要放在下面
 * 這段代碼不止這點錯誤。不知道如何表達。
 */
/*
class Exco1 extends Exception{}
class Exco2 extends Exco1{}

class Demo12{
    public static void main(String []args){
        try{
            throw new Exc1();
        }
        catch(Exception e){
            System.out.println("E");
        }
        catch(Exco0 e){
            System.out.println("0");
        }    
    }    
}
*/
//=============================================================================
//19
interface Test1{
    void func();
}
class Demo13{
    void show(Test1 t){
        t.func();
    }
    public static void main(String []args){
        //補齊代碼代碼:(匿名內部類)
        
        new Demo13().show(
                new Test1(){
                    public void func(){
                        System.out.println("匿名內部類");
                }});
        /*1、調用showv();>>>>new  Demo13().show(null);
         *2、用匿名函數替換null即可
         */    
    }
}

//=============================================================================
/*20  國際考題
 * 寫出結果  :
 * 134
 * 13423
 * 我寫的是
 *  134  
 *  234
 *  爲什麼錯呢,
 *  1、因爲我忽略了output是字符串,是字符串連接
 *  foo(0)執行完了以後,output="134"  這個時候在執行foo(1) 肯定就變成了 "134234"
 *  2、return 語句是結束語句,output+="4";是不執行的,
 */
class Test4{
    public static String output="";
    public static void foo(int i){
        try{
            if(i==1)
                throw new Exception();
                output+="1";
        }
        catch(Exception e){
            output+="2";
            return;
        }
        finally{
            output+="3";
        }
        output+="4";
    }
    public static void main(String args[]){
        foo(0);
        System.out.println("output"+output);
        foo(1);
        System.out.println(output);
    }
}


//=============================================================================
/*21
 * 建立一個圖形接口,聲明一個面積函數。圓形和矩形都實現這個接口,並得出兩個圖形的面積。
 * 注:體現面向對象特徵,對數值判斷。用異常處理,不合法的數值要出現”非法“提示,不再進行計算
 */


//=============================================================================
/*22
 * 補足compare函數內的代碼,不允許添加其他函數。                         考的是最大值
 */
class Circle{
    private static double pi=3.14;
    private double radius;
    public int radiusr2;
    public Circle(double r){
        radius=r;
    }
    public static  double compare(Circle [] cir){
        
        //補齊代碼,其實就是求數組中最大值。
        int max=0;//double max=cir[0].radius;
        for(int i=0;i<cir.length;i++){
            if(cir[i].radius>cir[max].radius){//(cir[i].radius>max)
                max=i;                        //max=cir[i].radius
            }
        }
        return max;
    }
}

class TC1{
    public static void main(String []args){
         Circle cir[]=new Circle[3];//對象類型數組 ,裏面都是對象,而求的是對象的r。
         cir[0]=new Circle(1.0);//賦值
         cir[1]=new Circle(2.0);
         cir[2]=new Circle(4.0);
         System.out.println("最大的半徑值是:"+Circle.compare(cir));//求的是最大半徑注意
    }
}
/*首先main方法中,new的是一個對象類型的數組,裏面存的都是對象,要求比較的是對象裏的值
 * 所以在函數中,寫循環的時候一定要注意。註釋中是2種方法,都可以。
 */

//=============================================================================
/*23
 * 寫出運行結果:4
 */
class Demo14{
    
    private static int j=0;
    private static boolean methodB(int k){
        j+=k;
        return true;
    }
    public static void methodA(int i){
        boolean b;
        b = i < 10 |  methodB(4);
        b = i < 10 || methodB(8);//這句話methodB(8)沒有執行,因爲b = i < 10爲真後面就不需要執行了。
    }
    public static void main (String []args){
        methodA(0);
        System.out.println(j);
    }
}

//=============================================================================
/*24
 * 假如我們在開發一個系統時,需要對員工進行建模,員工包含3個屬性:姓名、工號、工資
 * 經理也是員工,除了好友員工的屬性外,另外還有一個獎金屬性,請使用繼承的思想設計出員工類和經理類
 * 要求類中提供必要的方法進行屬性的訪問。
 */

//=============================================================================
/*25
 * 在一個類中編寫一個方法,這個方法搜索一個字符數組中是否存在某個字符,
 * 如果存在,則返回這個字符在字符串中第一次出現的位置,(序號從0開始計算,)
 * 否則,返回-1,要搜索的字符數組和字符都以參數的形式傳遞給該方法,
 * 如果傳入的數組爲null,應該拋出IllegalArgumentException異常
 * 在類main方法中以各種可能出現的情況測試驗證方法驗證該方法編寫是否正確。
 * 例如,字符不存在、字符存在、傳入數組爲null等。
 */

//=============================================================================
/*26
 * 補足代碼compare 函數內的代碼,不添加其他函數                              考的是this
 */
class Circle2{
    private double radius2;
    public Circle2(double r){
        radius2=r;
    }
    public Circle2 compare (Circle2 cir){
    //    補足代碼
        /* 過度思維
        if(this.radius2>cir.radius2)
            return this;
        return cir;
        */
        return ((this.radius2>cir.radius2)?this:cir) ;
        
    }
}
class TC2{
    public static void main (String []args){
        Circle2 cir1=new Circle2(3.0);
        Circle2 cir2=new Circle2(5.0);
        Circle2 cir;
        cir=cir1.compare(cir2);
        if(cir1==cir)
            System.out.println("1半徑大");
        else
            System.out.println("2半徑大");
    }
 }




---------- android培訓、 java培訓 、期待與您交流!----------
黑馬官網: http://edu.csdn.net/heima
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章