Java期末考试编程解析

本文档详细解析了一次Java编程期末考试的五个题目,包括改错、随机数生成、面向对象设计、GUI界面制作和文件操作。提供了代码实现及注意事项,如随机数的去重、面向对象的类设计、GUI组件交互和文件字符流操作。同时,文档中还分享了考试中遇到的挑战和解决策略,可供学习者参考。
摘要由CSDN通过智能技术生成

本题目源于2022春学期大二JOU期末考题,最新刚出炉的!
前言:这5个题目在短时间仅使用Textpad情况下是比较有难度的,不过题目大多比较常规,除了随机数使用了去重知识外也没有难点,属于比较中等难度的题目,下面将按照题目顺序一一解答,同时也为考过的同学提供代码参考,另外也可以让学弟学妹们有所借鉴。
题目题面仅根据本人回忆得出,但是程序结果运行没有问题。

总之,考试难点几乎没有,但还是有很多坑点的,在短时间内使用Textpad是比较有挑战性的为什么我们机房不配vscode环境啊,难受,其他例如netbeans根本用不惯),所以考前需要对netbeans可以进行熟悉,关于netbeans未来的学弟学妹们可以了解一下也是不错的编辑器。

有更好的做法可以留言,同时欢迎大家讨论,相互学习!

题目一 改错题

提供一个错误的程序给你,需要进行改错。
由于错误版本我也找不到了,这里就直接放正确代码:
这里已经将改错的点附上了基本上满足题意的,详情请看代码。
提几个注意点吧,首先使用的extends Thread,当然main方法要使用staic,睡眠异常也需要注意。

import java.awt.*;
import javax.swing.*;
import java.io.*;//为IOException导入包

public class MainClass extends JFrame // 继承需要extends,拼写错误
{
	MyThread t1 = new MyThread(this);// 需要构造方法,使用类
	int i = 1;

	public MainClass() {
		setTitle("动态图形");// setTitle拼写错误
		setSize(400, 400);
		setVisible(true);
		t1.run();// 拼写错误
	}

	// Graphics拼写错误
	public void paint(Graphics g) {
		// super.paint(g);//调用父类的方法
		g.setColor(Color.red);
		if (i < 20)
			g.fillOval(10 * i, 100, 10, 10);
		else if (i >= 20 && i < 30)
			g.fillOval(200, 100 + 10 * (i - 20), 10, 10);
		else
			g.fillOval(10 * i - 100, 200, 10, 10);
	}

	// main方法少了static
	public static void main(String args[]) {
		new MainClass();
	}
}

// Thread使用的是extends他不是接口
class MyThread extends Thread {
	MainClass a;

	MyThread(MainClass b) {
		a = b;
	}

	// run方法需要返回类型是void
	public void run() {
		while (true) {
			a.repaint();
			a.i++;

			try {
				sleep(500);
			}
			// 睡眠需要有打断异常
			catch (InterruptedException e) {
			}
		}
	}
}

运行结果图:
在这里插入图片描述

题目二 随机数

题目大意:请写出一个如下程序,班级名称需要自己输入,班级人数也是自己输入的。在周一到周五使用随机数不重复地按照此式排序,只要满足题意即可。
在这里插入图片描述
这个题目我一开始是第一个做的,是比较懵逼的,对于后面的不重复一时间无从下手,后来经过思考想到了去重办法,不过考试的时候没有很好地处理好,还是有些疏忽,不过总体框架是对的,下面将附上2种代码,一个是使用set去重,一个是用数组进行判断存在后添加到数组中。
set去重版本:

import java.util.*;

public class OOProgram2_2020122128 {
    public static void main(String[] args) {
        System.out.print("请输入班级名称:");
        Scanner sys = new Scanner(System.in);
        String str = sys.nextLine();
        System.out.println();
        System.out.print("请输入班级人数:");
        Scanner in = new Scanner(System.in);
        int num = in.nextInt();
        System.out.println();
        System.out.println("该班每天抽到以下同学参加核酸检测");
        System.out.println();
        System.out.println("周一\t周二\t周三\t周四\t周五");
        System.out.println();

        int count = 0;

        // 使用set进行去重操作
        Set<Integer> set = new LinkedHashSet<Integer>();
        Random r = new Random();
        while (set.size() < num) {
            int i = r.nextInt(num) + 1;
            set.add(i);
        }
        Iterator<Integer> it = set.iterator();
        while (it.hasNext()) {
            int i = it.next();
            System.out.print(i + "  \t");
            count++;
            if (count % 5 == 0) {
                System.out.println();
            }
        }
    }
}

数组去重版本关键代码,只要从上面的程序中从set开始的set到输出全部替换这个就可,因为考虑到不太有人会用set故放了此数组版本。

// 使用数组进行去重
        int[] a = new int[num + 1];
        ArrayList<Integer> b = new ArrayList<Integer>();
        Random r = new Random();
        for (int i = 0; i < num; i++) {
            a[i] = 0;
        }
        while (b.size() < num) {
            int x = r.nextInt(num) + 1;
            a[x]++;
            if (a[x] == 1) {
                b.add(x);
            }
        }

        for (int i = 0; i < b.size(); i++) {
            System.out.print(b.get(i) + " \t");
            count++;
            if (count % 5 == 0) {
                System.out.println();
            }
        }

