题1: 
编写一个程序,它先将键盘上输入的一个字符串转换成十进制整数,然后打印出这个十进制整数对应的二进制形式。 
十进制数转二进制数的方式是用这个数除以2,余数就是二进制数的最低位,接着再用得到的商作为被除数去除以2,这次得到的余数就是次低位,如此循环,直到被除数为0为止。其实,只要明白了打印出一个十进制数的每一位的方式(不断除以10,得到的余数就分别是个位,十位,百位),就很容易理解十进制数转二进制数的这种方式。这个程序要考虑输入的字符串不能转换成一个十进制整数的情况,并对转换失败的原因要区分出是数字太大,还是其中包含有非数字字符的情况。 试题2: 
请用移位的方式打印出一个十进制整数的十六进制形式。提示:按每4个二进制位对整数进行移位和去高位处理,得到的结果就是十六进制数的一位,然后按下面三种方式之一(作为作业,要求每种方式都用到)计算出一个十六进制数值对应的十六进制形式: 
1)0-9之间的数值直接加上字符'0',9以上的数值减去10以后再加上字符'A' 
2)定义一个数组,其中包含0-F这些字符,然后用要计算的数值作为数组的索引号,即可获得其对应的十六进制数据。 
3)Character.forDigit静态方法可以将一个十六进制的数字转变成其对应的字符表示形式,例如,根据数值15返回字符'F'。 题3: 
请结合正则表达式与String.split方法,从"http://www.it315.org/get.jsp?user=zxx&pass=123"这样的URL地址中提取出每个参数的名称和值。这里要注意在正则表达式中要对?进行转义处理. 题4: 
编写一个程序,用于实现文件的备份,程序运行时的命令语法为: 
java MyCopy <sourcefile> <destfile> 题5: 
请编写一个字符输入流的包装类,通过这个包装类对底层字符输入流进行包装,让程序通过这个包装类读取某个文本文件(例如,一个java源文件)时,能够在读取的每行前面都加上有行号和冒号。

