Java-对象数组练习2
·
一、案例练习

package com.lkbhua.Test5;
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 void setBrand(String brand) {
this.brand = brand;
}
public String getBrand() {
return brand;
}
public void setPrice(int price) {
this.price = price;
}
public int getPrice() {
return price;
}
public void setColor(String color) {
this.color = color;
}
public String getColor() {
return color;
}
}
package com.lkbhua.Test5;
public class PhoneTest {
public static void main(String[] args) {
// 1、创建一个数组
Phone[] arr = new Phone[3];
// 2、创建手机的对象
Phone phone1 = new Phone("iphone", 8888, "黑色" );
Phone phone2 = new Phone("华为", 6666, "蓝色" );
Phone phone3 = new Phone("小米", 3333, "白色" );
// 3、将手机对象,保存到数组中
arr[0] = phone1;
arr[1] = phone2;
arr[2] = phone3;
// 4、遍历数组,并输出手机信息
for (int i = 0; i < arr.length; i++) {
Phone p = arr[i];
System.out.println(p.getBrand() + ":" + p.getPrice() + ":" + p.getColor());
}
System.out.println("------------------------------------------------");
// 5、获取手机的平均价格
int sum = 0;
for (int i = 0; i < arr.length; i++) {
Phone p = arr[i];
sum = sum + p.getPrice();
}
int avg = sum / arr.length;
System.out.println("平均价格:" + avg);
}
}
二、案例需求2

package com.lkbhua.Test6;
public class Girls {
private String name;
private int age;
private int height;
private int hobby;
public Girls() {
}
public Girls(String name, int age, int height, int hobby) {
this.name = name;
this.age = age;
this.height = height;
this.hobby = hobby;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setAge(int age) {
this.age = age;
}
public int getAge() {
return age;
}
public void setHeight(int height) {
this.height = height;
}
public int getHeight() {
return height;
}
public void setHobby(int hobby) {
this.hobby = hobby;
}
public int getHobby() {
return hobby;
}
}
package com.lkbhua.Test6;
public class GirlsTest {
public static void main(String[] args) {
// 1、定义一个数组
Girls[] arr = new Girls[4];
// 2、创建对象
Girls g1 = new Girls("张三", 18, 170, 1);
Girls g2 = new Girls("李四", 19, 160, 2);
Girls g3 = new Girls("王五", 20, 180, 3);
Girls g4 = new Girls("赵六", 21, 190, 4);
// 3、将创建的对象保存到数组中
arr[0] = g1;
arr[1] = g2;
arr[2] = g3;
arr[3] = g4;
// 4、遍历数组,并输出对象中的数据
for (int i = 0; i < arr.length; i++) {
Girls g = arr[i];
System.out.println(arr[i].getName() + ":" + arr[i].getAge() + ":" + arr[i].getHeight() + ":" + arr[i].getHobby());
}
System.out.println("------------------------------------------------");
// 5、获取数组中元素的平均年龄
int sum = 0;
for (int i = 0; i < arr.length; i++) {
Girls g = arr[i];
sum = sum + g.getAge();
}
int avg = sum / arr.length;
System.out.println("平均年龄为:" + avg);
System.out.println("------------------------------------------------");
// 6、统计有几个年龄比平均年龄小
int count = 0;
for (int i = 0; i < arr.length; i++) {
Girls g = arr[i];
if (g.getAge() < avg) {
count++;
System.out.println(g.getName()+":"+g.getAge()+":"+g.getHeight()+":"+g.getHobby());
}
}
System.out.println("年龄比平均年龄小的有:" + count + "个");
}
}
声明:
以上均来源于B站@ITheima的教学内容!!!
本人跟着视频内容学习,整理知识引用
更多推荐
所有评论(0)