Java语言程序设计与数据结构(基础篇)课后练习题 第十二章(二)

12.8

本來想用上一題的老办法,但是这个题要自定义异常,没法用。。。

import java.util.Scanner;

public class dishierzhang {

public static void main(String[] args) {
	// TODO Auto-generated method stub
	Scanner input = new Scanner(System.in);
	try {
		System.out.print("Enter a hex string: ");
		HexToDecUseHexFormatException hex = new HexToDecUseHexFormatException(input.next());
		System.out.println("The decimal value for hex number " + hex.getHexString() + " is " + hex.hex2Dec());
	} catch (HexFormatException ex) {
		System.out.println(ex.getMessage());
	}

}

}

class HexFormatException extends Exception {

String hexString;
public HexFormatException(String hexString) {
	super("HexFormatException: " + hexString);
	this.hexString = hexString;
}

public String getHexString() {
	return hexString;
}

}

class HexToDecUseHexFormatException {

String hexString;
public HexToDecUseHexFormatException(String hexString) throws HexFormatException {
	setHexString(hexString);
}

public void setHexString(String hexString) throws HexFormatException {
	for (int i = 0; i < hexString.length(); i++) {
		if (judge(hexString.charAt(i)) == -1) {
			throw new HexFormatException(hexString + " is not a hex string");
		}
	}

	this.hexString = hexString;
}

public String getHexString() {
	return hexString;
}

private int judge(char ch) {
	if ((ch >= '0') && (ch <= '9')) {
		return (ch - '0');
	} else if ((ch >= 'A') && (ch <= 'F')) {
		return (ch - 'A' + 10);
	} else
		return -1;
}

public int hex2Dec() {
	int result = 0;
	for (int i = hexString.length() - 1; i >= 0; i--) {
		result += judge(hexString.charAt(i)) * (Math.pow(16, (hexString.length() - i - 1)));
	}
	return result;
}

}

12.9

import java.util.Scanner;

public class dishierzhang {

public static void main(String[] args) {
	// TODO Auto-generated method stub
	Scanner input = new Scanner(System.in);
	System.out.print("Enter a binary string: ");
	String s = input.next();
	try {
		int i = bin2Dec(s);
		System.out.print(i);
	} catch (NumberFormatException e) {
		System.out.print("Wrong!\n" + e);
		System.exit(1);
	}
}

public static int bin2Dec(String hex) throws BinaryFormatException {
	int value = 0;
	for (int i = 0; i < hex.length(); i++) {
		char hexChar = hex.charAt(i);
		if (hexChar == '0' || hexChar == '1') {
			value = value * 2 + binChar2Dec(hexChar);
		} else {
			throw new NumberFormatException("Not a binstring!");
		}
	}
	return value;
}

public static int binChar2Dec(char ch) {
	return ch - '0';
}

}

class BinaryFormatException extends NumberFormatException {

public BinaryFormatException(String s) {
	super(s);
}

}

12.10

运行时间很长!!!!!!!,等结果的话要耐心点。。。

import java.util.ArrayList;

public class dishierzhang {

public static void main(String[] args) {
	// TODO Auto-generated method stub
	ArrayList<Double> list = new ArrayList<Double>();
    double i = 123456;
    while(true){
        i++;
        try{
            list.add(i);
		} catch (OutOfMemoryError e) {
			// TODO: handle exception
			System.out.print("Wrong!\n" + e);
			System.exit(1);
		}
	}
}

}

12.11

import java.io.File;
import java.io.PrintWriter;
import java.util.Scanner;

public class dishierzhang {

public static void main(String[] args) throws Exception {
	// TODO Auto-generated method stub
	if (args.length != 2) {
		System.out.println("Wrong!");
		System.exit(1);
	}

	File file = new File(args[1]);
	StringBuffer str = new StringBuffer();
	if (!file.exists()) {
		System.out.println("Wrong! file not exist!");
		System.exit(1);
	}
	try (Scanner input = new Scanner(file);) {
		while (input.hasNext()) {
			String str1 = input.nextLine();
			str.append(str1.replaceAll(args[0], "") + "\n");
		}
	}
	file.delete();
	File file1 = new File(args[1]);
	try (PrintWriter output = new PrintWriter(file1);) {
		output.print(str);

	}
}

}

12.12

还是喜欢ctrl+shift+F。

import java.io.File;
import java.io.PrintWriter;
import java.util.Scanner;

public class dishierzhang {

public static void main(String[] args) throws Exception {
	// TODO Auto-generated method stub
	if (args.length != 1) {
		System.out.println("Wrong!");
		System.exit(1);
	}

	File sourceFile = new File(args[0]);
	if (!sourceFile.exists()) {
		System.out.println("Source file " + args[0] + " not exist");
		System.exit(2);
	}

	StringBuilder b = new StringBuilder();
	Scanner input = new Scanner(sourceFile);

	while (input.hasNext()) {
		String str = input.nextLine();
		String str1 = str.trim();
		if (str1.length() > 0 && str1.charAt(0) == '{') {
			b.append(" {");
			if (str1.length() > 1)
				b.append("\r\n" + str.replace('{', ' '));
		} else
			b.append("\r\n" + str);
	}

	input.close();

	PrintWriter output = new PrintWriter(sourceFile);
	output.print(b.toString());
	output.close();
}

}

12.13

import java.io.File;
import java.util.Scanner;

public class dishierzhang {

public static void main(String[] args) throws Exception {

	if (args.length != 1) {
		System.out.println("Wrong!");
		System.exit(1);
	}

	File file = new File(args[0]);
	String name = file.getName();
	System.out.println("File " + name + " has\n");
	if (!file.exists()) {
		System.out.println("Not Exist.");
		System.exit(2);
	}

	try (Scanner input = new Scanner(file)) {
		int countOfCharacters = 0;
		int countOfLine = 0;
		int countOfWords = 0;

		while (input.hasNext()) {
			String line = input.nextLine();
			countOfCharacters += line.length();
			countOfLine++;
			countOfWords += countWords(line);
		}

		System.out.println(countOfCharacters + " characters\n");
		System.out.println(countOfLine + " lines\n");
		System.out.println(countOfWords + "words");

	}

	catch (Exception ex) {
		ex.printStackTrace();
	}

}

private static int countWords(String line) {
	String[] words = line.split("[^\\w\\d]+");
	return words.length;
}

}

12.14

import java.io.File;
import java.util.Scanner;

public class dishierzhang {

public static void main(String[] args) throws Exception {
	Scanner input1 = new Scanner(System.in);
	System.out.print("Enter a name of file: ");
	String strName = input1.nextLine();
	File file = new File(strName);
	if (!file.exists()) {
		System.out.println("Not Exists!");
		System.exit(1);
	}
	double sum = 0;
	int count = 0;
	Scanner input2 = new Scanner(file);
	while (input2.hasNext()) {
		sum += input2.nextDouble();
		input2.skip(" ");
		count++;
	}
	input1.close();
	input2.close();
	System.out.println("The sum is " + sum);
	System.out.println("Average is " + (sum / count));
}

}

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

xupengboo

你的鼓励将是我创作最大的动力。

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值