博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
《Java语言程序设计与数据结构》编程练习答案(第十二章)
阅读量:4169 次
发布时间:2019-05-26

本文共 6061 字,大约阅读时间需要 20 分钟。

《Java语言程序设计与数据结构》编程练习答案(第十二章)

英文名:Introduction to Java Programming and Data Structures, Comprehensive Version, 11th Edition

12.1

public class book {
public static void main(String[] args) {
if(args.length!=3){
System.out.println("fuck you~~"); System.exit(1); } int result = 0; try{
int opt1 = Integer.parseInt(args[0]); } catch (NumberFormatException ex) {
System.out.print("Wrong input: "+args[0]); } try{
int opt2 = Integer.parseInt(args[2]); } catch (NumberFormatException ex) {
System.out.print("Wrong input: "+args[2]); } }}

12.2

import java.util.*;public class book {
public static void main(String[] args) {
Scanner input = new Scanner(System.in); System.out.print("Enter two integers: "); boolean in1 = true; boolean in2 = true; int jojo = 0; int dio = 0; while(in1){
try{
jojo=input.nextInt(); in1 = false; } catch (InputMismatchException jj){
input.nextLine(); System.out.print("Please enter an integer; "); } } while(in2){
try{
dio=input.nextInt(); in2 = false; } catch (InputMismatchException jj){
input.nextLine(); System.out.print("Please enter an integer; "); } } int sum=jojo+dio; System.out.print("The sum is "+sum); }}

12.3

import java.util.*;public class book {
public static void main(String[] args) {
Scanner input = new Scanner(System.in); int[] ass = new int[100]; for(int i=0;i<100;i++) ass[i]=(int)(Math.random()*100); System.out.print("Enter your index: "); int index = input.nextInt(); try{
int jj = ass[index]; } catch (ArrayIndexOutOfBoundsException k){
System.out.println("Out of Bounds!"); System.exit(1); } System.out.println("The number of the index is "+ass[index]); }}

12.4

👨

12.5

public class book {
public static void main(String[] args) {
try{
Triangle ass = new Triangle(1,1,4); }catch(IllegalTriangleException jj){
System.out.println("You smart ass!"); System.out.println(jj); System.exit(1); } }}class IllegalTriangleException extends Exception{
private double s1; private double s2; private double s3; public IllegalTriangleException(double aa, double bb, double cc){
super("Invalid sides "+aa+" "+bb+" "+cc); s1=aa; s2=bb; s3=cc; }}class Triangle{
private double s1; private double s2; private double s3; public Triangle(double aa,double bb,double cc) throws IllegalTriangleException {
if(aa

12.6

import java.util.*;public class book {
public static void main(String[] args) {
Scanner input = new Scanner(System.in); System.out.print("Enter a hex-string: "); String jojo = input.next(); try{
int dio = hexToDecimal(jojo); System.out.print(dio); }catch (NumberFormatException starplatium){
System.out.print(starplatium); System.exit(1); } } public static int hexToDecimal (String hex) throws NumberFormatException{
int decimalValue = 0; hex=hex.toUpperCase(); for(int i=0;i
='A'&&hexChar<='F')||(hexChar>='0'&&hexChar<='9')){
decimalValue=decimalValue*16+hexCharToDecimal(hexChar); } else{
throw new NumberFormatException("Not a hexstring!"); } } return decimalValue; } public static int hexCharToDecimal(char ch){
if(ch>='A'&&ch<='F') return 10+ch-'A'; else return ch-'0'; }}

12.9

import java.util.*;public class book {
public static void main(String[] args) {
Scanner input = new Scanner(System.in); System.out.print("Enter a bin-string: "); String jojo = input.next(); try{
int dio = binToDecimal(jojo); System.out.print(dio); }catch (NumberFormatException starplatium){
System.out.print(starplatium); System.exit(1); } } public static int binToDecimal (String hex) throws BinaryFormatException{
int decimalValue = 0; for(int i=0;i

12.10

import java.util.*;public class book {
public static void main(String[] args) {
ArrayList
h = new ArrayList
(); double i = 114514; while(true){
i++; try{
h.add(i); } catch(OutOfMemoryError killerQueen){
System.out.print("Ohhhhhh!"); System.exit(1); } } }}

12.11

import java.util.*;import java.io.*;public class book {
public static void main(String[] args) throws Exception {
if(args.length!=2){
System.out.println("Usage: damn fool!"); System.exit(1); } File src = new File(args[1]); StringBuilder tmp= new StringBuilder(); if(!src.exists()){
System.out.println("src does not exist!"); System.exit(2); } try(Scanner input = new Scanner(src);) {
while(input.hasNext()){
String jj = input.nextLine(); tmp.append(jj.replaceAll(args[0], "")+'\n'); } } src.delete(); File dst = new File(args[1]); try(PrintWriter output = new PrintWriter(dst);) {
output.print(tmp); } }}

考虑到时间,直接跳转到17章

转载地址:http://vuwai.baihongyu.com/

你可能感兴趣的文章
程序员高效出书避坑和实践指南
查看>>
计算机方面毕业生怎样写简历
查看>>
从软件公司的异同点讲起,聊聊未来的程序员该如何选公司和谋规划
查看>>
我不想安于当前的限度,以达到所谓的幸福,回顾下2020年的我
查看>>
如何在面试中介绍自己的项目经验(面向java改进版)
查看>>
通过写n本书的积累,我似乎找到了写好技术文章的方法(回复送我写的python股票电子书)
查看>>
如果很好说出finalize用法,面试官会认为你很资深
查看>>
Java面试官经验谈:如何甄别候选人真实的能力,候选人如何展示值钱技能
查看>>
分析若干没面试机会和没体现实力的简历
查看>>
用python的matplotlib和numpy库绘制股票K线均线
查看>>
以互联网公司的经验告诉大家,架构师究竟比高级开发厉害在哪?
查看>>
GanttProject 使用的控件第三方包:jdnc-modifBen.jar
查看>>
ps、grep和kill联合使用杀掉进程
查看>>
openfire中的mina框架使用
查看>>
去掉Windows Messager的自动登录
查看>>
dspace可以检索中文了
查看>>
利用Eclipse编辑中文资源,配置文件
查看>>
将中文转为unicode 及转回中文函数
查看>>
《程序员》专访金蝶:是谁不相信国产软件?
查看>>
debian的gnome下的xmms乱码解决方案
查看>>