### JAVA实验项目知识点详解 #### 项目一:Java 语言基础(一) 1. **编写控制台输出“Hello World!”的 Java 应用程序** - **记事本编写** ```java public class HelloWorld { public static void main(String[] args) { System.out.println("Hello World!"); } } ``` - **Eclipse编写** ```java public class HelloWorld { public static void main(String[] args) { System.out.println("Hello World!"); } } ``` 2. **程序输出分析** - **示例程序** ```java public class Exercise3_1 { public static void main(String[] args) { System.out.print("This is a "); System.out.println("string. "); } } ``` - **输出结果**:"This is a string. " 3. **绘制三角形图案** - **示例程序** ```java public class TrianglePattern { public static void main(String[] args) { for (int i = 1; i <= 5; i++) { for (int j = 1; j <= i; j++) { System.out.print("*"); } System.out.println(); } } } ``` - **输出结果** ``` * ** *** **** ***** ``` 4. **判断闰年** - **示例程序** ```java import java.util.Scanner; public class LeapYear { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("请输入年份: "); int year = scanner.nextInt(); if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) { System.out.println(year + " 是闰年"); } else { System.out.println(year + " 不是闰年"); } } } ``` #### 项目二:Java 语言基础(二) 1. **测试典型运算符的使用** - **示例程序** ```java public class OperatorTest { public static void main(String[] args) { int a = 10; int b = 20; System.out.println("a + b = " + (a + b)); System.out.println("a - b = " + (a - b)); System.out.println("a * b = " + (a * b)); System.out.println("a / b = " + (a / b)); System.out.println("a % b = " + (a % b)); } } ``` 2. **斐波那契数列** - **示例程序** ```java public class Fibonacci { public static void main(String[] args) { int n = 10; int t1 = 0, t2 = 1; System.out.print("前 " + n + " 项: "); for (int i = 1; i <= n; ++i) { System.out.print(t1 + " "); int sum = t1 + t2; t1 = t2; t2 = sum; } } } ``` 3. **水仙花数** - **示例程序** ```java public class NarcissisticNumber { public static void main(String[] args) { for (int i = 100; i < 1000; i++) { int x = i / 100; int y = i / 10 % 10; int z = i % 10; if (Math.pow(x, 3) + Math.pow(y, 3) + Math.pow(z, 3) == i) { System.out.println(i); } } } } ``` #### 项目三:模块化程序设计 1. **计算数字的和及平均值** - **示例程序** ```java import java.util.Scanner; public class SumAndAverage { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int sum = 0; int count = 0; double average = 0.0; while (true) { System.out.print("请输入一个数字 (输入 0 结束): "); int number = scanner.nextInt(); if (number == 0) { break; } sum += number; count++; } if (count > 0) { average = (double) sum / count; } System.out.println("总和: " + sum); System.out.println("平均值: " + average); } } ``` 2. **购物折扣及赠券计算** - **示例程序** ```java import java.util.Scanner; public class DiscountAndCoupon { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("请输入购物总额: "); double totalAmount = scanner.nextDouble(); double discountAmount = 0; double couponAmount = 0; if (totalAmount >= 1000) { discountAmount = totalAmount * 0.2; couponAmount = 200; } else if (totalAmount >= 500) { discountAmount = totalAmount * 0.15; couponAmount = 100; } else if (totalAmount >= 300) { discountAmount = totalAmount * 0.1; couponAmount = 70; } else { discountAmount = totalAmount * 0.05; } System.out.println("折扣金额: " + discountAmount); System.out.println("赠送购物券金额: " + couponAmount); } } ``` #### 项目四:数组的应用 1. **一维数组操作** - **示例程序** ```java import java.util.Arrays; public class ArrayOperations { public static void main(String[] args) { int[] src1 = {1, 2, 3, 4, 5, 6}; int[] dest1 = Arrays.copyOf(src1, src1.length); System.out.println(Arrays.toString(dest1)); } } ``` 2. **二维数组操作** - **示例程序** ```java public class MatrixOperations { public static void main(String[] args) { int[][] matrix = new int[6][6]; for (int i = 0; i < 6; i++) { for (int j = 0; j < 6; j++) { matrix[i][j] = (int) (Math.random() * 21 + 10); } } // 打印矩阵 for (int i = 0; i < 6; i++) { for (int j = 0; j < 6; j++) { System.out.print(matrix[i][j] + " "); } System.out.println(); } // 求最大元素值及其位置 int max = Integer.MIN_VALUE; int[] maxPosition = new int[2]; for (int i = 0; i < 6; i++) { for (int j = 0; j < 6; j++) { if (matrix[i][j] > max) { max = matrix[i][j]; maxPosition[0] = i; maxPosition[1] = j; } } } System.out.println("最大值: " + max + " 位置: (" + maxPosition[0] + ", " + maxPosition[1] + ")"); // 转置矩阵 int[][] transposedMatrix = new int[6][6]; for (int i = 0; i < 6; i++) { for (int j = 0; j < 6; j++) { transposedMatrix[i][j] = matrix[j][i]; } } // 打印转置矩阵 System.out.println("转置矩阵:"); for (int i = 0; i < 6; i++) { for (int j = 0; j < 6; j++) { System.out.print(transposedMatrix[i][j] + " "); } System.out.println(); } } } ``` 3. **冒泡排序** - **示例程序** ```java public class BubbleSort { public static void main(String[] args) { int[] arr = {4, 3, 9, 1, 6, 2, 8, 5, 7, 0}; for (int i = 0; i < arr.length - 1; i++) { for (int j = 0; j < arr.length - 1 - i; j++) { if (arr[j] > arr[j + 1]) { int temp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = temp; } } } System.out.println(Arrays.toString(arr)); } } ``` #### 项目五:类和对象 1. **程序改错** - **第一个程序** ```java public class Test1 { private String data1 = "中国,我爱你!"; public static void main(String[] args) { System.out.println(data1); // 修改:添加static关键字 } } ``` - **第二个程序** ```java public class Test1 { public void showData() { System.out.println("中国,我爱你!"); } public static void main(String[] args) { Test1 test1 = new Test1(); // 修改:实例化对象并调用方法 test1.showData(); } } ``` 2. **定义学生类** - **示例程序** ```java public class Student { private long studentID; private int classID; private String name; private String sex; private int age; // Getter and Setter methods public long getStudentID() { return studentID; } public void setStudentID(long studentID) { this.studentID = studentID; } public int getClassID() { return classID; } public void setClassID(int classID) { this.classID = classID; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } // Constructor public Student(long studentID, int classID, String name, String sex, int age) { this.studentID = studentID; this.classID = classID; this.name = name; this.sex = sex; this.age = age; } // toString method @Override public String toString() { return "Student{" + "studentID=" + studentID + ", classID=" + classID + ", name='" + name + '\'' + ", sex='" + sex + '\'' + ", age=" + age + '}'; } // Main method to create objects and print information public static void main(String[] args) { Student student1 = new Student(123456, 1, "张三", "男", 20); System.out.println(student1); } } ``` 以上是针对给定文件中的各个任务进行的详细解析和示例代码展示,希望能够帮助理解和掌握相关的Java基础知识和技术要点。
- 粉丝: 0
- 资源: 1
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助