Android实验-重载(流程图、代码)

1 要求

1.编写一个在安卓下运行的减法类Subtract,其内置减法运算应囊括参数类型为String、Integer、float和double的减法运算。返回数据类型为String。

2.画出流程图,并与程序一一对应。

3.减法类定义在单独的java文件中。

4.当实参数量为2时,实现两个数的相减;当实参数量为1时,实现减一操作,当实参数量为0时输出结果0。

5.减法类中内置容错功能。

6.运行时在Logcat中输出以下计算公式和计算结果:

   a) Subtract(“2.01”, “2.001”)

   b) Subtract(学号倒数第二位, 倒数第一位)

   c) Subtract(-1.1, 1.001)

   d) Subtract(2.01f)

   e) Subtract()

   f) Subtract(“-0”)

   g) Subtract(“abc”)

7.计算结果要准确。

2 流程图

3 Android程序代码

  1)Subtract.java

package com.cg;
import android.util.Log;
import java.math.*;
public class Subtract {
    private Log Log = null;
    private String x1;
    private String y1;
    private double x2;
    private double y2;
    private Integer x3;
    private Integer y3;
    private float x4;
    private String x5;
    public Subtract(){
        test();
        //构造方法,与类同名
    }
    public Subtract(String x1,String y1){
        this.x1=x1;
        this.y1=y1;
        test(x1,y1);
    }
    public Subtract(double x2,double y2){
        this.x2=x2;
        this.y2=y2;
        test(x2,y2);
    }
    public Subtract(Integer x3,Integer y3){
        this.x3=x3;
        this.y3=y3;
        test(x3,y3);
    }
    public Subtract(float x4){
        this.x4=x4;
        test(x4);
    }
    public Subtract(String x5){
        this.x5=x5;
        test(x5);
    }
    String test(String x, String y) {
        try {
            Double doubleString1 = Double.parseDouble(x);
            Double doubleString2 = Double.parseDouble(y);//将字符串转换为double类型
            double i;
            i = doubleString1 - doubleString2;
            BigDecimal bd = new BigDecimal(i);
            double test1 = bd.setScale(3, BigDecimal.ROUND_HALF_UP).doubleValue();//保持精度。保留小数点后三位
            Log.i("cg86", "test result 2String:" + x + " " + y + " " + test1);
            Log.i("cg86", "test result 2String:" + x + " " + y + " " + i);
            return String.valueOf(test1);
        } catch (NumberFormatException e) {
            // 处理异常的代码
            Log.e("cg86", "Error: Input cannot be converted to double");
            return "Error: Input cannot be converted to double";
            //当不能转为为可计算的double型数时抛出异常
        }
    }//字符串双参数
    String test(String x) {
        // x-1;
        try {
            Double doubleString1 = Double.parseDouble(x);
            double i;
            i = doubleString1 - 1;
            Log.i("cg86", "test result 1String:" + " " + i);
            return String.valueOf(i);
        } catch (NumberFormatException e) {
            Log.e("cg86", "Error: Input cannot be converted to double");
            return "Error: Input cannot be converted to double";
        }
    }//字符串单参数
    String test(Integer x,Integer y) {
        try {
            Integer i;
            i = x - y;
            Log.i("cg86", "test result 2Integer:" + x + " " + y + " " + i);
            return String.valueOf(i);
        } catch (NumberFormatException e) {
            Log.e("cg86", "Error: Input cannot be calculate");
            return "Error: Input cannot be calculate";
        }
    }//Integer 双参数
    String test(float x) {
        try {
            float i;
            i=x-1;
            Log.i("cg86", "test result 1float:"  +" "+ i);
            return String.valueOf(i);
        }catch (NumberFormatException e){
            Log.e("cg86", "Error: Input cannot be calculate");
            return "Error: Input cannot be calculate";
        }

    }//float 单个参数
    String test(double x,double y) {
        try {
            double i;
            i=x-y;
            BigDecimal bd = new BigDecimal(i);
            double test1 = bd.setScale(3, BigDecimal. ROUND_HALF_UP ).doubleValue();//保持精度
            Log.i("cg86", "test result 2double:" + x + " "+ y + " " +test1);
            return String.valueOf(test1);
        }catch (NumberFormatException e) {
            Log.e("cg86", "Error: Input cannot be calculate");
            return "Error: Input cannot be calculate";
        }
    }//double 双参数
    String test(double x) {
        try {
            double i;
            i=x-1;
            Log.i("cg86", "test result 1float:"  +" "+ i);
            return String.valueOf(i);
        }catch (NumberFormatException e){
            Log.e("cg86", "Error: Input cannot be calculate");
            return "Error: Input cannot be calculate";
        }
    }//float 单个参数
    String test(){
        int i=0;
        Log.i("cg86", "test result:" +" "+ i);
        return String.valueOf(i);
    }//无参数
}

  2)MainActivity.java

package com.cg;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
public class MainActivity extends AppCompatActivity {
    public static final String TAG = "cg86";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        setTitle("2146935186 天林望海");
        Log Log = null;
        Log.d(MainActivity.TAG, "Debug message");
        Log.i(MainActivity.TAG, "Info message");
        Log.w(MainActivity.TAG, "Warning message");
        Log.e(MainActivity.TAG, "Error message");
        Integer i = new Integer(7);
        Subtract test1 = new Subtract();
        Subtract test2 = new Subtract("2.01", "2.001");
        Subtract test3 = new Subtract(-1.1, 1.001);
        Subtract test4 = new Subtract(i, i);
        Subtract test6 = new Subtract(2.01f);
        Subtract test5 = new Subtract("1.1.1", "2.2.2");
        Subtract test7 = new Subtract("abc");
        Subtract test8 = new Subtract("1.0K");
    }
}

4 实验结果图

图片

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

天林望海

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值