编程语言
首页 > 编程语言> > java课堂作业第十一周

java课堂作业第十一周

作者:互联网

题目

第一题

Complex类,设计一个名为complex的类来表示复数以及完成复数运算的add,subs tract,multiply,divide和abs方法,并且覆盖tostring方法以返回一个表示复数的字符串

第二题

在Rational类中使用BigInteger,使用BigInteger表示分子和分母,重新设计和实现Rational类

第三题

打印Collatz练习题中100万以内产生序列最长的五个数字,以及这五个数字产生的collatz序列数值。

代码

package ch11;

import javax.swing.*;
import java.util.Scanner;

import java.math.RoundingMode;
import java.math.BigDecimal;
public class a {
    public static void main(String[] args) {
        System.out.print("Enter the first complex number: ");
        Scanner input = new Scanner(System.in);
        double real_1 = input.nextDouble();
        double imaginary_1 = input.nextDouble();
        Complex c_1 = new Complex(real_1, imaginary_1);
        System.out.print("Enter the second complex number:");
        //double real_2 = input.nextDouble();
        //double imaginary_2 = input.nextDouble();
        Complex c_2 = new Complex(input.nextDouble(), input.nextDouble());
        System.out.println(c_1.toString() + " + " + c_2.toString() + " = " + c_1.add(c_1, c_2));
        System.out.println(c_1.toString() + " - " + c_2.toString() + " = " + c_1.substract(c_1, c_2));
        System.out.println(c_1.toString() + " * " + c_2.toString() + " = " + c_1.multiply(c_1, c_2));
        System.out.println(c_1.toString() + " / " + c_2.toString() + " = " + c_1.divide(c_1, c_2));
        System.out.println( " |"+c_1.toString() + " | ="   + c_1.abs(c_1));
    }
}


class Complex implements Cloneable {
    //dataField
    private double realPart;
    private double imaginaryPart;

    //constructor
    public Complex() {
        realPart = 0;
        imaginaryPart = 0;
    }

    public Complex(double a) {
        realPart = a;
    }

    public Complex(double a, double b) {
        realPart = a;
        imaginaryPart = b;
    }

    //accessor
    public double getRealPart() {
        return realPart;
    }

    public double getImaginaryPart() {
        return imaginaryPart;
    }

    //method
    public Complex add(Complex a, Complex b) {
        Complex tempC = new Complex(a.realPart + b.realPart, a.imaginaryPart + b.imaginaryPart);
        return tempC;
    }

    public Object substract(Complex a, Complex b) {
        Complex tempC = new Complex(a.realPart - b.realPart, a.imaginaryPart - b.imaginaryPart);
        return tempC;
    }

    public Complex multiply(Complex a, Complex b) {
        Complex tempC = new Complex(a.realPart * b.realPart - a.imaginaryPart * b.imaginaryPart, a.realPart * b.imaginaryPart + a.imaginaryPart * b.realPart);
        return tempC;
    }

    public Complex divide(Complex a, Complex b) {
        double tempA = a.realPart * b.realPart + a.imaginaryPart * b.imaginaryPart;
        double tempB = a.imaginaryPart * b.realPart - a.realPart * b.imaginaryPart;
        double temp = Math.pow(b.imaginaryPart, 2) + Math.pow(b.realPart, 2);
        Complex tempC = new Complex(tempA / temp, tempB / temp);
        return tempC;
    }

    public double abs(Complex a) {
        return Math.sqrt(a.realPart * a.realPart + a.imaginaryPart * a.imaginaryPart);
    }

    @Override
    public String toString() {
        //if (this.realPart == 0 && this.imaginaryPart != 0) return formatDouble(imaginaryPart) + "i";
        if (this.imaginaryPart > 0) return formatDouble(realPart) + "+" + formatDouble(imaginaryPart) + 'i';
        else if (this.imaginaryPart < 0) return formatDouble(realPart) + "+" + formatDouble(imaginaryPart) + 'i';
        else return "" + formatDouble(realPart);
    }

    @Override
    public Complex clone() {
        Complex tempC;
        try {
            tempC = (Complex) super.clone();
        } catch (CloneNotSupportedException ex) {
            return null;
        }
        return tempC;
    }

    //美化浮点显示
    public static String formatDouble(double d) {
        BigDecimal bg = new BigDecimal(d).setScale(2, RoundingMode.UP);
        double num = bg.doubleValue();
        if (Math.round(num) - num == 0) {
            return String.valueOf((long) num);
        }
        return String.valueOf(num);
    }
}

package ch11;

import java.math.BigInteger;

public class b {
    public static void main(String[] args) {
        Rational rational1 = new Rational(new BigInteger(14 + ""),
                new BigInteger(37 + ""));
        Rational rational2 = new Rational(new BigInteger(23 + ""),
                new BigInteger(67 + ""));
        System.out.println("14/37 + 23/67 = "+rational1.add(rational2));
        System.out.println("14/37 - 23/67 = "+rational1.subtract(rational2));
        System.out.println("14/37 * 23/67 = "+rational1.multiple(rational2));
        System.out.println("14/37 / 23/67 = "+rational1.divide(rational2));
    }
}




class Rational extends Number implements Comparable {
    private BigInteger numerator;// 分子
    private BigInteger denominator;// 分母

    public Rational() {
        this(BigInteger.ZERO, BigInteger.ONE);
    }

