恒定橡果的java习题五

1.自定义常用数学类

【问题描述】定义一个实现常用数学运算的类MyMath,类中提供max()、min()、sum()与average()四个静态方法,每个方法带有三个整形参数,分别实现对三个整数求取最大值、最小值、和值及平均值的运算。在主类中对任意输入的三个整数,调用MyMath类的四种静态方法,求取结果并输出。【输入形式】
【输出形式】三个数的最大值, 最小值,和以及平均值(保留1位小数)

【样例输入】

7 8 9

【样例输出】

max=9
min=7
sum=24
avg=8.0

【样例说明】输入三个整数

public class main {
    public static void main(String[] args) {
        Scanner shu =new  Scanner(System.in);
        int x = shu.nextInt();
        int y = shu.nextInt();
        int z = shu.nextInt();

        int maxshu = MyMath.max(x, y, z);//调用求最大值
        int minshu = MyMath.min(x, y, z);//调用求最小值
        int sumshu = MyMath.sum(x, y, z);//调用求合
        double aveshu = MyMath.average(x, y, z);//调用取平均值

        System.out.println("max=" + maxshu);
        System.out.println("min=" + minshu);
        System.out.println("sum=" + sumshu);
        System.out.printf("avg=%.1f",aveshu);

    }
}
class MyMath {//设置MyMath类
    public static int max(int a, int b, int c) {

        return Math.max(Math.max(a, b), c);
    }//定义取最大值的方法

    public static int min(int a, int b, int c) {
        return Math.min(Math.min(a, b), c);
    }//定义取最小值的方法

    public static int sum(int a, int b, int c) {

        return a + b + c;
    }//定义取合的方法

    public static double average(int a, int b, int c) {

        return (double) sum(a, b, c) / 3;
    }//定义取平均值的方法
}

2.复数封装类的实现

【问题描述】实现复数的封装类,要求封装类能实现加减乘除四则运算,并用测试实例测试封装类的功能,要求复数类重写toString()方法。
【输入形式】输入两个复数,实部先输入,接下来是虚部(可以接受小数)
【输出形式】输出两个复数的加减乘除的运算结果
【样例输入】

1 2

3 4
【样例输出】

Add:4.0+6.0i
Sub:-2.0-2.0i
Multi:-5.0+10.0i
Div:0.44+0.08i

public static void main(String[] args) {
        Scanner shu=new Scanner(System.in);
        double q= shu.nextDouble();
        double w= shu.nextDouble();
        double e= shu.nextDouble();
        double r= shu.nextDouble();
        ComplexNumber f1 = new ComplexNumber(q, w);
        ComplexNumber f2 = new ComplexNumber(e, r);

        ComplexNumber add1 = f1.add(f2);
        System.out.println("Add:" + add1);

        ComplexNumber sub1 = f1.subtract(f2);
        System.out.println("Sub:" + sub1);

        ComplexNumber mul1 = f1.multiply(f2);
        System.out.println("Multi:" + mul1);

        ComplexNumber div1 = f1.divide(f2);
        System.out.println("Div:" + div1);
    }
    static class ComplexNumber {
        private final double real;
        private final double imaginary;

        public ComplexNumber(double real, double imaginary) {
            this.real = real;
            this.imaginary = imaginary;
        }

        public ComplexNumber add(ComplexNumber oth) {
            double realPart = this.real + oth.real;
            double imaginaryPart = this.imaginary + oth.imaginary;
            return new ComplexNumber(realPart, imaginaryPart);
        }

        public ComplexNumber subtract(ComplexNumber oth) {
            double realPart = this.real - oth.real;
            double imaginaryPart = this.imaginary - oth.imaginary;
            return new ComplexNumber(realPart, imaginaryPart);
        }

        public ComplexNumber multiply(ComplexNumber oth) {
            double realPart = this.real * oth.real - this.imaginary * oth.imaginary;
            double imaginaryPart = this.real * oth.imaginary + this.imaginary * oth.real;
            return new ComplexNumber(realPart, imaginaryPart);
        }

        public ComplexNumber divide(ComplexNumber oth) {
            double denominator = oth.real * oth.real + oth.imaginary * oth.imaginary;
            double realPart = (this.real * oth.real + this.imaginary * oth.imaginary) / denominator;
            double imaginaryPart = (this.imaginary * oth.real - this.real * oth.imaginary) / denominator;
            return new ComplexNumber(realPart, imaginaryPart);
        }

        @Override
        public String toString() {
            if (imaginary >= 0) {
                return real + "+" + imaginary + "i";
            } else {
                return real + "" + imaginary + "i";
            }
        }
    }

方法类似于第一题,其中方法要考虑复数的情况

3.计算一元二次方程的实根

【问题描述】编写程序用于计算一元二次方程的实根。要求定义Equation类以表示一元二次方程, 包括:方程系数、求解方法、实根的个数与类型、每个实根的值、输出方法。再定义 EquationDemo类演示求根过程。

(1)Equation类有成员变量 a, b, c,代表一元二次方程的三个系数。rootType 代表实 根的个数与类型:0表示无实根;1表示有两个相等实根;2表示有两个实根。root1, root2 代表两个可能的实根。
(2)Equation类有构造方法Equation (float a , float b , float c ) ,形参 a, b, c为给定一元 二次方程的系数。
(3)Equation类有成员方法void Solving ( ) 求解一元二次方程,并将结果置于相应的属 性之中。

【输入形式】

输入方程的三个系数a,b,c
【输出形式】

输出方程的根的个数以及方程的根(精确到小数点后四位)
【样例输入】

1 2.0 1

【样例输出】

root number: 1
root1 = -1.0000
public static void main(String[] args) {
        Scanner shu = new Scanner(System.in);
        float q = shu.nextFloat();
        float w = shu.nextFloat();
        float e = shu.nextFloat();
        Equation equ = new Equation(q, w, e);
        equ.Solving();
        equ.printResult();
    }

    static class Equation {
        private float a, b, c;
        private int rootType;
        private float root1, root2;

        public Equation(float a, float b, float c) {
            this.a = a;
            this.b = b;
            this.c = c;
        }

        public void Solving() {
            float delta = b * b - 4 * a * c;
            if (delta < 0) {
                rootType = 0;
            } else if (delta == 0) {
                rootType = 1;
                root1 = -b / (2 * a);
            } else {
                rootType = 2;
                root1 = (-b + (float)Math.sqrt(delta)) / (2 * a);
                root2 = (-b - (float)Math.sqrt(delta)) / (2 * a);
            }
        }

        public void printResult() {
            System.out.printf("root number: %d ", rootType);
            System.out.println();
            if (rootType == 1) {
                System.out.printf("root1 = %.4f ", root1);
            } else if (rootType == 2) {
                System.out.printf("root1 = %.4f\n ", root1);
                System.out.printf("root2 = %.4f ", root2);
            }
        }
    }

也是第一题方法的变形,就不多解释了

  • 7
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值