第一题
程序改错题,具体代码没有,毕竟是机考的,简单描述一下就是多线程+GUI中的Graphics类绘图,加上类名要更改的格式大概是14个左右,反正能运行达到考试的界面要求就OK了,主要错误还是蛮简单的,期末主要考察线程创建的俩种方式,大家要记住,1:继承自Thread类,2:实现Runnable接口,考试好像是用的实现接口的方式定义的,当时那个错误应该是用implements关键字,而不是用extends,其余的错误像重写的run方法,start()方法写成run()方法,public的l写成了1,super关键字大写,构造方法的传参对象错误之类的,还有就是文件名改了,自己的本类类名也要改,包括像构造方法。
第二题
第二题是一共九个数,让你去先输出原数组,按照格式[a1,a2,a3,a4,a5]这样的格式去输出,然后再排序(从小到大),然后再求中位数,但是考试的时候那个题目指定了元素的个数(9个),数组长度就是9,当时想了一下应该是跟长度的一半有关,但是因为上一个步骤调冒泡排序稍微没控制时间,所以耍了一点小聪明,我直接输出的第五个数字,哈哈哈哈哈哈哈,说实话时间还是挺紧的,主要是这道题我觉得可能是老师为了方便改卷所以就指定了长度和具体数字,严格来说应该是要去写一个寻找中位数的一个小判断(长度为奇,偶时的不同情况)下面附上源码。
import java.util.Arrays;
public class Demo1 {
public static void main(String[] args) {
//题目所给的是九个double类型的数据
//具体的数据不记得了,这里我就用几个数据代替一下,反正就是那个意思
double []a={5.2,3.5,6.2,1.2,2.5,6.9,9.8,10.2,2.1};
//先按照指定的格式输出原数组
int count=0;
System.out.print("原数组为:");
System.out.print("[");
for(int i=0;i<8;i++)
{
System.out.print(a[i]+",");
count++;
if(count==8)
{
System.out.print(a[count]+"]");
}
}
System.out.println();
System.out.print("排序后的数组为:");
System.out.print("[");
Arrays.sort(a); //这里如果考试不会手写冒泡就用Arrays中的sort方法,但是别忘记导包是util下的
//默认排序是从小到大的
int count1=0;
for(int i=0;i<8;i++)
{
System.out.print(a[i]+",");
count1++;
if(count1==8)
{
System.out.print(a[count1]+"]");
}
}
System.out.println();
System.out.print("中位数为:");
//求中位数
//考试的那个题目直接指定了长度,当时也没多想就直接输出了第五个数
// 其实求中位数也不难,先分别判定一下是奇长度还是偶长度,长度除二取整,奇数你会发现就是中位数下角标
//偶长度的话,求出来是那俩个数的后面一个数,所以它前面的是 j-1,然后除二
if((a.length)%2==0)//长度为偶时
{
int j=(a.length)/2;
double num=(a[j]+a[j-1])/2;//中位数的下角标
System.out.print(num);
}
//长度为奇时
else {
int j=(a.length)/2;//中位数的下角标
System.out.print(a[j]);
}
}
}
代码运行如下,反正就是按照格式去输出。
第三题
第三题是关于面向对象的题目,题目是给了一个类图,我大概回忆了一下,具体应该是这样的,下面我给大家放上UML类图。
题目的具体要求是:一共有三个类,测试类,Car类,BYDCar继承自Car,并且要重写toString方法去返回成员的所有信息,在重写的抽象方法中打印输出,比亚迪新能源汽车环保,畅销全球,并给予相应的数据在Text类中进行测试,要求运行结果如下(印象中记得是这样的):
下面是三个类的源码,我是分开写的
Car类:
package JOUtest;
public abstract class Car
{
protected String brand;
protected double price;
protected String producer;
public Car()
{
}
public Car(String brand, double price,String producer)
{
this.brand=brand;
this.price=price;
this.producer=producer;
}
public abstract void aboutcar();
}
BYDcar类:
package JOUtest;
public class BYDcar extends Car {
private int power;
private int quality;
public BYDcar()
{
}
public BYDcar(String brand,double price,String producer,int power,int quality)
{
super( brand, price, producer);
this.power=power;
this.quality=quality;
}
@Override
public void aboutcar() {
System.out.println("比亚迪新能源汽车环保,畅销全球");
}
public String toString()
{
return "品牌:"+brand+"\n"+"价格:"+price+"万元"+"\n"+"厂商:"+producer+"\n"+"功率:"+power+"瓦"+"\n"+"质量:"+quality+"kg"+"\n";
}
}
测试类:
package JOUtest;
public class Text {
public static void main(String[] args)
{
BYDcar byDcar=new BYDcar("比亚迪汽车", 12.23,"比亚迪",245,245);
System.out.println(byDcar.toString());
byDcar.aboutcar();
}
}
第四题
这题是GUI界面编程题,题目描述大概如下:实现一个加法运算,要求手动输入的数据能够跟第三个框内的数据进行一个验证,点击按钮进行验证,验证不对有错误弹窗提示,验证成功有成功弹窗提示,以及还有一个是否输入正确数据的提示,总共是一个界面内有三个框,三个标签,加上三个弹窗提示,具体界面实现考试的时候是给你了的,就按照去实现就行了,下面是界面的要求如下:
这是主界面,每个组件的位置必须按照图示要求放,我用的是空布局,你们也可以其他的布局达到效果就OK了。
然后是三个弹窗,这三个弹窗还有要求的,要求验算正确的弹窗图标应该是消息类型的,错误是错误类型的,数据有误是警告类型的,效果是这样的,也要按考试要求来
下面附上源码:
判断是否输入正确的数据格式我是用的正则表达式,反正不能输入字母符号之类的。
package JOUtest;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class GUIadd extends JFrame {
JLabel num1;
JLabel num2;
JTextField f1;
JTextField f2;
JLabel l1;
JTextField f3;
JButton jb1;
public GUIadd()
{
num1=new JLabel("数字一:");
num2=new JLabel("数字二:");
f1=new JTextField(10);
f2=new JTextField(10);
l1=new JLabel("验证计算结果");
f3=new JTextField(10);
jb1=new JButton("验证");
num1.setBounds(50,80,70,30);
f1.setBounds(100,80,90,30);
num2.setBounds(50,130,70,30);
f2.setBounds(100,130,90,30);
l1.setBounds(105,165,100,30);
f3.setBounds(100,230,90,30);
jb1.setBounds(100,195,90,30);
this.add(num1);
this.add(num2);
this.add(f1);
this.add(f2);
this.add(l1);
this.add(f3);
this.add(jb1);
this.setTitle("加法验算器");
this.setLayout(null);
this.setBounds(500,500,400,400);
this.setVisible(true);
jb1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
yanzheng();
}
});
}
public void yanzheng() {
String regular="[a-zA-Z@!$#%^&*()-+=?]";
String str3=f3.getText();
String str1= f1.getText();
String str2= f2.getText(); //去俩个数据,然后分别转int
if(str1.matches(regular)|| str1.equals("") || str2.matches(regular) || str2.equals("") || str3.equals("") || str3.matches(regular))
{
JOptionPane.showMessageDialog(null,"请输入正确的数据!","计算结果值",JOptionPane.WARNING_MESSAGE);
}
int a=Integer.parseInt(str1);
int b=Integer.parseInt(str2);
int sum=Integer.parseInt(str3);
int result=a+b;
if(result==sum)
{
JOptionPane.showMessageDialog(null,"计算结果验证正确为:"+sum,"计算结果",JOptionPane.INFORMATION_MESSAGE);
}
else if(result!=sum){
JOptionPane.showMessageDialog(null,"计算结果验证不正确"+"\n"+"正确结果为:"+(a+b),"计算结果",JOptionPane.ERROR_MESSAGE);
}
}
public static void main(String[] args) {
new GUIadd();
}
}
/*class MyException extends Exception
{
public MyException()
{
super();
JOptionPane.showMessageDialog(null,"请输入正确的数据格式","计算结果值",JOptionPane.WARNING_MESSAGE);
}
public MyException(String message)
{
super(message);
}
}*/
第五题
这题主要关于文件写入和读,分为俩个部分,第一个是考试给了你一个文件,然后让你去读出来内容,第二个是一句话然后全部转大写,然后写入文件中,其实还是简单的,没啥难点,就是考试时间真的太紧了,具体代码如下:
package JOUtest;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
public class FileDemo
{
public static void main(String[] args) throws Exception {
File file=new File("D:\\a.txt");
file.createNewFile();
FileReader fileReader=new FileReader("D:\\a.txt");
BufferedReader bufferedReader=new BufferedReader(fileReader);
//考试这里我从文件里面读数据使用的是字符流,readLine读的是一行,当时考试是三行数据,所以要用三个readLine
System.out.println( bufferedReader.readLine());
System.out.println( bufferedReader.readLine());
System.out.println( bufferedReader.readLine());
//第二个操作是,把一句话转大写然后再写入文件
FileOutputStream fileOutputStream=new FileOutputStream("D:\\b.txt");
String str="i love china forever";
String result=str.toUpperCase();
byte [] b=new byte[1024];
b=result.getBytes();
fileOutputStream.write(b);
}
}
代码运行结果如下: