日期:2014-05-20  浏览次数:20741 次

高分求教5个JAVA题,解决即给分!!
题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源文件)时,能够在读取的每行前面都加上有行号和冒号。

------解决方案--------------------
我帮你做一题,怎么也得给我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 ");