自学Java打卡第2Day

学习目标:

一星期学完面向对象


学习时间:

下午 5点-下午 8 点


学习产出:

练习1:

定义数组存储3个商品对象。

商品属性:商品id,名字,价格,库存。

创建三个商品对象,并把商品对象存到数组中。

public class Goods {
    private String id;
    private String name;
    private double price;
    private int count;

    public Goods() {
    }

    public Goods(String id, String name, double price, int count) {
        this.id = id;
        this.name = name;
        this.price = price;
        this.count = count;
    }

    public String getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

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

    public double getPrice() {
        return price;
    }

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

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }
}
public class GoodsTest {
    public static void main(String[] args) {
        //1.创建一个数组
        Goods[] arr=new Goods[3];
        //2.创建三个商品对象
        Goods g1=new Goods("001","小米11",4399,100);
        Goods g2=new Goods("002","乐事意大利红烩",8,1000);
        Goods g3=new Goods("003","恩西玛",30,200);
        //3.把商品添加到数组里
        arr[0]=g1;
        arr[1]=g2;
        arr[2]=g3;

        //4.遍历
        for (int i = 0; i < arr.length; i++) {
            Goods goods=arr[i];
            System.out.println(goods.getId()+","+goods.getName()+","+goods.getPrice()+","+goods.getCount());
        }
    }
}
001,小米11,4399.0,100
002,乐事意大利红烩,8.0,1000
003,恩西玛,30.0,200

练习2:

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

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

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

public class Car {
    private String name;
    private double price;
    private String color;

    public Car() {
    }

    public Car(String name, double price, String color) {
        this.name = name;
        this.price = price;
        this.color = color;
    }

    public String getName() {
        return name;
    }

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

    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;
    }
}
import java.util.Scanner;

public class CatTest {
    public static void main(String[] args) {
        Scanner scan=new Scanner(System.in);
        Car[] arr=new Car[4];
        for (int i = 1; i < arr.length; i++) {
            System.out.println("请输入第"+i+"个数据:");
            //创建汽车对象
            Car c= new Car();
            System.out.print("品牌:");
            c.setName(scan.next());
            System.out.print("价格:");
            c.setPrice(scan.nextDouble());
            System.out.print("颜色:");
            c.setColor(scan.next());
            arr[i]=c;
        }
        for (int i = 1; i <arr.length ; i++) {
            Car car=arr[i];
            System.out.println("品牌:"+car.getName()+"价格:"+car.getPrice()+"颜色:"+car.getColor());
        }
    }
}
请输入第1个数据:
品牌:大众
价格:120000
颜色:黑色
请输入第2个数据:
品牌:保时捷
价格:1200000
颜色:银灰色
请输入第3个数据:
品牌:劳斯莱斯
价格:3000000
颜色:芭比粉
品牌:大众价格:120000.0颜色:黑色
品牌:保时捷价格:1200000.0颜色:银灰色
品牌:劳斯莱斯价格:3000000.0颜色:芭比粉

练习3:

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

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

要求:计算三部手机的平均价格

public class Phone {
    private String name;
    private double price;
    private String color;

    public Phone() {
    }

    public Phone(String name, double price, String color) {
        this.name = name;
        this.price = price;
        this.color = color;
    }

    public String getName() {
        return name;
    }

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

    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 PhoneTest {
    public static void main(String[] args) {
        //创建手机对象
        Phone[] arr=new Phone[3];
        Phone p1=new Phone("小米",1999,"白色");
        Phone p2=new Phone("华为",4999,"蓝色");
        Phone p3=new Phone("oppo",2999,"黑色");
        //把手机对象添加到数组中
        arr[0]=p1;
        arr[1]=p2;
        arr[2]=p3;
        //获取三部手机的价格
        double sum=0;
        for (int i = 0; i < arr.length; i++) {
            Phone phone=arr[i];
            sum+=phone.getPrice();
        }
        //求平均值,保留小数点后1位
        double avg=sum/arr.length;
        System.out.printf("%.1f",avg);
    }
}
3332.3

练习4:

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

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

要求1:计算出4个女朋友的平均年龄

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

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;
    }
}
public class GirlFriendTest {
    public static void main(String[] args) {
        GirlFriend[] arr=new GirlFriend[4];
        GirlFriend gf1=new GirlFriend("小丹丹",18,"萌妹子","吃零食");
        GirlFriend gf2=new GirlFriend("小理理",19,"萌妹子","旅游");
        GirlFriend gf3=new GirlFriend("小慧慧",21,"萌妹子","玩游戏");
        GirlFriend gf4=new GirlFriend("小团团",22,"萌妹子","看电影");

        arr[0]=gf1;
        arr[1]=gf2;
        arr[2]=gf3;
        arr[3]=gf4;

        int sum=0;
        for (int i = 0; i < arr.length; i++) {
            GirlFriend gf = arr[i];
            sum+=gf.getAge();
        }

        int avg=sum/ arr.length;

        int count=0;
        for (int i = 0; i < arr.length; i++) {
            GirlFriend gf = arr[i];
            if(gf.getAge()<avg){
                count++;
                System.out.println(gf.getName());

            }
        }
        System.out.println("一共有"+count+"个女朋友");
    }
}
小丹丹
小理理
一共有2个女朋友

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值