JAVA編程思想第四版—第二章—習題與答案
(1) 參照本章的第一個例子,創建一個“Hello,World”程序,在屏幕上簡單地顯示這句話。注意在自己的類裏隻需一個方法(“main”方法會在程序啟動時執行)。記住要把它設為static形式,並置入自變量列表——即使根本不會用到這個列表。用javac編譯這個程序,再用java運行它。
public class HelloWorld
{
public static void main(String[] args)
{
System.out.println("Hello,World");
}
}
(2) 寫一個程序,打印出從命令行獲取的三個自變量。
public class GetArgs
{
public static void main(String[] args)
{
System.out.println(args[0]);
System.out.println(args[1]);
System.out.println(args[2]);
}
}
//java GetArgs a 3 12
(3)
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 is "+height+" feet tall");
}
void info(String s){
System.out.println(s+":Tree is "+height+" feettall");
}
}
public class OverLoading{
public static void main(String[] args){
new Tree();
for (int i=0;i<5;i++){
Tree t=new Tree(i);
t.info();
t.info("overloading method");
}
}
}
輸出結果:
Microsoft Windows [版本 6.1.7600]
版權所有 (c) 2009 Microsoft Corporation。保留所有權利。
D:\>javac OverLoading.java
D:\>java OverLoading
Planting a seedling
Creating new tree that is 0 feet tall
Tree is 0 feet tall
overloading method:Tree is 0 feettall
Creating new tree that is 1 feet tall
Tree is 1 feet tall
overloading method:Tree is 1 feettall
Creating new tree that is 2 feet tall
Tree is 2 feet tall
overloading method:Tree is 2 feettall
Creating new tree that is 3 feet tall
Tree is 3 feet tall
overloading method:Tree is 3 feettall
Creating new tree that is 4 feet tall
Tree is 4 feet tall
overloading method:Tree is 4 feettall
D:\>
(4)
//輸出當前文件兩倍長度
public class Demo {
public static void main(String[] args) {
class StoreStuff {
int storage(String s) {
return s.length() * 2;
}
}
StoreStuff x = new StoreStuff();
System.out.println(x.storage("hi"));
}
}
/**
output:
4
/
(5)
public class DataOnlyTestTwo{
public static void main(String[] args){
class DataOnly{
int a;
double b;
boolean c;
void show()
{
System.out.println(a);
System.out.println(b);
System.out.println(c);
}
}
DataOnly test=new DataOnly();
test.a=234;
test.b=2.1234545;
test.c=true;
test.show();
}
}
/**
output:
234
2.1234545
true
/
(6)
//精度設置,封裝調用
public class DataOnlyTest{
public static void main(String[] args){
class DataOnly{
int a;
double b;
boolean c;
void show()
{
System.out.println(a);
System.out.println(b);
System.out.println(c);
}
}
DataOnly test=new DataOnly();
test.a=20;
test.b=3.141592653;
test.c=true;
test.show();
}
}
/**
output:
20
3.141592653
true
*/
(7)
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();
}
}
/**
output:
3
2.71828
false
*/
(8)
//數據元素測試
public class Primitive{
static int i;
static char c;
public static void main(String[] args){
System.out.println("int="+i);
System.out.println("char="+c);
}
}
/**
output:
int = 0
char =
*/
(9)
// object/Rainbow.java
// TIJ4 Chapter Object, Exercise 11, page 90
// Turn the AllColorsOfTheRainbow into a program that compiles and runs.
public class RainBow {
public static void 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.anIntegerRepresentingColors = " + 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 anIntegerRepresentingColors = newColor;
}
}
Microsoft Windows [版本 6.1.7600]
版權所有 (c) 2009 Microsoft Corporation。保留所有權利。D:\>javac RainBow.java
D:\>java RainBow
atc.anIntegerRepresentingColors = 0
After color change, atc.anIntegerRepresentingColors = 7
atc.hue = 77
D:\>
(10)
class StaticTest{
static int i=47;
}
class Increamentable{
static void increment(){StaticTest.i++;};
}
public class Test{
public static void main(String[] args){
System.out.println("StaticTest.i="+StaticTest.i);
StaticTest st1=new StaticTest();
StaticTest st2=new StaticTest();
System.out.println("st1.i="+st1.i);
System.out.println("st2.i="+st2.i);
Increamentable.increment();
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("st1.i="+st1.i);
System.out.println("Create another StaticTest,st3");
StaticTest st3=new StaticTest();
System.out.println("st3.i="+st3.i);
}
}
Microsoft Windows [版本 6.1.7600]
版權所有 (c) 2009 Microsoft Corporation。保留所有權利。
D:\>javac Test.java
D:\>java Test
StaticTest.i=47
st1.i=47
st2.i=47
After Incrementable increment() called:
st1.i=48
st2.i=48
After st1.i=3,
st1.i=3
st1.i=3
Create another StaticTest,st3
st3.i=3
D:\>
(11)
public class test {
public static void main(String[] args){
boolean b=false;
char c='x';
byte t=8;
short s=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("CharacterC="+C);
Byte T=t;
System.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);
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("Double D="+D);
}
}
最後更新:2017-04-02 06:52:03