生活随笔
收集整理的這篇文章主要介紹了
java编程思想第四版第三章要点习题
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
使用"簡短的" 和正常的 打印語句來編寫一個程序 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("正常的方式打印");}
} ?
創建一個包含了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);}} ?
創建一個包含一個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);}
} ?
編寫一個計算速度的程序, 壓縮使用的距離和時間都是常量.(略)創建一個名為Dog的類, 他包含兩個String與:name和says。 在main()方法中,創建兩個Dog對象, 一個名為spot(它的叫聲為ruff!),另一個名為scruffy(它的叫聲為Wurf!).然后顯示他們的名字和叫聲。 package net.mindview.operators;class Dog{public String name;public String says;@Overridepublic String toString() {return "名字:"+this.name + ",語言:"+this.says;}
}
public class DogTest {public static void main(String[] args) {// TODO Auto-generated method stubDog 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);}
} ?
在練習5的基礎上,創建一個新的Dog對象, 并對其賦值為spot對象。測試==和equals()方法來比較所有引用的結果。 package net.mindview.operators;class Dog{public String name;public String says;@Overridepublic String toString() {return "名字:"+this.name + ",語言:"+this.says;}
}
public class DogTest {public static void main(String[] args) {// TODO Auto-generated method stubDog 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!
true
true 這個結果需要特別說明一下, String是特殊的引用類型, 當他被直接賦值時,就是把這個值對應的引用位置賦值給String變量了, 所以, 兩次結果都是true。 如果你用new String()賦值, 結果就不同了.
編寫一個程序, 模擬扔硬幣的結果 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;}}} ?
fadsfasfasfdafasdffasdfafasdffasdfdasffadsf
轉載于:https://www.cnblogs.com/ITPower/p/8509809.html
總結
以上是生活随笔為你收集整理的java编程思想第四版第三章要点习题的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。