一、练习五:对象数组(手机)
需求 : 定义数组存储3部手机对象。
手机的属性:品牌,价格,颜色。
要求,计算出三部手机的平均价格
代码示例
Phone.java
package com.itheima.test6;
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;
}
}
PhoneTest.java
package com.itheima.test6;
import java.math.BigDecimal;
public class PhoneTest {
public static void main(String[] args) {
//1.创建一个数组
Phone[] arr = new Phone[3];
//2.创建手机的对象
Phone p1 = new Phone("小米",1999,"白色");
Phone p2 = new Phone("华为",4999,"蓝色");
Phone p3 = new Phone("魅族",3999,"红色");
//3.把手机对象添加到数组当中
arr[0] = p1;
arr[1] = p2;
arr[2] = p3;
//4.获取三部手机的平均价格
int sum = 0;
for (int i = 0; i < arr.length; i++) {
//i表示的是索引 arr[i]表示的是元素(在这里就是手机对象)
Phone phone = arr[i];
sum = sum + phone.getPrice();
}
//5.求平均值
//建议:数据能不写死,尽量不写死
//int avg = sum / arr.length; // 这样打印出来的只能是整数
double avg2 = sum * 1.0 / arr.length; // 整数操作只能得到整数,想要得到小数,必须有浮点数参与运算
System.out.println(avg2);//3665.6666666666665,在代码中,如果有小数参与计算,结果有可能不精确的
// 如果想要精确运算,并且小数点后面想要保留两位、四舍五入等等,使用 BigDecimal 就可以实现精确运算了
}
}
二、练习六:对象数组(女朋友)
需求:定义数组存储4个女朋友的对象。女朋友的属性:姓名、年龄、性别、爱好
要求1:计算出四女朋友的平均年龄
要求2:统计年龄比平均值低的女朋友有几个?并把她们的所有信息打印出来。
代码示例
GirlFriend.java
package com.itheima.test7;
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;
}
}
GirlFriendTest.java
package com.itheima.test7;
public class GirlFriendTest {
public static void main(String[] args) {
//1.定义数组存入女朋友的对象
GirlFriend[] arr = new GirlFriend[4];
//2.创建女朋友对象
GirlFriend gf1 = new GirlFriend("小诗诗",18,"萌妹子","吃零食");
GirlFriend gf2 = new GirlFriend("小丹丹",19,"萌妹子","玩游戏");
GirlFriend gf3 = new GirlFriend("小惠惠",20,"萌妹子","看书,学习");
GirlFriend gf4 = new GirlFriend("小莉莉",21,"憨妹子","睡觉");
//3.把对象添加到数组当中
arr[0] = gf1;
arr[1] = gf2;
arr[2] = gf3;
arr[3] = gf4;
//4.求和
int sum = 0;
for (int i = 0; i < arr.length; i++) {
//i表示的是索引,arr[i]表示的是数组中的元素(这就指的就是女朋友对象)
GirlFriend gf = arr[i];
//累加
sum = sum + gf.getAge();
}
//5.平均值
int avg = sum / arr.length;
//6.统计年龄比平均值低的有几个,打印他们的信息
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() + ", " + gf.getAge() + ", " + gf.getGender() + ", " + gf.getHobby());
}
}
System.out.println(count + "个");
}
}