java——实验5

java——实验5,第1张

程序设计1

import java.text.ParseException;

import java.text.SimpleDateFormat;

import java.util.Date;



public class MyBookUtil {



    // 检查目标字符串中是否包含指定的关键字

    public static boolean check(String info, String key) 

    {

        // 在此完成方法的定义

        return info.contains(key); 

    }



    // 截取图书信息中的书名

    public static String getName(String info)

    {

        // 在此完成方法的定义

        return info.substring(info.indexOf("书名:")+3,info.indexOf(",出版时间"));

    }



    // 截取图书信息中的价格

    public static double getPrice(String info) 

    {

        // 在此完成方法的定义        

        return Double.parseDouble(info.substring(info.indexOf("价格:")+3,info.indexOf("元,页数")));

    }



    // 截取图书信息中的页码

    public static int getPage(String info) {

        // 在此完成方法的定义

        return Integer.parseInt(info.substring(info.indexOf("页数:")+3,info.lastIndexOf("页")));

    }

}

 程序设计2

import java.math.BigInteger;

public class BigCalc {
    private BigInteger num1;
    private BigInteger num2;

    public BigCalc(String str1, String str2) {
        num1 = new BigInteger(str1);
        num2 = new BigInteger(str2);
    }

    // 加法运算
    public BigInteger doAdd() 
    {
        return num1.add(num2);    
    }

    // 减法运算
    public BigInteger doSub() 
    {
        return num1.subtract(num2);
    }

    // 乘法运算
    public BigInteger doMult() 
    {
        return num1.multiply(num2);
    }

    // 除法运算
    public BigInteger doDiv() 
    {
        return num1.divide(num2);
    }

    // 获取因子个数
    public static BigInteger getFactorCount(BigInteger num) 
    {
        BigInteger count = new BigInteger("0"); // 计数结果
        BigInteger zero = BigInteger.ZERO; // BigInteger内置字段,代表0
        BigInteger one = BigInteger.ONE; // BigInteger内置字段,代表1
        // 在此完成方法的定义,获取参数num的因子的个数
        
        for(BigInteger temp = BigInteger.ONE;temp.compareTo(num)<0;temp=temp.add(one))
            if(num.remainder(temp)==zero)
                count=count.add(one);
        return count;
    }
}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/719714.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-25
下一篇 2022-04-25

发表评论

登录后才能评论

评论列表(0条)

保存