《Java语言程序设计》试卷1
商丘师范学院2026——2027学年度第二学期期终考试
《Java语言程序设计》试卷1
要求:将下面五题的 代码和运行截图 分别写在相应的题目下方。
文件命名为:学号+姓名,例如1234张三。
编程题(本题满分100分)
- (15分)将一个4*4的整型矩阵逆置并输出。(16分)
2. package test1;
public class test1 {
public static void main(String[] args) {
int[][] matrix = {
{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12},
{13, 14, 15, 16}
};
System.out.println("原4*4矩阵:");
printMatrix(matrix);
int[][] reverseMat = new int[4][4];
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
reverseMat[j][i] = matrix[i][j];
}
}
System.out.println("\n逆置后的矩阵:");
printMatrix(reverseMat);
}
public static void printMatrix(int[][] arr) {
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[i].length; j++) {
System.out.print(arr[i][j] + "\t");
}
System.out.println();
}
}
}

2.(20分)定义一个三角形类,具有成员:三条边长度,分别写get和set方法,无参和有参的构造方法,求周长和面积的方法。在测试类中创建对象并求出其周长和面积。
提示:可使用java.lang.Math类的sqrt(double)方法求平方根。
class Triangle {
private double a;
private double b;
private double c;
public Triangle() {}
public Triangle(double a, double b, double c) {
this.a = a;
this.b = b;
this.c = c;
}
// get set
public double getA() { return a; }
public void setA(double a) { this.a = a; }
public double getB() { return b; }
public void setB(double b) { this.b = b; }
public double getC() { return c; }
public void setC(double c) { this.c = c; }
public double getPerimeter() {
return a + b + c;
}
public double getArea() {
double p = getPerimeter() / 2;
return Math.sqrt(p * (p – a) * (p – b) * (p – c));
}
}
测试
public class test {
public static void main(String[] args) {
Triangle tri = new Triangle(3, 4, 5);
System.out.println(“三角形周长:” + tri.getPerimeter());
System.out.println(“三角形面积:” + tri.getArea());
}
}

3.(30分)4.定义一个动物类Animal以及宠物接口pet,及他的两个子类:狗类Dog,猫类Cat,具体属性方法如下图所示(也可以根据需要再添加其他成员)。捕食和撒娇方法的实现可以自行设计。
在测试类中,分别创建狗和猫的对象,并通过pet接口或者animal类分别调用他们捕食的方法。

package test3;
public abstract class Animal {
protected String name;
public Animal() {
}
public Animal(String name) {
this.name = name;
}
public abstract void hunt();
}
package test3;
public interface Pet {
public void coquetry();
}
package test3;
public class Dog extends Animal implements Pet{
private String length;
private String height;
public Dog() {
}
public Dog(String name, String length, String height) {
super(name);
this.length = length;
this.height = height;
}
@Override
public void hunt() {
System.out.println(super.name+”正在捕食”);
}
@Override
public void coquetry() {
System.out.println(super.name+”小狗正在和主人玩耍,撒娇”);
}
}
package test3;
public class Cat extends Animal implements Pet{
private String length;
private String height;
public Cat() {
}
public Cat(String name, String length, String height) {
super(name);
this.length = length;
this.height = height;
}
@Override
public void hunt() {
System.out.println(super.name+”小猫正在捕食”);
}
@Override
public void coquetry() {
System.out.println(super.name+”正在和主人玩耍,撒娇”);
}
}
package test3;
public class test {
public static void main(String[] args) {
Dog dog = new Dog(“旺财”,”100cm”,”50cm”);
dog.hunt();
dog.coquetry();
Cat cat = new Cat(“小花”,”100cm”,”50cm”);
cat.hunt();
cat.coquetry();
}
}
4.(15分)编写java程序,采用任意一种IO流,实现将file.txt复制到tofile.txt中。

package test4;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class FileCopy {
public static void main(String[] args) {
// 源文件、目标文件路径
String src = "C:\\Users\\Administrator\\Desktop\\file.txt";
String dest = "C:\\Users\\Administrator\\Desktop\\tofile.txt";
try (
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(src));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(dest))
) {
byte[] buf = new byte[1024];
int len;
while ((len = bis.read(buf)) != -1) {
bos.write(buf, 0, len);
}
bos.flush();
System.out.println("文件复制完成!");
} catch (IOException e) {
e.printStackTrace();
}
}
}
5.(20分)在mysql中创建eams数据库,并在库中建立user表,包括id、用户名和密码字段。
使用jdbc执行登录操作,并输出登录结果。



CREATE DATABASE IF NOT EXISTS eams;
USE eams;
CREATE TABLE IF NOT EXISTS user (
id INT PRIMARY KEY AUTO_INCREMENT,
username VARCHAR(20) NOT NULL UNIQUE,
password VARCHAR(20) NOT NULL
);
INSERT INTO user(username,password) VALUES (“admin”,”123456″),(“test”,”666666″);
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;
public class JDBCLogin {
static final String URL = “jdbc:mysql://localhost:3306/eams?useSSL=false&serverTimezone=UTC”;
static final String DB_USER = “root”; // 自己的mysql账号
static final String DB_PWD = “root”; // 自己的mysql密码
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print(“请输入用户名:”);
String username = sc.next();
System.out.print(“请输入密码:”);
String password = sc.next();
sc.close();
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
Class.forName(“com.mysql.cj.jdbc.Driver”);
conn = DriverManager.getConnection(URL, DB_USER, DB_PWD);
String sql = “SELECT * FROM user WHERE username=? AND password=?”;
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, username);
pstmt.setString(2, password);
rs = pstmt.executeQuery();
if (rs.next()) {
System.out.println(“登录成功!欢迎用户:” + username);
} else {
System.out.println(“用户名或密码错误,登录失败!”);
}
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
} finally {
try {
if (rs != null) rs.close();
if (pstmt != null) pstmt.close();
if (conn != null) conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}