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

一、问答题
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)。
 

1.不是。"\\hello"是。
2.4和3。
3.false和true。
4.负数。
5.是true。
6.3和-1。
7.会发生NumberFormatException异常。
二、选择题
1.A。2.C。3.B。4.D。5.C。
三、阅读程序
1.【代码】:苹果。
2.【代码】:Love:Game。
3.【代码1】:15。【代码2】:abc我们。
4.【代码】:13579。
5.【代码】:9javaHello。
6.属于上机实习程序,解答略。
7.属于上机实习程序,解答略。

四、编程题
1.public class E {
  public static void main (String args[ ]) {
     String s1,s2,t1="ABCDabcd";
     s1=t1.toUpperCase();
     s2=t1.toLowerCase(); 
     System.out.println(s1);
     System.out.println(s2);
     String s3=s1.concat(s2);
      System.out.println(s3);
   }
}
2.   public class E {
  public static void main (String args[ ]) {
     String s="ABCDabcd";
     char cStart=s.charAt(0);
     char cEnd = s.charAt(s.length()-1);
     System.out.println(cStart);
     System.out.println(cEnd);
   }
}
3.   import java.util.*;
public class E {
  public static void main (String args[ ]) {
    int year1,month1,day1,year2,month2,day2;
      try{ year1=Integer.parseInt(args[0]);
           month1=Integer.parseInt(args[1]);
           day1=Integer.parseInt(args[2]);
           year2=Integer.parseInt(args[3]);
           month2=Integer.parseInt(args[4]);
           day2=Integer.parseInt(args[5]);
       }
       catch(NumberFormatException e)
         { year1=2012;
           month1=0;
           day1=1;
           year2=2018;
           month2=0;
           day2=1;
       } 
       Calendar calendar=Calendar.getInstance(); 
       calendar.set(year1,month1-1,day1);  
       long timeYear1=calendar.getTimeInMillis();
       calendar.set(year2,month2-1,day2);  
       long timeYear2=calendar.getTimeInMillis();
       long 相隔天数=Math.abs((timeYear1-timeYear2)/(1000*60*60*24));
       System.out.println(""+year1+"年"+month1+"月"+day1+"日和"+
                            year2+"年"+month2+"月"+day2+"日相隔"+相隔天数+"天");
   }
}
4.   import java.util.*;
public class E {
  public static void main (String args[ ]) {
   double a=0,b=0,c=0;
      a=12;
      b=24;
      c=Math.asin(0.56);
      System.out.println(c);
      c=Math.cos(3.14);
      System.out.println(c);
      c=Math.exp(1);
      System.out.println(c);
      c=Math.log(8);
      System.out.println(c);
   }
}
5.public class E {
      public static void main (String args[ ]) {
        String str = "ab123you你是谁?";
        String regex = "\\D+";
        str = str.replaceAll(regex,"");
        System.out.println(str);
      }
}
6. import java.util.*;
public class E {
   public static void main(String args[]) {
      String cost = "数学87分,物理76分,英语96分";
      Scanner scanner = new Scanner(cost);
      scanner.useDelimiter("[^0123456789.]+");
      double sum=0;
      int count =0;
      while(scanner.hasNext()){
         try{  double score = scanner.nextDouble();
               count++;
               sum = sum+score;
               System.out.println(score);
         } 
         catch(InputMismatchException exp){
              String t = scanner.next();
         }   
      }
      System.out.println("总分:"+sum+"分");
      System.out.println("平均分:"+sum/count+"分");
   }
}

 

  • 4
    点赞
  • 31
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值