Java-类与对象练习

一、字符串查找元素

1.FindMethod

package com.nsys.class02;

/**
 * @Author nsys
 * @Date 2021/11/7 9:23
 * @Description 类与对象_练习_1
 */
public class FindMethod {
    /*
    题目:
        定义方法find,实现在[字符串数组]中查找[某个字符串元素],
        并返回索引,找不到返回-1
    思考:
        当字符串数组为【空】的情况
        当字符串数组为【null】的情况
     */

    /*
    1)修饰符类型:public
    2)是否为类方法:是,类方法不加static
    3)返回值类型:int
    4)方法名:findStr
    5)参数值类型:字符串数组,字符串
     */

    public int findStr(String[] str, String element) {
	// ★这里有个坑
	// 必须先判断字符串数组是否为空,再判断数组长度
	// 若先判断数组长度,当字符串数组为null时
	// 此时字符串的长度是不存在,会报NullPointerException
        if (str != null && str.length != 0) {
            for (int i = 0; i < str.length; i++) {
                if (element.equals(str[i])) {
                    return i;
                }
            }
        }
        return -1;
    }


    /*
    定义方法find,实现在[字符串]中查找[某个字符元素],
    并返回索引,找不到返回-1
     */

       /*
    1)修饰符类型:public
    2)是否为类方法:是
    3)返回值类型:int
    4)方法名:findChar
    5)参数值类型:字符串,字符
     */

    public int findChar(String str, char ch) {
        for (int i = 0; i < str.length(); i++) {
            if (str.charAt(i) == ch) {
                System.out.println(ch);
                return i;
            }
        }
        return -1;
    }

}

2.Main

package com.nsys.class02;

/**
 * @Author nsys
 * @Date 2021/11/7 10:10
 * @Description
 */
public class Main {
    public static void main(String[] args) {
        FindMethod fd = new FindMethod();
        String[] strArray = {"xiaoming", "xiaohong", "xianggang"};
        String[] strArray2 = null;
        // findStr(strArray,"xiaoming");
        // 检查怎么没有返回值,原来忘记定义变量接收了
        int result = fd.findStr(strArray2, "xiaoming");
        System.out.println(result);


        int result2 = fd.findChar("ABC", 'B');
        System.out.println(result2);


    }
}

二、更改书的价格

1.Book

package com.nsys.class03;

/**
 * @Author nsys
 * @Date 2021/11/7 10:51
 * @Description 类与对象_练习_2
 */
public class Book {
    /*
    题目:
        实现更改某本书价格,
        如果价格大于150,则改为150
        如果价格大于100,则改为100
        否则不变
     */

    // 定义书的属性
    public String subject;
    public int price;

    // 书的构造方法
    public Book(String subject, int price) {
        this.subject = subject;
        this.price = price;
    }

    // 改变书的价格方法
    /*
    1)修饰符:public
    2)类方法:是,不需要static
    3)返回值:字符串,书的价格
    4)方法名:updatePrice
    5)参数:int newPrice传入更改的价格
     */
    public String updatePrice(int newPrice) {
        if (newPrice > 150) {
            int tempPrice = this.price;
            this.price = 150;
            return "1)调整书的价格大于150,则书的价格由" + tempPrice + "调整为" + this.price;

        } else if (newPrice > 100) {
            int tempPrice = this.price;
            this.price = 100;
            return "2)调整书的价格大于100并且小于等于150,则书的价格由" + tempPrice + "调整为" + this.price;

        } else if (newPrice > 0) {
            int tempPrice = this.price;
            this.price = newPrice;
            return "3)调整书的价格大于0并且小于等于100,则书的价格由" + tempPrice + "调整为" + this.price;
        }
        return "4)修改书的价格错误:" + newPrice;
    }
}

2.TestMain

package com.nsys.class03;

/**
 * @Author nsys
 * @Date 2021/11/7 11:12
 * @Description
 */
