fanjialong hace 1 mes
padre
commit
528578c840

+ 11 - 0
java-base-project10/day10/day10.iml

@@ -0,0 +1,11 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<module type="JAVA_MODULE" version="4">
+  <component name="NewModuleRootManager" inherit-compiler-output="true">
+    <exclude-output />
+    <content url="file://$MODULE_DIR$">
+      <sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
+    </content>
+    <orderEntry type="inheritedJdk" />
+    <orderEntry type="sourceFolder" forTests="false" />
+  </component>
+</module>

+ 69 - 0
java-base-project10/day10/src/_01_对象数组/Car.java

@@ -0,0 +1,69 @@
+package _01_对象数组;
+
+public class Car {
+    private String brand;
+    private String color;
+    private double price;
+
+
+    public Car() {
+    }
+
+    public Car(String brand, String color, double price) {
+        this.brand = brand;
+        this.color = color;
+        this.price = price;
+    }
+
+    /**
+     * 获取
+     * @return brand
+     */
+    public String getBrand() {
+        return brand;
+    }
+
+    /**
+     * 设置
+     * @param brand
+     */
+    public void setBrand(String brand) {
+        this.brand = brand;
+    }
+
+    /**
+     * 获取
+     * @return color
+     */
+    public String getColor() {
+        return color;
+    }
+
+    /**
+     * 设置
+     * @param color
+     */
+    public void setColor(String color) {
+        this.color = color;
+    }
+
+    /**
+     * 获取
+     * @return price
+     */
+    public double getPrice() {
+        return price;
+    }
+
+    /**
+     * 设置
+     * @param price
+     */
+    public void setPrice(double price) {
+        this.price = price;
+    }
+
+    public String toString() {
+        return "Car{brand = " + brand + ", color = " + color + ", price = " + price + "}";
+    }
+}

+ 25 - 0
java-base-project10/day10/src/_01_对象数组/CarTest.java

@@ -0,0 +1,25 @@
+package _01_对象数组;
+
+import java.util.Scanner;
+
+public class CarTest {
+    public static void main(String[] args) {
+        Car[] cars = new Car[3];
+        Scanner scanner = new Scanner(System.in);
+        for (int i = 0; i < cars.length; i++) {
+            System.out.println("请录入品牌");
+            String brand = scanner.next();
+            System.out.println("请录入价格");
+            double price  = scanner.nextDouble();
+            System.out.println("请录入颜色");
+            String color = scanner.next();
+            Car car = new Car(brand,color,price);
+            cars[i] = car;
+        }
+
+        // 打印数组看俩面元素内容
+        for (Car car : cars) {
+            System.out.println(car);
+        }
+    }
+}

+ 86 - 0
java-base-project10/day10/src/_01_对象数组/GirlFriend.java

@@ -0,0 +1,86 @@
+package _01_对象数组;
+
+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;
+    }
+
+    /**
+     * 获取
+     * @return name
+     */
+    public String getName() {
+        return name;
+    }
+
+    /**
+     * 设置
+     * @param name
+     */
+    public void setName(String name) {
+        this.name = name;
+    }
+
+    /**
+     * 获取
+     * @return age
+     */
+    public int getAge() {
+        return age;
+    }
+
+    /**
+     * 设置
+     * @param age
+     */
+    public void setAge(int age) {
+        this.age = age;
+    }
+
+    /**
+     * 获取
+     * @return gender
+     */
+    public String getGender() {
+        return gender;
+    }
+
+    /**
+     * 设置
+     * @param gender
+     */
+    public void setGender(String gender) {
+        this.gender = gender;
+    }
+
+    /**
+     * 获取
+     * @return hobby
+     */
+    public String getHobby() {
+        return hobby;
+    }
+
+    /**
+     * 设置
+     * @param hobby
+     */
+    public void setHobby(String hobby) {
+        this.hobby = hobby;
+    }
+
+    public String toString() {
+        return "GirlFriend{name = " + name + ", age = " + age + ", gender = " + gender + ", hobby = " + hobby + "}";
+    }
+}

+ 26 - 0
java-base-project10/day10/src/_01_对象数组/GirlFriendTest.java

