我是靠谱客的博主 壮观热狗,最近开发中收集的这篇文章主要介绍Java语言程序设计与数据结构(基础篇)课后练习题 第十二章 (一)12.112.212.312.412.512.612.7,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

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 %snThe monthly payment is %.2fnThe total payment is %.2fn",
			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);
}

}

最后

以上就是壮观热狗为你收集整理的Java语言程序设计与数据结构(基础篇)课后练习题 第十二章 (一)12.112.212.312.412.512.612.7的全部内容,希望文章能够帮你解决Java语言程序设计与数据结构(基础篇)课后练习题 第十二章 (一)12.112.212.312.412.512.612.7所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(53)

评论列表共有 0 条评论

立即
投稿
返回
顶部