分享

Java 2 实用教程(第5版)耿祥义版 习题八...

 春和秋荣 2020-10-19
一、问答题
1."\hello"是正确的字符串常量吗?
2."你好KU".length()和"\n\t\t".length()的值分别是多少?
3."Hello".equals("hello")和"java".equals("java")的值分别是多少?
4."Bird".compareTo("Bird fly")的值是正数还是负数?
5."I love this game".contains("love")的值是true吗?
6."RedBird".indexOf("Bird")的值是多少?"RedBird".indexOf("Cat")的值是多少?
7.执行Integer.parseInt("12.9");会发生异常吗?
二、选择题
1.下列哪个叙述是正确的?
A. String 类是final 类,不可以有子类。
B.    String 类在java.util包中。
C.    "abc"=="abc"的值是false .
D.    "abc".equals("Abc")的值是true
2.下列哪个表达式是正确的(无编译错误)?
A.  int m =Float.parseFloat("567"); 
B.    int m =Short.parseShort("567")
C.    byte m =Integer.parseInt("2");
D.    float m =Float.parseDouble("2.9") 
3.对于如下代码,下列哪个叙述是正确的?
A. 程序编译出现错误。
B.    程序标注的【代码】的输出结果是bird。
C.    程序标注的【代码】的输出结果是fly。
D.    程序标注的【代码】的输出结果是null。 
public class E{ 
   public static void main(String[] args){ 
      String strOne="bird"; 
      String strTwo=strOne; 
      strOne="fly"; 
      System.out.println(strTwo); 
  } 
}
4.对于如下代码,下列哪个叙述是正确的?
A. 程序出现编译错误。
B.    无编译错误,在命令行执行程序:“java E I love this game”,程序输出this。
C.无编译错误,在命令行执行程序:“java E let us go”,程序无运行异常。 
D.无编译错误,在命令行执行程序:“java E 0 1 2 3 4 5 6 7 8 9”程序输出3。 
public class E {
   public static void main (String args[]) {
     String s1 = args[1];
     String s2 = args[2];
     String s3 = args[3];
     System.out.println(s3); 
   }
}
5.下列哪个叙述是错误的?
A. "9dog".matches("\\ddog")的值是true。
B."12hello567".replaceAll("[123456789]+","@")返回的字符串是@hello@。
C.    new Date(1000)对象含有的时间是公元后1000小时的时间
D. "\\hello\n"是正确的字符串常量。
三、阅读程序
1.请说出E类中标注的【代码】的输出结果。
public class E { 
   public static void main (String[]args)   { 
      String str = new String ("苹果"); 
      modify(str); 
      System.out.println(str);   //【代码】
   } 
   public static void modify (String s)  { 
       s = s + "好吃";  
   } 
}   
2.请说出E类中标注的【代码】的输出结果。
import java.util.*;
class GetToken {
 String s[];
   public String getToken(int index,String str) { 
      StringTokenizer fenxi = new StringTokenizer(str); 
      int number = fenxi.countTokens();
      s = new String[number+1];
      int k = 1;
      while(fenxi.hasMoreTokens()) {
         String temp=fenxi.nextToken();
          s[k] = temp;
          k++;
      }
      if(index<=number)
        return s[index];
      else 
        return null;
   }
}
class E {
  public static void main(String args[]) {
      String str="We Love This Game";
      GetToken token=new GetToken();
      String s1 = token.getToken(2,str),
              s2 = token.getToken(4,str);
      System.out.println(s1+":"+s2);     //【代码】
   }
}
3.请说出E类中标注的【代码1】,【代码2】的输出结果。
public class E {
  public static void main(String args[]) {
      byte d[]="abc我们喜欢篮球".getBytes();
      System.out.println(d.length);   //【代码1】
      String s=new String(d,0,7);
      System.out.println(s);         //【代码2】
   }
}
4.请说出E类中标注的【代码】的输出结果。
class MyString {
  public String getString(String s) {
      StringBuffer str = new StringBuffer();
      for(int i=0;i<s.length();i++) {
         if(i%2==0) {
            char c = s.charAt(i);
            str.append(c);
          }
      }
     return new String(str);
   }
}
public class E {
public static void main(String args[ ]) {
String s = "1234567890";
      MyString ms = new MyString();
      System.out.println(ms.getString(s)); //【代码】
   }
}
5.请说出E类中标注的【代码】,的输出结果。
public class E {
  public static void main (String args[ ]) {
      String regex = "\\djava\\w{1,}" ;
      String str1 = "88javaookk";
      String str2 = "9javaHello";
      if(str1.matches(regex)) {
          System.out.println(str1); 
      }
      if(str2.matches(regex)) {
          System.out.println(str2); //【代码】 
      }  
   }
}
6.上机实习下列程序,学习怎样在一个月内(一周内、一年内)前后滚动日期,例如,假设是3月(有31天)10号,如果在月内滚动,那么向后滚动10天就是3月20日,向后滚动25天,就是3月4号(因为只在该月内滚动)。如果在年内滚动,那么向后滚动25天,就是4月4号。
import java.util.*;
public class RollDayInMonth {
   public static void main(String args[]) {
      Calendar calendar=Calendar.getInstance();   
      calendar.setTime(new Date());  
      String s = String.format("%tF(%<tA)",calendar);
      System.out.println(s);
      int n = 25;
      System.out.println("向后滚动(在月内)"+n+"天");
      calendar.roll(calendar.DAY_OF_MONTH,n);
      s = String.format("%tF(%<ta)",calendar);
      System.out.println(s);
      System.out.println("再向后滚动(在年内)"+n+"天");
      calendar.roll(calendar.DAY_OF_YEAR,n);
      s = String.format("%tF(%<ta)",calendar);
      System.out.println(s);
    }  
}
7.上机实习下列程序(学习Runtime类),注意观察程序的输出结果。
public class Test{
    public static void main(String args[]) {
        Runtime runtime = Runtime.getRuntime();
        long free = runtime.freeMemory();
        System.out.println("Java虚拟机可用空闲内存 "+free+" bytes");
        long total = runtime.totalMemory();
        System.out.println("Java虚拟机占用总内存 "+total+" bytes");
        long n1 = System.currentTimeMillis();
        for(int i=1;i<=100;i++){
           int j = 2;
           for(;j<=i/2;j++){
             if(i%j==0) break;
           }
           if(j>i/2)  System.out.print(" "+i);
        }
        long n2 = System.currentTimeMillis();
        System.out.printf("\n循环用时:"+(n2-n1)+"毫秒\n");
        free = runtime.freeMemory();
        System.out.println("Java虚拟机可用空闲内存 "+free+" bytes");
        total=runtime.totalMemory();
        System.out.println("Java虚拟机占用总内存 "+total+" bytes");
    }
}
四、编程题
1.字符串调用public String toUpperCase()方法返回一个字符串,该字符串把当前字符串中的小写字母变成大写字母;.字符串调用public String toLowerCase()方法返回一个字符串,该字符串把当前字符串中的大写字母变成小写字母。String类的public String concat(String str)方法返回一个字符串,该字符串是把调用该方法的字符串与参数指定的字符串连接。编写一个程序,练习使用这3个方法。
2.String类的public char charAt(int index)方法可以得到当前字符串index位置上的一个字符。编写程序使用该方法得到一个字符串中的第一个和最后一个字符。
3.计算某年、某月、某日和某年、某月、某日之间的天数间隔。要求年、月、日使用main方法的参数传递到程序中(见例子4)。
4.编程练习Math类的常用方法。
5.编写程序剔除一个字符串中的全部非数字字符,例如,将形如“ab123you”的非数字字符全部剔除,得到字符串“123”(参看例子10)。
6.使用Scanner类的实例解析字符串:"数学87分,物理76分,英语96分"中的考试成绩,并计算出总成绩以及平均分数(参看例子14)。
  

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多