Java and physics simulations

Lesson 1 of 4 · Java – physics simulations

Computer simulations are used to better understand, describe and predict physical phenomena. The Java programming language together with the OSP (Open Source Physics) library gives physics enthusiasts the means to make them. The Easy Java Simulations (EJS) tool makes it possible to create computer simulations easily, without too much programming, so that the student can focus on the physical phenomenon itself.

The Open Source Physics (OSP) project is a group of physicists and teachers who work on providing high-quality computer teaching resources to get students involved in physics, computing and computer modelling. See more about OSP and the Easy Java Simulations tool on the website: www.compadre.org/osp/

The EJS tool is meant for physicists and anyone who wants to study physical phenomena with less effort spent on writing program code, because the tool does that. It lets you create simulations with either the Java or the JavaScript programming language. Files created with Java have the extension .ejs, while those made with JavaScript have the extension .ejss.

This tool can be used for educational or scientific purposes, and it can also be used to make computer games. See some of the computer simulations on the website kosi-hitac.onrender.com/en/

Use in education

Using simulations in education has a significant effect on understanding complex physical phenomena. Java and OSP (Open Source Physics) tools let students explore and visualise phenomena such as projectile motion, waves, electromagnetism and many others. With simulations, students can experiment with different parameters and see the results straight away, which helps them understand theoretical concepts better.

For example, a simulation of a body moving under the influence of gravity lets students easily see how different starting speeds and launch angles affect the range and the path of the body. Instead of relying only on analytical methods, students can use simulations to connect mathematical models intuitively with the physical phenomena they are studying.

Research has shown that using such tools increases student engagement, improves understanding of the material and helps students remember it better through interactive learning. OSP and EJS tools are used by many educational institutions around the world, not only in physics teaching, but also in computer science, mathematics and engineering.

Technical details

Java JDK: To work with the EJS tool, it is recommended to use the Java Development Kit (JDK) version 8 or newer. The JDK contains all the tools you need for compiling, debugging and running Java applications. Installing the JDK is the first step before you start writing Java code or using the EJS tool. You can download the JDK from the official Oracle page.

JDK installation steps:

  1. Download the JDK installation package from the Oracle site.
  2. Run the installation file and follow the instructions on the screen.
  3. After installation, add the JDK bin directory to the PATH system variable so that the Java tools are available from the command line.

Writing Java code: When writing Java code for physics simulations, you will often work with classes and objects that let you model real phenomena. For example, creating a simulation of a body moving in a gravitational field requires creating a Body class that defines properties such as mass, velocity and position of the body, as well as methods that calculate its motion.

An example of simple Java code:

public class Body {
    double mass;
    double velocity;
    double position;

    public Body(double mass, double velocity, double position) {
        this.mass = mass;
        this.velocity = velocity;
        this.position = position;
    }

    public void updatePosition(double time) {
        position += velocity * time;
    }
}

This code shows the basic elements for defining a body in a simulation and a simple method for updating its position over time.

The OSP library

The Open Source Physics (OSP) project is a collection of tools and resources designed to help physicists, students and teachers create and use computer simulations for studying physical phenomena. The OSP library offers a wide range of ready-made classes and methods that make it easy to model and visualise physical systems, without having to write large amounts of code from scratch.

You can read an interesting article about OSP with examples: kosi-hitac.onrender.com/en/osp_introduction

How does the OSP library work? The OSP library works as a framework that lets users quickly develop interactive simulations of physical systems. It contains ready-made components for drawing graphs, solving differential equations, animation and many other features that are useful in physics. Users can combine these components to create complex simulations that accurately describe real physical processes.

What the OSP library offers

  • Modelling tools: the OSP library contains tools for easily setting up starting conditions and parameters and defining physical models.
  • Data visualisation: the library lets you visualise simulations through graphs, diagrams and animations, which helps you understand complex physical phenomena better.
  • Support for different methods: OSP supports different numerical methods for solving differential equations, optimisation and other computing operations often used in physics.
  • Accessibility: OSP is designed to be accessible to users with limited programming experience too, letting them focus on the physics side of the problem.

Why is OSP useful for physicists? OSP lets physicists quickly and efficiently develop simulations that can be used for research, education or demonstrations. Since the library contains ready-made components for methods and processes often used in physics, the time needed to develop a simulation is greatly reduced. This lets researchers and teachers focus on the physics itself, not on the technical details of programming.

Examples of simulations

  • Studying a harmonic oscillator
  • A simulation of a satellite's motion
  • The interaction of particles in a plasma

You can find more examples of simulations that use the OSP library on the Compadre OSP site.

An introduction to programming and the Java language

Java is a high-level programming language developed by "Sun Microsystems" in 1995. Since the EJS program generates code written in Java, this page describes some elements of this programming language.

To make an application in Java you need:

  • A text editor, e.g. Notepad, for writing the source code
  • The Java JDK (Java Development Kit), the platform for Java
  • One of the programming tools – optional

Java JDK

This is the platform for Java. It contains the software and tools you need for compiling, finding mistakes (debugging) and running applets and applications written in the Java programming language. It contains a library of ready-made classes, the API.

The road to an application. During the development of an application you need to create:

Source code. Instructions written in the Java programming language and saved in .java files

Executable code. Instructions translated by compilers and saved in .class files

Clicking Run starts the executable code inside the development tool.

Programming tools are programs that help the programmer develop an application.

Some of the tools used for Java:

• NetBeans (Sun Microsystems)

• Eclipse (IBM)

• IntelliJ IDEA

• JDeveloper (Oracle)

• JBuilder (Borland)

• Xcode (Apple – only for Mac OS X)

• JCreator

In the next video lesson, watch the introduction to programming and the Java language, and how simple physics examples are made with the Eclipse tool. About this tool we can say the following:

Videos in Serbian with English subtitles – turn on subtitles (CC) in the YouTube player.

An introduction to Java programming and the Eclipse IDE – part 1

An introduction to Java programming and the Eclipse IDE – part 2

Physics examples – writing applications with Eclipse

Example 1.1 – Average speed. The version for a single case

A car travels from place A to place B at speed v. If the distance between the places is 130 km and the car arrives in place B after 98 minutes of driving, find the average speed over the distance travelled.

Example 1.2 – Average speed. The version that solves a group of tasks with different inputs

A car travels from place A to place B at speed v. If the distance between the places is s [km] and the car arrives in place B after t minutes of driving, find the average speed over the distance travelled.

Solution

Example 2 – Two cars meeting

Two cars set off at the same time towards each other from two towns D km apart, moving at speeds V1 [km/h] and V2 [km/h]. After what time and at what place will the cars meet?

Solution

Next

Classes and objects applied in physics >|

Check your knowledge

Open the quiz

Quiz: Physics simulations

1. What is a computer simulation of motion?




2. In uniform motion the distance changes as:




3. What happens if the step dt is bigger?




4. Why is Java suitable for simulations?