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

12.1

加上清单7-9的代码,在以命令行方式验证即可。

import java.io.IOException;

public class dishierzhang {

public static void main(String[] args) throws IOException {
	// TODO Auto-generated method stub
	if (args.length != 3) {
		System.out.println("Wrong!");
		System.exit(1);
	}
	int result = 0;
	try {
		int i1 = Integer.parseInt(args[0]);
	} catch (NumberFormatException e) {
		// TODO: handle exception
		System.out.print("Wrong input: " + args[0]);
	}
	try {
		int i2 = Integer.parseInt(args[2]);
	} catch (NumberFormatException e) {
		// TODO: handle exception
		System.out.print("Wrong input: " + args[2]);
	}
}

}

12.2

import java.util.InputMismatchException;
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 two integers: ");
	boolean b1 = true;
	boolean b2 = true;
	int i1 = 0;
	int i2 = 0;
	
	while (b1) {
		try {
			i1 = input.nextInt();
			b1 = false;
		} catch (InputMismatchException e) {
			// TODO: handle exception
			input.nextLine();
			System.out.print("Wrong! Please re-enter: ");
		}
	}

	while (b2) {
		try {
			i2 = input.nextInt();
			b2 = false;
		} catch (InputMismatchException e) {
			// TODO: handle exception
			input.nextLine();
			System.out.print("Wrong! Please re-enter: ");
		}
	}

	int sum = i1 + i2;
	System.out.print("The sum is " + sum);
}

}

12.3

import java.util.Scanner;

public class dishierzhang {

public static void main(String[] args) {
	// TODO Auto-generated method stub
	Scanner input = new Scanner(System.in);
	int[] n = new int[100];
	for (int i = 0; i < n.length; i++)
		n[i] = (int) (Math.random() * 100);
	System.out.print("Enter your index: ");
	int index = input.nextInt();
	try {
		int n1 = n[index];
	} catch (ArrayIndexOutOfBoundsException e) {
		// TODO: handle exception
		System.out.println("Out of bounds!");
		System.exit(1);
	}
	System.out.println("The number of the index is " + n[index]);
}

}

12.4

import java.util.Date;
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 annual interest rate , for example,8.25: ");
	double annualInterestRate = input.nextDouble();
	try {
		if (annualInterestRate <= 0)
			throw new IllegalArgumentException();
	} catch (IllegalArgumentException e) {
		// TODO: handle exception
		System.out.println("Wrong!");
		System.exit(1);
	}

	System.out.print("Enter number of years as an integer: ");
	int numberOfYears = input.nextInt();
	try {
		if (numberOfYears <= 0)
			throw new IllegalArgumentException();
	} catch (IllegalArgumentException e) {
		// TODO: handle exception
		System.out.println("Wrong!");
		System.exit(1);
	}

	System.out.print("Enter loan amount, for example,120000.95: ");
	double loanAmount = input.nextDouble();
	try {
		if (loanAmount <= 0)
			throw new IllegalArgumentException();
	} catch (IllegalArgumentException e) {
		// TODO: handle exception
		System.out.println("Wrong!");
		System.exit(1);
	}

	Loan loan = new Loan(annualInterestRate, numberOfYears, loanAmount);

	System.out.printf("The loan was created on %s\nThe monthly payment is %.2f\nThe total payment is %.2f\n",
			loan.getLoanDate().toString(), loan.getMonthlyPayment(), loan.getTotalPayment());
}

}

class Loan {

private double annualInterestRate;
private int numberOfYears;
private double loanAmount;
private Date loanDate;

public Loan() {
	this(2.5, 1, 1000);
}

public Loan(double annualInterestRate, int numberOfYears, double loanAmount) {
	this.annualInterestRate = annualInterestRate;
	this.numberOfYears = numberOfYears;
	this.loanAmount = loanAmount;
	loanDate = new Date();
}

public double getAnnualInterestRate() {
	return annualInterestRate;
}

public void setAnnualInterestRate(double annualInterestRate) {
	this.annualInterestRate = annualInterestRate;
}

public int getNumberOfYears() {
	return numberOfYears;
}

public void setNumberOfYears(int numberOfYears) {
	this.numberOfYears = numberOfYears;
}

public double getLoanAmount() {
	return loanAmount;
}

public void setLoanAmount(double loanAmount) {
	this.loanAmount = loanAmount;
}

public double getMonthlyPayment() {
	double monthlyInterestRate = annualInterestRate / 1200;
	double monthlyPayment = loanAmount * monthlyInterestRate
			/ (1 - (1 / Math.pow(1 + monthlyInterestRate, numberOfYears * 12)));
	return monthlyPayment;
}

public double getTotalPayment() {
	double totalPayment = getMonthlyPayment() * numberOfYears * 12;
	return totalPayment;
}

public Date getLoanDate() {
	return loanDate;
}

}

12.5

public class dishierzhang {

public static void main(String[] args) {
	// TODO Auto-generated method stub
	try {
		Triangle t = new Triangle(2, 2, 5);
	} catch (IllegalTriangleException e) {
		// TODO: handle exception
		System.out.println("Wrong!");
		System.out.println(e);
		System.exit(1);;
	}
}

}

class IllegalTriangleException extends Exception{

private double side1;
private double side2;
private double side3;
public IllegalTriangleException(double s1,double s2,double s3){
	super("Invalid sides: "+s1+" "+s2+" "+s3);
	this.side1 = s1;
	this.side2 = s2;
	this.side3 = s3;
}

}

class Triangle{

private double side1;
private double side2;
private double side3;
public Triangle(double s1,double s2,double s3) throws IllegalTriangleException{
	if(s1<s2+s3 && s2<s1+s3 && s3<s1+s2){
		this.side1 = s1;
		this.side2 = s2;
		this.side3 = s3;
	}else
		throw new IllegalTriangleException(s1,s2,s3);
}

}

12.6

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 hex-string: ");
	String s = input.next();
	try {
		int i = hexToDecimal(s);
		System.out.print(i);
	} catch (NumberFormatException e) {
		System.out.print(e);
		System.exit(1);
	}
}

public static int hexToDecimal(String hexString) throws NumberFormatException {
	int value = 0;
	hexString = hexString.toUpperCase();
	for (int i = 0; i < hexString.length(); i++) {
		char hexChar = hexString.charAt(i);
		if ((hexChar >= 'A' && hexChar <= 'F') || (hexChar >= '0' && hexChar <= '9')) {
			value = value * 16 + hexCharToDecimal(hexChar);
		} else {
			throw new NumberFormatException("Wrong! Not a hexstring.");
		}
	}
	return value;
}

public static int hexCharToDecimal(char ch) {
	if (ch >= 'A' && ch <= 'F')
		return 10 + ch - 'A';
	else
		return ch - '0';
}

}

12.7

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 number: ");
	String s = input.next();
	try {
    		Integer.parseInt(s, 2);
	} catch (NumberFormatException e) {
    	// TODO: handle exception
    	System.out.println("Wrong! not binary number.");
    	System.exit(1);
	}
	System.out.println("Decimal number: " + bin2Dec(s));
}
public static int bin2Dec(String binaryString) {
	return Integer.parseInt(binaryString, 2);
}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

xupengboo

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

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

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

打赏作者

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

抵扣说明:

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

余额充值