解决方案 »

  1.   

    自己作业不做,索性把题COPY过来了都~~
    你这样还不如做到哪里不懂拿出来问的好
      

  2.   

    我帮你做一题,怎么也得给我20分吧。第四题:package org.luyang.io;import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStreamReader;public class FileCopy {
        public boolean copyfile(String src, String dec) {
            File srcFile = new File(src);
            File decFile = new File(dec);
            try {            FileInputStream fis = new FileInputStream(srcFile);
                FileOutputStream ops = new FileOutputStream(decFile);
                int n;
                byte buff[] = new byte[1024 * 4];
                decFile.createNewFile();
                while ((n = fis.read(buff)) != -1) {
                    ops.write(buff, 0, n);            }
                ops.flush();
                fis.close();
                ops.close();        } catch (FileNotFoundException e) {
                System.out.println(e);
            } catch (IOException er) {
                System.out.println(er);
                return false;
            }
            return true;    }    public static void main(String[] args) {
            FileCopy cf = new FileCopy();
            FilePath path = cf.new FilePath();
            if (cf.copyfile(path.soureFilePath, path.destFilePath)) {
                System.out.println("success");
            } else {
                System.out.println("failed");
            }
        }    /**
         * filePath read
         * @author luyang
         *
         */
        class FilePath {
            String soureFilePath = null;
            String destFilePath = null;
            public FilePath() {
                String s = readInput();
                String[] arr = s.split("\\s+");
                if (null == arr || arr.length != 4) {
                    System.out.println("format error");
                    return;
                }
                soureFilePath = arr[2];
                destFilePath = arr[3];
            }        private String readInput() {
                String s2 = null;
                try {
                    BufferedReader in = null;
                    in = new BufferedReader(new InputStreamReader(System.in));
                    s2 = in.readLine();
                } catch (IOException ex) {
                    System.out.println("file can't read");
                }
                return s2;
            }
        }}
      

  3.   

    看在都是过来人的份上,我给你做一题简单的:
    题4:
       public static void main(String[] args) {
          System.out.println(copy("D:/Work/ProjectPlanReport[4].pdf", "D:/Work/ProjectPlanReport[4]2.pdf"));
       }
       
       public static boolean copy(String src, String des) {
          try {
             File fileSrc = new File(src);
             File fileDes = new File(des);
             FileInputStream fis = new FileInputStream(fileSrc);
             FileOutputStream fos = new FileOutputStream(fileDes);
             byte[] bytes = new byte[1024];
             int c;
             while((c = fis.read(bytes)) != -1) {
                fos.write(bytes, 0, c);
             }
             fis.close();
             fos.close();
             return true;
          } catch (Exception ex) {
             return false;
          }
       }
      

  4.   

    很遗憾的告诉luyang1016(闭月羞花猫),你这样做很不错,但有局限性,局限性在于你copy不了非txt文件。而我的尝试过可以copy一个dvd,mp3等等东西的。
      

  5.   

    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;public class MyCopy {
       public static void main(String[] args) {
          System.out.println(copy(args));
       }
       
       public static boolean copy(String[] args) {
          if (args.length != 2) {
             return false;
          }
          String src = args[0];
          String des = args[1];
          try {
             File fileSrc = new File(src);
             File fileDes = new File(des);
             FileInputStream fis = new FileInputStream(fileSrc);
             FileOutputStream fos = new FileOutputStream(fileDes);
             byte[] bytes = new byte[1024];
             int c;
             while((c = fis.read(bytes)) != -1) {
                fos.write(bytes, 0, c);
             }
             fis.close();
             fos.close();
             return true;
          } catch (Exception ex) {
             return false;
          }
       }
    }搂住运行下面语句即可:java MyCopy <sourcefile> <destfile>
      

  6.   

    第三题:import java.util.HashMap;
    import java.util.Iterator;
    import java.util.Map;public class URLSplit {    private Map paras;    public URLSplit() {
            paras = new HashMap();
        }    /**
         * @param args
         */
        public static void main(String[] args) {
            String url = "http://www.it315.org/get.jsp?user=zxx&pass=123&yy-ere=99";
            URLSplit us = new URLSplit();
            us.urlsplit(url);        Map result = us.paras;
            Iterator it = result.keySet().iterator();
            while (it.hasNext()) {
                Object obj = it.next();
                System.out.println(obj + ":   " + result.get(obj));
            }    }    public void urlsplit(String url) {        if (url != null) {
                String[] s1 = url.split("\\?");
                if (s1.length == 1) {
                    System.out.println("该Url不带参数!");
                } else if (s1.length == 2) {
                    this.paras.put("baseUrl", s1[0]);
                    String[] s2 = s1[1].split("\\&");                if (s2.length > 0) {                    for (int i = 0; i < s2.length; i++) {
                            String[] s3 = s2[i].split("=");
                            if (s3.length == 2) {
                                this.paras.put(s3[0], s3[1]);
                            }
                        }
                    }            } else {
                    System.out.println("该Url不合法!");
                }
            }    }}
      

  7.   

    非常感谢大家!这可不是我的作业!而是我准备参加个J2EE培训,他们要求做完这些题才有资格报名,我一个人实在不会,所有让大家帮忙把代码写出来!!然后自己再研究明白了就OK
        谢谢大家!谁把其他几题也做了啊!顺便标下题号!再谢!!
      

  8.   

    第一题(只考虑正数)
    import java.util.regex.*;public class Dec2Bin{
    public static void main(String[] args){
    if(args.length == 0){
    System.out.println("未输入需要转换的字符串!");
    return;
    }
    if(!Pattern.matches("\\d+", args[0])){
    System.out.println("参数中含有非法字符!");
    return;
    }
    try{
    int src = Integer.parseInt(args[0]);
    String result = "";//二进制结果
    while(src > 0){
    result = src % 2 + result;
    src /= 2;
    }
    System.out.println(result);
    }catch(NumberFormatException nfe){
    System.out.println("参数超出了int型数据表示范围!");
    }
    }
    }
      

  9.   

    谢谢大家!!还有2题,大家再帮忙啊!!另回 TONYBLARED(奔放的犀牛) 你给第2段代码中,
    运行不成功,运行命令如下 java MyCopy 111.txt 222.txt(在当前目录下存在这两文件,且111有内容,22为空) ,结果是FLASE 我在catch部分加了打印错误代码的部分,也把错误代码打出来了,还是 flase 用绝对路径运行时没错误代码,结果也是 flas ,等待帮忙中,谢谢!!!
      

  10.   

    第五题:import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.IOException;public class Test20070120 {    /**
         * @param args
         */
        public static void main(String[] args) {
            File file = new File("c:/SendmailAction.java");
            if (file.exists()) {
                FileReader fr;
                try {
                    fr = new FileReader(file);
                    BufferUnit br = new BufferUnit(fr);
                    String str = "";
                    while ((str = br.readLine()) != null)
                        System.out.println(str);
                } catch (FileNotFoundException e) {                e.printStackTrace();
                } catch (IOException e) {                e.printStackTrace();
                }        }
        }
    }
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.Reader;public class BufferUnit extends BufferedReader {
        
        
        private  int row;    public BufferUnit(Reader arg0) {
            
            super(arg0);
            this.row =1;    }
        
        public String readLine() throws IOException{
            String s=super.readLine();
            if(s!=null){
                s=this.row+": "+s;
                this.row++;
            }
            return s;
        }}
      

  11.   

    谢谢大家!还剩第二题,完即结帖,谁再帮忙啊?
        另回:TONYBLARED(奔放的犀牛)   你的代码在命令行下能用,用工具时候才出现我上面说的问题,
    可能是工具问题! SORRY!
      

  12.   

    public static String printIntegerInHex(int num){
      final char[] chars = new char[]{'0', '1', '2', '3',
                      '4', '5', '6', '7',
                      '8', '9', 'A', 'B',
                      'C', 'D', 'E', 'F'};  String result = "";
      
      // 如果是负数则要转换成正数
      if(num < 0){
        result += "-";
        
        num = 0 - num;
      }
      
      int filter = 15; // 15的最低四位都是1,呵呵,正好用!
      for(int i = 1; i <= 8; ++i){
        // 把要转换的那四位移到最低四位处,并&之!
        int b = (num << (i * 4)) & filter;
        
        // 分别用了三种方法来转换!
        switch(i){
        // 方法一
        case 1 :
          if(b <=9)
            result += (char)('0' + b);
          else
            result += 'A' + (b - 10); 
          break;
        // 方法二
        case 2 :
          result += chars[b];
          break;
        // 方法三
        default :
          result += Character.forDigit(b, 16);
        }
      }
      
      // 因为Character.forDigit()方法会返回小写字母,所以ToUpperCase一下!
      return result.toUpperCase();
    }
      

  13.   

    看来楼主对工具不太熟啊,如果想在工具里面用的话,要在命令行参数里加东西,不同的工具是不一样的。
    例如Eclipse中
    1)在左边的Package Exploror中选定MyCopy类
    2)菜单Run -> Run... -> (x=)Arguments -> 在Programm arguments中输入参数。
      

  14.   

    晕了,大家连这是哪的题都知道啊?
    我准备参加张孝祥的J2EE培训,这是他们的测试题!!都解决了,谢谢大家啊!
    马上给分!
      

  15.   

    回  eric1028  你的代码好象还有漏洞,当传递进去的参数小于16的时候一切正常,大于16的时候就出错了!
    比如  16——>00000000,17——>00000001,依次类推,我找不出问题在哪!还得麻烦你!谢谢!
      

  16.   

    这样就OK,前面代码问题的原因研究中……^_^ public static String printIntegerInHex(int num){
        final char[] chars = new char[]{'0', '1', '2', '3',
                        '4', '5', '6', '7',
                        '8', '9', 'A', 'B',
                        'C', 'D', 'E', 'F'};    String result = "";
        
        // 如果是负数则要转换成正数
        if(num < 0){
          result += "-";
          
          num = 0 - num;
        }
        
        int filter = 15; // 15的最低四位都是1,呵呵,正好用!
        for(int i = 7; i >= 0; --i){
          // 把要转换的那四位移到最低四位处,并&之!
          int b = (num >> (i * 4)) & filter;
          
          // 分别用了三种方法来转换!
          switch(i % 3){
          // 方法一
          case 1 :
            if(b <=9)
              result += (char)('0' + b);
            else
              result += 'A' + (b - 10); 
            break;
          // 方法二
          case 2 :
            result += chars[b];
            break;
          // 方法三
          default :
            result += Character.forDigit(b, 16);
          }
        }
        
        // 因为Character.forDigit()方法会返回小写字母,所以ToUpperCase一下!
        return result.toUpperCase();
      }