




版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
1、Java 編程思想(第四版)習題答案第二章練習 1: public class PrimitiveTest static int i;static char c;public static void main(String args) System.out.println("int = " + i);System.out.println("char = " + c);練習 2: public class HelloWorld public static void main(String args) System.out.println("Hel
2、lo World!");練習 3: public class ATNTest public static void main(String args) class ATypeName int i;double d;boolean b;void show() System.out.println(i);System.out.println(d);System.out.println(b);ATypeName a = new ATypeName();a.i = 3;a.d = 2.71828;a.b = false;a.show();練習 4: public class DataOnly
3、Test public static void main(String args) class DataOnly int i;double d;boolean b;void show() System.out.println(i);System.out.println(b);DataOnly data = new DataOnly();data.i = 3;data.d = 2.71828;data.b = false;data.show();練 5: public class DOTest2 public static void main(String args) class DataOnl
4、y int i;double d;boolean b;void show() System.out.println(i);System.out.println(d);System.out.println(b);DataOnly data = new DataOnly();data.i = 234;data.d = 2.1234545;data.b = true;data.show();練 6: public class StorageTest public static void main(String args) class StoreStuff int storage(String s)
5、return s.length() * 2;StoreStuff x = new StoreStuff();System.out.println(x.storage("hi");練 7:class StaticTest static int i = 47;class Incrementable static void increment() StaticTest.i+; public class ITest public static void main(String args) System.out.println("StaticTest.i= " +
6、 StaticTest.i);StaticTest st1 = new StaticTest();StaticTest st2 = new StaticTest();System.out.println("st1.i= " + st1.i);System.out.println("st2.i= " + st2.i);Incrementable sf = new Incrementable();sf.increment();System.out.println("After sf.increment() called: ");Syste
7、m.out.println("st1.i = " + st1.i);System.out.println("st2.i = " + st2.i);Incrementable.increment();System.out.println("After Incrementable.increment called: ");System.out.println("st1.i = " + st1.i);System.out.println("st2.i = " + st2.i);練習 8: class
8、StaticTest static int i = 47;class Incrementable static void increment() StaticTest.i+; public class OneStaticTest public static void main(String args) System.out.println("StaticTest.i= " + StaticTest.i);StaticTest st1 = new StaticTest();StaticTest st2 = new StaticTest();System.out.println
9、("st1.i= " + st1.i);System.out.println("st2.i= " + st2.i);Incrementable.increment();System.out.println("After Incrementable.increment() called: ");System.out.println("st1.i = " + st1.i);System.out.println("st2.i = " + st2.i);Incrementable.increment()
10、;System.out.println("After Incrementable.increment called: ");System.out.println("st1.i = " + st1.i);System.out.println("st2.i = " + st2.i);st1.i = 3;System.out.println("After st1.i = 3, ");System.out.println("st1.i = " + st1.i);System.out.println(&q
11、uot;st2.i = " + st2.i);System.out.println("Create another StaticTest, st3.");StaticTest st3 = new StaticTest();System.out.println("st3.i = " + st3.i);練習 9: public class AutoboxTest public static void main(String args) boolean b = false;char c = 'x'byte t = 8;short s
12、= 16;int i = 32;long l = 64;float f = 0.32f;double d = 0.64;Boolean B = b;System.out.println("boolean b = " + b);System.out.println("Boolean B = " + B);Character C = c;System.out.println("char c = " + c);System.out.println("Character C = " + C);Byte T = t;Syst
13、em.out.println("byte t = " + t);System.out.println("Byte T = " + T);Short S = s;System.out.println("short s = " + s);System.out.println("Short S = " + S);Integer I = i;System.out.println("int i = " + i);System.out.println("Integer I = " + I
14、);Long L = l;System.out.println("long l = " + l);System.out.println("Long L = " + L);Float F = f;System.out.println("float f = " + f);System.out.println("Float F = " + F);Double D = d;System.out.println("double d = " + d);System.out.println("Dou
15、ble D = " + D);練習 10 : public class CommandArgTest public static void main(String args) intargs1=1,2,3;System.out.println("args0 = " + args10);System.out.println("args1 = " + args11);System.out.println("args2 = " + args12);練習 11:public class Rainbow public static v
16、oid main(String args) AllTheColorsOfTheRainbow atc = new AllTheColorsOfTheRainbow();System.out.println("atc.anIntegerRepresentingColors = " + atc.anIntegerRepresentingColors);atc.changeColor(7);atc.changeTheHueOfTheColor(77);System.out.println("After color change, atc.anIntegerReprese
17、ntingColors ="+atc.anIntegerRepresentingColors);System.out.println("atc.hue = " + atc.hue);class AllTheColorsOfTheRainbow int anIntegerRepresentingColors = 0;int hue = 0;void changeTheHueOfTheColor(int newHue) hue = newHue;int changeColor(int newColor) return anIntegerRepresentingColo
18、rs = newColor;練習 12 : public class DocTest /* Entry poing to class & application.* param args array of string arguments* throws exceptions No exceptions thrown*/public static void main(String args) System.out.println("Hello, it's: ");System.out.println(new Date();練習 13-1: public cl
19、ass Documentation1 /* A field comment */public int i;/* A method comment */ public void f() 2: public class Documentation2 Date d = new Date();void showDate() System.out.println("Date = " + d);3: public class Documentation3 public static void main(String args) Date d = new Date();System.ou
20、t.println("d = " + d);練習 14 : public class Documentation4 public int i = 2;private int j = 3;public static void main(String args) Date d = new Date();System.out.println("d = " + d);練習 15 : public class HelloDocTest public static void main(String args) System.out.println("Hel
21、lo World!");練習 16 :class Tree int height;Tree() System.out.println("Planting a seedling");height = 0;Tree(int initialHeight) height = initialHeight;System.out.println("Creating new tree that is " + height + " feet tall"); void info() System.out.println("Tree i
22、s " + height + " feet tall");void info(String s) System.out.println(s + ": Tree is " + height + " feet tall");public class Overloading public static void main(String args) for(int i = 0; i < 5; i+) Tree t = new Tree(i);();("overloading method&qu
23、ot;);/ Overloaded constructor:new Tree();第三章練習 1: public class PrintTest public static void main(String args) print("Hello, from short form."); P.rintln("Hello from greggordon form."); System.out.println("Hello from long form.");練習 2: class Tube float level;public class
24、 Assign public static void main(String args) Tube t1 = new Tube();Tube t2 = new Tube();t1.level = 0.9f;t2.level = 0.47f;+ t2.level);+ t2.level);+ t2.level);P.rintln("1: t1.level: " + t1.level + ", t2.level: " t1 = t2;P.rintln("2: t1.level: " + t1.level + ", t2.leve
25、l: " t1.level = 0.27f;P.rintln("3: t1.level: " + t1.level + ", t2.level: "練習 3: class Box float a;public class PassObject2 static void f(Box y) y.a = 2.71828f;public static void main(String args) Box x = new Box();x.a = 3.1416f;print("1: x.a = " + x.a);f(x);print(&
26、quot;2: x.a = " + x.a);練習 4: class VelocityCalculator static float velocity (float d, float t) if(t = 0) return 0f;else return d/t;public class VelocityTester public static void main(String args) float d = 565.3f;float t = 3.6f;System.out.println("Distance: " + d);System.out.println(&
27、quot;Time: " + t);float v = VelocityCalculator.velocity(d, t); System.out.println("Velocity: " + v);練習 5: class Dog String name;String says;void setName(String n) name = n;void setSays(String s) says = s;void showName() P.rintln(name);void speak() P.rintln(says);public class DogTest p
28、ublic static void main(String args) Dog spot = new Dog(); spot.setName("Spot");spot.setSays("Ruff!");Dog scruffy = new Dog(); scruffy.setName("Scruffy"); scruffy.setSays("Wurf!"); spot.showName();spot.speak(); scruffy.showName(); scruffy.speak();練習 6: class Dog String name;String says;void setName(String n) name = n;void setSays(String s) says = s;void showName() P.rintln(name);void speak() P.rintln(says);public class DogCompare public static void main(String args) Dog spot = new Dog();spo
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經權益所有人同意不得將文件中的內容挪作商業或盈利用途。
- 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025山煤國際井下操作技能人員招聘150人(山西)筆試參考題庫附帶答案詳解
- 25年公司廠級員工安全培訓考試試題新版
- 2024-2025新入職工安全培訓考試試題答案A卷
- 2025簡約式門面房屋租賃合同樣本
- 2025融資租賃合同金融范本
- 2025授權融資合同范本
- 就業協議書失效
- 2025企業實習生合同
- 2025餐飲服務承包合同范本
- 2025裝飾裝潢工程承包合同
- 2025年裝維智企工程師(三級)復習模擬100題及答案
- 國家管網集團西南管道昆明輸油氣分公司突發環境事件綜合應急預案
- 停送電培訓課件
- 醫院培訓課件:《核心制度-護理值班和交接班制度》
- 解題秘籍05 圓的綜合問題(9種題型匯-總+專題訓練)(解析版)-2025年中考數學重難點突破
- 美學《形象設計》課件
- 江蘇省建筑與裝飾工程計價定額(2014)電子表格版
- 2022年續聘申請書
- 單片機病房呼叫系統設計
- 交通信號系統紅綠燈安裝專項施工方案
- DB14∕T 2024-2020 出口水果包裝廠管理規范
評論
0/150
提交評論