Java 面向对象案例 03(黑马)

代码:

java 复制代码
public class phoneTest {
    public static void main(String[] args) {

        phone [] arr = new phone[3];

        phone p1 = new phone("华为",6999,"白色");
        phone p2 = new phone("vivo",4999,"蓝色");
        phone p3 = new phone("苹果",7999,"黑色");

        arr[0]=p1;
        arr[1]=p2;
        arr[2]=p3;

        double avg = (p1.getPrice()+ p2.getPrice()+p3.getPrice())/3;
        System.out.println(avg);

    }
}
java 复制代码
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;
    }

    public String getBrand() {
        return brand;
    }

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

    public double getPrice() {
        return price;
    }

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

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }
}

运行结果:

代码:

java 复制代码
public class girlTest {
    public static void main(String[] args) {
        girl [] arr = new girl[4];

        girl g1 = new girl("小红",18,'女',"跳舞");
        girl g2 = new girl("小芳",22,'女',"唱歌");
        girl g3 = new girl("小刘",23,'女',"骑车");
        girl g4 = new girl("小美",19,'女',"爬山");

        arr[0]=g1;
        arr[1]=g2;
        arr[2]=g3;
        arr[3]=g4;

        double sum=0;
        for(int i=0;i<arr.length;i++){
            girl girl=arr[i];
            sum+=girl.getOld();
        }
        double avg = sum/arr.length;
        System.out.println(avg);
        int count=0;//统计思想

        for(int i=0;i<arr.length;i++){

            girl girl=arr[i];
            if(girl.getOld()<avg) {
                count++;
                System.out.println(girl.getName() + "," + girl.getOld() + "," + girl.getHobby() + "," + girl.getSex());
            }
        }
        System.out.println("有"+count+"个");

    }
}
java 复制代码
public class girl {
    private String name;
    private int old;
    private char sex;
    private String hobby;

    public girl() {
    }

    public girl(String name, int old, char sex, String hobby) {
        this.name = name;
        this.old = old;
        this.sex = sex;
        this.hobby = hobby;
    }

    public String getName() {
        return name;
    }

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

    public int getOld() {
        return old;
    }

    public void setOld(int old) {
        this.old = old;
    }

    public char getSex() {
        return sex;
    }

    public void setSex(char sex) {
        this.sex = sex;
    }

    public String getHobby() {
        return hobby;
    }

    public void setHobby(String hobby) {
        this.hobby = hobby;
    }
}

代码结果:

自己写的代码:

java 复制代码
import java.sql.SQLOutput;
import java.util.Scanner;
public class studentTest {
    public static void main(String[] args) {

        Scanner input = new Scanner (System.in);
        student [] arr = new student[3];

        student s1 = new student("heima001","小白",19);
        student s2 = new student("heima002","小黑",19);
        student s3 = new student();

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

        System.out.println("输入第3个同学的学号:");

        boolean flag = true;
        do {
            String n = input.next();
            if ((!n.equals(s1.getNov())) && (!n.equals(s2.getNov()))) {
                s3.setNov(n);
                flag=false;
            } else {
                System.out.println("该用户已存在,输入失败,请重新输入:");
            }
        }while(flag);
        System.out.println("输入第3个同学的名字:");
        String m = input.next();
        s3.setName(m);
        System.out.println("输入第3个同学的年龄:");
        int q = input.nextInt();
        s3.setAge(q);

        for(int i=0;i<arr.length;i++){
            student student = arr[i];
            System.out.println(student.getNov()+","+student.getName()+","+student.getAge());
        }
        System.out.println("请输入要删除的学生的学号:");
        String p = input.next();
        int count =0;
        for(int j=0;j< arr.length;j++){
            student student = arr[j];
            if(p.equals(student.getNov())) {
                student.setAge(0);
                student.setName(null);
                student.setNov(null);
                System.out.println("删除成功");
                break;
            }else{
                count++;
            }
            if(count==arr.length){
                System.out.println("删除失败");
            }


        }
        for(int i=0;i<arr.length;i++){
            student student = arr[i];
            System.out.println(student.getNov()+","+student.getName()+","+student.getAge());
        }

        for(int i=0;i<arr.length;i++){
            student student = arr[i];
            if(student.getNov()=="heima002"){
               int age =student.getAge()+1;
               student.setAge(age);
                System.out.println(student.getAge());
            }
            //System.out.println(student.getNov()+","+student.getName()+","+student.getAge());
        }


    }
}
java 复制代码
public class student {
    private String Nov;
    private String name;
    private int age;

    public student() {
    }

    public student(String nov, String name, int age) {
        Nov = nov;
        this.name = name;
        this.age = age;
    }

    public String getNov() {
        return Nov;
    }

    public void setNov(String nov) {
        Nov = nov;
    }

    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;
    }
}

缺少如果数组满了要添加对象进去的判断:

1、判断id是否重复:

2、判断数组是否已满:

3、如果数组已经满了,那么创建一个新数组,并且将老数组里的元素复制进去:

4、进行添加和判断操作

5、打印元素的时候,需要分两种情况讨论,一个是添加成功的数组,一个是没有添加成功的数组,可以写一个方法:

6、在上面调用遍历的方法:

若数组不满,删除stu3,那么在数组中事null,但是不能通过null直接使用遍历,相当于用null调用了其他方法,会爆粗,

所以对判断唯一性的方法进行修改:需要判断stu是否为null

补充:拆分题干:

7、对于判断id写一个方法:

8、删除id并遍历:

9、给指定那个学号的学生年龄加一

相关推荐
福大大架构师每日一题几秒前
rust 1.92.0 更新详解:语言特性增强、编译器优化与全新稳定API
java·javascript·rust
xiaogc_a4 分钟前
【无标题】
java
m5655bj5 分钟前
使用 C# 设置 Word 段落对齐样式
开发语言·c#·word
福尔摩斯张7 分钟前
基于TCP的FTP文件传输系统设计与实现(超详细)
linux·开发语言·网络·网络协议·tcp/ip·udp
源码技术栈10 分钟前
智慧工地微服务架构+Java+Spring Cloud +Uni-App +MySql开发,在微信公众号、小程序、H5、移动端
java·ai·saas·智慧工地·智慧工地项目·可视化大屏·智慧工地系统
技术净胜11 分钟前
MATLAB 环境搭建与认知实战教程:从下载安装到入门全解析教程
开发语言·matlab
爱吃大芒果11 分钟前
Flutter 自定义 Widget 开发:从基础绘制到复杂交互
开发语言·javascript·flutter·华为·ecmascript·交互
老华带你飞13 分钟前
健身房预约|基于springboot 健身房预约小程序系统(源码+数据库+文档)
java·数据库·vue.js·spring boot·后端·小程序
帅得不敢出门17 分钟前
MTK Android11 APP调用OTA升级
android·java·开发语言·framework
Swift社区18 分钟前
用 Task Local Values 构建 Swift 里的依赖容器:一种更轻量的依赖注入思路
开发语言·ios·swift