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

本文主要是介绍(附代码)根据输入信息构建不同人种类,统一打印不同国家地区的身高、体重信息(泛型数组应用,多态、重载、泛型、反射等概念体验),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!


实现效果:

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

    }

 

}

 

这篇关于(附代码)根据输入信息构建不同人种类,统一打印不同国家地区的身高、体重信息(泛型数组应用,多态、重载、泛型、反射等概念体验)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/789426

相关文章

Python打印对象所有属性和值的方法小结

《Python打印对象所有属性和值的方法小结》在Python开发过程中,调试代码时经常需要查看对象的当前状态,也就是对象的所有属性和对应的值,然而,Python并没有像PHP的print_r那样直接提... 目录python中打印对象所有属性和值的方法实现步骤1. 使用vars()和pprint()2. 使

CSS中的Static、Relative、Absolute、Fixed、Sticky的应用与详细对比

《CSS中的Static、Relative、Absolute、Fixed、Sticky的应用与详细对比》CSS中的position属性用于控制元素的定位方式,不同的定位方式会影响元素在页面中的布... css 中的 position 属性用于控制元素的定位方式,不同的定位方式会影响元素在页面中的布局和层叠关

SpringBoot3应用中集成和使用Spring Retry的实践记录

《SpringBoot3应用中集成和使用SpringRetry的实践记录》SpringRetry为SpringBoot3提供重试机制,支持注解和编程式两种方式,可配置重试策略与监听器,适用于临时性故... 目录1. 简介2. 环境准备3. 使用方式3.1 注解方式 基础使用自定义重试策略失败恢复机制注意事项

Python实例题之pygame开发打飞机游戏实例代码

《Python实例题之pygame开发打飞机游戏实例代码》对于python的学习者,能够写出一个飞机大战的程序代码,是不是感觉到非常的开心,:本文主要介绍Python实例题之pygame开发打飞机... 目录题目pygame-aircraft-game使用 Pygame 开发的打飞机游戏脚本代码解释初始化部

Java中Map.Entry()含义及方法使用代码

《Java中Map.Entry()含义及方法使用代码》:本文主要介绍Java中Map.Entry()含义及方法使用的相关资料,Map.Entry是Java中Map的静态内部接口,用于表示键值对,其... 目录前言 Map.Entry作用核心方法常见使用场景1. 遍历 Map 的所有键值对2. 直接修改 Ma

MySQL JSON 查询中的对象与数组技巧及查询示例

《MySQLJSON查询中的对象与数组技巧及查询示例》MySQL中JSON对象和JSON数组查询的详细介绍及带有WHERE条件的查询示例,本文给大家介绍的非常详细,mysqljson查询示例相关知... 目录jsON 对象查询1. JSON_CONTAINS2. JSON_EXTRACT3. JSON_TA

统一返回JsonResult踩坑的记录

《统一返回JsonResult踩坑的记录》:本文主要介绍统一返回JsonResult踩坑的记录,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录统一返回jsonResult踩坑定义了一个统一返回类在使用时,JsonResult没有get/set方法时响应总结统一返回

全屋WiFi 7无死角! 华硕 RP-BE58无线信号放大器体验测评

《全屋WiFi7无死角!华硕RP-BE58无线信号放大器体验测评》家里网络总是有很多死角没有网,我决定入手一台支持Mesh组网的WiFi7路由系统以彻底解决网络覆盖问题,最终选择了一款功能非常... 自2023年WiFi 7技术标准(IEEE 802.11be)正式落地以来,这项第七代无线网络技术就以超高速

基于Python构建一个高效词汇表

《基于Python构建一个高效词汇表》在自然语言处理(NLP)领域,构建高效的词汇表是文本预处理的关键步骤,本文将解析一个使用Python实现的n-gram词频统计工具,感兴趣的可以了解下... 目录一、项目背景与目标1.1 技术需求1.2 核心技术栈二、核心代码解析2.1 数据处理函数2.2 数据处理流程

Java反射实现多属性去重与分组功能

《Java反射实现多属性去重与分组功能》在Java开发中,​​List是一种非常常用的数据结构,通常我们会遇到这样的问题:如何处理​​List​​​中的相同字段?无论是去重还是分组,合理的操作可以提高... 目录一、开发环境与基础组件准备1.环境配置:2. 代码结构说明:二、基础反射工具:BeanUtils