综合java案例,高速跑动距离对比?

wen java案例 1

Java案例:高速跑动距离对比分析

下面用一个完整的Java案例,对比不同交通工具/运动员在高速状态下的跑动距离,包含面向对象设计、接口、集合排序、Stream统计等知识点。

综合java案例,高速跑动距离对比?


需求说明

对比以下对象在 1小时 内的高速跑动距离:

对象 速度(km/h)
猎豹 110
赛马 60
短跑运动员 36
高铁 350
赛车 300

要求:

  1. 计算各自1小时跑动距离
  2. 按距离降序排序输出
  3. 统计平均速度、最快对象、总距离

代码实现

定义接口

public interface Runnable {
    String getName();      // 名称
    double getSpeed();     // 速度 km/h
    default double distance(double hours) {  // 默认方法计算距离
        return getSpeed() * hours;
    }
}

定义实体类

public class Runner implements Runnable {
    private String name;
    private double speed;
    public Runner(String name, double speed) {
        this.name = name;
        this.speed = speed;
    }
    @Override public String getName() { return name; }
    @Override public double getSpeed() { return speed; }
    @Override
    public String toString() {
        return String.format("%-8s 速度:%6.1f km/h", name, speed);
    }
}

主程序:对比分析

import java.util.*;
import java.util.stream.*;
public class SpeedDistanceCompare {
    public static void main(String[] args) {
        List<Runner> list = Arrays.asList(
            new Runner("猎豹",   110),
            new Runner("赛马",    60),
            new Runner("短跑运动员", 36),
            new Runner("高铁",   350),
            new Runner("赛车",   300)
        );
        double hours = 1.0;   // 跑动时间
        // 1. 按距离降序排序
        List<Runner> sorted = list.stream()
            .sorted(Comparator.comparingDouble(
                (Runner r) -> r.distance(hours)).reversed())
            .collect(Collectors.toList());
        System.out.println("===== 1小时高速跑动距离排行 =====");
        int rank = 1;
        for (Runner r : sorted) {
            System.out.printf("第%d名  %s  距离:%8.1f km%n",
                rank++, r, r.distance(hours));
        }
        // 2. 统计信息
        DoubleSummaryStatistics stat = list.stream()
            .mapToDouble(r -> r.distance(hours))
            .summaryStatistics();
        Runner fastest = list.stream()
            .max(Comparator.comparingDouble(Runner::getSpeed))
            .orElseThrow();
        System.out.println("\n===== 统计结果 =====");
        System.out.printf("总跑动距离 : %.1f km%n", stat.getSum());
        System.out.printf("平均距离   : %.1f km%n", stat.getAverage());
        System.out.printf("最大距离   : %.1f km%n", stat.getMax());
        System.out.printf("最小距离   : %.1f km%n", stat.getMin());
        System.out.printf("最快对象   : %s (%.0f km/h)%n",
                fastest.getName(), fastest.getSpeed());
        // 3. 分组对比:生物 vs 机械
        Set<String> biology = Set.of("猎豹", "赛马", "短跑运动员");
        Map<String, Double> group = list.stream()
            .collect(Collectors.groupingBy(
                r -> biology.contains(r.getName()) ? "生物" : "机械",
                Collectors.summingDouble(r -> r.distance(hours))
            ));
        System.out.println("\n===== 分组对比 =====");
        group.forEach((k, v) ->
            System.out.printf("%s组总距离: %.1f km%n", k, v));
        // 4. 生物最快 vs 机械最慢
        Runner bioFastest = list.stream()
            .filter(r -> biology.contains(r.getName()))
            .max(Comparator.comparingDouble(Runner::getSpeed)).orElseThrow();
        Runner mechSlowest = list.stream()
            .filter(r -> !biology.contains(r.getName()))
            .min(Comparator.comparingDouble(Runner::getSpeed)).orElseThrow();
        System.out.println("\n===== 跨类对比 =====");
        System.out.printf("生物中最快: %s %.0f km/h, 机械中最慢: %s %.0f km/h%n",
            bioFastest.getName(), bioFastest.getSpeed(),
            mechSlowest.getName(), mechSlowest.getSpeed());
        System.out.printf("差距倍数: %.2f 倍%n",
            mechSlowest.getSpeed() / bioFastest.getSpeed());
    }
}

运行结果

===== 1小时高速跑动距离排行 =====
第1名  高铁     速度: 350.0 km/h  距离:   350.0 km
第2名  赛车     速度: 300.0 km/h  距离:   300.0 km
第3名  猎豹     速度: 110.0 km/h  距离:   110.0 km
第4名  赛马     速度:  60.0 km/h  距离:    60.0 km
第5名  短跑运动员 速度:  36.0 km/h  距离:    36.0 km
===== 统计结果 =====
总跑动距离 : 856.0 km
平均距离   : 171.2 km
最大距离   : 350.0 km
最小距离   : 36.0 km
最快对象   : 高铁 (350 km/h)
===== 分组对比 =====
生物组总距离: 206.0 km
机械组总距离: 650.0 km
===== 跨类对比 =====
生物中最快: 猎豹 110 km/h, 机械中最慢: 赛车 300 km/h
差距倍数: 2.73 倍

知识点总结

知识点 应用位置
接口 + default方法 Runnable 提供默认距离计算
面向对象封装 Runner 封装名称、速度
Stream 排序 sorted(Comparator...reversed())
DoubleSummaryStatistics 一次遍历统计总和/均值/最值
groupingBy 分组 生物/机械分组统计
Optional 处理 max()/min() 返回 Optional

可扩展方向

  1. 可变时间对比:将 hours 改为参数,对比 0.5h、1h、2h 距离。
  2. 加速度模型:引入加速阶段,s = v₀t + ½at²,更贴近真实。
  3. CSV/JSON输入:从文件动态读取对比对象。
  4. GUI展示:用 Swing/JavaFX 画柱状图直观对比。

如果你想要 多时间维度对比表 或 加加速度物理模型版本,我可以继续补充。

抱歉,评论功能暂时关闭!