java 13周

这个博客展示了如何用Java实现复数(Hello类)和有理数(Rational类)的基本运算,包括加减乘除以及求模。在Hello类中,实现了复数的加减乘除及打印和求模操作。而在Rational类中,定义了有理数的构造方法、加减乘除操作,并实现了Comparable接口。示例中演示了这些运算的使用。
摘要由CSDN通过智能技术生成

public class Hello{
    private double realPart;
    private double imaginPart;
    public Hello(double r,double i){
    this.realPart = r;
        this.imaginPart = i;
    }
    public Hello(){
        realPart = 0;
        imaginPart = 0;
    }
    public static Hello complexAdd(Hello a,Hello b){
        Hello c = new Hello();
        c.realPart = a.realPart+b.realPart;
        c.imaginPart = a.imaginPart+b.imaginPart;
        return c;
    }
    // minus
    public static Hello complexminus(Hello a,Hello b){
        Hello c = new Hello();
        c.realPart = a.realPart-b.realPart;
        c.imaginPart = a.imaginPart-b.imaginPart;
        return c;
    }

    // multiply
    public static Hello multiply(Hello a,Hello b){
        Hello c = new Hello();
        c.realPart = a.realPart*b.realPart - a.imaginPart*b.imaginPart;
        c.imaginPart = a.imaginPart*b.realPart +  a.realPart* b.imaginPart;
        return c;
    }


    // divide

    public static Hello divide(Hello a,Hello b){
        double d = Math.sqrt(b.realPart*b.realPart) + Math.sqrt(b.imaginPart*b.imaginPart);
        Hello c = new Hello();
        c.realPart = (a.realPart*b.realPart + a.imaginPart*b.imaginPart) / d;
        c.imaginPart = (-a.imaginPart*b.realPart +  a.realPart* b.imaginPart) / d;
        return c;
    }

    public static void toString(Hello a){
        System.out.println(a.realPart+"+"+a.imaginPart+"i");
    }

    public static double scale(Hello a){
        double d = Math.sqrt(a.realPart*a.realPart + a.imaginPart*a.imaginPart);
        return d;
    }


    public static void main(String[] args){
        Hello a,b;
        Hello c1 = new Hello(3.5,5.5);
        Hello c2 = new Hello(-3.5,1);
        Hello.toString(complexAdd(c1,c2));
        Hello.toString(complexminus(c1,c2));
        Hello.toString(multiply(c1,c2));

        Hello.toString(divide(c1,c2));

        System.out.println(scale(c1));



    }
}
import java.math.BigInteger;

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

    /**
     * @param args
     */
    public static void main(String[] args) {
// TODO Auto-generated method stub
        Rational rational1 = new Rational(new BigInteger(14 + ""),
                new BigInteger(37 + ""));
        Rational rational2 = new Rational(new BigInteger(23 + ""),
                new BigInteger(67 + ""));

        System.out.println(rational1 + "+" + rational2 + "=" +rational1.add(rational2));
        System.out.println(rational1 + "-" + rational2 + "=" +rational1.subtract(rational2));
        System.out.println(rational1 + "*" + rational2 + "=" +rational1.multiple(rational2));
        System.out.println(rational1 + "/" + rational2 + "=" +rational1.divide(rational2));
        System.out.println(rational2 + "is"  +rational2.doubleValue());

        Rational rational = new Rational(new BigInteger(0 + ""),
                new BigInteger(1 + ""));
        for (int i = 2; i <=100; i++) {
            rational = rational.add(new Rational(new BigInteger(i-1 + ""),
                    new BigInteger(i + "")));
        }
        System.out.println("final is"  +rational);
        System.out.println("value is"  +rational.doubleValue());




    }

    public Rational() {
// TODO Auto-generated constructor stub
        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;
        }
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
资源包主要包含以下内容: ASP项目源码:每个资源包中都包含完整的ASP项目源码,这些源码采用了经典的ASP技术开发,结构清晰、注释详细,帮助用户轻松理解整个项目的逻辑和实现方式。通过这些源码,用户可以学习到ASP的基本语法、服务器端脚本编写方法、数据库操作、用户权限管理等关键技术。 数据库设计文件:为了方便用户更好地理解系统的后台逻辑,每个项目中都附带了完整的数据库设计文件。这些文件通常包括数据库结构图、数据表设计文档,以及示例数据SQL脚本。用户可以通过这些文件快速搭建项目所需的数据库环境,并了解各个数据表之间的关系和作用。 详细的开发文档:每个资源包都附有详细的开发文档,文档内容包括项目背景介绍、功能模块说明、系统流程图、用户界面设计以及关键代码解析等。这些文档为用户提供了深入的学习材料,使得即便是从零开始的开发者也能逐步掌握项目开发的全过程。 项目演示与使用指南:为帮助用户更好地理解和使用这些ASP项目,每个资源包中都包含项目的演示文件和使用指南。演示文件通常以视频或图文形式展示项目的主要功能和操作流程,使用指南则详细说明了如何配置开发环境、部署项目以及常见问题的解决方法。 毕业设计参考:对于正在准备毕业设计的学生来说,这些资源包是绝佳的参考材料。每个项目不仅功能完善、结构清晰,还符合常见的毕业设计要求和标准。通过这些项目,学生可以学习到如何从零开始构建一个完整的Web系统,并积累丰富的项目经验。
资源包主要包含以下内容: ASP项目源码:每个资源包中都包含完整的ASP项目源码,这些源码采用了经典的ASP技术开发,结构清晰、注释详细,帮助用户轻松理解整个项目的逻辑和实现方式。通过这些源码,用户可以学习到ASP的基本语法、服务器端脚本编写方法、数据库操作、用户权限管理等关键技术。 数据库设计文件:为了方便用户更好地理解系统的后台逻辑,每个项目中都附带了完整的数据库设计文件。这些文件通常包括数据库结构图、数据表设计文档,以及示例数据SQL脚本。用户可以通过这些文件快速搭建项目所需的数据库环境,并了解各个数据表之间的关系和作用。 详细的开发文档:每个资源包都附有详细的开发文档,文档内容包括项目背景介绍、功能模块说明、系统流程图、用户界面设计以及关键代码解析等。这些文档为用户提供了深入的学习材料,使得即便是从零开始的开发者也能逐步掌握项目开发的全过程。 项目演示与使用指南:为帮助用户更好地理解和使用这些ASP项目,每个资源包中都包含项目的演示文件和使用指南。演示文件通常以视频或图文形式展示项目的主要功能和操作流程,使用指南则详细说明了如何配置开发环境、部署项目以及常见问题的解决方法。 毕业设计参考:对于正在准备毕业设计的学生来说,这些资源包是绝佳的参考材料。每个项目不仅功能完善、结构清晰,还符合常见的毕业设计要求和标准。通过这些项目,学生可以学习到如何从零开始构建一个完整的Web系统,并积累丰富的项目经验。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值