Java面向对象综合训练
一、文字版格斗游戏
二、对象数组练习 对象数组1
对象数组2
对象数组3
对象数组4
对象数组5
一、文字版格斗游戏
Role类
import java. util. Random ;
public class Role {
private String name;
private int blood;
private char gender;
private String face;
String [ ] boyfaces = { "风流俊雅" , "气宇轩昂" , "相貌英俊" , "五官端正" , "相貌平平" , "一塌糊涂" , "面目狰狞" } ;
String [ ] girlfaces = { "美奂绝伦" , "沉鱼落雁" , "婷婷玉立" , "身材娇好" , "相貌平平" , "相貌简陋" , "惨不忍睹" } ;
String [ ] attacks_desc = {
"%s使出了一招[背心钉],转到对方的身后,一掌向%s背心的灵台穴拍去。" ,
"%s使出了一招[游空探爪],飞起身形自 半空中变掌为抓锁向%S。" ,
"%s大喝一声,身形下伏,一招[劈雷坠地],捶向%s双腿。" ,
"%s运气于掌,一瞬间掌心变得血红,一式[掌心雷],推向%S。" ,
"%s阴手翻起阳手跟进,一招[没遮拦],结结实实的捶向%S。" ,
"%s.上步抢身,招中套招,一招[劈挂连环],连环攻向%S。" ,
} ;
String [ ] injureds_desc = {
"结果%s退了半步,毫发无损" ,
"结果给%s造成一处瘀伤" ,
"结果一击命中,%s痛得弯下腰" ,
"结果%S痛苦地闷哼了一声,显然受了点内伤" ,
"结果%s摇摇晃晃,一跤摔倒在地" ,
"结果%s脸色一下变得惨白,连退了好几步" ,
"结果「轰的一声,%s口中鲜血狂喷而出" ,
"结果%s一声惨叫, 像滩软泥般塌了下去"
} ;
public Role ( ) {
}
public Role ( String name, int blood, char gender) {
this . name = name;
this . blood = blood;
this . gender = gender;
setFace ( gender) ;
}
public char getGender ( ) {
return gender;
}
public void setGender ( char gender) {
this . gender = gender;
}
public String getFace ( ) {
return face;
}
public void setFace ( char gender) {
Random random = new Random ( ) ;
if ( gender == '男' ) {
int index = random. nextInt ( boyfaces. length) ;
this . face = boyfaces[ index] ;
} else if ( gender == '女' ) {
int index = random. nextInt ( girlfaces. length) ;
this . face = girlfaces[ index] ;
} else {
this . face = "面目狰狞" ;
}
}
public String getName ( ) {
return name;
}
public void setName ( String name) {
this . name = name;
}
public int getBlood ( ) {
return blood;
}
public void setBlood ( int blood) {
this . blood = blood;
}
public String toString ( ) {
return "Role{name = " + name + ", blood = " + blood + "}" ;
}
public void attack ( Role role) {
Random random = new Random ( ) ;
int index = random. nextInt ( attacks_desc. length) ;
String KungFu = attacks_desc[ index] ;
System . out. printf ( KungFu , this . getName ( ) , role. getName ( ) ) ;
int hurt = random. nextInt ( 20 ) + 1 ;
int remainBlood = role. getBlood ( ) - hurt;
remainBlood = Math . max ( remainBlood, 0 ) ;
role. setBlood ( remainBlood) ;
if ( remainBlood > 90 ) {
System . out. printf ( injureds_desc[ 0 ] , role. getName ( ) ) ;
} else if ( remainBlood > 80 ) {
System . out. printf ( injureds_desc[ 1 ] , role. getName ( ) ) ;
} else if ( remainBlood > 70 ) {
System . out. printf ( injureds_desc[ 2 ] , role. getName ( ) ) ;
} else if ( remainBlood > 60 ) {
System . out. printf ( injureds_desc[ 3 ] , role. getName ( ) ) ;
} else if ( remainBlood > 40 ) {
System . out. printf ( injureds_desc[ 4 ] , role. getName ( ) ) ;
} else if ( remainBlood > 20 ) {
System . out. printf ( injureds_desc[ 5 ] , role. getName ( ) ) ;
} else if ( remainBlood > 10 ) {
System . out. printf ( injureds_desc[ 6 ] , role. getName ( ) ) ;
} else {
System . out. printf ( injureds_desc[ 7 ] , role. getName ( ) ) ;
}
System . out. println ( ) ;
}
public void showRoleInfo ( ) {
System . out. println ( "姓名为:" + getName ( ) ) ;
System . out. println ( "血量为:" + getBlood ( ) ) ;
System . out. println ( "性别为:" + getGender ( ) ) ;
System . out. println ( "长相为:" + getFace ( ) ) ;
}
}
测试类
public class GameTest {
public static void main ( String [ ] args) {
Role r1 = new Role ( "鸠摩智" , 100 , '男' ) ;
Role r2 = new Role ( "乔峰" , 100 , '男' ) ;
while ( true ) {
r1. attack ( r2) ;
if ( r2. getBlood ( ) == 0 ) {
System . out. println ( r1. getName ( ) + "K.O了" + r2. getName ( ) ) ;
break ;
}
r2. attack ( r1) ;
if ( r1. getBlood ( ) == 0 ) {
System . out. println ( r2. getName ( ) + "K.O了" + r1. getName ( ) ) ;
break ;
}
}
}
}
输出结果
二、对象数组练习
对象数组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 String toString ( ) {
return "Goods{id = " + id + ", name = " + name + ", price = " + price + ", count = " + count + "}" ;
}
}
测试类
public class GoodsTest {
public static void main ( String [ ] args) {
Goods [ ] arr = new Goods [ 3 ] ;
Goods g1 = new Goods ( "001" , "华为P40" , 5999.0 , 100 ) ;
Goods g2 = new Goods ( "002" , "保温杯" , 227.0 , 50 ) ;
Goods g3 = new Goods ( "003" , "枸杞" , 12.7 , 70 ) ;
arr[ 0 ] = g1;
arr[ 1 ] = g2;
arr[ 2 ] = g3;
for ( int i = 0 ; i < arr. length; i++ ) {
Goods goods = arr[ i] ;
System . out. println ( goods. getId ( ) + "," + goods. getName ( ) + "," + goods. getPrice ( ) + "," + goods. getCount ( ) ) ;
}
}
}
输出结果
对象数组2
定义数组存储3个汽车对象 汽车的属性:品牌、价格、颜色 创建三个汽车对象,数据通过键盘录入而来,并把数据存入到数组当中。
汽车类
public class Car {
private String brand;
private int price;
private String color;
public Car ( ) {
}
public Car ( 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;
}
public String toString ( ) {
return "Car{brand = " + brand + ", price = " + price + ", color = " + color + "}" ;
}
}
测试类
import java. util. Scanner ;
public class CarTest {
public static void main ( String [ ] args) {
Car [ ] arr = new Car [ 3 ] ;
Scanner scanner = new Scanner ( System . in) ;
for ( int i = 0 ; i < arr. length; i++ ) {
Car car = new Car ( ) ;
System . out. println ( "请输入汽车品牌" ) ;
String brand = scanner. next ( ) ;
car. setBrand ( brand) ;
System . out. println ( "请输入汽车价格" ) ;
int price = scanner. nextInt ( ) ;
car. setPrice ( price) ;
System . out. println ( "请输入汽车颜色" ) ;
String color = scanner. next ( ) ;
car. setColor ( color) ;
arr[ i] = car;
}
for ( int i = 0 ; i < arr. length; i++ ) {
Car car = arr[ i] ;
System . out. println ( car. getBrand ( ) + "," + car. getPrice ( ) + "," + car. getColor ( ) ) ;
}
}
}
输出结果
对象数组3
定义数组存储3部手机对象 手机的属性:品牌,价格,颜色 要求:计算出三部手机的平均价格
手机类
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;
}
public String toString ( ) {
return "Phone{brand = " + brand + ", price = " + price + ", 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 ( "华为" , 2999 , "黑色" ) ;
Phone p3 = new Phone ( "魅族" , 3999 , "灰色" ) ;
arr[ 0 ] = p1;
arr[ 1 ] = p2;
arr[ 2 ] = p3;
int sum = 0 ;
for ( int i = 0 ; i < arr. length; i++ ) {
Phone phone = arr[ i] ;
sum += phone. getPrice ( ) ;
}
int avg = sum / arr. length;
System . out. println ( avg) ;
}
}
输出结果
对象数组4
定义数组存储4个女朋友的对象 女朋友的属性:姓名、年龄、性别、爱好 要求1:计算出四个女朋友的平均年龄 要求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 String toString ( ) {
return "GirlFriend{name = " + name + ", age = " + age + ", gender = " + gender + ", hobby = " + hobby + "}" ;
}
}
测试类
public class GirlFriendTest {
public static void main ( String [ ] args) {
GirlFriend [ ] arr = new GirlFriend [ 4 ] ;
GirlFriend gf1 = new GirlFriend ( "女朋友1号" , 18 , "萌妹子" , "吃零食" ) ;
GirlFriend gf2 = new GirlFriend ( "女朋友2号" , 19 , "萌妹子" , "玩游戏" ) ;
GirlFriend gf3 = new GirlFriend ( "女朋友3号" , 20 , "萌妹子" , "看书" ) ;
GirlFriend gf4 = new GirlFriend ( "女朋友4号" , 21 , "萌妹子" , "睡觉" ) ;
arr[ 0 ] = gf1;
arr[ 1 ] = gf2;
arr[ 2 ] = gf3;
arr[ 3 ] = gf4;
int sum = 0 ;
for ( int i = 0 ; i < arr. length; i++ ) {
GirlFriend girlFriend = arr[ i] ;
sum += girlFriend. getAge ( ) ;
}
int avg = sum / arr. length;
int count = 0 ;
for ( int i = 0 ; i < arr. length; i++ ) {
GirlFriend girlFriend = arr[ i] ;
if ( girlFriend. getAge ( ) < avg) {
count++ ;
System . out. println ( girlFriend. getName ( ) + "," + girlFriend. getAge ( ) + "," + girlFriend. getGender ( ) + "," + girlFriend. getHobby ( ) ) ;
}
}
System . out. println ( count) ;
}
}
输出结果
对象数组5
定义一个长度为3的数组,数组存储1-3名学生对象作为初始数据,学生对象的学号,姓名各不相同。 学生的属性:学号、姓名、年龄。 要求1:再次添加一个学生对象,并在添加的时候进行学号的唯一性判断。 要求2:添加完毕之后,遍历所有学生信息。 要求3:通过id删除学生信息,如果存在,则删除,如果不存在,则提示删除失败。 删除完毕后,遍历所有学生信息。 查询数组id为“1”的学生,如果存在,则他的年龄+1岁
学生类
public class Student {
private int id;
private String name;
private int age;
public Student ( ) {
}
public Student ( int id, String name, int age) {
this . id = id;
this . name = name;
this . age = age;
}
public int getId ( ) {
return id;
}
public void setId ( int id) {
this . id = id;
}
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 toString ( ) {
return "Student{id = " + id + ", name = " + name + ", age = " + age + "}" ;
}
}
测试类
public class StudentTest {
public static void main ( String [ ] args) {
Student [ ] arr = new Student [ 3 ] ;
Student stu1 = new Student ( 1 , "张三" , 18 ) ;
Student stu2 = new Student ( 2 , "李四" , 19 ) ;
Student stu3 = new Student ( 3 , "王五" , 20 ) ;
arr[ 0 ] = stu1;
arr[ 1 ] = stu2;
arr[ 2 ] = stu3;
Student stu4 = new Student ( 4 , "赵六" , 21 ) ;
boolean flag = contains ( arr, stu4. getId ( ) ) ;
if ( flag) {
System . out. println ( "当前id重复,请修改id后再进行添加" ) ;
} else {
int count = getCount ( arr) ;
if ( count == arr. length) {
Student [ ] newArr = createNewArr ( arr) ;
newArr[ count] = stu4;
arr = newArr;
printArr ( arr) ;
} else {
arr[ count] = stu4;
printArr ( arr) ;
}
}
int index = getIndex ( arr, 5 ) ;
if ( index >= 0 ) {
arr[ index] = null ;
printArr ( arr) ;
} else {
System . out. println ( "当前id不存在,删除失败" ) ;
}
int index2 = getIndex ( arr, 2 ) ;
if ( index2 >= 0 ) {
Student student = arr[ index2] ;
int newAge = student. getAge ( ) + 1 ;
student. setAge ( newAge) ;
printArr ( arr) ;
} else {
System . out. println ( "当前id不存在,修改失败" ) ;
}
}
public static int getIndex ( Student [ ] arr, int id) {
for ( int i = 0 ; i < arr. length; i++ ) {
Student student = arr[ i] ;
if ( student != null ) {
int studentId = student. getId ( ) ;
if ( studentId == id) {
return i;
}
}
}
return - 1 ;
}
public static void printArr ( Student [ ] arr) {
for ( int i = 0 ; i < arr. length; i++ ) {
Student student = arr[ i] ;
if ( student != null ) {
System . out. println ( student. getId ( ) + "," + student. getName ( ) + "," + student. getAge ( ) ) ;
}
}
}
public static Student [ ] createNewArr ( Student [ ] arr) {
Student [ ] newArr = new Student [ arr. length + 1 ] ;
for ( int i = 0 ; i < arr. length; i++ ) {
newArr[ i] = arr[ i] ;
}
return newArr;
}
public static int getCount ( Student [ ] arr) {
int count = 0 ;
for ( int i = 0 ; i < arr. length; i++ ) {
if ( arr[ i] != null ) {
count++ ;
}
}
return count;
}
public static boolean contains ( Student [ ] arr, int id) {
for ( int i = 0 ; i < arr. length; i++ ) {
Student student = arr[ i] ;
if ( student != null ) {
int studentId = student. getId ( ) ;
if ( studentId == id) {
return true ;
}
}
}
return false ;
}
}
输出结果