@@ -0,0 +1,26 @@
+package _01_对象数组;
+
+public class GirlFriendTest {
+
+    public static void main(String[] args) {
+        GirlFriend[] girlFriends = new GirlFriend[4];
+        girlFriends[0] = new GirlFriend("小丽",18,"女","逛街");
+        girlFriends[1] = new GirlFriend("小红",28,"女","吃饭");
+        girlFriends[2] = new GirlFriend("小美",38,"女","打麻将");
+        girlFriends[3] = new GirlFriend("小王",48,"女","逛街");
+
+        int sum = 0;
+        for (GirlFriend girlFriend : girlFriends) {
+            sum += girlFriend.getAge();
+        }
+        int avg = sum / girlFriends.length;
+        int count = 0;
+        System.out.println("平均年龄为:"+ avg);
+        for (GirlFriend girlFriend : girlFriends) {
+            if(girlFriend.getAge() < avg){
+                count++;
+                System.out.println(girlFriend);
+            }
+        }
+    }
+}

+ 68 - 0
java-base-project10/day10/src/_01_对象数组/Phone.java

@@ -0,0 +1,68 @@
+package _01_对象数组;
+
+public class Phone {
+    private String brand;
+    private double price;
+    private String color;
+
+    public Phone() {
+    }
+
+    public Phone(String brand, double price, String color) {
+        this.brand = brand;
+        this.price = price;
+        this.color = color;
+    }
+
+    /**
+     * 获取
+     * @return brand
+     */
+    public String getBrand() {
+        return brand;
+    }
+
+    /**
+     * 设置
+     * @param brand
+     */
+    public void setBrand(String brand) {
+        this.brand = brand;
+    }
+
+    /**
+     * 获取
+     * @return price
+     */
+    public double getPrice() {
+        return price;
+    }
+
+    /**
+     * 设置
+     * @param price
+     */
+    public void setPrice(double price) {
+        this.price = price;
+    }
+
+    /**
+     * 获取
+     * @return color
+     */
+    public String getColor() {
+        return color;
+    }
+
+    /**
+     * 设置
+     * @param color
+     */
+    public void setColor(String color) {
+        this.color = color;
+    }
+
+    public String toString() {
+        return "Phone{brand = " + brand + ", price = " + price + ", color = " + color + "}";
+    }
+}

+ 17 - 0
java-base-project10/day10/src/_01_对象数组/PhoneTest.java

@@ -0,0 +1,17 @@
+package _01_对象数组;
+
+public class PhoneTest {
+    public static void main(String[] args) {
+        Phone[] phones = new Phone[3];
+        phones[0] = new Phone("小米",1000,"红色");
+        phones[1] = new Phone("华为",2000,"红色");
+        phones[2] = new Phone("苹果",3000,"红色");
+
+        double sum = 0;
+        for (Phone phone : phones) {
+            sum += phone.getPrice();
+        }
+        System.out.println("总和为:"+ sum);
+        System.out.println("平均值为:" +(sum / phones.length));
+    }
+}

+ 69 - 0
java-base-project10/day10/src/_02_复杂对象练习/Student.java

@@ -0,0 +1,69 @@
+package _02_复杂对象练习;
+
+public class Student {
+    private Long id;
+    private String name;
+    private int age;
+
+
+    public Student() {
+    }
+
+    public Student(Long id, String name, int age) {
+        this.id = id;
+        this.name = name;
+        this.age = age;
+    }
+
+    /**
+     * 获取
+     * @return id
+     */
+    public Long getId() {
+        return id;
+    }
+
+    /**
+     * 设置
+     * @param id
+     */
+    public void setId(Long id) {
+        this.id = id;
+    }
+
+    /**
+     * 获取
+     * @return name
+     */
+    public String getName() {
+        return name;
+    }
+
+    /**
+     * 设置
+     * @param name
+     */
+    public void setName(String name) {
+        this.name = name;
+    }
+
+    /**
+     * 获取
+     * @return age
+     */
+    public int getAge() {
+        return age;
+    }
+
+    /**
+     * 设置
+     * @param age
+     */
+    public void setAge(int age) {
+        this.age = age;
+    }
+
+    public String toString() {
+        return "Student{id = " + id + ", name = " + name + ", age = " + age + "}";
+    }
+}

+ 94 - 0
java-base-project10/day10/src/_02_复杂对象练习/StudentTest.java

