Java笔记_this关键字_HomeWork(1 - 4 题)

第一题

/**
 * @ClassName HomeWork01
 * @Description TODO
 * @Author Orange
 * @Date 2021/4/23 8:14
 * @Version 1.0
 **/

//题目要求定义方法max,实现某个double数组的最大值,并返回!

public class HomeWork01 {
    public static void main(String[] args) {
        double arr[] = {23.4, 212.4, 223.1, 42, 23122.9};
        A01 a = new A01();
        System.out.println("\n最大值max = " + a.max(arr));
    }
}

class A01 {
    public double max(double [] arr) {
        System.out.println("===寻找数组中的最大值:===");
        double max = 0;
        for(int i = 0; i < arr.length; i++) {
            if(arr[i] > max)
                max = arr[i];
            System.out.println(max);
        }
        return max;
    }
}

/*
程序运行结果:
------------------------
===寻找数组中的最大值:===
23.4
212.4
223.1
223.1
23122.9

最大值max = 23122.9
------------------------
*/

第一题改进:

/**
 * @ClassName HomeWork01_1
 * @Description TODO
 * @Author Orange
 * @Date 2021/4/23 9:42
 * @Version 1.0
 **/

//题目要求定义方法max,实现某个double数组的最大值,并返回!

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

        //疑问点:要是数组是空数组怎么办?数组的值为null怎么办?

        double arr[] = {};//12.3, 242, 1.99, 234.08,1940.65

        A03 a = new A03();
        Double res = a.max(arr);

        if(res != null) {                       //改进点1:考虑数组值可能为null时的处理
            System.out.println("\n最大值max = " + res);
        }else {
            System.out.println("输入的数组有误!");
        }
    }
}

class A03 {
    public Double max(double [] arr) {

        System.out.println("===寻找数组中的最大值:===");

        if(arr != null && arr.length > 0) {        //改进点2:避免了输入的数组值为null和
                                                   //数组为空的情况下程序报错的情况
            double max = 0;
            for (int i = 0; i < arr.length; i++) {
                if (arr[i] > max)
                    max = arr[i];
                System.out.println(max);
            }
            return max;
        }else {
            return null;
        }
    }
}

/*
程序运行结果:
------------------------
===寻找数组中的最大值:===
输入的数组有误!
------------------------
*/

第二题

/**
 * @ClassName HomeWork02
 * @Description TODO
 * @Author Orange
 * @Date 2021/4/23 8:32
 * @Version 1.0
 **/

// 编写类A02,定义方法find,实现查找某字符串是否在字符串数组中,
// 并返回索引,如果找不到返回-1!

public class HomeWork02 {
    public static void main(String[] args) {
        String arr[] = {"峰哥牛逼!", "青哥牛逼!", "还是峰哥牛逼!"};
        String str = "还是峰哥牛逼!";
        A02 a = new A02();
        System.out.println("输入字符串:" + str + " 返回值:" + a.find(str, arr));
    }
}

class A02 {
    public int find (String str, String str_arr []) {
        int i = 0;
        for(; i < str_arr.length; i++) {
            if(str.equals(str_arr[i]))
                break;
        }
        if(str.equals(str_arr[i])) {
            return i;
        }else {
            return -1;
        }
    }
}
/*
程序运行结果:
--------------------------------
输入字符串:还是峰哥牛逼! 返回值:2
--------------------------------
*/

第三题

/**
 * @ClassName HomeWork03
 * @Description TODO
 * @Author Orange
 * @Date 2021/4/23 9:06
 * @Version 1.0
 **/

// 类描述:
// 编写类Book,定义方法updatePrice,实现更改某本书的价格,
// 具体:如果价格 > 150,则更改为150,如果价格 > 100,更改为100,否则不变

import java.util.Scanner;
public class HomeWork03 {
    public static void main(String[] args) {
        Scanner myScanner = new Scanner(System.in);
        System.out.println("请输入此书的价格:");
        double book_price = myScanner.nextDouble();
        Book a = new Book();
        System.out.println("更新后的价格为:" + a.updatePrice(book_price));
    }
}

class Book {
    public double updatePrice (double price) {
        double new_price;
        if(price > 150) {
            new_price = 150;
        }else if(price > 100) {
            new_price = 100;
        }else {
            new_price = price;
        }
        return new_price;
    }
}

/*
程序运行结果:
--------------------------------
请输入此书的价格:
199
更新后的价格为:150.0
--------------------------------
*/

第四题

/**
 * @ClassName This_Homework04
 * @Description TODO
 * @Author Orange
 * @Date 2021/4/24 18:35
 * @Version 1.0
 **/

/*
编写类A04实现数组的复制功能copyArr,输入旧数组,返回一个新数组,
元素和旧数组一样
Write class A04 to implement the array copy function copyArr,
enter the old array, and return a new array with the same
elements as the old array.
*/

public class HomeWork04 {
    public static void main(String[] args) {
        int arr[] = {1,5,3,2,6,7,8,3,23};       //初始化外部数组

        A04 a = new A04(arr.length);            //创建类A04对象a
        a.copyArr(arr);                         //调用对象a 复制数组方法copyArr
        a.info();                               //调用对象a 显示数组方法info
    }
}

class A04 {
    int arr[] = null;                           //声明一个int[]类型的arr引用变量,
                                                //变量会占用一块内存空间,它没有分配初始值。

    public A04(int length) {                    //构造器,设置数组arr的长度
        arr= new int[length];
    }

    public void copyArr (int arr[]) {

        for(int i = 0; i < arr.length; i++) {   //将外部数组元素依次赋给对象a的数组元素
            this.arr[i] = arr[i];
        }
        //return this.arr;
    }

    public void info() {
        System.out.println("===对象数组输出===");
        for(int i = 0; i < this.arr.length; i++) {      //输出对象数组
            System.out.print(this.arr[i] + "\t");
        }

    }
}
/*
Program running results:
------------------------------------
===对象数组输出===
1	5	3	2	6	7	8	3	23
------------------------------------
*/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

OrangeQG

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

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

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

打赏作者

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

抵扣说明:

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

余额充值