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

文章展示了Java编程中的类定义、对象实例化、数组操作以及如何计算平均值。分别介绍了phone类和girl类,以及处理学生类的实例,包括添加、删除和修改操作。
摘要由CSDN通过智能技术生成

代码:

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

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

运行结果:

代码:

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+"个");

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

代码结果:

自己写的代码:

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


    }
}
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、给指定那个学号的学生年龄加一

  • 48
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值