@@ -0,0 +1,94 @@
+package _02_复杂对象练习;
+
+public class StudentTest {
+    public static void main(String[] args) {
+        //1 创建出来一个学生数组
+        Student[] students = new Student[3];
+        //2 可能是2 肯呢个是1  可能3个
+        students[0] = new Student(01L,"zhangsan",18);
+        students[1] = new Student(02L,"lisi",18);
+        students[2] = new Student(03L,"zhaoliu",18);
+        // 要添加学生
+        Student student = new Student(04L,"wangwu",20);
+        // 把student 添加到数组中
+        // 在添加之前需要判断这个添加学生学号是否在数组当中已经存在
+        // 如果不存在就可以添加, 如果存在提醒学号已经是存在了
+        boolean flag = contains(student.getId(),students);
+        if(flag){
+            System.out.println("添加学号已经存在了");
+        }else{
+            System.out.println("添加学号不存在可以正常添加");
+            // 判断数组是否有存满
+            /**
+             * 问题:如何判断数组有没有存满
+             * 如果数组当中不为空的数量 和数组长度相同的说明 数组已经存满了
+             * 如果数组党总不为空的数量小于数组的长度说明没有满
+             */
+            int count = getElementNotNullInArrayCount(students);
+            if(count == students.length){
+                System.out.println("数组已经满了");
+                // 创建一个新的数组, 新的数组长度是之前数组长度+1
+                Student[] newStus = new Student[students.length+1];
+                // 把之前数组内容放到新的数组中
+                for (int i = 0; i < students.length; i++) {
+                    // newStus[0] = students[0]
+                    // newStus[1]= students[1]
+                    // newStus[2] = students[2]
+                    // newStus[3] = students[3]
+                    newStus[i] = students[i];
+                }
+                // 把添加的元素放到数组当中最后一个位置
+                newStus[newStus.length-1] = student;
+                // 打印数组
+                printArr(newStus);
+            }else{
+                System.out.println("数组还没有满");
+                // 直接把要添加元素放到count索引位置就可以了
+                students[count] = student;
+                // 打印数组当中元素内容
+                printArr(students);
+            }
+        }
+    }
+
+    /**
+     * 打印数组
+     * @param students
+     */
+    private static void printArr(Student[] students) {
+        for (Student student : students) {
+            System.out.println(student);
+        }
+    }
+
+    private static int getElementNotNullInArrayCount(Student[] students) {
+        // 不为空数量为0
+        int count = 0;
+        for (Student student : students) {
+            if(student!=null){
+                count++;
+            }
+        }
+        return count;
+    }
+    /**
+     * 判断传入学号是否在数组当中已经存在了
+     * 返回值true 表示学号已经在数组当中存在了
+     * 返回值false 表示学号是在数组当中不存在
+     */
+    private static boolean contains(Long id, Student[] students) {
+        /**
+         * NullPointerException : 空指针异常
+         * 原因: 使用null 去调用了属性或者是方法
+         */
+        for (Student student : students) {
+            // 判断当前student 不为哦那个做id 比较, 因为数组当中如果没存满,可能为空null
+            if(student!=null){
+                if(student.getId() == id){
+                    return true;
+                }
+            }
+        }
+        return false;
+    }
+}

+ 62 - 0
java-base-project10/day10/src/_02_复杂对象练习/StudentTest1.java

@@ -0,0 +1,62 @@
+package _02_复杂对象练习;
+
+public class StudentTest1 {
+    public static void main(String[] args) {
+        //1 创建出来一个学生数组
+        Student[] students = new Student[3];
+        students[0] = new Student(1L,"zhangsan",18);
+        students[1] = new Student(2L,"lisi",18);
+        students[2] = new Student(3L,"wangwu",18);
+
+        // 根据id 查询是否存在
+        boolean flag = contains(1L, students);
+        if(flag){
+            System.out.println("删除id存在能够正常删除");
+            // 把删除元素锁在位置的值修改成Null
+            removeElementById(1L,students);
+            // 打印数组当中元素
+            printArr(students);
+        }else{
+            System.out.println("删除id 在数组当中不存在");
+        }
+    }
+    /**
+     * 打印数组
+     * @param students
+     */
+    private static void printArr(Student[] students) {
+        for (Student student : students) {
+            if(student!=null){
+                System.out.println(student);
+            }
+        }
+    }
+    /**
+     * 把id 锁在位置的值设置成null
+     */
+    private static void removeElementById(long id, Student[] students) {
+        for (int i = 0; i < students.length; i++) {
+            Student student = students[i];
+            // 注意我们拿传递id  和 student 当中id 进行比较
+            if(id == student.getId()){
+                students[i] = null;
+            }
+        }
+    }
+
+    private static boolean contains(Long id, Student[] students) {
+        /**
+         * NullPointerException : 空指针异常
+         * 原因: 使用null 去调用了属性或者是方法
+         */
+        for (Student student : students) {
+            // 判断当前student 不为哦那个做id 比较, 因为数组当中如果没存满,可能为空null
+            if(student!=null){
+                if(student.getId() == id){
+                    return true;
+                }
+            }
+        }
+        return false;
+    }
+}

