Java历年期末考试复习总结

目录

一、Java期末试题

1.1 2022期末

1.2 2022补考

1.3 程序填空题

1.4 往年其他考试题(自行参考)

二、2023Java题型以及分值分布

2.1 2023期末考试题(记忆版本)


一、Java期末试题

1.1 2022期末

1.1.1 给出程序输出结果

代码块:

class MyException extends Exception{
    private int detail;
    MyException(int a){
        detail = a;
    }
    public String toString(){
        return "Exception("+detail+")";
    }
}
public class Eception_Demo {
    static void compute(int a) throws MyException{
        System.out.println("Called compute("+a+")");
        if(a > 20)throw new MyException(a);
        System.out.println("Normal exit!");
    }
    public static void main(String [] args){
        try{
            compute(20);
            compute(33);
            compute(1);
        }catch (MyException e){
            System.out.println("Exception caught :" + e.toString());
        }
    }
}

输出结果:

Called compute(20)
Normal exit!
Called compute(33)
Exceptiob caught :Exception(33)

1.1.2 编写一个 MatrixText 类,其中定义一个整型二维数组(10 行10 列) 要求数组中的随机整数不能重复,最终能在屏幕上按行列显示数据,并能输出数组中的最大数极其行号和列号。

import java.util.Random;

public class MatrixText {
    private int[][] matrix;

    public MatrixText() {
        matrix = new int[10][10];
        Random random = new Random();
        int[] used = new int[100];
        for(int i =0; i < 100; i++){
            used[i] = 0;
        }
        for (int i = 0; i < 10; i++) {
            for (int j = 0; j < 10; j++) {
                int num;
                do {
                    num = random.nextInt(100);
                } while (used[num] == 1);
                matrix[i][j] = num;
                used[num] = 1;
            }
        }
    }
    public void printMatrixText(){
        System.out.println("二维数组是:");
        for (int i = 0; i < 10; i++) {
            for (int j = 0; j < 10; j++) {
                System.out.print("\t" + matrix[i][j] + "\t");
            }
            System.out.println();
        }
    }
    public void findMaxNum(){
        int maxNum = matrix[0][0], max_col = 0, max_row = 0;
        for (int i = 0; i < 10; i++) {
            for (int j = 0; j < 10; j++) {
                if(matrix[i][j] > maxNum){
                    maxNum = matrix[i][j];
                    max_row = i + 1;
                    max_col = j + 1;
                }
            }
        }
        System.out.println("最大数是" + maxNum);
        System.out.println("行号是" + max_row);
        System.out.println("列号是" + max_col);
    }
    public static void main(String [] args){
        MatrixText mat = new MatrixText();
        mat.printMatrixText();
        mat.findMaxNum();
    }
}


1.1.3 使用javasocket 编程,结合多线程技术,编写服务器端的程序。要求: 服务器端能监听多个客户端的连接(端口号为 7777),一旦和客户端连接成功后,能读取客户端的发送过来的信息,并在服务器的本地控制台上输出,如果读取到的信息中以“VIP”字串开头 (不区分大小写),那么将读取的信息加上前缀“Vip2022:回发给客户端,否则,将读取到的信息加上前缀”2022:"回发给客户端。“

package examination;

import java.io.*;
import java.net.*;

public class Server {
    public static void main(String[] args) throws IOException {
        //创建服务器端的 Socket,监听 7777 端口
        ServerSocket serverSocket = new ServerSocket(7777);

        //启动服务器,等待客户端连接
        while (true) {
            Socket socket = serverSocket.accept(); //阻塞式方法,等待客户端连接
            //为每个客户端启动一个线程
            new ClientThread(socket).start();
        }
    }
}

class ClientThread extends Thread {
    private Socket socket;

    public ClientThread(Socket socket) {
        this.socket = socket;
    }