    public Rational(BigInteger numerator, BigInteger denominator) {
        BigInteger gcd = gcd(numerator, denominator);
        this.numerator = ((denominator.compareTo(BigInteger.ZERO)) > 0 ? BigInteger.ONE
                : new BigInteger(-1 + "")).multiply(numerator).divide(gcd);
        this.denominator = denominator.abs().divide(gcd);
    }

    public static BigInteger gcd(BigInteger a, BigInteger b) {
        BigInteger n1 = a.abs();
        BigInteger n2 = b.abs();
        BigInteger remainder = n1.remainder(n2);
        while (remainder.compareTo(BigInteger.ZERO) > 0) {
            n1 = n2;
            n2 = remainder;
            remainder = n1.remainder(n2);
        }
        return n2;
    }

    public BigInteger getNumerator() {
        return numerator;
    }

    public BigInteger getDenominator() {
        return denominator;
    }

    public Rational add(Rational secondRational) {
        BigInteger n = numerator.multiply(secondRational.getDenominator()).add(
                denominator.multiply(secondRational.getNumerator()));
        BigInteger d = denominator.multiply(secondRational.getDenominator());
        return new Rational(n, d);
    }

    public Rational subtract(Rational secondRational) {
        BigInteger n = numerator.multiply(secondRational.getDenominator())
                .subtract(denominator.multiply(secondRational.getNumerator()));
        BigInteger d = denominator.multiply(secondRational.getDenominator());
        return new Rational(n, d);
    }

    public Rational multiple(Rational secondRational) {
        BigInteger n = numerator.multiply(secondRational.getNumerator());
        BigInteger d = denominator.multiply(secondRational.getDenominator());
        return new Rational(n, d);
    }

    public Rational divide(Rational secondRational) {
        BigInteger n = numerator.multiply(secondRational.getDenominator());
        BigInteger d = denominator.multiply(secondRational.getNumerator());
        return new Rational(n, d);
    }

    @Override
    public boolean equals(Object obj) {
// TODO Auto-generated method stub
        if (this.getNumerator().compareTo(((Rational) obj).getNumerator()) == 0) {
            return true;
        } else {
            return false;
        }
    }

    @Override
    public String toString() {
// TODO Auto-generated method stub
        if (denominator.compareTo(BigInteger.ONE) == 0) {
            return numerator.toString();
        } else {
            return numerator.toString() + "/" + denominator.toString();
        }
    }

    @Override
    public int intValue() {
// TODO Auto-generated method stub
        return numerator.divide(denominator).intValue();
    }

    @Override
    public long longValue() {
// TODO Auto-generated method stub
        return numerator.divide(denominator).longValue();
    }

    @Override
    public float floatValue() {
// TODO Auto-generated method stub
        return numerator.divide(denominator).floatValue();
    }

    @Override
    public double doubleValue() {
// TODO Auto-generated method stub
        return numerator.divide(denominator).doubleValue();
    }

    @Override
    public int compareTo(Object o) {
// TODO Auto-generated method stub
        if (this.getNumerator().compareTo(((Rational) o).getNumerator()) > 0) {
            return 1;
        } else if (this.getNumerator().compareTo(((Rational) o).getNumerator()) < 0) {
            return -1;
        } else {
            return 0;
        }
    }
}

package ch11;

import java.util.ArrayList;

public class c {
    public static int calculate(int upLimit) {

        int s[] = new int[upLimit+1];
        int s1[] = new int[upLimit+1];
        for (int i = 3; i <= upLimit; i++) {
            ArrayList a = calculateCollatzLen(i);
            int len = a.size();
            s[i] = len;
            //System.out.println(i+" "+a);
            s1[i] = len;
            a.clear();
        }
        //s1=s.clone();

        java.util.Arrays.sort(s);

//        for (int i = 3; i <= upLimit; i++) {
//            for (int j = i + 1; j <= upLimit; j++) {
//                if (s[i] < s[j]) {
//                    int temp = s[i];
//                    s[i] = s[j];
//                    s[j] = temp; // 两个数交换位置
//                }
//            }
//        }


        int max[]=new int[5];
        for(int i=3;i<=upLimit;i++){
            if(s1[i]==s[upLimit])
                max[0]=i;
            if(s1[i]==s[upLimit-1])
                max[1]=i;
            if(s1[i]==s[upLimit-2])
                max[2]=i;
            if(s1[i]==s[upLimit-3])
                max[3]=i;
            else if(s1[i]==s[upLimit-4])
                max[4]=i;

        }
        System.out.println(max[0]);
        for(int i=0;i<5;i++){
            System.out.println("max"+i+"d="+max[i]);
            ArrayList list = calculateCollatzLen(max[i]);
            System.out.println(list);
            list.clear();
        }
//        System.out.println();

        return 0;
    }
    //return 链表
    public static ArrayList calculateCollatzLen(int i) {
        long n = i;
        int count = 1;
        ArrayList a = new ArrayList();
        while (n != 1) {

            if (n % 2 == 0) {
                n = n / 2;
            } else {
                n = n * 3 + 1;
            }
            a.add(n);
            count++;
        }
        return a;
    }

    public static void main(String[] args) {
        c.calculate(1000000);
    }
}

 

标签:BigInteger,return,第十一,课堂作业,imaginaryPart,Complex,Rational,java,public
来源: https://blog.csdn.net/zhang24360/article/details/117651415