第七章练习题

import java.util.Arrays;

public class Import {
    public static void main(String[] args) {
        double[] arr = {1,288,70.5,-1,5,89};
        A a = new A();
        System.out.println(a.max(arr));

    }
}
class A{
    public double max(double[] arr){
       Arrays.sort(arr);
       return arr[arr.length - 1];
       /*
       * 还可以这样写
       * int max = arr[0];
       * for(int i = 1;i < arr.length ;i++){
       *        if(arr[i] > max  ){
       *            max = arr[i];
       *            }
       * 
       * }
       * return max;
       * */
    }
}

上面的代码也可以进行改进啊,增加代码的健壮性,返回值类型为double的方法,返回值类型可以是return。

2、

public class Test {
    public static void main(String[] args) {
        Demo01 p = new Demo01();
        String[] str = {"w","加油","我爱你"};
        System.out.println(p.find("加油",str));
    }
}


public class Demo01 {
    public int  find(String str1, String[] str) {

            for (int i = 0; i < str.length; i++) {
                if (str1.equals(str[i])) {
                    return i;
                }
            }
            return -1;

        }
    }

        只是为了实现。

3、

public class Test {
    public static void main(String[] args) {
        Demo01 p = new Demo01();
        System.out.println(p.updatePrice(222.5));

    }
}
public class Demo01 {
    double price;

    public double updatePrice(double price) {
        if (price > 150) {
            this.price = 150;
        } else if (price > 100) {
            this.price = 100;
        } else {
            this.price = price;
        }
        return this.price;
    }
}

加入构造器:

public class Test {
    public static void main(String[] args) {
        Demo01 p = new Demo01(120.8);
        p.updatePrice();
        p.show();

    }
}


public class Demo01 {
    double price;

    public Demo01(double price) {
        this.price = price;
    }

    public void updatePrice() {
        if (price > 150) {
            this.price = 150;
        } else if (price > 100) {
            this.price = 100;
        } else {
            this.price = price;
        }

    }
    public void show(){
        System.out.println("该书籍的价格" + this.price);
    }
}

 4、前面的数组的知识又忘记了

复制一个新的数组需要另外开辟一个地址。int[] name = new int[地址长度];

public class Test {
    public static void main(String[] args) {

        Demo01 p = new Demo01();
        int[] arr = {20, 30, 50, 11, 30};
        int[] arr5 = p.copyArr(arr);
        System.out.println("变化后的数组啊");
        for (int i = 0; i < arr5.length; i++) {
            System.out.print(arr5[i] + "\t");
        }


    }
}

public class Demo01 {

    public int[] copyArr(int[] arr) {

        int[] arr3 = new int[arr.length];

        for (int i = 0; i < arr.length; i++) {
            arr3[i] = arr[i];
        }
        return arr3;
    }


}

5、

public class Test {
    public static void main(String[] args) {

        Demo01 p = new Demo01(5.2);
        System.out.println(p.area());
        System.out.println(p.perimeter());
    }
}

public class Demo01 {
    double r;

    public Demo01(double r) {
        this.r = r;
    }

    public double area(){
        double area = 3.14*r;
        return area;
    }
    public double perimeter(){

        return 2*r*3.14;
    }


}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值