笔记《JAVA语言程序设计与数据结构(基础篇)》部分习题解答


本书课后习题多而精,故选取有代表意义的习题,由此记录每道题学到的知识。纯新手向,从中学到的知识有简单有新奇。

第十三章 抽象类和接口

*13.17

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个#)

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();
            }
        }
    }
}

笔记:可在try…catch…中套一个while(true){},这样当出现异常后(本题的InputMismatchException)可以再次输入,避免只允许一次的情况。


 上一篇
java环境配置以及软件下载(eclipse或idea)详细教程 java环境配置以及软件下载(eclipse或idea)详细教程
本文从jdk下载安装到软件正式使用,都有详细图文教程,适合新手观看。若有疑问可在下方评论留言或QQ:1073015375联系~ 前言本教程基于Windows平台,MacOs平台自带环境。 jdk下载安装点击这里jdk官网 jdk
2019-09-06
下一篇 
Hexo-NexT安装和优化教程(持续更新~) Hexo-NexT安装和优化教程(持续更新~)
本教程参考了NexT官方中文文档以及收集了一些他人博客的内容,并加以整合。在文章下将依次标明地址。 前排提醒 为了主题设置更加简便,推荐使用VS Code进行主题代码的更新 同时 Mac 终端输入sudo su进入root用户, Wi
2019-08-09
  目录