+ 49 - 0
java-base-project10/day10/src/_02_复杂对象练习/StudentTest2.java

@@ -0,0 +1,49 @@
+package _02_复杂对象练习;
+
+import java.util.Scanner;
+
+public class StudentTest2 {
+    public static void main(String[] args) {
+        Student[] students = new Student[3];
+        students[0] = new Student(1L,"zhangsan",18);
+        students[1] = new Student(2L,"lisi",18);
+        students[2] = new Student(3L,"wangwu",18);
+
+        Scanner scanner = new Scanner(System.in);
+        System.out.println("请录入要查找学号");
+        long studentNumber = scanner.nextLong();
+        // 判断查找id 在数组当中是否存在
+        boolean flag = contains(studentNumber, students);
+        if(flag){
+            // 找到id 锁在位置
+            for (int i = 0; i < students.length; i++) {
+                Student students1 = students[i];
+                if(students1.getId() == studentNumber){
+                    // 如果相同说明这个学生就是我们要找的血行
+                    // 让学生年龄+1
+                    students1.setAge(students1.getAge() + 1);
+                    System.out.println(students1);
+                }
+            }
+        }else{
+            System.out.println("查找id 不存在");
+        }
+    }
+
+
+    private static boolean contains(Long id, Student[] students) {
+        /**
+         * NullPointerException : 空指针异常
+         * 原因: 使用null 去调用了属性或者是方法
+         */
+        for (Student student : students) {
+            // 判断当前student 不为哦那个做id 比较, 因为数组当中如果没存满,可能为空null
+            if(student!=null){
+                if(student.getId() == id){
+                    return true;
+                }
+            }
+        }
+        return false;
+    }
+}

+ 76 - 0
java-base-project10/day10/src/_03_static/Student.java

@@ -0,0 +1,76 @@
+package _03_static;
+
+public class Student {
+    private String name;
+    private int age;
+
+    private static String teacherName = "fanjialong";
+
+
+
+    public Student() {
+    }
+
+    public Student(String name, int age, String teacherName) {
+        this.name = name;
+        this.age = age;
+        this.teacherName = teacherName;
+    }
+
+
+    public void study(){
+        System.out.println("学生学习方法");
+    }
+
+    /**
+     * 获取
+     * @return name
+     */
+    public String getName() {
+        return name;
+    }
+
+    /**
+     * 设置
+     * @param name
+     */
+    public void setName(String name) {
+        this.name = name;
+    }
+
+    /**
+     * 获取
+     * @return age
+     */
+    public int getAge() {
+        return age;
+    }
+
+    /**
+     * 设置
+     * @param age
+     */
+    public void setAge(int age) {
+        this.age = age;
+    }
+
+    /**
+     * 获取
+     * @return teacherName
+     */
+    public String getTeacherName() {
+        return teacherName;
+    }
+
+    /**
+     * 设置
+     * @param teacherName
+     */
+    public void setTeacherName(String teacherName) {
+        this.teacherName = teacherName;
+    }
+
+    public String toString() {
+        return "Student{name = " + name + ", age = " + age + ", teacherName = " + teacherName + "}";
+    }
+}

+ 18 - 0
java-base-project10/day10/src/_03_static/StudentTest.java

@@ -0,0 +1,18 @@
+package _03_static;
+
+public class StudentTest {
+    public static void main(String[] args) {
+        Student student = new Student();
+        student.setAge(10);
+        student.setName("zhangsan");
+
+        Student student1 = new Student();
+        student1.setAge(10);
+        student1.setName("lisi");
+
+        System.out.println(student);
+        System.out.println(student1);
+
+
+    }
+}