(附代码)根据输入信息构建不同人种类,统一打印不同国家地区的身高、体重信息(泛型数组应用,多态、重载、泛型、反射等概念体验)

来源:互联网 发布:头上粉色爱心 软件 编辑:程序博客网 时间:2024/06/02 18:12


实现效果:

根据用户输入的一组信息,批量构建不同人种类,这里我要求用户在每个人的身高体重信息前输入一个数字作为标识符(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, 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("~啊啊啊~啊啊啊啊啊~");

    }

 

}

 

0 0
原创粉丝点击