    //处理客户端请求
    public void run() {
        try {
            //获取 I/O 流
            BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            PrintWriter out = new PrintWriter(socket.getOutputStream(), true);

            //循环读取客户端发送的消息,等待客户端发送消息,如果读到的内容为 null,说明客户端已关闭连接,跳出循环
            String line;
            while ((line = in.readLine()) != null) {
                //根据消息内容进行处理,并回复客户端
                if (line.toUpperCase().startsWith("VIP")) { //如果读到的内容以 "VIP" 开头(不区分大小写)
                    out.println("Vip2022:" + line); //加上前缀 "Vip2022:",回复客户端
                } else { //否则
                    out.println("2022:" + line); //加上前缀 "2022:",回复客户端
                }
            }

            //关闭 I/O 流和客户端 Socket
            in.close();
            out.close();
            socket.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

1.1.4 

 设计一个个人数据类 Pclass,包含两个私有的宇符串类型的属性: 姓名 (name) 和血型(blood),方法 showName(显示姓名,方法showBlood () 显示血型,并要为私有的属性编写相应的 get 和 set 方法。包含两个构造器,一个构造器只能初始化姓名,另一个构造器可以用来初始化姓名和血型,并能能够记录创建个人对象的总个数,编写一个公有类 Test用来测试 Pclass 类,分别使用 Pclass 的两个构造器来定义对象,并调用其方法。

1)Pclass.java

public class PClass {
    private String name;
    private String blood;
    private static int count = 0;
    public PClass(String name) {
        this.name = name;
        count ++;
    }

    public PClass(String name, String blood) {
        this.name = name;
        this.blood = blood;
        count ++;
    }

    public void showName() {
        System.out.println(name);
    }

    public void showBlood() {
        System.out.println(blood);
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getBlood() {
        return blood;
    }

    public void setBlood(String blood) {
        this.blood = blood;
    }
    public static int getCount() {
        return count;
    }
}

2)Test

public class Test {
    public static void main(String[] args) {
        PClass p1 = new PClass("张三");
        p1.showName();
        p1.showBlood();   // 没有blood属性,为null
        p1.setBlood("A");
        p1.showBlood();

        PClass p2 = new PClass("李四", "B");
        p2.showName();
        p2.showBlood();

        System.out.println(PClass.getCount());  // 输出2
    }
}

1.1.5  D 盘根目录下的 sample.txt 文件包含 200 行建筑设计中的各类文件信息,每一行表示一个文件,每一行的结构都是一样的(文件创建的时间、文件大小 (KB) 和文件名),下面给出了 3 行示例信息,编写程序实现统计 jpg 类型文件的数量,同时统计 jpg 文件大小的总和(MB)。

代码:

import java.io.*;
import java.net.FileNameMap;
import java.util.*;
public class examFile2 {
    public static void main(String [] args) throws IOException{
        File f = new File("sample.txt");
        if(!f.exists()){
            System.out.println(f + "文件不存在!!");
            System.exit(1);
        }
        Scanner r = new Scanner(f);
        //jpg_sum_size是jpg文件大小的总和, jpg_count是jpg类型文件的数量
        int jpg_sum_size = 0, jpg_count = 0;
        HashMap<String, Integer> FileTypesNum = new HashMap();
        while(r.hasNext()){
            String date = r.next();
            String time = r.next();
            double size = r.nextDouble();
            String name = r.next();
            String file_type = name.split("\\.")[1];
            if (file_type.equals("jpg")){
                jpg_count++;
                jpg_sum_size += size;
            }
        }
        System.out.println("jpg类型文件的数量:" + jpg_count);
        System.out.println("jpg文件大小的总和是:" + jpg_sum_size/1024 + "MB");
    }
}

1.1.6 使用面向对象的设计,利用封装、继承、多态的机制,设计和实现一个各种“形“的管理系统,具体有矩形、正方形和圆形,要求能够分别求各种形的周长和面积,其中 ShapeTest 类中有一个能求各种形的周长之和的方法(例如,能求 3 个矩形,2 个正方形和 3 个圆的周长之和),并提供了 main 函数 (程序入口) 进行测试。

abstract class Shape {
    protected double area;  // 面积
    protected double perimeter;  // 周长

    public abstract double getArea();  // 求面积的方法
    public abstract double getPerimeter();  // 求周长的方法
}

class Rectangle extends Shape {
    private double width;
    private double height;

    public Rectangle(double width, double height) {
        this.width = width;
        this.height = height;
    }

    @Override
    public double getArea() {
        area = width * height;
        return area;
    }

    @Override
    public double getPerimeter() {
        perimeter = 2 * (width + height);
        return perimeter;
    }
}

class Square extends Rectangle {
    public Square(double side) {
        super(side, side);
    }
}

class Circle extends Shape {
    private double radius;

    public Circle(double radius) {
        this.radius = radius;
    }

    @Override
    public double getArea() {
        area = Math.PI * radius * radius;
        return area;
    }

    @Override
    public double getPerimeter() {
        perimeter = 2 * Math.PI * radius;
        return perimeter;
    }
}

public class ShapeTest {
    public static double getAllarea(Shape[] shapes){
        double totalPerimeter = 0;
        for (int i = 0; i < shapes.length; i++){
            totalPerimeter += shapes[i].getPerimeter();
        }
        return totalPerimeter;
    }
    public static void main(String[] args) {
        Shape[] shapes = new Shape[8];
        shapes[0] = new Rectangle(2.0, 3.0);
        shapes[1] = new Rectangle(4.0, 5.0);
        shapes[2] = new Rectangle(1.5, 2.5);
        shapes[3] = new Square(2.0);
        shapes[4] = new Square(3.0);
        shapes[5] = new Circle(2.0);
        shapes[6] = new Circle(3.0);
        shapes[7] = new Circle(4.0);
        System.out.println("各种形的周长之和为:" + getAllarea(shapes));
    }
}

1.2 2022补考

1.2.1 程序分析题(10分)

代码块:

class MyException2 extends Exception{
    private int detail;
    MyException2(int a){
        detail = a;
    }
    public String toString(){
        return "MyException{"+detail+"}";
    }
}
public class ExceptionDemo2 {
    static void compute(int a)throws MyException2{
        System.out.println("Called compute["+a+"]");
        if(a > 10)throw new MyException2(a);
        System.out.println("Normal exit!");
    }
    public static void main(String [] args){
        try{
            compute(3);
            compute(21);
            compute(7);
        }catch (MyException2 e){
            System.out.println("Exceptiob caught :" + e.toString());
        }
    }
}

程序输出结果:

Called compute[3]
Normal exit!
Called compute[21]
Exceptiob caught :MyException{21}

1.2.2  编写一个 Java 类,有多个人的成绩存放在 score 数组中,请在类中编写函数函数的功能是:将高于平均分的人数作为函数值返回,将高于平均分的分数放在up 所指的数组中。例如,当 score 数组中的数据为 24,35,88,76,90,54,59.66,96 时,函数返回的人数应该是 5,up 中的数据应为 88,76,90,66,96;并在类中编写 main 函,对所编写的函数进行测试。(15 分,将程序写在答题纸上)

package examination;
/*
1. score数组存放多个人的成绩,getUpScore()方法返回高于平均分的人数,并将高于平均分的成绩存入up数组。
2. getUpScore()方法先计算score数组的平均分,然后遍历score数组,如果成绩大于平均分,就存入up数组,并统计人数count。
3. 最后返回高于平均分的人数count。
4. main()方法调用getUpScore()方法,得到高于平均分的人数和up数组,并打印输出。
5. 这样就测试了getUpScore()方法的实现,满足作业要求。
 */
public class Score {
    private int[] scores = {24, 35, 88, 76, 90, 54, 59, 66, 96};

    public int getUpScore(int[] up) {
        int sum = 0;
        for (int i = 0; i < scores.length; i++) {
            sum += scores[i];
        }
        int aver = sum / scores.length;
        int j = 0;
        for (int i = 0; i < scores.length; i++) {
            if (scores[i] > aver) {
                up[j] = scores[i];
                j++;
            }
        }
        return j;
    }

    public static void main(String[] args) {
        Score s = new Score();
        int[] up = new int[100];
        int count = s.getUpScore(up);

        System.out.println("高于平均分的人数:" + count);
        for (int i = 0; i < count; i++) {
            System.out.print(up[i] + "  ");
        }
    }
}

1.2.3 通过 Java 多线程的技术,创建线程t 和 s,分别在控制台上显示字符串“您好”和“他好”,分别暂停的时间为 25mm(毫秒)和 75mm,分别显示的次数为20 次。主线程 (main)在命令行窗口输出 15 次“大家好。(15分)

public class ThreadTest {
    public static void main(String[] args) throws InterruptedException {
        Thread t = new Thread() {
            @Override
            public void run() {
                for (int i = 0; i < 20; i++) {
                    System.out.println("您好");
                    try {
                        Thread.sleep(25);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        };

        Thread s = new Thread() {
            @Override
            public void run() {
                for (int i = 0; i < 20; i++) {
                    System.out.println("他好");
                    try {
                        Thread.sleep(75);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        };

        for (int i = 0; i < 15; i++) {
            System.out.println("大家好");
        }

        t.start();
        s.start();
    }
}

1.2.4 创建一个圆的类,半径设置为私有数据域(实数类型),并有相关的访问器和修改器。通过其构造器,能够构建一个默认的圆对象(默认半径为 1),也能构建一个指定半径的圆对象,并能能够记录创建圆对象的总个数。提供返回圆的面积,返回圆的周长的方法,其中涉及 PI 的取值,使用 java.lang.Math 类属性的 PI 值(20 分,将程序写在答题纸上)

public class Circle {
    private double radius;
    private static int count = 0;

    public Circle() {
        radius = 1.0;
        count++;
    }

    public Circle(double radius) {
        this.radius = radius;
        count++;
    }

    public double getArea() {
        return Math.PI * radius * radius;
    }

    public double getPerimeter() {
        return 2 * Math.PI * radius;
    }

    public double getRadius() {
        return radius;
    }

    public void setRadius(double radius) {
        this.radius = radius;
    }

    public static int getCount() {
        return count;
    }
    public static void main(String [] args){
        Circle c1 = new Circle();
        System.out.println(c1.getArea());
        System.out.println(c1.getPerimeter());
        c1.setRadius(23);
        System.out.println(c1.getRadius());
        System.out.println(c1.getPerimeter());

    }
}

1.2.5  E 盘根目录下的 sample.txt 文件包含 200 行建筑设计中的各类文件信息,每行表示一个文件,每一行的结构都是一样的(文件创建的时间、文件大小(KB)和文件名),下面给出了 4 行示例信息。编写程序实现统计各种类型文件的数量,同时统计文件大小超过 100000KB 的文件个数。(20 分,将程序写在答题纸上)

2018/10/12 17:11 189,137  20181005-01页面_1.jpg
2018/10/10 16:19 137,564  20181005-01.pdf
2018/10/12 17:11 189,124  20181005-01_页面_2.jpg
2018/10/12 15:49  12,022  wwww.docx

代码:

import java.io.*;
import java.util.*;
public class examFile1 {
    public static void main(String [] args) throws IOException{
        File f = new File("E:/sample.txt");
        if (!f.exists()) {
            System.out.println("File is not exits.");
            //使用System.exit(0) 表示正常退出程序,使用非零的退出状态码(如System.exit(1))表示程序以异常或错误的方式退出。
            System.exit(1);
        }
        // 文件大小大于100000KB的文件数量计数
        int count = 0;
        Scanner r = new Scanner(f);
        //存储文件类型(key)和个数(value)
        HashMap<String, Integer> fileTypeNums = new HashMap<>();
        while (r.hasNext()){
            String date = r.next();
            String time = r.next();
            int size = r.nextInt();
            String name = r.next();
            if (size > 100000){
                count ++;
            }
            //点在正则表达式中是一个特殊字符,表示匹配任意字符。因此,在使用 split() 方法时,需要对点进行转义,即使用 \\. 来表示匹配真正的点字符。
            String file_type= name.split("\\.")[1];
            if(fileTypeNums.containsKey(file_type)){
                fileTypeNums.put(file_type, fileTypeNums.get(file_type) + 1);
            }
            else {
                fileTypeNums.put(file_type, 1);
            }
        }
        //输出文件类型及个数
        Set<String> keys = fileTypeNums.keySet();
        for (Object key : keys) {
            System.out.println(key + "类型的文件共有" + fileTypeNums.get(key) + "个");
        }
        //输出文件大小大于100000kb的个数
        System.out.println("文件大小大于100000kb的个数为:" + count);
    }
}

1.2.6 使用面向对象的设计,利用封装、继承、多态的机制,设计和实现一个停车场的收费模拟系统,给出相应类的设计和实现。已知停车场有三种车型,收费者会根据不同的车型收取不同的停车费,其中,客车:15 元/小时,货车:12 元/小时,轿车:3元/小时。其中测试类 Parker 提供 charzeFees 法,根据车停的实际时间(小时)来收费,并提供了 mmaim 函数(程序入口)进行测试。(20 分,将程序写在答题纸上)

package examination;
abstract class Vehicle {
    public String name;
    public abstract double calculateFees(int hours);
}

//客车类
class Bus extends Vehicle {
    public Bus() {
        this.name = "客车";
    }

    @Override
    public double calculateFees(int hours) {
        return hours * 15;
    }
}

//货车类
class Truck extends Vehicle {
    public Truck() {
        this.name = "货车";
    }

    @Override
    public double calculateFees(int hours) {
        return hours * 12;
    }
}

//轿车类
class Car extends Vehicle {
    public Car() {
        this.name = "轿车";
    }

    @Override
    public double calculateFees(int hours) {
        return hours * 3;
    }
}

public class Parker {
    public double chargeFees(Vehicle v, int hours) {
        return v.calculateFees(hours);
    }

    public static void main(String[] args) {
        Parker p = new Parker();

        Vehicle bus = new Bus();
        System.out.println(bus.name + "停车" + 5 + "小时,收费:" + p.chargeFees(bus, 5) + "元");
        System.out.println(bus.name + "停车" + 5 + "小时,收费:" + bus.calculateFees(5) + "元");
        Vehicle truck = new Truck();
        System.out.println(truck.name + "停车" + 8 + "小时,收费:" + p.chargeFees(truck, 8) + "元");

        Vehicle car = new Car();
        System.out.println(car.name + "停车" + 10 + "小时,收费:" + p.chargeFees(car, 10) + "元");
    }
}

1.3 程序填空题

1.3.1 阅读下列说明和 Java 代码,将应填入(n)处的字句写在答题纸的对应栏内。

【说明】现欲实现一个图像浏览系统,要求该系统能够显示 BMP、JPEG 和 GIF 三种格式的文件,并且能够在 Windows 和 Linux 两种操作系统上运行。系统首先将 BMP、JPEG 和 GIF三种格式的文件解析为像素矩阵,然后将像素矩阵显示在屏幕上。系统需具有较好的扩展性以支持新的文件格式和操作系统。为满足上述需求并减少所需生成的子类数目,采用桥接(Bridge)设计模式进行设计所得类图如图 7-1 所示。

采用该设计模式的原因在于:系统解析 BMP、GIF 与 JPEG 文件的代码仅与文件格式相关,而在屏幕上显示像素矩阵的代码则仅与操作系统相关。

采用该设计模式的原因在于:系统解析 BMP、GIF 与 JPEG 文件的代码仅与文件格式相关,而在屏幕上显示像素矩阵的代码则仅与操作系统相关。

【Java 代码】

 

参考答案

(1)this.imp     (2)ImageImp   (3)imp.doPaint(m)   (4)new BMP()

 (5)  new WinImp()      (6)image1.setImp(imageImp1)     (7)   17

1.3.2  阅读下列说明和Java代码,将应填入 (n) 处的字句写在答题纸上。

  【说明】 现欲构造一文件/目录树,采用组合(Composite)设计模式来设计,得到的类图如 下图所示:

代码:

 该程序运行后输出结果为:

c:\ composite TestComposite.java Window

参考答案

(1)abstract    (2)extends  (3)null (4)extends (5)List

(6)childList   (7)printTree  (8)printTree(file)

1.4 往年其他考试题(自行参考)

1.4.1 数组的操作,通过键盘给3*4的二维数组输入数据,然后按行输出数组元素,并输出数组中最大的元素及行和列号

import java.util.Scanner;
public class inputArray {
    public static void main(String [] args){
        int[][] array = new int[3][4];
        System.out.println("请输入3*4维数组中的元素,空格或回车分割:");
        Scanner reader = new Scanner(System.in);
        for (int i = 0; i < 3; i++){
            for (int j = 0; j < 4; j++)
                array[i][j] = reader.nextInt();
        }
        int max = array[0][0];
        int max_row = 0;
        int max_col = 0;
        System.out.println("数组中的元素是");
        for (int i = 0; i < 3; i++){
            for (int j = 0; j < 4; j++){
                if(array[i][j] > max){
                    max = array[i][j];
                    max_row = i;
                    max_col = j;
                }
                System.out.print("\t" + array[i][j]);
            }
            System.out.println();
        }
        System.out.println("max:" + max + "  max_row:" + max_row + "  max_col:" + max_col);
    }
}

1.4.2 编写一个公有Car类,包含这样两个属性:当前速度speed(整型)及车主的姓名name(字符串型)包含一个show()方法可用来显示两个属性的值,包含一个构造器可以初始化两个属性,main方法中定义两个Car类型的对象c,d并调用各自的方法。 

public class car {
    public int speed;
    public String name;
    car(int _speed, String _name){
        speed = _speed;
        name = _name;
    }
    public void show(){
        System.out.println("速度" + speed);
        System.out.println("名字" + name);
    }
    public static void main(String [] args){
        car c = new car(134, "freezing");
        car d = new car(324,"rebecca");
        c.show();
        d.show();
    }
}

1.4.3 读程序写出结果

class MyException extends Exception
{   String a;
    public MyException(String s){a=s;}
    public void show(){System.out.println(a);}
}
class MyGen{
    void mygen() throws MyException  {
        System.out.println("开始产生例外");
        throw new MyException("my exception!");       }
}
public class Eception_Demo{
    public static void main(String args[]){
        MyGen mg = new MyGen();
        System.out.println("程序开始");
        try{
            mg.mygen();
        }catch(MyException e)
        {System.out.print("MyException:" );
            e.show();
        }
        System.out.println("程序结束");
    }
}

输出:

程序开始
开始产生例外
MyException:my exception!
程序结束

二、2023Java题型以及分值分布

  • 读程序1道10分
  • 程序填空1道 20分
  • 编程5道 70分(分值分布不清楚)

2.1 2023期末考试题(记忆版本)

1、程序阅读(10分)

class MyException extends Exception{
    private int detail;
    MyException(int a){
        detail = a;
    }
    public String toString(){
        return "Exception("+detail+")";
    }
}
public class Eception_Demo {
    static void compute(int a) throws MyException{
        System.out.println("Called compute[#"+a+"#]");
        if(a > 20)throw new MyException(a);
        System.out.println("Normal exit!");
    }
    public static void main(String [] args){
        try{
            compute(15);
            compute(21);
            compute(7);
        }catch (MyException e){
            System.out.println("Exceptiob caught :" + e.toString());
        }finally {
            System.out.println("Finally");
        }
    }
}

输出结果:

Called compute[#15#]
Normal exit!
Called compute[#21#]
Exceptiob caught :Exception(21)
Finally

2、程序填空题 2*10个空 (20分)

类似但不完全相同

(1) class Shape {
    protected double area;  // 面积
    protected double perimeter;  // 周长
    public(2) double getArea();  // 求面积的方法
    
}

class Rectangle (3) Shape {
    private double width;
    private double height;

    public Rectangle(double width, double height) {
        this.width = width;
        this.height = height;
    }

    @Override
    public double getArea() {
        return(4);
    }

}

class Circle(5) Shape {
    private double radius;

    public Circle(double radius) {
        this.radius = radius;
    }

    @Override
    public double getArea() {
        return (6) * radius * radius;
    }

}

public class ShapeTest {
    public static void main(String[] args) {
        Shape[] shapes = new (7);
        shapes[0] = new Circle(2.0);
        shapes[1] = new Circle(4.0);
        shapes[2] = new(8)(1.5, 2.5);
        double totalArea = 0;
        for (int i = 0, i < (9); i++){
            totalArea += (10);
        }
    }
}

3、编写一个 Java 类,有多个人的成绩存放在 score 数组中,最多存放100个人,请在类中编写函数函数的功能是:将高于平均分的人数作为函数值返回,将高于平均分的分数放在up 所指的数组中。例如,当score 数组中的数据为 24,35,88,76,90,54,5966,96 时,函数返回的人数应该是 5,up 中的数据应为 88,76,90,66,96;编写 printArray函数,对score数组进行才输出,每个数字占5个宽度,每输出五个数换行一次,并在类中编写 main 函数,对所编写的函数进行测试。(10 分,将程序写在答题纸上)

4、上课编写的售票的代码,对代码进行修复和补全主函数,记不清了(10分)

public class Tickets {
    private int num = 50;
    public int getNum(){
        return num;
    }
    public synchronized void saleTicket(){
        if(num > 0){
            System.out.println(Thread.currentThread() + " : No." + num + " ticket is sailed");
            try {
                Thread.sleep(5);
            }catch (InterruptedException e){
                e.printStackTrace();
            }
            num--;
        }
    }
}

5、创建一个圆的类,半径设置为私有数据域,并有相关的访问器和修改器。通过其构造器,能够构建一个默认的圆对象(默认半径为 0.5),也能构建一个指定半径的圆对象,并能能够记录创建圆对象的总个数。提供返回圆的面积方法,其中涉及 PI 的取值,使用java.lang.Math 类属性的 PI值。(15分)

6、E 盘根目录下的 sample.txt 文件包含 200 行中的各类文件信息,每一行表示一个文件,每一行的结构都是一样的(文件创建的时间、文件大小(KB)和文件名),下面给出了 4 行示例信息。编写程序实现统计各种类型文件的数量,同时统计各种文件类型的大小,并且将文件大小转换为GB,1000000K B = 1GB。(15 分,将程序写在答题纸上)

 7、使用面向对象的设计,利用封装、继承、多态的机制,设计和实现一个停车场的收费模拟系统,给出相应类的设计和实现。已知停车场有三种车型,收费者会根据不同的车型收取不同的停车费,其中,客车:10 元/小时,货车:8 元/小时,轿车:2 元/小时。(20 分,将程序写在答题纸上)

猜你喜欢

转载自blog.csdn.net/weixin_45440484/article/details/131229727