java实训代码

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

java实训代码
Java实训代码
Java作为一门广泛应用于软件开发领域的编程语言,其实践性非常强,因此在学习Java时,实践是非常重要的。

在学习Java时,很多学生
都会参加Java实训课程,下面将介绍一些Java实训代码的例子。

一、Hello World
Hello World是最简单的程序,也是最基础的程序。

在Java中,Hello World代码如下:
```java
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
```
这段代码中,“public class HelloWorld”表示定义一个公共类
HelloWorld,“public static void main(String[] args)”表示定义一个公共静态方法main,并且该方法不返回任何值。

“System.out.println()”表示输出内容到控制台。

二、计算器
计算器是一个非常基础的应用程序,在Java中可以通过Swing来实现。

下面是一个简单的计算器代码:
```java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Calculator extends JFrame implements ActionListener {
private JTextField textField;
private JButton[] buttons;
private String[] buttonLabels = { "7", "8", "9", "/", "4", "5", "6", "*", "1", "2", "3", "-", ".", "0", "=", "+" };
private double result = 0;
private String operator = "=";
private boolean calculating = true;
public Calculator() {
super("Calculator");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLayout(new BorderLayout());
textField = new JTextField("0", 12);
add(textField, BorderLayout.NORTH);
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new GridLayout(4, 4));
buttons = new JButton[buttonLabels.length];
for (int i = 0; i < buttonLabels.length; i++) {
buttons[i] = new JButton(buttonLabels[i]);
buttons[i].addActionListener(this);
buttonPanel.add(buttons[i]);
}
add(buttonPanel, BorderLayout.CENTER);
pack();
}
public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand();
if (command.charAt(0) >= '0' && command.charAt(0) <= '9' || command.equals(".")) {
if (calculating) {
textField.setText(command);
calculating = false;
} else {
textField.setText(textField.getText() + command);
}
} else {
if (calculating) {
if (command.equals("-")) {
textField.setText(command);
calculating = false;
} else {
operator = command;
}
} else {
double x = Double.parseDouble(textField.getText()); calculate(x);
operator = command;
calculating = true;
}
}
}
private void calculate(double n) {
if (operator.equals("+")) result += n;
else if (operator.equals("-")) result -= n;
else if (operator.equals("*")) result *= n;
else if (operator.equals("/")) result /= n;
textField.setText("" + result);
}
public static void main(String[] args) {
Calculator calculator = new Calculator();
calculator.setVisible(true);
}
}
```
这段代码中,“JFrame”表示创建一个窗体,“JTextField”表示创
建一个文本框,“JButton”表示创建一个按钮。

“addActionListener()”表示为按钮添加一个监听器,“GridLayout”表示布局方式为网格布局。

三、学生信息管理系统
学生信息管理系统是一个比较完整的项目,涉及到了Java中的多种知识点,包括面向对象编程、Swing界面设计、文件读写等。

下面是一
个简单的学生信息管理系统代码:
```java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
public class StudentManagementSystem extends JFrame implements ActionListener {
private JTextField nameField, idField, scoreField;
private JTextArea infoArea;
private JButton addButton, queryButton, deleteButton, modifyButton;
private Student[] students = new Student[100];
private int count = 0;
public StudentManagementSystem() {
super("Student Management System");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLayout(new BorderLayout());
JPanel inputPanel = new JPanel();
inputPanel.setLayout(new GridLayout(3, 2));
inputPanel.add(new JLabel("Name:"));
nameField = new JTextField(10);
inputPanel.add(nameField);
inputPanel.add(new JLabel("ID:"));
idField = new JTextField(10);
inputPanel.add(idField);
inputPanel.add(new JLabel("Score:"));
scoreField = new JTextField(10);
inputPanel.add(scoreField);
JPanel buttonPanel = new JPanel();
addButton = new JButton("Add");
addButton.addActionListener(this);
buttonPanel.add(addButton);
queryButton = new JButton("Query");
queryButton.addActionListener(this);
buttonPanel.add(queryButton);
deleteButton = new JButton("Delete");
deleteButton.addActionListener(this);
buttonPanel.add(deleteButton);
modifyButton = new JButton("Modify");
modifyButton.addActionListener(this);
buttonPanel.add(modifyButton);
JPanel infoPanel = new JPanel();
infoArea = new JTextArea(10, 20);
infoArea.setEditable(false);
JScrollPane scrollPane = new JScrollPane(infoArea); infoPanel.add(scrollPane);
add(inputPanel, BorderLayout.NORTH);
add(buttonPanel, BorderLayout.CENTER);
add(infoPanel, BorderLayout.SOUTH);
pack();
load();
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == addButton) {
String name = nameField.getText().trim();
String id = idField.getText().trim();
double score =
Double.parseDouble(scoreField.getText().trim());
students[count] = new Student(name, id, score); count++;
save();
clearInputFields();
showInfo("Add success.");
} else if (e.getSource() == queryButton) {
String id = idField.getText().trim();
for (int i = 0; i < count; i++) {
if (students[i].getId().equals(id)) {
nameField.setText(students[i].getName());
scoreField.setText("" + students[i].getScore()); return;
}
}
clearInputFields();
showInfo("No such student.");
} else if (e.getSource() == deleteButton) {
String id = idField.getText().trim();
for (int i = 0; i < count; i++) {
if (students[i].getId().equals(id)) {
for (int j = i; j < count - 1; j++) {
students[j] = students[j + 1];
}
count--;
save();
clearInputFields();
showInfo("Delete success.");
return;
}
}
clearInputFields();
showInfo("No such student.");
} else if (e.getSource() == modifyButton) {
String id = idField.getText().trim();
for (int i = 0; i < count; i++) {
if (students[i].getId().equals(id)) {
String name = nameField.getText().trim(); double score =
Double.parseDouble(scoreField.getText().trim()); students[i].setName(name);
students[i].setScore(score);
save();
clearInputFields();
showInfo("Modify success.");
return;
}
}
clearInputFields();
showInfo("No such student.");
}
}
private void clearInputFields() {
nameField.setText("");
idField.setText("");
scoreField.setText("");
}
private void showInfo(String info) {
infoArea.append(info + "\n");
}
private void load() {
try {
BufferedReader reader = new BufferedReader(new FileReader("students.txt"));
String line;
while ((line = reader.readLine()) != null) {
String[] fields = line.split(",");
if (fields.length == 3) {
String name = fields[0];
String id = fields[1];
double score = Double.parseDouble(fields[2]); students[count] = new Student(name, id, score); count++;
}
}
reader.close();
} catch (IOException e) {}
}
private void save() {
try {
BufferedWriter writer = new BufferedWriter(new
FileWriter("students.txt"));
for (int i = 0; i < count; i++) {
writer.write(students[i].getName() + "," + students[i].getId() + "," + students[i].getScore());
writer.newLine();
}
writer.close();
} catch (IOException e) {}
}
public static void main(String[] args) {
StudentManagementSystem system = new StudentManagementSystem();
system.setVisible(true);
}
}
class Student {
private String name;
private String id;
private double score;
public Student(String name, String id, double score) {
= name;
this.id = id;
this.score = score;
}
public String getName() {
return name;
}
public void setName(String name) { = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public double getScore() {
return score;
}
public void setScore(double score) {
this.score = score;
}
}
```
这段代码中,涉及到了面向对象编程的知识点,包括类、对象、构造函数、封装等。

同时也涉及到了文件读写的知识点,包括文件输入输出流等。

总结
Java实训代码的例子非常多,可以涵盖Java语言的各个方面。

在学习Java时,通过实践编写Java程序可以更好地理解和掌握Java语言。

相关文档
最新文档