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);
    	
    }
}


 

上一篇:详解:Drools规则引擎探究以及在IOT的应用


下一篇:VS2010中使用JSONCPP方法