第七章 场与势能——集合与泛型
7.1 磁感线分布图(List集合特性)
物理现象:磁感线的有序分布 ? List集合的有序存储
实验器材:
import java.util.ArrayList;
import java.util.LinkedList;
class MagneticField {
public static void main(String[] args) {
// 磁场强度记录(ArrayList像条形磁铁)
List fieldA = new ArrayList<>();
fieldA.add(1.2); // N极
fieldA.add(0.8);
fieldA.add(0.5); // S极
// 磁感线动态变化(LinkedList像环形磁铁)
List fieldB = new LinkedList<>();
fieldB.addFirst("N极");
fieldB.addLast("S极");
}
}
7.2 电荷相互作用(Map键值对存储)
库仑定律编程版:
import java.util.HashMap;
class ChargeSystem {
public static void main(String[] args) {
// 电荷存储(键:电荷量,值:位置)
Map charges = new HashMap<>();
charges.put("+5C", new Point(0, 0));
charges.put("-3C", new Point(5, 5));
// 计算电场力
double force = calculateForce(charges);
System.out.printf("电场力:%.2fN", force);
}
static double calculateForce(Map charges) {
// 根据库仑定律计算(F=k*q1*q2/r2)
return 8.98e9 * 5 * 3 / Math.pow(7.07, 2);
}
}
7.3 电磁屏蔽原理(泛型类型安全)
安全防护机制:
class ShieldBox { // 泛型就像屏蔽罩
private T content;
void put(T item) {
// 只能放入指定类型(屏蔽外部干扰)
this.content = item;
}
T get() {
return content;
}
}
// 实验验证
ShieldBox safeBox = new ShieldBox<>();
safeBox.put(3.14);
// safeBox.put("金属"); // 编译错误!就像异物无法穿透屏蔽层
第八章 能量转换系统——文件与IO
8.1 实验数据记录(文件读写)
示波器数据存储:
import java.nio.file.*;
class DataLogger {
public static void main(String[] args) throws IOException {
// 创建实验报告(写入文件)
Path report = Paths.get("experiment.txt");
Files.write(report, Arrays.asList(
"时间(s)\t电压(V)",
"0.1\t3.2",
"0.2\t5.1"
), StandardOpenOption.CREATE);
// 读取历史数据
List records = Files.readAllLines(report);
records.forEach(System.out::println);
}
}
8.2 变压器工作原理(数据流转换)
电压转换模拟:
import java.io.*;
class Transformer {
public static void main(String[] args) {
try (InputStream primary = new FileInputStream("input220V.dat");
OutputStream secondary = new FileOutputStream("output12V.dat")) {
int voltage;
while ((voltage = primary.read()) != -1) {
// 降压转换(220V→12V)
secondary.write((int)(voltage * 0.0545));
}
}
}
}
8.3 电磁波传播(网络通信基础)
简易信号发射器:
import java.net.*;
class RadioTransmitter {
public static void main(String[] args) {
try (DatagramSocket socket = new DatagramSocket()) {
String signal = "SOS";
byte[] data = signal.getBytes();
InetAddress address = InetAddress.getByName("192.168.1.100");
DatagramPacket packet = new DatagramPacket(
data, data.length, address, 6000
);
socket.send(packet);
System.out.println("信号已发射!");
}
}
}
第九章 综合实验设计——项目实战
9.1 温度监控系统(传感器原理)
热敏电阻模拟程序:
class TempMonitor {
static class Sensor {
double currentTemp;
double read() {
// 模拟热敏电阻特性:温度↑电阻↓
currentTemp = 25 + Math.random() * 10;
return currentTemp;
}
}
public static void main(String[] args) {
Sensor sensor = new Sensor();
while (true) {
double temp = sensor.read();
System.out.printf("当前温度:%.1f℃%n", temp);
if (temp > 30) System.out.println("?? 温度过高!");
try { Thread.sleep(1000); } catch (InterruptedException e) {}
}
}
}
9.2 天体运动模拟(数学函数应用)
万有引力系统:
class CelestialSystem {
static class Planet {
double mass;
double x, y;
void updatePosition(Star sun) {
// 简化版万有引力公式:F=G*M*m/r2
double G = 6.67e-11;
double r = Math.sqrt(Math.pow(x,2) + Math.pow(y,2));
double acceleration = G * sun.mass / (r*r);
x += acceleration * 0.01; // 模拟时间步长
y += acceleration * 0.01;
}
}
}
9.3 电路仿真程序(GUI基础)
可视化电路实验台:
import javax.swing.*;
class CircuitSimulator extends JFrame {
public CircuitSimulator() {
setTitle("电路实验台");
setSize(800, 600);
JPanel breadboard = new JPanel();
breadboard.add(new JLabel(new ImageIcon("breadboard.png")));
JButton powerBtn = new JButton("通电");
powerBtn.addActionListener(e -> JOptionPane.showMessageDialog(this, "灯泡亮起!"));
add(breadboard, BorderLayout.CENTER);
add(powerBtn, BorderLayout.SOUTH);
setVisible(true);
}
public static void main(String[] args) {
new CircuitSimulator();
}
}
下篇实验室任务:
- 开发"磁场探测器"程序,用Map存储不同位置的磁场强度
- 实现"实验数据归档系统",自动将超过1个月的数据压缩备份
- 设计"太阳系模拟器",用集合存储行星并实现轨道计算
// 任务3示例框架
class SolarSystem {
List planets = new ArrayList<>();
void addPlanet(String name, double mass) {
planets.add(new Planet(name, mass));
}
void simulateOrbit() {
planets.forEach(p -> p.updatePosition());
}
}
毕业设计建议:
- 用JavaFX实现动态磁场可视化
- 结合Raspberry Pi制作实体温度监控装置
- 开发物理实验报告自动生成系统(模板引擎+数据图表)
电磁综合篇知识图谱:
┌───────────┐
│ 集合与泛型 │
└─────┬─────┘
├─→ 磁感线 → List有序性
├─→ 电荷对 → Map键值存储
└─→ 电磁屏蔽 → 泛型安全
┌───────────┐
│ 文件与IO │
└─────┬─────┘
├─→ 数据记录 → 文件读写
├─→ 电压转换 → 流处理
└─→ 电磁波 → 网络通信
┌───────────┐
│ 项目实战 │
└─────┬─────┘
├─→ 温度监控 → 传感器模拟
├─→ 天体运动 → 数学建模
└─→ 电路仿真 → GUI应用
学习总结:通过本系列教程,您已从基础语法到面向对象,最终掌握高级应用开发能力。就像完成了一个完整的电磁学实验体系设计,现在可以尝试用Java创造属于自己的数字物理世界!