J2SE测试题及源代码

1、编写一个程序,这个程序把一个整数数组中的每个元素用逗号连接成一个字符串,例如,根据内容为[1][2] [3]的数组形成内容为"1,2,3"的字符串。

class  ConnectDemo
{
 public static void main(String[] args)
 {
  int[] array = new int[]{1,2,3,4,5,6,7,8,9};
  StringBuffer sb = new StringBuffer();
  for(int i=0;i<array.length;i++)
  {
   sb.append(array[i]);
   sb.append(",");
  }
  sb.deleteCharAt(sb.length()-1);
  System.out.println(sb);
 }
}

2、 请在一个类中编写一个方法,这个方法搜索一个字符数组中是否存在某个字符,如果存在,则返回这个字符在字符数组中第一次出现的位置(序号从0开始计算),否则,返回-1。要搜索的字符数组和字符都以参数形式传递传递给该方法,如果传入的数组为null,应抛出IllegalArgumentException异常。在类的main方法中以各种可能出现的情况测试验证该方法编写得是否正确,例如,字符不存在,字符存在,传入的数组为null等。

class  SearchDemo
{
 public static void main(String[] args)
 {
  char[] array = new char[]{'a','b','c','d','e','f','7','$'};
  char[] array_null = null;
  Search s1 = new Search();
  s1.searchChar(array,'d');
  Search s2 = new Search();
  s2.searchChar(array,'8');
  Search s3 = new Search();
  s3.searchChar(array_null,'8');
 }
}
class Search
{
 public void searchChar(char[] array,char ch)
 {
  if(array == null)
   throw new IllegalArgumentException("传入数组参数为空!");
  else
  {
   int i=0;
   while(i<array.length)
   { 
    if(ch != array[i])
     i++;
    else
    {
     System.out.println(i);
     break;
    }
   }
   if(i == array.length)
    System.out.println("-1");
  }
  
 }
}

3、编写一个程序,它先将键盘上输入的一个字符串转换成十进制整数,然后打印出这个十进制整数对应的二进制形式。这个程序要考虑输入的字符串不能转换成一个十进制整数的情况,并对转换失败的原因要区分出是数字太大,还是其中包含有非数字字符的情况。

import java.io.*;
class StringToBinaryDemo
{
 public static void main(String[] args)
 {
  BufferedReader br = new BufferedReader( new InputStreamReader(System.in));
  int num = 0;
  try
  {
   String s = br.readLine();
   num = Integer.parseInt(s);
  }
  catch(NumberFormatException e)
  {
   System.out.println("传入字符串错误!");
  }
  catch(IOException e)
  {
   e.getMessage();
  }
  int array[] = new int[20];
  int i = 0;
  while(num!=0)
  {
   try
   {
    array[i]=num%2;
    num/=2;
    i++;
   }
   catch (ArrayIndexOutOfBoundsException e)
   {
    System.out.println("传入字符过大!");
    System.exit(-1);
   }
  }
  for(int j=i-1;j>=0;j--)
  System.out.print(array[j]+" ");
 }
}

4、请用移位的方式打印出一个十进制整数的十六进制形式。提示:按每4个二进制位对整数进行移位和去高位处理,得到的结果就是十六进制数的一位,然后按下面三种方式之一(作为作业,要求每种方式都用到)计算出一个十六进制数值对应的十六进制形式:

(1)0-9之间的数值直接加上字符'0',9以上的数值减去10以后再加上字符'A'

(2)定义一个数组,其中包含0-F这些字符,然后用要计算的数值作为数组的索引号,即可获得其对应的十六进制数据。

(3)Character.forDigit静态方法可以将一个十六进制的数字转变成其对应的字符表示形式,例如,根据数值15返回字符'F'。

class ToHex
{
 public static void main(String[] args)
 {
  int num = 266;
  int temp = 0;
  int[] array = new int[8];
  System.out.println(num);
  for(int i = 0;i<8;i++)
  {
   temp = num & 15;
   num = num>>4;
   array[i] = temp;
  }
  System.out.println("第一种方法:"+first(array));
  System.out.println("第二种方法:"+second(array));
  System.out.println("第三种方法:"+third(array));
 }