题目三 面向对象

这个题目看似简单还是有一些坑的,首先这是UML图,对于protected、public、private是有专门的符号的
-号代表权限为private,没有符号代表权限为default,#号代表权限为protected。所以题目给的属性基本就是protected,那么如果调用属性就得自己设置get方法了。
其次是父类使用抽象类,子类需要重载。需要完成的结果图如下:
这个题基本送分了。

public class OOProgram3_2020122128 {
    public static void main(String[] args) {
        HUAWEIMobilePhone hwAWEIMobilePhone = new HUAWEIMobilePhone("HUAWEI", "P50 Pro", "2022年3月1日", 7288);
        System.out.println("手机信息如下:");
        System.out.println("品牌:" + hwAWEIMobilePhone.getbrand() + "  型号:" + hwAWEIMobilePhone.getModel() + "  发布时间:"
                + hwAWEIMobilePhone.getRelease() + "  单价:" + hwAWEIMobilePhone.getPrice());
        hwAWEIMobilePhone.aboutOS();
        System.out.println();
    }
}

abstract class MobliePhone {
    protected String brand;
    protected String releaseData;
    protected int price;

    public MobliePhone(String name, String releaseData, int price) {
        super();
        this.brand = name;
        this.releaseData = releaseData;
        this.price = price;
    }

    abstract void aboutOS();
}

class HUAWEIMobilePhone extends MobliePhone {
    protected String brand;
    protected String releaseData;
    protected int price;
    private String model;

    public HUAWEIMobilePhone(String brand, String model, String releaseData, int price) {
        super(brand, releaseData, price);
        this.brand = brand;
        this.model = model;
        this.releaseData = releaseData;
        this.price = price;
    }

    void aboutOS() {
        System.out.println("华为【微澜浩海】手机搭载了自主研发的鸿蒙HarmonyOS 2操作系统");
    }

    String getbrand() {
        return brand;
    }

    String getRelease() {
        return releaseData;
    }

    int getPrice() {
        return price;
    }

    String getModel() {
        return model;
    }
}

题目四 GUI

完成如下的界面图,选择求平方的时候计算,选择求立方的时候计算
在这里插入图片描述
思路:设置接口比较方便,如果自己重新定义一个监听class可能存在麻烦,因为需要getText();
注意点:首先对按钮进行监听,然后点击按钮以后对radio进行监听,(这个顺序应该没什么讲究,如果讲究的话,只要把两个if顺序换一下就可以了)

import javax.swing.*;
import java.awt.event.*;

public class OOProgram4_2020122128 {
    public static void main(String[] args) {
        new GUI();
    }
}

class GUI extends JFrame implements ActionListener {
    JLabel jl = new JLabel("请输入一个整数");
    JRadioButton jr1 = new JRadioButton("求平方");
    JRadioButton jr2 = new JRadioButton("求立方");
    JTextField jt = new JTextField(20);
    JButton jb = new JButton("计算");
    JPanel jp = new JPanel();

    public GUI() {
        jp.add(jl);
        jp.add(jt);
        jp.add(jr1);
        jp.add(jr2);
        jp.add(jb);
        jb.addActionListener(this);
        add(jp);

        setVisible(true);
        setSize(350, 150);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public void actionPerformed(ActionEvent e) {
        int x = Integer.parseInt(jt.getText());

        if ("计算".equals(e.getActionCommand())) {
            if (jr1.isSelected()) {
                JOptionPane.showMessageDialog(null, "该数的平方是" + x * x, "计算结果", JOptionPane.INFORMATION_MESSAGE);
            }
            if (jr2.isSelected()) {
                JOptionPane.showMessageDialog(null, "该数的平方是" + x * x * x, "计算结果", JOptionPane.INFORMATION_MESSAGE);
            }
        }
    }
}

题目五 文件操作

使用字符流在文本(dest.txt)输入”庆祝共青团成立100周年——青春心向党,建功新时代!“。然后使用合适的流将其打印,效果图如下:
在这里插入图片描述
考试的时候不巧的是我出现了乱码,比较糟糕,也没有什么时间检查了,其实后来发现就是编码读取的问题,应该使用FileReader读比较好,可惜我用的是流,难受了。。。注释代码就是我考场代码了

import java.io.*;

public class OOProgram5_2020122128 {
    public static void main(String[] args) {
        try {
            FileOutputStream rfFileOutputStream = new FileOutputStream("dest.txt");
            CharArrayWriter caw = new CharArrayWriter();
            caw.write("庆祝共青团成立100周年——青春心向党,建功新时代!");
            char c[] = caw.toCharArray();
            String s = "";
            for (int i = 0; i < c.length; i++) {
                s += c[i];
            }
            byte[] b = new byte[256];
            b = s.getBytes();
            rfFileOutputStream.write(b);
            rfFileOutputStream.close();

            // File f = new File("dest.txt");
            // InputStream in = null;
            // in = new FileInputStream(f);
            // int cn = 0;
            // while ((cn = in.read()) != -1) {
            // System.out.print((char) cn);
            // }
            // in.close();
            FileReader rm = new FileReader("dest.txt");
            // char[] cm = new char[256];
            int n = 0;
            while ((n = rm.read()) != -1) {
                System.out.print((char) n);
            }
            rm.close();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}
  • 24
    点赞
  • 105
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值