Java-黑马Java学习作业-day09面向对象(综合练习)

学习视频链接:https://www.bilibili.com/video/BV17F411T7Ao

文章目录

    • 题目1-对象数组汽车
    • 题目2-对象数组手机
    • 题目3-对象数组女朋友
    • 题目4-复杂对象数组操作


题目1-对象数组汽车

​ 定义数组存储3部汽车对象。

​ 汽车的属性:品牌,价格,颜色。

​ 创建三个汽车对象,数据通过键盘录入而来,并把数据存入到数组当中。

  • Car类
package cn.kox.homework01;

/**
 * @ClassName: Car
 * @Author: Kox
 * @Data: 2023/1/19
 * @Sketch:
 */
public class Car {
    private String brand;
    private int price;
    private String color;

    public Car() {
    }

    public Car(String brand, int price, String color) {
        this.brand = brand;
        this.price = price;
        this.color = color;
    }

    public String getBrand() {
        return brand;
    }

    public void setBrand(String brand) {
        this.brand = brand;
    }

    public int getPrice() {
        return price;
    }

    public void setPrice(int price) {
        this.price = price;
    }

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }
}
  • 测试类
package cn.kox.homework01;

import java.util.Scanner;

/**
 * @ClassName: Test
 * @Author: Kox
 * @Data: 2023/1/19
 * @Sketch: 题目1
 */
public class Test {
    public static void main(String[] args) {
        Car[] arr = new Car[3];
        Scanner sc = new Scanner(System.in);
        for (int i = 0; i < arr.length; i++) {
            Car c = new Car();
            System.out.printf("请输入第%s个汽车的品牌:", i + 1);
            String brand = sc.next();
            c.setBrand(brand);
            System.out.printf("请输入第%s个汽车的价格:", i + 1);
            int price = sc.nextInt();
            c.setPrice(price);
            System.out.printf("请输入第%s个汽车的颜色:", i + 1);
            String color = sc.next();
            c.setColor(color);
            arr[i] = c;
        }
        for (int i = 0; i < arr.length; i++) {
            Car car = arr[i];
            System.out.printf("%s,%s,%s", car.getBrand(), car.getPrice(), car.getColor());
            System.out.println();
        }
    }
}

题目2-对象数组手机

​ 定义数组存储3部手机对象。

​ 手机的属性:品牌,价格,颜色。

​ 要求,计算出三部手机的平均价格

  • Phone类
package cn.kox.homework02;

/**
 * @ClassName: Phone
 * @Author: Kox
 * @Data: 2023/1/19
 * @Sketch:
 */
public class Phone {
    private String brand;
    private int price;
    private String color;

    public Phone() {
    }

    public Phone(String brand, int price, String color) {
        this.brand = brand;
        this.price = price;
        this.color = color;
    }

    public String getBrand() {
        return brand;
    }

    public void setBrand(String brand) {
        this.brand = brand;
    }

    public int getPrice() {
        return price;
    }

    public void setPrice(int price) {
        this.price = price;
    }

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }
}
  • 测试类
package cn.kox.homework02;

/**
 * @ClassName: Test
 * @Author: Kox
 * @Data: 2023/1/19
 * @Sketch: 题目2
 */
public class Test {
    public static void main(String[] args) {
        Phone p1 = new Phone("小米", 100, "白色");
        Phone p2 = new Phone("苹果", 150, "蓝色");
        Phone p3 = new Phone("红米", 50, "红色");

        Phone[] arr = new Phone[3];
        arr[0] = p1;
        arr[1] = p2;
        arr[2] = p3;

        int sum = 0;
        for (int i = 0; i < arr.length; i++) {
            Phone phone = arr[i];
            sum += phone.getPrice();
        }

        int avg = sum / arr.length;
        System.out.println(avg);

    }
}

题目3-对象数组女朋友

​ 定义数组存储4个女朋友的对象

​ 女朋友的属性:姓名、年龄、性别、爱好

​ 要求1:计算出四女朋友的平均年龄

​ 要求2:统计年龄比平均值低的女朋友有几个?并把她们的所有信息打印出来。

  • Girlfriend类
package cn.kox.homework03;

/**
 * @ClassName: Girlfriend
 * @Author: Kox
 * @Data: 2023/1/19
 * @Sketch:
 */
public class Girlfriend {
    private String name;
    private int age;
    private String gender;
    private String hobby;

    public Girlfriend() {
    }

