首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2SE开发 >

5个JAVA题,解决即给分!

2012-01-21 
高分求教5个JAVA题,解决即给分!!题1:编写一个程序,它先将键盘上输入的一个字符串转换成十进制整数,然后打

高分求教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 ");


}
return s2;
}
}

}

[解决办法]
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>

[解决办法]
第三题:

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不合法! ");
}
}

}

}
[解决办法]
第一题(只考虑正数)
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型数据表示范围! ");
}
}
}

[解决办法]
第五题:

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;
}

}
[解决办法]
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();
}

热点排行