public class TestMain {
    public static void main(String[] args) {
        Book b = new Book("数据结构", 35);
        String res1 = b.updatePrice(50);
        String res2 = b.updatePrice(100);
        String res3 = b.updatePrice(-1);
        String res4 = b.updatePrice(35);
        String res5 = b.updatePrice(111);
        String res6 = b.updatePrice(150);
        String res7 = b.updatePrice(200);
        /*
            3)调整书的价格大于0并且小于等于100,则书的价格由35调整为50
            3)调整书的价格大于0并且小于等于100,则书的价格由50调整为100
            4)修改书的价格错误:-1
            3)调整书的价格大于0并且小于等于100,则书的价格由100调整为35
            2)调整书的价格大于100并且小于等于150,则书的价格由35调整为100
            2)调整书的价格大于100并且小于等于150,则书的价格由100调整为100
            1)调整书的价格大于150,则书的价格由100调整为150
         */
        System.out.println(res1);
        System.out.println(res2);
        System.out.println(res3);
        System.out.println(res4);
        System.out.println(res5);
        System.out.println(res6);
        System.out.println(res7);

    }
}

三、拷贝数组

JAVA拷贝分为两种方式,一种是引用拷贝,一种是对象拷贝

  • 引用拷贝:和对象拷贝的不同之处在于,引用拷贝只会生成一个新的对象引用地址,但两个地址其最终指向的还是同一个对象
  • 对象拷贝:这种方式会重新生成一个新的对象,生成的新对象与原来的对象没有任何关联

1.ArrayDemo

package com.nsys.class04;

/**
 * @Author nsys
 * @Date 2021/11/7 12:37
 * @Description 复制数组
 */
public class ArrayDemo {
    /*
        题目:
        复制数组,输入一个旧数组,返回一个新数组,元素和旧数组一致
        注意:
        本题为对象拷贝
     */

    /*
        思路:
        1)添加类属性:不需要
        2)添加构造方法:不需要
        3)添加类方法:
            - 修饰符:public
            - 类方法:不需要static
            - 返回值:int[]
            - 方法名:copyArray
            - 参数类型:int[]
     */

    public int[] copyArray(int[] oldArr) {
        if (oldArr != null && oldArr.length != 0) {
            // 动态初始化一个新的数组,数组长度为就数组的长度
            int[] newArr = new int[oldArr.length];
            // 遍历旧数组,将元素对应的值赋值给新数组
            for (int i = 0; i < oldArr.length; i++) {
                newArr[i] = oldArr[i];
            }
            return newArr;
        }
        return oldArr;
    }

    /*
        法二,直接用clone()方法
     */
    public int[] copyArray2(int[] oldArr) {
        if (oldArr != null && oldArr.length != 0) {
            // 动态初始化一个新的数组,数组长度为就数组的长度
            int[] newArr = new int[oldArr.length];
            // 克隆数组
            newArr = oldArr.clone();
            return newArr;
        }
        return oldArr;
    }



}

2.TestMain

package com.nsys.class04;

import java.util.Arrays;

/**
 * @Author nsys
 * @Date 2021/11/7 13:44
 * @Description 测试
 */
public class TestMain {
    public static void main(String[] args) {
        // 测试数据
        int[] array1 = {};
        int[] array2 = null;
        int[] array3 = {1,2,3};
        int[] array4 = {4,5,6};

        // 创建ArrayDemo对象
        ArrayDemo a = new ArrayDemo();
        // 调用a的copyArray复制数组的方法,将数组传入
        int[] res1 = a.copyArray(array3);
        // 2021年11月7日13:55:51姑且叫地址~
          /*
        旧数组地址:[I@1b6d3586,数组元素值为:[1, 2, 3]
        新数组地址:[I@4554617c,数组元素值为:[1, 2, 3]
         */
        System.out.println("旧数组地址:" +array3+",数组元素值为:"+ Arrays.toString(array3));
        System.out.println("新数组地址:" +res1+",数组元素值为:"+ Arrays.toString(res1));

        // 修改新数组的元素,旧数组是否会改变?
        /*
        旧数组地址:[I@1b6d3586,数组元素值为:[1, 2, 3]
        新数组地址:[I@4554617c,数组元素值为:[4, 2, 3]
         */
        res1[0] = 4;
        System.out.println("-------------------更改新数组的元素-------------------------");
        System.out.println("旧数组地址:" +array3+",数组元素值为:"+ Arrays.toString(array3));
        System.out.println("新数组地址:" +res1+",数组元素值为:"+ Arrays.toString(res1));

        // 调用克隆方法
        // 创建ArrayDemo对象
        ArrayDemo b = new ArrayDemo();
        // 调用a的copyArray复制数组的方法,将数组传入
        int[] res2 = a.copyArray2(array4);
        System.out.println("-------------------使用clone()方法-------------------------");
        System.out.println("旧数组地址:" +array4+",数组元素值为:"+ Arrays.toString(array4));
        System.out.println("新数组地址:" +res2+",数组元素值为:"+ Arrays.toString(res2));




    }
}