    public Girlfriend(String name, int age, String gender, String hobby) {
        this.name = name;
        this.age = age;
        this.gender = gender;
        this.hobby = hobby;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public String getHobby() {
        return hobby;
    }

    public void setHobby(String hobby) {
        this.hobby = hobby;
    }
}
  • 测试类
package cn.kox.homework03;

/**
 * @ClassName: Test
 * @Author: Kox
 * @Data: 2023/1/19
 * @Sketch:
 */
public class Test {
    public static void main(String[] args) {
        Girlfriend[] arr = new Girlfriend[4];

        Girlfriend g1 = new Girlfriend("a", 1, "女", "aa");
        Girlfriend g2 = new Girlfriend("b", 2, "女", "bb");
        Girlfriend g3 = new Girlfriend("c", 3, "女", "cc");
        Girlfriend g4 = new Girlfriend("d", 4, "女", "dd");
        arr[0] = g1;
        arr[1] = g2;
        arr[2] = g3;
        arr[3] = g4;

        int sum = 0;
        for (int i = 0; i < arr.length; i++) {
            Girlfriend girlfriend = arr[i];
            sum += girlfriend.getAge();
        }
        int avg = sum / arr.length;
        int count = 0;
        for (int i = 0; i < arr.length; i++) {
            Girlfriend girlfriend = arr[i];
            if (girlfriend.getAge() < avg) {
                System.out.printf("name:%s, age:%s, gender:%s, hobby:%s", girlfriend.getName(), girlfriend.getAge(), girlfriend.getGender(), girlfriend.getHobby());
                System.out.println();
                count++;
            }
        }
        System.out.println(count);
    }
}

题目4-复杂对象数组操作

​ 定义数组存储3个学生对象。

​ 学生的属性:学号,姓名,年龄。

​ 要求1:添加的时候需要进行学号的唯一性判断。

​ 要求2:添加完毕之后,遍历所有学生信息。

​ 要求3:通过id删除学生信息

​ 如果存在,则删除,如果不存在,则提示删除失败。

​ 要求4:删除完毕之后,遍历所有学生信息。

​ 要求5:id为2的学生,年龄+1岁

  • Student类
package cn.kox.homework04;

/**
 * @ClassName: Student
 * @Author: Kox
 * @Data: 2023/1/19
 * @Sketch:
 */
public class Student {
    private int id;
    private String name;
    private int age;

    public Student() {
    }

    public Student(int id, String name, int age) {
        this.id = id;
        this.name = name;
        this.age = age;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}
  • 测试类
package cn.kox.homework04;

/**
 * @ClassName: Test
 * @Author: Kox
 * @Data: 2023/1/19
 * @Sketch:
 */
public class Test {
    public static void main(String[] args) {
        Student[] arr = new Student[3];
        Student s1 = new Student(1, "a", 18);
        Student s2 = new Student(2, "b", 19);
        Student s3 = new Student(3, "c", 20);

        arr[0] = s1;
        arr[1] = s2;
        arr[2] = s3;

        System.out.println("---分割线(添加学生对象)---");
        // 添加学生对象
        Student s4 = new Student(4, "d", 21);
        boolean flag = idJudge(arr, s4.getId());
        if (flag) {
            System.out.println("学号重复");
        } else {
            int index = arrJudge(arr);
            if (index == arr.length) {
                Student[] newArr = creatArr(arr);
                newArr[index] = s4;
                printArr(newArr);
            } else {
                arr[index] = s4;
                printArr(arr);
            }
        }
        System.out.println("---分割线(根据id删除信息)---");
        // 通过id删除学生对象
        int delId = 3;
        int delIndex = getIndex(arr, delId);
        if (delIndex > -1) {
            arr[delIndex] = null;
        } else {
            System.out.println("没有这个学号删除失败!");
        }
        printArr(arr);

        System.out.println("---分割线(查询id加年龄)---");
        int selectId = 2;
        int selectIndex = getIndex(arr, selectId);
        if (selectIndex > -1) {
            arr[selectIndex].setAge(arr[selectIndex].getAge() + 1);
        } else {
            System.out.println("不存在!");
        }
        printArr(arr);
    }


    public static boolean idJudge(Student[] arr, int id) {
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] != null) {
                if (arr[i].getId() == id) {
                    return true;
                }
            }
        }
        return false;
    }

    public static int arrJudge(Student[] arr) {
        int count = 0;
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] != null) {
                count++;
            }
        }
        return count;
    }

    public static Student[] creatArr(Student[] arr) {
        Student[] newArr = new Student[arr.length + 1];
        for (int i = 0; i < arr.length; i++) {
            newArr[i] = arr[i];
        }
        return newArr;
    }

    public static void printArr(Student[] arr) {
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] != null) {
                System.out.printf("id: %s, name: %s, age: %s", arr[i].getId(), arr[i].getName(), arr[i].getAge());
                System.out.println();
            }
        }
    }

    public static int getIndex(Student[] arr, int id) {
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] != null) {
                if (arr[i].getId() == id) {
                    return i;
                }
            }
        }
        return -1;
    }

}

你可能感兴趣的:(Java,SE,java,学习,开发语言,算法)