目录

  • 1 Java语言概述
    • 1.1 Java语言的特点
    • 1.2 Java虚拟机
    • 1.3 Java开发环境
    • 1.4 编译执行和解释执行
    • 1.5 第一个java程序1
    • 1.6 第一个java程序2
    • 1.7 第一个java程序3
    • 1.8 章节测验
  • 2 Java基础语法
    • 2.1 基本数据类型
    • 2.2 赋值语句
    • 2.3 表达式
    • 2.4 运算符
    • 2.5 类型转换
    • 2.6 章节测验
  • 3 类与对象
    • 3.1 面向对象的概念
    • 3.2 类和对象的概念
    • 3.3 构造方法
    • 3.4 章节测验
  • 4 继承与多态
    • 4.1 继承
    • 4.2 多态
    • 4.3 final关键字
    • 4.4 static关键字
    • 4.5 抽象类
    • 4.6 接口
    • 4.7 内部类
    • 4.8 章节测验
  • 5 数组和字符串
    • 5.1 数组
    • 5.2 字符串
    • 5.3 章节测验
  • 6 常用类与接口
    • 6.1 课件
    • 6.2 重要知识点
  • 7 异常处理
    • 7.1 课件
    • 7.2 重要知识点
  • 8 文件和流
    • 8.1 课件
    • 8.2 重要知识点
  • 9 Elements of Programming
    • 9.1 You First Program
      • 9.1.1 Exercise
      • 9.1.2 Program
    • 9.2 Built-in Types of Data
      • 9.2.1 Exercise
      • 9.2.2 Program
    • 9.3 Conditionals and Loops
      • 9.3.1 Exercise
      • 9.3.2 Program
    • 9.4 Arrays
      • 9.4.1 Exercise
      • 9.4.2 Program
    • 9.5 Input and Output
      • 9.5.1 Exercise
      • 9.5.2 Program
    • 9.6 Case Study: Random Web Surfer
  • 10 Functions
    • 10.1 Static Methods
      • 10.1.1 Exercise
      • 10.1.2 Program
    • 10.2 Libraries and Clients
      • 10.2.1 Exercise
      • 10.2.2 Program
    • 10.3 Recursion
      • 10.3.1 Exercise
      • 10.3.2 Program
    • 10.4 Case Study: Percolation
  • 11 Object-Oriented Programming
    • 11.1 Using Data Types
    • 11.2 Creating Data Types
    • 11.3 Designing Data Types
    • 11.4 Case Study: N-Body Simulation
  • 12 参考资料
    • 12.1 主要参考书
    • 12.2 慕课资源
Case Study: N-Body Simulation

In this section, we write an object-oriented program that dynamically simulates the motion of n bodies under the influence of mutual gravitational attraction.

Bouncing balls.

 The data type Ball.java represents a ball with a given position (rx,ry) that moves with a fixed velocity (vx,vy) in the box with coordinates between −1 and +1. When it collides with the boundary, it bounces according to the law of elastic collision.

The client BouncingBalls.java takes a command-line argument n and creates n random bouncing balls.



N-body simulation.

 The bouncing ball simulation is based on Newton's first law of motion: a body in motion remains in motion at the same velocity unless acted on by an outside force. Embellishing that example to incorporate gravity leads us to a basic problem that has fascinated scientists for ages. Given a system of n bodies, mutually affected by gravitational forces, the problem is to describe their motion.
  • Body data type. The data type Body.java represents a body with a given position (rx,ry), velocity (vx,vy), and mass m. It appliesNewton's third law of motion (which explains the gravitational force between two bodies) to determine the net force acting on a body:

    F=Gm1m2r2

    and Newton's second law of motion (which explains how outside forces directly affect acceleration and velocity).

    F=ma

    It uses the Vector.java data type to represent displacement, velocity, and force as vector quantities.


  • Universe data type. Universe.java takes a command-line argument dt, reads in a universe from standard input, and simulates the universe using time quantum dt. Here is an example of the data file format:


    The following static images for 2body.txt, 3body.txt, and 4body.txt are made by modifying Universe.java and Body.java to draw the bodies in white, and then black on a gray background.