作业是这样子的。
write two classes, DataEncoder and DataDecoder.Class DataEncoder should contain the following method:   String stringToBits (String input)Which takes a string and converts it to a string where each character is represented by a binary number, expressed
as a string of '1' and '0' characters.methods:
   int String.codePointAt (int)
   String Interger.toBinaryString (int)
   String String.substring (int)
Remember to pad the output of each character to the number of bits you select. For example, the code of '+' is 101011
, which is only six binary digit. In order to fit in an 8-bit frame, you need to add two zero in the beginning:001010001.Include a main program so that you can test the coversion from the commandline. For example(using 8 bits):
>java DataEncoder Java can be fun
Java : 01001010011000010111011001100001
can : 011000110110000101101110
be : 0110001001100101
fun : 011001100111010101101110Class DataDecoder should contain the following method:
String bitsToString (String input, int bits)Which takes a binary string as generated by
DataEncoder.StringToBits(), and recreats the original string. The bits parameter says how many bits there are in 
each character. Return the string.method: 
    Integer.parseInt(...)
    StringBuffer.appendCodePoint(...)Include a main program so that you can test the conversion from the commandline. For example (using 8 bits):
>java DataDecoder 01001010011000010111011001100001
01001010011000010111011001100001 : JavaFor B and A grades, meeet the requirements for grade C and :Let DataEncoder detect before encoding, if any character in the encoded string needs 16 or 24 bits. Use the first
two bits in the string to encode this:00 =  8 bits
01 = 16 bits
10 = 24 bits
11 = 32 bitsThen use that width for all characters in that string.methods:
    int Character.charCount (int codePoint)Let DataDecoder parse the first two bits of the input string and select the number of bits per character accordingly.一定要用给出的method
求助大侠帮帮忙。我没多少分数。就一点全给了。

