本书课后习题多而精,故选取有代表意义的习题,由此记录每道题学到的知识。纯新手向,从中学到的知识有简单有新奇。
第十三章 抽象类和接口 *13.17 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 import java.util.Scanner; /** * \* Program: Complex class *13.17 * \* User: breidincasimi * \* Date: 2019-08-14 * \* Time: 09:45 * \* Description: 复数的四则运算和绝对值 */ public class Complex { public double a; public double b; public Complex() { } public Complex(double a, double b) { this.a = a; this.b = b; } public Complex(double a) { this.a = a; } // 返回复数的实部 public double getRealPart() { return this.a; } // 返回复数的虚部 public double getImaginaryPart() { return this.b; } //2个复数的相 加 public void add(Complex other) { double real = this.a + other.a; double imaginary = this.b + other.b; System.out.print("("+this.a+" + "+this.b+") + ("+other.a+" + "+other.b+") = "); System.out.println(real + " + " + imaginary + "i"); } //2个复数的相 减 public void subtract(Complex other) { double real = this.a - other.a; double imaginary = this.b - other.b; System.out.print("("+this.a+" + "+this.b+") - ("+other.a+" + "+other.b+") = "); System.out.println(real + " + " + imaginary + "i"); } //2个复数的相 乘 public void multiply(Complex other) { double real = (this.a * other.a) - (this.b * other.b); double imaginary = (this.b * other.a) + (this.a * other.b); System.out.print("("+this.a+" + "+this.b+") * ("+other.a+" + "+other.b+") = "); System.out.println(real + " + " + imaginary + "i"); } //2个复数的相 除 public void divide(Complex other) { double fm = (other.a * other.a) + (other.b * other.b); double real = ((this.a * other.a) + (this.b * other.b)) / fm; double imaginary = ((this.b * other.a) - (this.a * other.b)) / fm; System.out.print("("+this.a+" + "+this.b+") / ("+other.a+" + "+other.b+") = "); System.out.println(real + " + " + imaginary + "i"); } //返回复数的绝对值 public static void abs(Complex other) { double fz = (other.a * other.a) + (other.b * other.b); System.out.print("|("+other.a+" + "+other.b+")| = "); System.out.println(Math.sqrt(fz)); } public static void main(String[] args) { Scanner input = new Scanner(System.in); //分别输入两个复数 System.out.print("Enter the first complex number: "); Complex p1 = new Complex(input.nextDouble(),input.nextDouble()); System.out.print("Enter the second complex number: "); Complex p2 = new Complex(input.nextDouble(),input.nextDouble()); //调用方法 p1.add(p2); p1.subtract(p2); p1.multiply(p2); p1.divide(p2); abs(p1); } }
笔记 :由相加的方法(public void add(Complex other))可以看出,可以传入Complex(本类)类的参数。由此可通过this.和参数.来比较。即为(Complex other),而非开始想到的这种(double c,double d,然后通过this.a和c比较,这种太麻烦了
第十二章 异常处理和文本I/O (3个#) *12.2 (4个#) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 import java.util.Scanner; /** * \* Program: InputMismatchException异常 *12.2 * \* User: casimibreidin * \* Date: 2019-08-15 * \* Time: 10:08 * \* Description: 编写一个程序,提示用户读取两个整数,然后显示它们的和。程序应该在输入不正确时 * 提醒用户再次读取整数。 */ public class InputMismatchException { public static void main(String[] args) { Scanner input = new Scanner(System.in); boolean hh = true; while (hh) { try { System.out.println("Enter two of number : "); int a = input.nextInt(); int b = input.nextInt(); int c = a + b; System.out.println("sum : " + c); hh = false; } catch (java.util.InputMismatchException e) { //异常和Class重名了,故用此写法 System.out.println("InputMismatchException!"); input.nextLine(); } } } }