第五次作业

第一题

package united6.test1;

public class MobilPhone {
        public String brand;

        public MobilPhone() {
            this.brand = "诺基亚";
        }

        public MobilPhone(String bra) {
            this.brand = bra;
        }

        public String buy() {
            return "没发工资,买一个" + brand + "牌子的手机吧!";
        }

        public String buy(String reason) {
            return reason + ",快买一个" + brand + "牌子的手机吧!";
        }
    }

    //以上为MobilPhone类定义代码

下为调用代码

package united6.test1;

public class MobilPhoneTest {
        public static void main(String[] args) {
            MobilPhone mp = new MobilPhone();
            mp.brand = "苹果";
            String details=mp.buy("发工资了");
            System.out.println(details);
        }
    }

运行结果
在这里插入图片描述

第二题

下为定义类代码

package unit6.test2;

public class Calculation {
    static int year;
    static double a;    //利率
    static double b;    //每月还贷金额
    static double c;    //总利息

    public double i(double i, int yearchice) {
        if (yearchice == 1) {
            a = 0.0603;
            year=36;
            c=i*a;
        }
        else if(yearchice==2){
            a=0.0612;
            year=60;
            c=i*a;
        }
        else if(yearchice==3){
            a=0.0639;
            year=240;
            c=i*a;
        }
        b=(i+c)/year;
        System.out.println("月供为:"+b);
        return b;
    }
}

下为调用类代码

package unit6.test2;

import java.util.Scanner;

public class two {
    public static void main(String[] args) {
        Calculation moey = new Calculation();
        Scanner input = new Scanner(System.in);
        System.out.println("请输入贷款金额::");
        double i = input.nextDouble();
        System.out.println("/请选择贷款年限:1.3年(36个月)\t2.5年(60个月)\t3.20年(240个月);");
        int n = input.nextInt();
        moey.i(i, n);
    }
}

运行结果

在这里插入图片描述

第三题

下为定义类代码

package unit6.test3;

public class Triangle {
    public boolean isTriangle(int a, int b, int c) {
        boolean flag = false;
        if ((a + b > c) && (a + c > b) && (b + c > a)) {
            flag = true;
        }
        return flag;
    }

    public String shape(int a, int b, int c) {
        String shape = "";
        if (a + b <= c || a + c <= b || c + b <= a) {
            System.out.println("不能构成三角形");
        } else if (a == b && b == c) {
            shape = "等边三角形";
            System.out.println("是" + shape);
        } else if (a * a == b * b + c * c || b * b == a * a + c * c || c * c == a * a + b * b) {
            shape = "直角三角形";
            System.out.println("是" + shape);
        } else if (a * a > b * b + c * c || b * b > a * a + c * c || c * c > a * a + b * b) {
            shape = "钝角三角形";
            System.out.println("是" + shape);
        } else {
            shape = "锐角三角形";
            System.out.println("是" + shape);
        }
        return shape;
    }
}

下为调用代码

package unit6.test3;

import java.util.Scanner;

public class test {
    public static void main(String[] args) {
        boolean i=true;
        Triangle t=new Triangle();
        String m="";
        Scanner input = new Scanner(System.in);
        do{
            System.out.println("输入第一个边:");
            int a = input.nextInt();
            System.out.println("输入第二个边:");
            int b= input.nextInt();
            System.out.println("输入第三个边:");
            int c = input.nextInt();
            t.isTriangle(a, b, c);
            t.shape(a, b, c);
            System.out.println("继续吗?(y/n)");
            m= input.next();
            if (m.equals("n")) {
                System.out.println("已输出结果,结束。");
                break;
            }
        }while (m.equals("y"));
    }
}

运行结果
在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值