四、圆的计算

1.Circle

package com.nsys.class05;

/**
 * @Author nsys
 * @Date 2021/11/7 14:13
 * @Description 计算圆的周长和面积
 */
public class Circle {
    /*
        思路:
        1)添加类属性:
            - 半径:double r
            - pi:double 3.14
        2)添加构造方法:
            - 初始化半径
        3)添加类方法:
            - 修饰符:public
            - 类方法:不需要static
            - 返回值:String
            - 方法名:circlePer,circleArea
            - 参数类型:无
     */
    // 类属性
    public double r;
    public double PI = 3.14;

    // 构造方法
    public Circle(double r) {
        if (r >= 0) {
            this.r = r;
        }
    }

    // 计算圆周长的方法
    public String circlePer() {
        double C = 2 * PI * r;
        return "圆的周长为:" + C;
    }

    // 计算圆面积的方法
    public String circleArea() {
        double S = 2 * PI * r * r;
        return "圆的面积为:" + S;
    }


}

2.TestMain

package com.nsys.class05;

/**
 * @Author nsys
 * @Date 2021/11/7 14:26
 * @Description 测试圆的周长与面积
 */
public class TestMain {
    public static void main(String[] args) {
        // 创建圆的对象
        /*
        圆的周长为:64.056
        圆的面积为:653.3711999999999
        圆的周长为:0.0
        圆的面积为:0.0
         */
        Circle c = new Circle(10.2);
        System.out.println(c.circlePer());
        System.out.println(c.circleArea());

        // 初始化构造c1圆时,半径长度-4,不通过判定
        // 所以圆的半径仍为double数据类型的默认值 0.0
        Circle c1 = new Circle(-4);
        System.out.println(c1.circlePer());
        System.out.println(c1.circleArea());


    }

}

五、加减乘除

1.Calc

package com.nsys.class06;

/**
 * @Author nsys
 * @Date 2021/11/7 14:40
 * @Description 计算加减乘除
 */
public class Calc {
        /*
        思路:
        1)添加类属性:无
        2)添加构造方法:无
        3)添加类方法:
            - 修饰符:public
            - 类方法:不需要static
            - 返回值:String
            - 方法名:add、sub、mul、div
            - 参数类型:无
     */

    public String add(double n1, double n2) {
        return "两数求和结果为:" + (n1 + n2);
    }

    public String sub(double n1, double n2) {
        return "两数相减结果为:" + (n1 - n2);
    }

    public String mul(double n1,double n2){
        return "两数相乘结果为:" + (n1 * n2);
    }

    public String div(double n1,double n2){
        if(n2 ==0){
            return "除数不为0呀,小伙子";
        }
        return "两数相除结果为:" + (n1 / n2);
    }

}

2.TestMain

package com.nsys.class06;

/**
 * @Author nsys
 * @Date 2021/11/7 14:49
 * @Description
 */
