一、2019年
1. 第一题
Test.java
Test.class
和Singer.class
e:\test01\src\ctgu\edu\cn
2. 第二题
import java.util.*;
public class MessageParser{
public double getSum(String message){
String regex = "[^0123456789.]+";
message = message.replaceAll(regex,"#");
StringTokenizer fenxi = new StringTokenizer(message,"#");
double sum = 0;
while(fenxi.hasMoreTokens()){
String item = fenxi.nextToken();
double price += Double.valueOf(item);
sum = sum + price;
}
return sum;
}
}
3. 第三题
```java public class Test{ public static void main(String[] args){
} }Screen screen = new Screen(false);
Machine machine = new Machine();
try{
machine.checkScreen(screen);
System.out.println("合格屏");
}catch(DefectiveScreenException e){
e.toString();
}
class DefectiveScreenException extends Exception{ @Override public void toString(){ System.out.println(“缺陷屏”); } }
class Screen{ public boolean isDefective;
public Screen(){
}
public Screen(boolean isDefective){
this.isDefective = isDefective;
}
}
class Machine{ public void checkScreen(Screen screen) throws DefectiveScreenException{ if(screen.isDefective()){ throw new DefectiveScreenException(); } } }
<a name="V1Xce"></a>
## 4. 第四题
多线程不考,暂时跳过
<a name="GeMNH"></a>
## 5. 第五题
```java
public class TestEmployee{
public static void main(String[] args){
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
Employee e1 = new Employee(1,"yxr","男",format.parse("2021-12-12"));
Employee e2 = new Employee(2,"zyl","女",format.parse("2021-12-13"));
System.out.println(e1);
System.out.println(e2);
}
}
class Employee{
private int no;
private String name;
private String sex;
private Date entryDate;
public Employee(){
}
public Employee(int no,String name,String sex,Date entryDate){
this.no = no;
this.name = name;
this.sex = sex;
this.entryDate = entryDate;
}
public void setNo(int no){
this.no = no;
}
public int getNo(){
return this.no;
}
public void setName(String name){
this.name = name;
}
public String getName(){
return this.name;
}
public void setSex(String sex){
this.sex = sex;
}
public String getSex(){
return this.sex;
}
public void setEntryDate(Date entryDate){
this.entryDate = entryDate;
}
public Date getEntryDate(){
return this.entryDate;
}
@Override
public String toString(){
return "姓名:" + name + "入职时间:" + entryDate;
}
}
6. 第六题
public class FrameTest{
public static void main(String[] args){
new MyFrame();
}
}
class MyFrame extends JFrame{
public MyFrame(){
Container container = getContentPane();
JButton button = new JButton("读取");
button.addActionListener(new ButtonClickController());
container.add(button);
setVisible(true);
setBounds(300,300,400,400);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}
class ButtonClickController implements ActionListener{
private ArrayList<String> strings;
private int clickSum;
public ButtonClickController(){
strings = new ArrayList<>();
clickSum = 0;
FileReader fileReader = null;
BufferedReader bufferdReader = null;
try{
fileReader = new FileReader("test.txt");
bufferdReader = new BufferedReader(fileReader);
String str = null;
while((str = bufferdReader.readLine()) != null){
strings.add(str);
}
}catch(IOException e){
e.printStackTrace();
}finally{
if(bufferdReader != null){
try{
bufferdReader.close();
}catch(IOException e){
e.printStackTrace();
}
}
}
}
@Override
public void actionPerformed(ActionEvent e){
if(clickSum >= strings.size()){
throw new RuntimeException("已到达文件末尾!");
}else{
System.out.println(strings.get(clickSum));
clickSum ++;
}
}
}
7. 第七题
public class TestShape{
public static void main(String[] args){
ArrayList<Shape> list = new ArrayList<>();
Circle c1 = new Circle(20.0);
Circle c2 = new Circle(5.2);
Triangle t1 = new Triangle(10,20);
Triangle t2 = new Triangle(5,3);
list.add(c1);
list.add(c2);
list.add(t1);
list.add(t2);
Collections.sort(list);
list.forEach(e -> System.out.println(e.getArea()));
}
}
interface Shape extends Comparable<Shape>{
double getArea();
@Override
int compareTo(Shape o);
}
class Circle implements Shape{
private double radius;
public Circle(){
}
public Circle(double radius){
this.radius = radius;
}
@Override
public double getArea(){
return Math.PI * radius * radius;
}
@Override
public int compareTo(Shape o){
Double d1 = getArea();
Double d2 = o.getArea();
return d1.compareTo(d2);
}
}
class Triangle implements Shape{
private double length;
private double width;
public Triangle(){
}
public Triangle(double length,double width){
this.length = length;
this.width = width;
}
@Override
public double getArea(){
return length*width;
}
@Override
public int compareTo(Shape o){
Double d1 = getArea();
Double d2 = o.getArea();
return d1.compareTo(d2);
}
}
8. 第八题
TestGUI.java
public class TestGUI{
public static void main(String[] args){
javax.swing.SwingUtilities.invokeLater(() -> {
ComputeFrame win = new ComputeFrame();
win.setTitle("TestGUI");
win.setBounds(100,100,420,260);
});
}
}
Circle.java
public class Circle{
private double radius;
public Circle(double radius){
this.radius = radius;
}
public Circle(){
}
public double getArea(){
return Math.PI * radius * radius;
}
public void setRadius(double radius){
this.radius = radius;
}
}
ComputeFrame.java
public class ComputeFrame extends JFrame implements ActionListener{
Circle circle;
JTextField textRadius;
JTextArea showArea;
JButton computeButton;
ComputeFrame(){
init();
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
void init(){
circle = new Circle();
textRadius = new JTextField(5);
showArea = new JTextArea();
computeButton = new JButton("计算面积");
JPanel pNorth = new JPanel();
pNorth.add(new JLabel("请输入半径:"));
pNorth.add(textRadius);
pNorth.add(computeButton);
computeButton.addActionListener(this);
add(pNorth,BorderLayout.NORTH);
add(new JScrollPane(showArea),BorderLayout.CENTER);
}
public void actionPerformed(ActionEvent e){
try{
double radius = Double.parseDouble(textRadius.getText());
circle.setRadius(radius);
double area = circle.getArea();
showArea.append("半径为:" + radius + "的圆形面积为:" + area + "\n");
}catch(Exception e){
showArea.append("\n" + e + "\n");
}
}
}