0723作业


常用工具类作业

一、填空题
1.在Java中每个Java基本类型在java.lang包中都在一个相应的包装类,把基本类型数据转换为对象,其中int对应的包装类是_Integer________; char对应的包装类是____Character________。
2.包装类Integer的静态方法可以将字符串类型的数字”123”转换成基本整型变量n,其实现语句是:_int n =Integer.parseInt("123");___________。
3.在Java中使用java.lang包中的__StringBuffer__________类来创建一个字符串对象,它代表一个字符序列可变的字符串,可以通过相应的方法改变这个字符串对象的字符序列。
4.StringBuilder类是StringBuffer类的替代类,两者的共同点是都是可变长度字符串,其中线程安全的类是___StringBulider___________。
5.DateFormat类可以实现字符串和日期类型之间的格式转换,其中将日期类型转换为指定的字符串格式的方法名是__format____________。
6.使用Math.random( )返回带正号的 double值,该值大于等于0.0且小于1.0。使用该函数生成[30,60]之间的随机整数的语句是_(int)(Math.random()*31+30)____________。
7.JDK1.5后提供了____________关键字,用以定义枚举类。枚举类是一种特殊的类,可以有自己的属性、方法和构造方法。

二、选择题
1. 以下选项中关于int和Integer的说法错误的是( BD )。(选择二项)

A. int是基本数据类型,Integer是int的包装类,是引用数据类型
B. int的默认值是0,Integer的默认值也是0
C. Integer可以封装了属性和方法提供更多的功能
D. Integer i=5;该语句在JDK1.5之后可以正确执行,使用了自动拆箱功能

2. 分析如下Java代码,该程序编译后的运行结果是( D )。(选择一项)
public static void main(String[ ] args) {
String str=null;
str.concat("abc");
str.concat("def");
System.out.println(str);
}

A null
B. abcdef
C. 编译错误
D. 运行时出现NullPointerException异常


3. 以下关于String类的代码的执行结果是( B )。(选择一项)
public class Test2 {
public static void main(String args[]) {
String s1 = new String("bjsxt");
String s2 = new String("bjsxt");
if (s1 == s2)
System.out.println("s1 == s2");
if (s1.equals(s2))
System.out.println("s1.equals(s2)");
}
}

A. s1 == s2
B. s1.equals(s2)
C. s1 == s2
s1.equals(s2)
D. 以上都不对

4. 以下关于StringBuffer类的代码的执行结果是( C )。(选择一项)
public class TestStringBuffer {
public static void main(String args[]) {
StringBuffer a = new StringBuffer("A");
StringBuffer b = new StringBuffer("B");
mb_operate(a, b);
System.out.println(a + "." + b);
}
static void mb_operate(StringBuffer x, StringBuffer y) {
x.append(y);
y = x;
}
}

A. A.B
B. A.A
C. AB.AB
D. AB.B

5. 给定如下Java代码,编译运行的结果是( C )。(选择一项)
public static void main(String []args){
String s1= new String("pb_java_OOP_T5");
String s2 = s1.substring(s1.lastIndexOf("_"));
System.out.println("s2="+s2);
}


A s2=_java_OOP_T5
B. s2=_OOP_T5
C. s2=_T5
D. 编译出错
6. 对于语句String s="my name is kitty",以下选项中可以从其中截取”kitty”的是(AB )(选择二项)

A s.substring(11,16)
B. s.substring(11)
C. s.substring(12,17)
D. s.substring(12,16)

7. 分析下面的Java程序段,编译运行后的输出结果是( B )。(选择一项)
public class Test {
public void changeString(StringBuffer sb) {
sb.append("stringbuffer2");
}
public static void main(String[] args) {
Test a = new Test();
StringBuffer sb = new StringBuffer("stringbuffer1");
a.changeString(sb);
System.out.println("sb = " + sb);
}
}

A sb = stringbuffer2stringbuffer1
B. sb = stringbuffer1
C. sb = stringbuffer2
D. sb = stringbuffer1stringbuffer2

8. 给定如下Java代码,编译运行的结果是( A )。(选择一项)
public static void main(String[] args) {
StringBuffer sbf = new StringBuffer("java");
StringBuffer sbf1 = sbf.append(",C#");
String sbf2 = sbf + ",C#";
System.out.print(sbf.equals(sbf1));
System.out.println(sbf2.equals(sbf));
}

A true false
B. true true
C. false false
D. false true


9. 分析下面的Java程序,编译运行后的输出结果是( D )。(选择一项)
public class Example {
String str = new String("good");
char[] ch = { 'a', 'b', 'c' };
public static void main(String args[]) {
Example ex = new Example( );
ex.change(ex.str, ex.ch);
System.out.print(ex.str + "and");
System.out.print(ex.ch);
}
public void change(String str, char ch[]) {
str = "test ok";
ch[0] = 'g';
}
}

