小米课
返回首页 | 淘宝问答 | 提交答案
关键字: 赤脚医生 优秀典型 理性情绪 情绪疗法 三个阶段 标杆管理 少数民族 省份 | 时间:2024-09-16 07:09

发布时间:2024-09-16 07:09

| 人浏览

【答案】Java习题(第6章)

第六章 1. Object类定义在java.lang包中,它是所有类的根类。

2. 参考代码如下:

public class Square implements Cloneable{

private double length;

public Square(double length){

this.length = length;

}

public void setLength(double length){

this.length = length;

}

public double getLength(){

return length;

}

public boolean equals(Square s){

if(this.length==s.length){

return true;

}else{

return false;

}

}

public String toString(){

return "Square[length="+length+"]";

}

public static void main(String args[])

throws CloneNotSupportedException{

Square sq1 = new Square();

sq1.setLength(10);

System.out.println(sq1);

Square sq2 = (Square)sq1.clone();

System.out.println(sq1);

System.out.println(sq1.equals(sq2));

}

}

3. 答:① Math.pow(x ,y) ② Math.min(x, y)

③ Math.round(d) ④Math.round(d) ⑤ Math.atan(d)

4. 答:返回1到6之间的随机整数。

5. 参考程序如下:

public class RandomTest{

public static void main(String[]args){

int[] tj = new int[6];

for(int i=0;i<1000;i++){

int r = (int)(Math.random()*6)+1;

switch(r){

case 1:tj[0]++;break;

case 2:tj[1]++;break;

case 3:tj[2]++;break;

case 4:tj[3]++;break;

case 5:tj[4]++;break;

case 6:tj[5]++;break;

}

}

for(int i=0;i<6;i++){

System.out.println("tj["+i+"]="+tj[i]);

}

}

}

将上述程序中循环变量值改为1000,即可生成1000个随机整数。从输出结果可以看到每个数出现的次数大致相同。

6. 参考程序如下:

public class AreaTest{

public static void main(String[]args){

double area = 0; // 存放面积变量

double degree = 30 ; // 存放角度变量

area = 1 / 2.0 * 4.0 * 5.0 * Math.sin(Math.toRadians(degree));

System.out.println("该三角形的面积为:" + area);

}

}

7. 答:C

8. 答:数据类型包装类可以将基本类型数据作为对象处理。对每种基本数据类型,Java都提供了一个数据类型包装类。

9. 参考程序如下:

import static java.lang.System.out;

public class NumberTest{

public static void main(String[]args){

out.println(Byte.MAX_VALUE);

out.println(Short.MAX_VALUE);

out.println(Integer.MAX_VALUE);

out.println(Long.MAX_VALUE);

out.println(Float.MAX_VALUE);

out.println(Double.MAX_VALUE);


out.println(Byte.MIN_VALUE);

out.println(Short.MIN_VALUE);

out.println(Integer.MIN_VALUE);

out.println(Long.MIN_VALUE);

out.println(Float.MIN_VALUE);

out.println(Double.MIN_VALUE);

}

}

10. 参考程序如下:

public class RuntimeTest {

public static void main (String[] args) {

Runtime runtime = Runtime.getRuntime();

long freeMemory = runtime.freeMemory();

System.out.println("Free Memory:"+freeMemory);

Circle circle[] = new Circle[10000];

for(int i=0;i<circle.length;i++){

circle[i] = new Circle();

}

freeMemory = runtime.freeMemory();

System.out.println("Free Memory:"+freeMemory);

for(int i=0;i<circle.length;i++){

circle[i] = null;

}

runtime.gc();

freeMemory = runtime.freeMemory();

System.out.println("Free Memory:"+freeMemory);

}

}

11. 参考程序如下:

import java.io.Console;

public class ConsoleTest {

public static void main (String[] args) {

Console console = System.console();

System.out.print("请输入一个字符串:");

String s = console.readLine();

System.out.println("您输入的字符串:"+s);

}

}

12. 代码如下:

Class.forName("oracle.jdbc.driver.OracleDriver ");

13. 答:

① 程序中使用Date类,应添加下面的import语句:

import java.util.Date;

② DateFormat是抽象类,要得到该类对象需使用getDateInstance()方法,如:

DateFormat df = DateFormat.getDateInstance();

程序输出:2005-8-10

getDateInstance()方法可带参数指定日期的样式,如:

DateFormat df = DateFormat.getDateInstance(DateFormat.LONG)

程序输出:2005年8月10日

14. 程序中4个空的内容为:

① import java.text.DateFormat;

② DateFormat.MEDIUM

③ DateFormat.FULL

④ df.format(d)

15. 程序的运行结果。

0 2 ab

4 6 ab

16. 程序的运行结果。

0

1

2 89

4

5

6 5

7


答案有错

上一篇:Java习题(第7章)

下一篇:暂无

小米课手机端XML联系我们