java編程思想第四版第三章要點習題

來源:互聯網
上載者:User

標籤:oid   []   pass   ram   sso   and   equals   時間   硬幣   

  1. 使用"簡短的" 和正常的 列印語句來編寫一個程式
    package net.mindview.util;public class Print {    /**     * 不帶有斷行符號     * @param s     */    public static void print(Object s){        System.out.print(s);    }        /**     * 帶有斷行符號     * @param s     */    public static void println(Object s){        System.out.println(s);    }}
    package net.mindview.operators;import java.util.Date;import static net.mindview.util.Print.*;public class HelloData {    public static void main(String[] args) {        print("hello, it is");        print(new Date());        System.out.println("正常的方式列印");    }}

     

  2. 建立一個包含了float域的類, 並用這個類來展示別名機制. (這裡懶得寫了, 就是把demo中的int換成float就可以了)
    package net.mindview.operators;class Tank{    int level;}public class Assignment {    public static void main(String[] args) {        Tank t1 = new Tank();        Tank t2 = new Tank();        t1.level = 27;        t2.level = 41;        System.out.println("t1.level:" + t1.level + ", t2.level:" + t2.level);        t2 = t1;        System.out.println("t1.level:" + t1.level + ", t2.level:" + t2.level);        t1.level = 5;        System.out.println("t1.level:" + t1.level + ", t2.level:" + t2.level);    }}

     

  3. 建立一個包含一個float域的類, 並用這個類來展示方法調用時的別名機制(將char改為float即可)
    package net.mindview.operators;class Letter{    char c;}public class PassObject {    static void  f(Letter y){        y.c = ‘z‘;    }    public static void main(String[] args) {        Letter x = new Letter();        x.c = ‘a‘;        System.out.println("1: x.c="+x.c);        //傳遞的時x所指向的引用        f(x);        System.out.println("1: x.c="+x.c);    }}

     

  4. 編寫一個計算速度的程式, 壓縮使用的距離和時間都是常量.(略)
  5. 建立一個名為Dog的類, 他包含兩個String與:name和says。 在main()方法中,建立兩個Dog對象, 一個名為spot(它的叫聲為ruff!),另一個名為scruffy(它的叫聲為Wurf!).然後顯示他們的名字和叫聲。
    package net.mindview.operators;class Dog{    public String name;    public String says;        @Override    public String toString() {        return "名字:"+this.name + ",語言:"+this.says;    }}public class DogTest {    public static void main(String[] args) {        // TODO Auto-generated method stub        Dog d1 = new Dog();        Dog d2 = new Dog();        d1.name = "spot";        d1.says = "Ruff!";        d2.name = "scruffy";        d2.says = "Wurf!";                System.out.println(d1);        System.out.println(d2);    }}

     

  6. 在練習5的基礎上,建立一個新的Dog對象, 並對其賦值為spot對象。測試==和equals()方法來比較所有引用的結果。
    package net.mindview.operators;class Dog{    public String name;    public String says;        @Override    public String toString() {        return "名字:"+this.name + ",語言:"+this.says;    }}public class DogTest {    public static void main(String[] args) {        // TODO Auto-generated method stub        Dog d1 = new Dog();        Dog d2 = new Dog();        d1.name = "spot";        d1.says = "Ruff!";        d2.name = "scruffy";        d2.says = "Wurf!";                System.out.println(d1);        System.out.println(d2);                Dog d3 = new Dog();        d3.name = "spot";        System.out.println(d1.name == d3.name);        System.out.println(d1.name.equals(d3.name));    }}

    輸出結果:

    名字:spot,語言:Ruff!名字:scruffy,語言:Wurf!truetrue

    這個結果需要特別說明一下, String是特殊的參考型別, 當他被直接賦值時,就是把這個值對應的引用位置賦值給String變數了, 所以, 兩次結果都是true。 如果你用new String()賦值, 結果就不同了.

  7. 編寫一個程式, 類比扔硬幣的結果
    package net.mindview.operators;import java.util.Random;import static net.mindview.util.Print.*;public class ThrowCron {    public static void main(String[] args) {        Random num = new Random();        int a = num.nextInt(100);        switch (a % 2){             case 0:                println("正面"); break;            case 1:                println("反面");break;        }    }}

     

  8. fadsfas
  9. fasfda
  10. fasdf
  11. fasdfa
  12. fasdf
  13. fasd
  14. fdasf
  15. fadsf

java編程思想第四版第三章要點習題

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.