A goodandabc
B. goodandgbc
C. test okandabc
D. test okandgbc

10. 以下程序片段中可以正常编译的是( C )。(选择一项)

A String s = "Gone with the wind";
String k = s+t;
String t = "good";
B. String s = "Gone with the wind";
String t;
t = s[3]+"one";
C. String s = "Gone with the wind";
String stanfard = s.toUpperCase();
D. String s = "home directory";
String t = s – "directory";

三、判断题
1.方法Integer.parseInt()的作用是将一个整数转变成String。( F )
2.JK1.5后提供了自动装箱和自动拆箱功能,从而可以实现基本数据类型和对应包装类之间的自动转换,简化了操作。( T )
3.执行语句String str="abcedf"; int len=str.length; 后,能够得到字符串的长度是6。( F )
4.运算符“==”用于比较引用时,如果两个引用指向内存同一个对象,则返回true。( T )
5.求x的y次方,其表达式为:Math.pow(x,y)。( T )

四、简答题
1.自动装箱和自动拆箱
自动装箱:将基本数据类型转换为基本包装类型
自动拆箱:将基本包装类型转换为基本数据类型
2.String、StringBuffer、StringBuilder区别与联系。
String是不可变的字符串
StringBuffer是可变的字符串缓冲区,线程安全,效率低
StringBuilder和stringBuffer功能一样,只是线程不安全,效率高
五、编码题
1.验证键盘输入的用户名不能为空,长度大于6,不能有数字。
提示:使用字符串String类的相关方法完成

public class UserName {
/*
 * 分析:1.导入键盘输入 得到输入的字符串
 *        2.对输入的字符串进行分析 分别为:不能为空  
 *          长度大于6   分析字符串长度
 *          不能有数字   将字符串转为字符数组,判断数组中有没有数字
 *         
 * */
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入你的用户名:");
        String s = sc.next();    
    
        if(s.length() <= 6 && s.length() > 0) {
            byte[] arr = s.getBytes();
            for(int i = 0;i < arr.length;i++) {
                if(arr[i]>=48 && arr[i] <=57) {
                    System.out.println("对不起,用户名中不能有数字");
                }
                else {
                    System.out.println("你输入的用户名符合要求,请继续下一步操作");
                }
            }
        }else if(s.length() >= 6){
            System.out.println("对不起,用户名的长度不能大于6");
        }else{
            System.out.println("对还不起,用户名不能为空");
        }
    }
}

2.接收从键盘输入的字符串格式的年龄,分数和入学时间,转换为整数、浮点数、日期类型,并在控制台输出。
提示:使用包装类Integer、Double和日期转换类DateFormat实现

package com.zuikc.otherclass;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;

import javax.lang.model.util.SimpleAnnotationValueVisitor6;

public class Demo_Student {
    public static void main(String[] args) {
        
        Scanner sc = new Scanner(System.in);
        System.out.println("输入你的年龄");
        String age = sc.next();
        System.out.println("输入你的分数");
        String score = sc.next();
        System.out.println("输入你的入学时间");
        String time = sc.next();
        
        int i = Integer.parseInt(age);
        System.out.println(i);
        double d = Double.parseDouble(score);
        System.out.println(d);
        
        Date date1 = new Date(time);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
        System.out.println(sdf.format(date1));
                

    } 
    
}


3.根据交通信号灯颜色决定汽车停车、行驶和慢行
提示:使用枚举实现

六、可选题
1.生成10个[10,23)之间的随机整数
提示:分别使用Math.random()和Random类的nextDouble()或nextInt()实现

package com.zuikc.otherclass;

public class Demo_Random {

    public static void main(String[] args) {
            for(int i = 0;i < 10;i++) {
                int a =(int)(Math.random()*14)+10;
                System.out.println(a);
            }
    }
    
    

}
package com.zuikc.otherclass;

import java.util.Random;

public class Demo1_Random {

    public static void main(String[] args) {
        for(int i = 0;i < 10;i++) {
            Random r = new Random();
            int a =(int)(r.nextDouble()*14)+10;
            System.out.println(a);
        }
    }

}
package com.zuikc.otherclass;

import java.util.Random;

public class Demo2_Random {
    public static void main(String[] args) {
        for(int i=0;i < 10;i++) {
            Random r = new Random();
            int a =(r.nextInt(13)+10);
            System.out.println(a);
        }
    }
}

猜你喜欢

转载自www.cnblogs.com/zhangzheng1989/p/9357659.html