(附代码)根据输入信息构建不同人种类,统一打印不同国家地区的身高、体重信息(泛型数组应用,多态、重载、泛型、反射等概念体验)
2016-10-18 15:43
1061 查看
实现效果:
根据用户输入的一组信息,批量构建不同人种类,这里我要求用户在每个人的身高体重信息前输入一个数字作为标识符(1 for America, 2 for Beijing, 3 forChina, 4 for others)。把这些人按人种归类后,统一打印不同国家地区的身高、体重信息,给出平均身高、平均体重,并让不同人种以独有形式speakHello、演示特技。
最后再打印一次全世界所有人的身高、体重信息,给出平均身高、平均体重,一起sayHello。
测试截图:
首先输入一批人的信息,前后两个人的信息可以换行或空格隔开,不影响输入,这里为了看得清楚换行隔开。输入结束后,系统把这些人按人种归类,统一打印不同国家地区的身高、体重信息,给出平均身高、平均体重,并让不同人种以独有形式speakHello、演示特技。最后再打印一次全世界所有人的身高、体重信息,给出平均身高、平均体重,一起sayHello。
程序清单:
TestPeople.java
import java.util.ArrayList;import java.util.Scanner;
public class TestPeople {
@SuppressWarnings("null")
publicstatic void main(String[] args) {
//TODO Auto-generated method stub
Scannerscan = new Scanner(System.in);
System.out.println("Enterthe adress (1 for America, 2 for Beijing, 3 for China, 4 for others), ");
System.out.println("heightand weight of people: ");
inttag;
doubleh, w;
ArrayList<AmericanPeople>A = new ArrayList<AmericanPeople>();
ArrayList<BeijingPeople>B = new ArrayList<BeijingPeople>();
ArrayList<ChinaPeople>C = new ArrayList<ChinaPeople>();
ArrayList<People>P = new ArrayList<People>();
AmericanPeoplea = null;
BeijingPeopleb = null;
ChinaPeoplec = null;
Peoplep = null;
while(scan.hasNext()) {
tag= scan.nextInt();
h= scan.nextDouble();
w = scan.nextDouble();
if(tag == 1) {
a= new AmericanPeople(h, w);
A.add(a);
P.add(a);
}
elseif (tag == 2) {
b= new BeijingPeople(h, w);
B.add(b);
P.add(b);
}
elseif (tag == 3) {
c= new ChinaPeople(h, w);
C.add(c);
P.add(c);
}
else{
p= new People(h, w);
P.add(p);
}
}
System.out.println("Listof American people:");
System.out.println("Height Weight");
for(AmericanPeople ap : A) {
System.out.printf("%.2fm %.2fkg\n", ap.height, ap.weight);
}
System.out.printf("Theaverage of height is %.2fm.\n", a.averageHeight(a.sumah, a.an));
System.out.printf("Theaverage of weight is %.2fkg.\n", a.averageWeight(a.sumaw, a.an));
System.out.println("Andthey say:");
a.speakHello();
a.AmericanBoxing();
System.out.printf("\n\n");
System.out.println("Listof Beijing people:");
System.out.println("Height Weight");
for(BeijingPeople bp : B) {
System.out.printf("%.2fm %.2fkg\n", bp.height, bp.weight);
}
System.out.printf("Theaverage of height is %.2fm.\n", b.averageHeight(b.sumbh, b.bn));
System.out.printf("Theaverage of weight is %.2fkg.\n", b.averageWeight(b.sumbw, b.bn));
System.out.println("Andthey say:");
b.speakHello();
b.BeijingOpera();
System.out.printf("\n\n");
System.out.println("Listof China people:");
System.out.println("Height Weight");
for(ChinaPeople cp : C) {
System.out.printf("%.2fm %.2fkg\n", cp.height, cp.weight);
}
System.out.printf("Theaverage of height is %.2fm.\n", c.averageHeight(c.sumch, c.cn));
System.out.printf("Theaverage of weight is %.2fkg.\n", c.averageWeight(c.sumcw,
4000
c.cn));
System.out.println("Andthey say:");
c.speakHello();
c.ChinaMartial();
System.out.printf("\n\n");
System.out.println("Listof All people:");
System.out.println("Height Weight");
for(People pp : P) {
System.out.printf("%.2fm %.2fkg\n", pp.height, pp.weight);
}
System.out.printf("Theaverage of height is %.2fm.\n", p.averageHeight(p.sumh, p.n));
System.out.printf("Theaverage of weight is %.2fkg.\n", p.averageWeight(p.sumw, p.n));
System.out.println("Andthey say:");
p.speakHello();
System.out.printf("\n\n");
}
}
People.java
public
class People {
double
height, weight;
static
double sumh = 0;
static
double sumw = 0;
static
int n = 0;
People(doubleh,doublew) {
height =
h;
weight =
w;
sumh +=
h;
sumw +=
w;
n++;
}
void speakHello() {
System.out.println("Hello!");
}
double averageHeight(doublesumh,intn){
return (double)sumh/n;
}
double averageWeight(doublesumw,intn){
return (double)sumw/n;
}
}
ChinaPeople.java
public
class ChinaPeople extends People{
static
double sumch = 0;
static
double sumcw = 0;
static
int cn = 0;
ChinaPeople(doubleh,doublew) {
super(h,w);
// TODO Auto-generated constructor stub
sumch +=
h;
sumcw +=
w;
cn++;
}
/*Override*/
void speakHello() {
System.out.println("你好!");
}
double averageHeight(doublesumch,intcn){
return (double)sumch/cn;
}
double averageWeight(doublesumcw,intcn){
return (double)sumcw/cn;
}
void ChinaMartial() {
System.out.println("嚯嚯哈嘿,快使用双截棍!");
}
}
AmericanPeople.java
public
class AmericanPeople extends People{
static
double sumah = 0;
static
double sumaw = 0;
static
int an = 0;
AmericanPeople(doubleh,doublew) {
super(h,w);
// TODO Auto-generated constructor stub
sumah +=
h;
sumaw +=
w;
an++;
}
/*Override*/
void speakHello() {
System.out.println("Hallo!!!");
}
double averageHeight(doublesumah,intan){
return (double)sumah/an;
}
double averageWeight(doublesumaw,intan){
return (double)sumaw/an;
}
void AmericanBoxing() {
System.out.println("Ruff!!! I will beat u!");
}
}
BeijingPeople.java
public
class BeijingPeople extends People{
static
double sumbh = 0;
static
double sumbw = 0;
static
int bn = 0;
BeijingPeople(doubleh,doublew) {
super(h,w);
// TODO Auto-generated constructor stub
sumbh +=
h;
sumbw +=
w;
bn++;
}
/*Override*/
void speakHello() {
System.out.println("Hallo!!!");
}
double averageHeight(doublesumbh,intbn){
return (double)sumbh/bn;
}
double averageWeight(doublesumbw,intbn){
return (double)sumbw/bn;
}
void BeijingOpera() {
System.out.println("啊~啊啊啊~啊啊啊啊啊~");
}
}
相关文章推荐
- 反射的两个小例子(根据类名打印全部类信息、泛型数组)
- 安卓根据身高计算标准体重,限制输入非数字的代码
- 建立一个学生结构数组来记录学生信息(学号ID,姓名和C语言成绩),要求动态建立一个结构数组,数组长度从键盘输入,自行给数组元素赋值并打印学生信息,最后不要忘记释放内存。 结构st
- 黑马程序员--Java基础加强--03.代码简化 书写规律II_参数化数据类型【重载】【多态】【泛型】【泛型限定】【个人总结】
- 今天根据别人的代码简单的封装了一个将java bean生成xml的一个工具类,运用了反射和泛型,重用性高。
- 输入不同对象的姓名、性别、年龄、体重和住址等信息,并输出显示
- 根据浏览者的IP切换成不同语言,新浪网易IP地区信息查询API接口调用方法
- 反射:Class类、反射概念、类中各成员的反射、反射的应用、数组的反射、框架
- //输入不同对象的姓名、性别、年龄、体重和住址等信息,并输出显示。
- [疯狂Java]泛型:泛型构造器、泛型方法的重载问题、泛型数组的问题(应该摒弃)、?下限的典型应用
- 输入不同对象的姓名、性别、年龄、体重和住址等信息,并输出显示
- 完成一个应用,用户可以根据自己输入的地址下载网络上的图片和网页信息
- 通过录入用户信息实例―体验JavaBean与Servlet在JSP中应用的两种不同的实现过程
- java反射编写泛型数组代码
- 使用反射编写泛型数组代码
- 根据地区不同显示时间的javascript代码
- 安卓完成一个应用,用户可以根据自己输入的地址下载网络上的图片和网页信息
- Android学习小应用案例---根据身高和性别计算标准体重
- 输入不同对象的姓名、性别、年龄、体重和住址等信息,并输出显示。
- //输入不同对象的姓名、性别、年龄、体重和住址等信息,并输出显示。