public class TestMain {
    public static void main(String[] args) {

        Calc c = new Calc();
        /*
        两数求和结果为:4.2
        两数相乘结果为:-12.299999999999999
        两数相减结果为:-1.0
        除数不为0呀,小伙子
        两数相除结果为:0.06363636363636364
         */
        System.out.println(c.add(1.2,3));
        System.out.println(c.mul(3,-4.1));
        System.out.println(c.sub(2,3));
        System.out.println(c.div(2,0));
        System.out.println(c.div(2.1,33));

    }
}

六、复用构造器

1.Employee

package com.nsys.class07;

/**
 * @Author nsys
 * @Date 2021/11/7 16:02
 * @Description 复用构造器
 */
public class Employee {
    // 类属性
    public String name;
    public String sex;
    public int age;
    public String job;
    public double sal;

    // 构造器
    public Employee(String name, String sex, int age) {
        this.name = name;
        this.sex = sex;
        this.age = age;
    }

    public Employee(String job, double sal) {
        this.job = job;
        this.sal = sal;
    }

    // 复用前面的构造器
    public Employee(String name, String sex, int age, String job, double sal) {
        /*
           this访问构造器语法细节:
                - this(参数列表)
                - 只能在构造器中使用,即在一个构造器中访问另一个构造器
                - 必须放在方法体中第一条语句,仅可使用一次
         */
        this(name, sex, age);
        this.job = job;
        this.sal = sal;
    }

    public static void main(String[] args) {
        Employee e = new Employee("xiao","nan",1,"chif",1);
        System.out.println(e);
    }

}

七、猜拳

1.Tom

package com.nsys.class08;

import java.util.Random;

/**
 * @Author nsys
 * @Date 2021/11/7 16:54
 * @Description
 */
public class Tom {
    public String name;

    /*
        修饰符:public
        方法:类方法,不需要static
        返回值:void
        方法名:guess
        参数:int[]
     */
    public void guess(int[] arrayTest) {
        // 判断传入的数组是否为空
        if (arrayTest != null && arrayTest.length > 0) {
            // 调用randomMethod方法,生成随机0-2数组
            int[] randomNum = randomMethod(arrayTest.length);

            for (int i = 0; i < arrayTest.length; i++) {
                if (arrayTest[i] == randomNum[i]) {
                    System.out.println(randomNum[i] + ":平局");
                } else if (arrayTest[i] == 0 && randomNum[i] == 1) {
                    System.out.println("我为" + arrayTest[i] + ",电脑为" + randomNum[i] + "赢局");
                } else if (arrayTest[i] == 0 && randomNum[i] == 2) {
                    System.out.println("我为" + arrayTest[i] + ",电脑为" + randomNum[i] + "输局");
                } else if (arrayTest[i] == 1 && randomNum[i] == 0) {
                    System.out.println("我为" + arrayTest[i] + ",电脑为" + randomNum[i] + "输局");

                } else if (arrayTest[i] == 1 && randomNum[i] == 2) {
                    System.out.println("我为" + arrayTest[i] + ",电脑为" + randomNum[i] + "赢局");

                } else if (arrayTest[i] == 2 && randomNum[i] == 0) {
                    System.out.println("我为" + arrayTest[i] + ",电脑为" + randomNum[i] + "赢局");
                } else if (arrayTest[i] == 2 && randomNum[i] == 1) {
                    System.out.println("我为" + arrayTest[i] + ",电脑为" + randomNum[i] + "输局");
                }
            }
        }
    }

    public int[] randomMethod(int randomTime) {
        // 实例化一个随机对象
        Random rd = new Random();
        // 初始化一个数组,长度由猜的数组长度决定
        int[] randomArray = new int[randomTime];
        // 生成0-2随机数,添加到数组中
        for (int i = 0; i < randomArray.length; i++) {
            randomArray[i] = rd.nextInt(2);
        }
        return randomArray;
    }

    public static void main(String[] args) {
        // 测试数据
        int[] arrayTest1 = {1,0,2};
        Tom t = new Tom();
        t.guess(arrayTest1);
    }
}
posted @ 2021-11-07 10:46  难删亦删  阅读(152)  评论(0)    收藏  举报