实验名称??? 实验一 从面向过程到面向对象
实验目的???
1. 掌握Java语言简单数据类型、表达式、输入输出;
2. 理解Java类中main方法的编写及过程化编程;
3. 掌握Java数组的使用。???????????
实验内容
1. 输入年份,判断是否是闰年。闰年的条件是符合下面两个条件之一:
(1)能被400整除的年份是闰年;
(2)能被4整除,但不能被100整除的年份是闰年;
2. 输入0~100的成绩,输出优、良、中、及格、不及格的等级。
3. 使用while循环,利用公式求的值。
4. 白马百担问题。大马驮3担,中马驮2担,两匹小马驮1担,现有100匹马正好驮100担,问:大、中、小马各有几匹?
5. 实现冒泡法排序,如将10个整数按从小大顺序输出。代码中适当使用增强型for循环:for(数组元素类型 e: 数组名)。
6. 打印杨辉三角
import java.util.Scanner;
public class Main {
??? public static void main(String[] args) {
??????? Scanner scanner = new Scanner(System.in);
??????? // 1. 判断是否是闰年
??????? System.out.println("1. 请输入年份:");
??????? int year = scanner.nextInt();
??????? if (isLeapYear(year)) {
??????????? System.out.println(year + "年是闰年");
??????? } else {
??????????? System.out.println(year + "年不是闰年");
??????? }
??????? // 2. 成绩等级
??????? System.out.println("2. 请输入成绩(0~100):");
??????? int score = scanner.nextInt();
??????? printGrade(score);
??????? // 3. 使用while循环求值
??????? System.out.println("3. 使用while循环,输入n求1到n的和:");
??????? float pi = 0;
??????? float q = 1;
??????? float sign = 1;
??????? do {
??????????? pi += sign / q;
??????????? sign = -sign;
??????????? q += 2;
??????? } while(q <= 1000000);
??????? pi = 4 * pi;
??????? System.out.println("pi = " + pi);
??????? // 4. 白马百担问题
??????? System.out.println("4. 白马百担问题的解:");
??????? solveHorseProblem();
??????? // 5. 冒泡法排序
??????? System.out.println("5. 冒泡法排序:");
??????? int[] array = {5, 3, 8, 1, 2, 7, 4, 6, 10, 9};
??????? bubbleSort(array);
??????? System.out.print("排序后的数组:");
??????? for (int num : array) {
??????????? System.out.print(num + " ");
??????? }
??????? System.out.println();
??????? // 6. 打印杨辉三角
??????? System.out.println("6. 打印杨辉三角:");
??????? printPascalTriangle(8);
??? }
??? // 判断是否是闰年
??? private static boolean isLeapYear(int year) {
??????? return (year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0));
??? }
??? // 成绩等级
??? private static void printGrade(int score) {
??????? if (score >= 90) {
??????????? System.out.println("优");
??????? } else if (score >= 80) {
??????????? System.out.println("良");
??????? } else if (score >= 70) {
??????????? System.out.println("中");
??????? } else if (score >= 60) {
??????????? System.out.println("及格");
??????? } else {
??????????? System.out.println("不及格");
??????? }
??? }
??? // 使用while循环,求1到n的和
??? private static int calculateSum(int n) {
??????? int sum = 0;
??????? int i = 1;
??????? while (i <= n) {
??????????? sum += i;
??????????? i++;
??????? }
??????? return sum;
??? }
??? // 白马百担问题
??? private static void solveHorseProblem() {
??????? System.out.println("big\tmid\tsmall");
??????? System.out.println("----------------------------");
??????? for(int big = 0; big <= 33; big++) {
??????????? for(int mid = 0; mid <= 50; mid++)? {
??????????????? int small = 100 - big - mid;
??????????????? if(small % 2 == 0 && 3*big + 2*mid + small/2 == 100) {
??????????????????? System.out.println(big + "\t" + mid + "\t" + small);
??????????????? }
??????????? }
??????? }
??? }
??? // 冒泡法排序
??? private static void bubbleSort(int[] array) {
??????? int n = array.length;
??????? for (int i = 0; i < n - 1; i++) {
??????????? for (int j = 0; j < n - i - 1; j++) {
??????????????? if (array[j] > array[j + 1]) {
??????????????????? // 交换元素
??????????????????? int temp = array[j];
??????????????????? array[j] = array[j + 1];
??????????????????? array[j + 1] = temp;
??????????????? }
??????????? }
??????? }
??? }
??? // 打印杨辉三角
// 6. 打印杨辉三角
??? private static void printPascalTriangle(int rows) {
??????? int[][] triangle = new int[rows][];
??????? for (int i = 0; i < rows; i++) {
??????????? triangle[i] = new int[i + 1];
??????????? triangle[i][0] = triangle[i][i] = 1;
??????????? for (int j = 1; j < i; j++) {
??????????????? triangle[i][j] = triangle[i - 1][j - 1] + triangle[i - 1][j];
??????????? }
??????? }
??????? // 打印杨辉三角
??????? for (int i = 0; i < rows; i++) {
??????????? // 打印左侧空格
??????????? for (int k = 0; k < rows - i - 1; k++) {
??????????????? System.out.print(" ");
??????????? }
??????????? for (int j = 0; j <= i; j++) {
??????????????? System.out.print(triangle[i][j] + " ");
??????????? }
??????????? System.out.println();
??????? }
??? }
}
实验程序及结果(附录)
思考
如何取得二维数组a的行数以及第i行的列数?
在Java中,你可以使用以下方法来获取二维数组的行数以及特定行的列数:
1. **获取二维数组的行数:**
?? ```java
?? int rowCount = array.length;
?? ```
?? 这里,`array.length`将返回二维数组的行数。
2. **获取特定行的列数:**
?? int specificRow = 2; // 例如,获取第2行的列数
?? int columnCount = array[specificRow].length;
?? 这里,`array[specificRow].length`将返回第`specificRow`行的列数。
public class ArrayExample {
??? public static void main(String[] args) {
??????? // 创建一个二维数组
??????? int[][] array = {
??????????????? {1, 2, 3},
??????????????? {4, 5, 6},
??????????????? {7, 8, 9}
??????? };
??????? // 获取行数
??????? int rowCount = array.length;
??????? System.out.println("行数:" + rowCount);
??????? // 获取第2行的列数
??????? int specificRow = 1;
??????? int columnCount = array[specificRow].length;
??????? System.out.println("第" + (specificRow + 1) + "行的列数:" + columnCount);
??? }
}
在这个示例中,`rowCount`将输出3,表示二维数组有3行。`columnCount`将输出3,表示第2行有3列。
1. Java语言简介
1.1 Java的起源与发展
1.2 Java的特点与优势
2. 基本数据类型与表达式
2.1 整型、浮点型、字符型等数据类型
2.2 算术、关系、逻辑等表达式
// 示例代码:基本数据类型与表达式
public class DataTypesAndExpressions {
??? public static void main(String[] args) {
??????? // 数据类型示例
??????? int integerNumber = 10;
??????? double floatingPointNumber = 3.14;
??????? char character = 'A';
??????? // 表达式示例
??????? int result = 2 + 3 * 4;
??????? boolean isGreater = (result > 10);
??????? // 输出结果
??????? System.out.println("Integer: " + integerNumber);
??????? System.out.println("Floating Point: " + floatingPointNumber);
??????? System.out.println("Character: " + character);
??????? System.out.println("Result: " + result);
??????? System.out.println("Is Greater: " + isGreater);
??? }
}
3. 输入与输出
3.1?Scanner?类的使用
import java.util.Scanner;
public class InputOutputExample {
??? public static void main(String[] args) {
??????? // 使用Scanner类获取用户输入
??????? Scanner scanner = new Scanner(System.in);
??????? System.out.print("Enter your name: ");
??????? String name = scanner.nextLine();
??????? System.out.print("Enter your age: ");
??????? int age = scanner.nextInt();
??????? // 输出用户输入
??????? System.out.println("Name: " + name);
??????? System.out.println("Age: " + age);
??????? // 关闭Scanner
??????? scanner.close();
??? }
}
3.2 控制台输出与格式化输出
public class OutputFormattingExample {
??? public static void main(String[] args) {
??????? String name = "John";
??????? int age = 25;
??????? // 简单输出
??????? System.out.println("Name: " + name);
??????? System.out.println("Age: " + age);
??????? // 格式化输出
??????? System.out.printf("Name: %s, Age: %d%n", name, age);
??? }
}
4. 过程化编程与面向对象编程
4.1 过程化编程概念与示例
public class ProceduralExample {
??? public static void main(String[] args) {
??????? // 过程式编程示例
??????? int result = addNumbers(5, 7);
??????? System.out.println("Sum: " + result);
??? }
??? // 过程式编程中的过程
??? private static int addNumbers(int a, int b) {
??????? return a + b;
??? }
}
4.2 面向对象编程基本概念
// 面向对象编程示例
public class ObjectOrientedExample {
??? public static void main(String[] args) {
??????? // 创建对象
??????? Car myCar = new Car("Toyota", "Camry", 2022);
??????? // 访问对象的属性和方法
??????? myCar.displayInfo();
??????? myCar.startEngine();
??? }
}
// 定义一个简单的Car类
class Car {
??? String make;
??? String model;
??? int year;
??? // 构造方法
??? public Car(String make, String model, int year) {
??????? this.make = make;
??????? this.model = model;
??????? this.year = year;
??? }
??? // 方法
??? public void displayInfo() {
??????? System.out.println("Car: " + year + " " + make + " " + model);
??? }
??? public void startEngine() {
??????? System.out.println("Engine started!");
??? }
}
5. 主方法(main方法)
5.1 Java程序执行入口
// 主方法示例
public class MainMethodExample {
??? public static void main(String[] args) {
??????? // 主方法的代码逻辑
??????? System.out.println("Hello, Java!");
??? }
}
6. 数组的使用
6.1 一维数组与二维数组
// 数组示例
public class ArrayExample {
??? public static void main(String[] args) {
??????? // 一维数组
??????? int[] numbers = {1, 2, 3, 4, 5};
??????? // 二维数组
??????? int[][] matrix = {
??????????????? {1, 2, 3},
??????????????? {4, 5, 6},
??????????????? {7, 8, 9}
??????? };
??????? // 访问数组元素
??????? int firstElement = numbers[0];
??????? int matrixElement = matrix[1][2];
??????? // 输出结果
??????? System.out.println("First Element: " + firstElement);
??????? System.out.println("Matrix Element: " + matrixElement);
??? }
}
6.2 获取数组的长度与元素访问
// 数组示例:获取数组长度与元素访问
public class ArrayOperationsExample {
??? public static void main(String[] args) {
??????? // 一维数组
??????? int[] numbers = {1, 2, 3, 4, 5};
??????? // 获取数组长度
??????? int length = numbers.length;
??????? // 遍历数组并访问元素
??????? System.out.print("Array Elements: ");
??????? for (int i = 0; i < length; i++) {
??????????? System.out.print(numbers[i] + " ");
??????? }
??????? System.out.println();
??? }
}
7. 条件判断与循环
7.1?if-else语句
// 条件判断示例:if-else语句
public class IfElseExample {
??? public static void main(String[] args) {
??????? int number = 10;
??????? // 单条件判断
??????? if (number > 0) {
??????????? System.out.println("Number is positive");
??????? }
??????? // 多条件判断
??????? if (number > 0) {
??????????? System.out.println("Number is positive");
??????? } else if (number < 0) {
??????????? System.out.println("Number is negative");
??????? } else {
??????????? System.out.println("Number is zero");
??????? }
??? }
}
7.2?switch语句
// 条件判断示例:switch语句
public class SwitchExample {
??? public static void main(String[] args) {
??????? int dayOfWeek = 3;
??????? String dayName;
??????? // 使用switch语句判断星期几
??????? switch (dayOfWeek) {
??????????? case 1:
??????????????? dayName = "Monday";
??????????????? break;
??????????? case 2:
??????????????? dayName = "Tuesday";
??????????????? break;
??????????? case 3:
??????????????? dayName = "Wednesday";
??????????????? break;
??????????? case 4:
??????????????? dayName = "Thursday";
??????????????? break;
??????????? case 5:
??????????????? dayName = "Friday";
??????????????? break;
??????????? case 6:
??????????????? dayName = "Saturday";
??????????????? break;
??????????? case 7:
??????????????? dayName = "Sunday";
??????????????? break;
??????????? default:
??????????????? dayName = "Invalid day";
??????? }
??????? // 输出结果
??????? System.out.println("Day of the week: " + dayName);
??? }
}
7.3?while、do-while、for循环
// 循环示例
public class LoopExample {
??? public static void main(String[] args) {
??????? // while循环
??????? int i = 0;
??????? while (i < 5) {
??????????? System.out.print(i + " ");
??????????? i++;
??????? }
??????? System.out.println();
??????? // do-while循环
??????? int j = 0;
??????? do {
??????????? System.out.print(j + " ");
??????????? j++;
??????? } while (j < 5);
??????? System.out.println();
??????? // for循环
??????? for (int k = 0; k < 5; k++) {
??????????? System.out.print(k + " ");
??????? }
??????? System.out.println();
??? }
}
8. 方法与函数
8.1 方法的定义与调用
// 方法示例
public class MethodExample {
??? public static void main(String[] args) {
??????? // 调用方法
??????? int result = addNumbers(5, 7);
??????? // 输出结果
??????? System.out.println("Sum: " + result);
??? }
??? // 方法的定义
??? private static int addNumbers(int a, int b) {
????? ??return a + b;
??? }
}
8.2 方法的参数与返回值
// 方法参数与返回值示例
public class MethodParametersAndReturnValue {
??? public static void main(String[] args) {
??????? // 调用带参数的方法
??????? greetUser("John");
??????? // 调用带返回值的方法
??????? int sum = addNumbers(3, 5);
??????? System.out.println("Sum: " + sum);
??? }
??? // 带参数的方法
??? private static void greetUser(String name) {
??????? System.out.println("Hello, " + name + "!");
??? }
??? // 带返回值的方法
??? private static int addNumbers(int a, int b) {
??????? return a + b;
??? }
}
9. 逻辑运算与位运算
9.1 逻辑运算符(与、或、非)
// 逻辑运算符示例
public class LogicalOperatorsExample {
??? public static void main(String[] args) {
??????? boolean condition1 = true;
??????? boolean condition2 = false;
??????? // 逻辑与
??????? boolean resultAnd = condition1 && condition2;
??????? System.out.println("Result of AND: " + resultAnd);
??????? // 逻辑或
??????? boolean resultOr = condition1 || condition2;
??????? System.out.println("Result of OR: " + resultOr);
??????? // 逻辑非
??????? boolean resultNot = !condition1;
??????? System.out.println("Result of NOT: " + resultNot);
??? }
}
9.2 位运算符与操作
// 位运算符示例
public class BitwiseOperatorsExample {
??? public static void main(String[] args) {
??????? int number1 = 5;? // 二进制表示为 0101
??????? int number2 = 3;? // 二进制表示为 0011
??????? // 位与
??????? int resultAnd = number1 & number2;
??????? System.out.println("Result of AND: " + resultAnd);
??????? // 位或
??????? int resultOr = number1 | number2;
??????? System.out.println("Result of OR: " + resultOr);
??????? // 位异或
??????? int resultXor = number1 ^ number2;
??????? System.out.println("Result of XOR: " + resultXor);
??????? // 位非
??????? int resultNot = ~number1;
??????? System.out.println("Result of NOT: " + resultNot);
??? }
}
10. 排序算法与冒泡排序
10.1 常见排序算法概述
10.2 冒泡排序算法与实现
// 冒泡排序示例
public class BubbleSortExample {
??? public static void main(String[] args) {
??????? int[] array = {5, 3, 8, 1, 2, 7, 4, 6, 10, 9};
??????? // 调用冒泡排序方法
??????? bubbleSort(array);
??????? // 输出排序后的数组
??????? System.out.print("Sorted Array: ");
??????? for (int num : array) {
??????????? System.out.print(num + " ");
??????? }
??????? System.out.println();
??? }
??? // 冒泡排序算法
??? private static void bubbleSort(int[] array) {
??????? int n = array.length;
??????? for (int i = 0; i < n - 1; i++) {
??????????? for (int j = 0; j < n - i - 1; j++) {
??????????????? if (array[j] > array[j + 1]) {
??????????????????? // 交换元素
??????????? ????????int temp = array[j];
??????????????????? array[j] = array[j + 1];
??????????????????? array[j + 1] = temp;
??????????????? }
??????????? }
??????? }
??? }
}
11. 问题求解与编程思维
11.1 解决问题的步骤与方法
11.2 编程思维的培养
12. 附加题目:杨辉三角
12.1 数学概念与应用
12.2 二维数组的使用
// 杨辉三角示例
public class PascalTriangleExample {
??? public static void main(String[] args) {
??????? printPascalTriangle(8);
??? }
??? // 打印杨辉三角
??? private static void printPascalTriangle(int rows) {
??????? int[][] triangle = new int[rows][];
??????? for (int i = 0; i < rows; i++) {
??????????? triangle[i] = new int[i + 1];
??????????? triangle[i][0] = triangle[i][i] = 1;
??????????? for (int j = 1; j < i; j++) {
??????????????? triangle[i][j] = triangle[i - 1][j - 1] + triangle[i - 1][j];
??????????? }
??????? }
??????? // 打印杨辉三角
??????? for (int i = 0; i < rows; i++) {
??????????? // 打印左侧空格
??????????? for (int k = 0; k < rows - i - 1; k++) {
??????????????? System.out.print(" ");
??????????? }
??????????? for (int j = 0; j <= i; j++) {
??????????????? System.out.print(triangle[i][j] + " ");
??????????? }
??????????? System.out.println();
??????? }
??? }
}
13. 附加题目:白马百担问题
13.1 数学建模与问题求解
13.2 嵌套循环的应用
// 白马百担问题示例
public class HorseProblemExample {
??? public static void main(String[] args) {
??????? solveHorseProblem();
??? }
??? // 白马百担问题求解
??? private static void solveHorseProblem() {
??????? System.out.println("big\tmid\tsmall");
??????? System.out.println("----------------------------");
??????? for (int big = 0; big <= 33; big++) {
??????????? for (int mid = 0; mid <= 50; mid++) {
??????????????? int small = 100 - big - mid;
??????????????? if (small % 2 == 0 && 3 * big + 2 * mid + small / 2 == 100) {
??????????????????? System.out.println(big + "\t" + mid + "\t" + small);
??????????????? }
??????????? }
??????? }
??? }
}
14. 实践:实验程序分析与优化
14.1 对实验程序的分析
14.2 代码优化与改进建议
15. 总结与思考
15.1 课程内容的回顾
15.2 Java编程的发展方向