解决方案 »

  1.   

    呆着无聊,先试着翻译一下(也为楼下的创造点儿方便):
    写两个类,DataEncoder 和 DataDecoder
    类DataEncoder要包含如下方法: 
    String stringToBits (String input) 
    该方法将一个字符串的每一位字符转换成另外一个用'1'和'0'表示的字符串(这句不知翻得准不准,总之差不多,--译者)提供给你可使用的方法
      int String.codePointAt (int) 
      String Interger.toBinaryString (int) 
      String String.substring (int) 
    确保用来表示每一位字符的数字位数是一定的,如果位数不足,前边补0. 如采用每8位数字表示一位字符串的规则,当数字'101011'表示符号'+'时,你就需要在数字前边补两个零,00101011(此处原文应该错了吧,--译者注)还需要一个main方法用来测试你的转换,要求在命令行下运行(我讨厌命令行,--译者注), 运行示例如下:
    >java DataEncoder Java can be fun         
    Java : 01001010011000010111011001100001 
    can : 011000110110000101101110 
    be : 0110001001100101 
    fun : 011001100111010101101110 
    类DataDecoder要求包括如下方法: 
    String bitsToString (String input, int bits) 
    该方法将一个由DataEncoder.StringToBits()生成的二进制字符串,重新转换为原始字符串(我好像明白了,就是刚才那个方法的逆过程, --译者注),第二个参数bits表示每位原始字符串被表示成几位固定的二进制数。(我承认这句翻得烂。--译者注),该方法返回的是原始字符串。(这简直一定了。)
    提供给你可使用的方法:
        Integer.parseInt(...) 
        StringBuffer.appendCodePoint(...) 也要包含一个main方法来测试转换,在命令行执行。运行示例如下(参数bits值为8):
    >java DataDecoder 01001010011000010111011001100001 
    01001010011000010111011001100001 : Java 以上为基本要求,要想得高分,还要满足下面的要求(英文题目也讲这套,可恶!--译者有点儿飙了)要让 DataEncoder 的方法,满足这样的要求:根据转换方式的不同,转换后的数字的前两位要满足如下规则:
    00 =  8 bits(即每位字符被转为8位二进制数的情况) 
    01 = 16 bits (即每位字符被转为16位二进制数的情况) 
    10 = 24 bits (即每位字符被转为24位二进制数的情况) 
    11 = 32 bits (即每位字符被转为32位二进制数的情况) 既然规则确定了,那就做吧!!(是这意思吧?--译者)提供给你可使用的方法: 
        int Character.charCount (int codePoint) 要让DataDecoder对前两位数字进行解析,然后选择到底按几位数字表示一个字符的方式还原字符串。---楼主,看在我翻译这么辛苦的份上,多给分啊!!!
    开个玩笑,其实我觉得,题目挺不错的,很有针对性,也很有可做性。建议楼主自己尝试动手做一下,做的时候参考API文档(很重要,即便是高手,遇到一个以前没见过的方法时,也通常会这么做),了解题目中提供给你的那些方法是怎么用的,自己动手才真正有收获。别上来就想着要答案,虽然看别人的代码也是一种学习,终不如自己做来得快。祝好运了!
      

  2.   


    public class DataEncoder {    public DataEncoder() {
        }    public static String stringToBits(String input) {
            String output = "";
            String[] inputTemp = input.split(" ");
            for(int i = 0;i<inputTemp.length;i++) {
                if(inputTemp[i].trim().equals("")) continue;
                else {
                    String word = inputTemp[i];
                    output += word+": ";
                    for(int j = 0; j<word.length();j++) {
                        String code = Integer.toBinaryString(word.codePointAt(j));
                        code = FormatString(code);
                        output += code;
                    }
                    output += "\n";
                }
            } 
            return output;
        }
        private static String FormatString(String input) {
            String output = String.format("%08d", Integer.parseInt(input));
            return output;
        }    public static void main(String args[]) {
            String input = "";
            for(int i = 0;i<args.length;i++) {
                input += args[i]+" ";
            }
            System.out.println(DataEncoder.stringToBits(input));
        }
    }到Class DataDecoder should contain the following method:前
      

  3.   


    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package datacoder;/**
     *
     * @author siamca
     */
    public class DataEncoder {    public DataEncoder() {
        }    public static String stringToBits(String input) {
            String output = "";
            String[] inputTemp = input.split(" ");
            for(int i = 0;i<inputTemp.length;i++) {
                if(inputTemp[i].trim().equals("")) continue;
                else {
                    String word = inputTemp[i];
                    output += word+": ";
                    for(int j = 0; j<word.length();j++) {
                        String code = Integer.toBinaryString(word.codePointAt(j));
                        code = FormatString(code,8);
                        output += code;
                    }
                    output += "\n";
                }
            } 
            return output;
        }
        private static String FormatString(String input,int bit) {
            String output = String.format("%0"+bit+"d", Integer.parseInt(input)); //或者length差进行补0,嘛,那个有点长,懒得写了
            return output;
        }    public static void main(String args[]) {
            String input = "";
            for(int i = 0;i<args.length;i++) {
                input += args[i]+" ";
            }
            System.out.println(DataEncoder.stringToBits(input));
        }
    }
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package datacoder;/**
     *
     * @author siamca
     */
    public class DataDecoder {    DataDecoder() {
        }    public static String bitsToString(String input, int bits) {
            StringBuffer sb = new StringBuffer();
            int[] inputTemp = new int[input.length() / bits];
            for (int i = 0; i < inputTemp.length; i++) {
                int cCodePoint = Integer.parseInt(input.substring(i * bits, (i + 1) * bits), 2);
                if (Character.charCount(cCodePoint) == 2) {
                    sb.append("[character out of unicode]");
                } else {
                    sb.appendCodePoint(cCodePoint);
                }
            }
            return sb.toString();
        }    public static void main(String args[]) {
            try {
                String input = args[0];
                int bit = -1;
                if (input.startsWith("00")) {
                    bit = 8;
                } else if (input.startsWith("01")) {
                    bit = 16;
                } else if (input.startsWith("10")) {
                    bit = 24;
                } else if (input.startsWith("11")) {
                    bit = 32;
                }
                input = input.substring(2);
                System.out.println(DataDecoder.bitsToString(input, bit));        } catch (Exception ex) {
                System.out.println("error!");
            }
        }
    }
    总算找了个地方把Character.charCount安进去...
    就这样吧,功能是有滴,位数检查啊,增补字符检查啊啥滴,就没细做咯~~~
    楼主这个题很不错的,一趟下来巩固了好多基础知识,虽然贴给你代码了说这个有点小虚伪,但还是建议你自己做一做
      

  4.   

    For B and A grades, meeet the requirements for grade C and :Let DataEncoder detect before encoding, if any character in the encoded string needs 16 or 24 bits.我觉得这里有点表述不清,哪个字符(any character)会超过16或24bits呢,它又没提什么编码,难道是汉字之类的utf8吗 那也没必要再在前放个宽度表示,题目哪里的也没查到