 public static String first(int[] array)
 {
  String str = "";
  char ch = 0;
  char a = 0;
  for(int i = 0;i<8;i++)
  {
   if(array[i]<=9)
   {
    a= (char) ('0'+array[i]);
    str = a + str;
   }
   else
   {
    ch= (char) ((array[i]-10)+'a');
    str = ch + str;
   } 
  }
  return str;
 }

 public static String second(int[] array)
 {
  String str = "";
  char[] ch = {'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};
  for(int i = 0;i<8;i++)
  {
   str = ch[array[i]]+ str;
  }
  return str;
 }

 public static String third(int[] array)
 {
  String str = "";
  for(int i = 0;i<8;i++)
 {
  str = Character.forDigit(array[i], 16) + str;
 }
  return str;
 }
}

5、 编写一个程序,用于实现文件的备份,程序运行时的命令语法为:java MyCopy (sourcefile) (destfile)

import java.io.*;
import java.util.*;
class CopyDemo
{
 public static void main (String [] args)
 {  
   
  try
  {
   FileInputStream in = new FileInputStream(args[0]);
   FileOutputStream out = new FileOutputStream(args[1]);
   byte b[] = new byte[1024];
   int line = 0;
   while((line=in.read())!=-1)
   {
    out.write((byte)line);
   }
   in.close();
   out.close();
  }
  catch (IOException e)
  {
   e.getMessage();
  }
   
 }
}

6、  在javascript视频的第七讲的第一个片断,讲到了将一个保存有ip地址与地区对照关系的文本文件导入到数据库时,应该将其中的某些空格替换成逗号(,),即对于如下格式的文本文件内容:
        起始IP                   结束IP                      地区
        61.54.231.245         61.54.231.245          河南省安阳市 新世纪网吧
        61.54.231.246         61.54.231.246          河南省安阳市 未知地区
        61.54.231.9             61.54.231.247          河南省安阳市 红日网吧
        61.54.231.248          61.54.231.248          河南省安阳市 安阳师范
        61.54.231.249          61.54.231.249         河南省安阳市 黑蜘蛛网吧
应转换成下面的这种格式:
        61.54.231.245,61.54.231.245 ,河南省安阳市 新世纪网吧
        61.54.231.246,61.54.231.246,河南省安阳市 未知地区
        61.54.231.9,61.54.231.247 ,河南省安阳市 红日网吧
        61.54.231.248,61.54.231.248,河南省安阳市 安阳师范学院
        61.54.231.249,61.54.231.249,河南省安阳市 黑蜘蛛网吧(师范学院附近)
import java.io.*;
class IPTransform
{
 public static void main(String[] args) throws IOException
 {
  BufferedReader br = new BufferedReader(new FileReader("D://java//a//a.txt"));
  BufferedWriter bw = new BufferedWriter(new FileWriter("D://java//a//b.txt"));
  String s = null;
  
  while((s=br.readLine())!=null)
  {
   String str = s.replaceAll("(?<=//d)//s+",",");
   String[] array = str.split(",");
   for(int i = 0;i<array.length-1;i++)
   {
    String[] sub_array = array[i].split("//.");
    String num ="";
    for(int j = 0;j<sub_array.length;j++)
    {
     String sub_str = "";

     if(sub_array[j].length() == 1)
     {
      sub_str = "00" + sub_array[j];
     }
     else if (sub_array[j].length() == 2)
     {
      sub_str = "0" + sub_array[j];
     }
     else
     {
      sub_str = sub_array[j];
     }
     
     if( 3 == j )
     {
      num = num + sub_str + ',';
     }
     else
     {
      num = num + sub_str + '.';
     }

    }
    System.out.print(num);
    bw.write(num);
    if( i%2 ==1)
    {
     System.out.println(array[2]);
     bw.write(array[2]+"/r/n");
    }
   }
  }
  bw.flush();
  br.close();
  bw.close();
 }
}

7、 请编写一个字符输入流的包装类,通过这个包装类对底层字符输入流进行包装,让程序通过这个包装类读取某个文本文件(例如,一个java源文件)时,能够在读取的每行前面都加上有行号和冒号。

import java.io.*;
class BufferedReaderDemo
{
 public static void main (String args[])
 {
  try
  {
   BufferedReader br = new BufferedReader(
     new FileReader("e://1.txt"));
   String str = "";
   int num=1;
   while((str=br.readLine())!=null)
   {
    String s = num+":"+str;
    num++;
    System.out.println(s);
   }
   br.close();
  }
  catch (IOException e)
  {
   e.getMessage();
  }
 }
}

8、  请结合我们的《javascript网页开发》一书中介绍的正则表达式与String.split方法,从"http://www.it315.org/get.jsp?user=zxx&pass=123"这样的URL地址中提取出每个参数的名称和值。这里要注意在正则表达式中要对?进行转义处理

import java.util.*;
class  SplitDemo
{
 public static void main(String[] args)
 {
  String s = "
http://www.it315.org/get.jsp?user=zxx&pass=123";
  String array[] = s.split("//?");
  String s1 =array[1];
  String array1[] = s1.split("//&");
  
  Map<String,String> hashmap = new HashMap<String,String>();
  for(int i=0;i<array1.length;i++)
  {
   String s2 = array1[i];
  
   String array2[] =  s2.split("=");

   hashmap.put(array2[0],array2[1]);
  }
  System.out.println(hashmap);
 }
}


9、请按下面内容编写一个页面,点页面里的“全选”时,能选中或清除上面的所有水果。
          选择你喜欢的水果:
□苹果 □桔子 □香蕉 □葡萄 □桃子 □全选

<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
<script   language="javascript">    
function   selectAll()      
{    
 for(var i=0;i<document.form1.nameid.length;i++)    
 {    
  var temp=document.form1.nameid[i];    
  temp.checked=true;    
 }    
}
function   deleteAll()      
{    
 for(var i=0;i<document.form1.nameid.length;i++)    
 {    
  var temp=document.form1.nameid[i];    
  temp.checked=false;    
 }    
}    
</script> 
</head>
<body>
<form name="form1" method="post" action="">  
      <input type="checkbox" name="nameid" />苹果</br>  
      <input type="checkbox" name="nameid" />桔子</br>  
      <input type="checkbox" name="nameid" />香蕉</br>  
      <input type="checkbox" name="nameid" />萄萄</br>  
      <input type="checkbox" name="nameid" />桃子</br>  
      <input type="button" name=selectButton value="全选" onClick="selectAll()" />  
   <input type="button" name=selectButton value="重置" onClick="deleteAll()" />
</form>
</body>

 

10、请登陆访问http://www.it315.org/bbs/页面,这个页面左侧导航栏部分可以收缩、显示,请参照此页面编写一个也能把导航栏收缩、显示的页面。

<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
<style>
dl {overflow:hidden; height:16px;}
.open { overflow:visible}
.close { overflow:hidden; height:16px;}
</style>
<script>
function oc(dt)
{
 var dl = dt.parentNode;
 var dlarr = document.getElementsByTagName("dl");
 for(var x=0; x<dlarr.length; x++)
 {
  if(dlarr[x].id == dl.id)
  {
   if("open"==dl.className)
   {
    dl.className = "close";
   }
   else
   {
    dl.className = "open";
   }
  }
  else
  {
   dlarr[x].className= "close";
  }
 }
}
</script>
</head>
<body>

<dl id="dl_1" >

 <dt οnclick="oc(this)">中国</dt>
 <dd>北京</dd>
 <dd>上海</dd>

</dl>

<dl id="dl_2" >

 <dt οnclick="oc(this)">美国</dt>
 <dd>纽约</dd>
 <dd>华盛顿</dd>
 
</dl>

</body>
</html>


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值