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


 

目录
相关文章
|
6月前
|
设计模式 敏捷开发 Java
全网首发!Java界的四大名著之一:Java编程思想最新中文版已开源
老版《Java编程思想》(原书名《Thinking in Java》)得益作者开放深度研讨的创作方式,受到了全世界读者的追捧,被译为了十几种语言。但遗憾的是,在经历了 4 个版本的更新后,其最后一版发布于 2007 年,之后再无更新。
|
1月前
|
Java 数据安全/隐私保护
JAVA经典习题详解
JAVA经典习题详解
17 4
|
5月前
|
存储 安全 Java
小白聊《JAVA编程思想》-一切都是对象(一)
小白聊《JAVA编程思想》-一切都是对象(一)
27 0
|
1月前
|
Java 数据安全/隐私保护
java学习笔记(基础习题)
java学习笔记(基础习题)
33 0
|
5月前
|
Java
Java编程思想(面向对象)第四版PDF分享
探索《Java编程思想》第四版,理解Java不仅是特性集合,更是解决问题的强大工具。本书深入设计层面,构建编程思维模型,助你逐步精通Java。[阅读更多](https://zhangfeidezhu.com/?p=355) ![Java编程思想](https://ucc.alicdn.com/pic/developer-ecology/nrw3f3oqlpmag_c8ff959a921545f1bbabcefd37f029cf.png)
60 1
Java编程思想(面向对象)第四版PDF分享
|
5月前
|
存储 Java 容器
小白聊《JAVA编程思想》-对象导论二
小白聊《JAVA编程思想》-对象导论二
27 0
|
5月前
|
存储 Java 数据安全/隐私保护
小白聊《JAVA编程思想》 对象导论一
小白聊《JAVA编程思想》 对象导论一
29 0
|
5月前
|
设计模式 安全 Java
Java编程思想重点开发必看
Java编程思想重点开发必看
28 0
|
5月前
|
设计模式 Java 程序员
《On Java 8》中文版,又名《Java 编程思想》中文第五版
《On Java 8》中文版,又名《Java 编程思想》中文第五版
219 0
|
6月前
|
Java
<Java>逻辑控制,方法详解,重载,牛客习题,IDEA调试方法...
<Java>逻辑控制,方法详解,重载,牛客习题,IDEA调试方法
40 0