第四届全国ITAT教育工程就业技能大赛复赛试题Java组A卷答案

1、 编写一个Java应用程序,在其中编写一个类,该类封装了一元二次方程共有的属性和功能,即该类有刻画方程系数的3个成员变量以及计算实根的方法。并给出计算实根的过程。(20分) 

2、 编写一个Java应用程序,开启一个文本文件(以本程序源文件为例读取),一次读取其内的一行文本,令每行形成一个String,并将读出的String对象置于LinkedList中,以相反次序显示出LinkedList内的所有内容。(25分)

3、 Windows操作系统自带的计算器是个很方便的小工具,利用JavaGUI编程,实现一个Java GUI计算器应用程序界面,窗口标题为“计算器”,窗口布局如下图所示,在此计算器应用程序中实现“+-*/”运算操作。(25分)

4、 编写一个Java网络应用程序,该应用分服务器端程序和客户端程序两部分。服务器程序启动后,监听端口5678的请求;客户端程序启动后,向服务器端口5678发送请求。服务器不断接收客户机所写入的信息,并且服务器也会做出"Receive"为回应,告知客户机已接收到消息。要求:服务器程序采用多线程开发,允许多个客户端程序同时连接。客户机发送"End"字符串时,客户端程序退出。(服务端程序和客户端程序分别命名为Server.javaClient.java

30分)

附加题:

5、 有500个小朋友拉成一个圆圈,从其中一个小朋友开始依次编号1-500,从1号小朋友开始循环1-3报数,数到3的小朋友就退出。编写一个Java应用程序,计算出最后一个小朋友的号码是多少?(20分)

6、 编写一个Java应用程序,文件名为Folder.java,编译后执行结果如下图所示,实现指定文件夹下目录和文件的文本形式的树状结构显示。要求:

1. 输入: java Folder,显示当前文件夹下的目录和文件的文本形式树状显示;

2. 输入: java Folder C:\test>,显示C:\test文件夹下的目录和文件的文本形式树状显示;(C:\test可以为任意文件夹路径名)

3. 输入: java Folder add aa,在当前文件夹下面添加一个子目录(子目录名为:aa),同时显示当前文件夹下的最新目录和文件的文本形式树状显示;

4. 输入: java Folder del aa,在当前文件夹下面删除一个子目录(子目录名为:aa),同时显示当前文件夹下的最新目录和文件的文本形式树状显示;(30分)

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
T1:

public class RealRoot {

private double a;

private double b;

private double c;

public RealRoot(double a,double b,double c)

{

this.a=a;

this.b=b;

this.c=c;

}

public void getRealRoot()

{

double temp=b*b-4*a*c;

double root1,root2;

if(a==0)

{

if(b==0)

{

if(c==0)

System.out.println("方程的根为任意实数");

else

System.out.println("方程无根");

}

else

System.out.println("方程的根为:"+(-c/b));

}

else

{

if(temp>0)

{

root1=(-b+Math.sqrt(temp))/2*a;

                root2=(-b-Math.sqrt(temp))/2*a;

                System.out.println("方程的两个根为:"+root1+" "+root2);

}

else if(temp==0)

System.out.println("方程有一个根:"+((-b)/2*a));

else

System.out.println("方程无根");

}

}

}

 

 

T2:

import java.io.BufferedReader;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.IOException;

import java.io.InputStreamReader;

import java.util.Iterator;

import java.util.LinkedList;

public class ReadCode {

/**

 * @param args

 */

public static void main(String[] args) {

LinkedList<String> list=new LinkedList<String>();

String str;

try {

BufferedReader br=new BufferedReader

(new InputStreamReader

(new FileInputStream("D:\\Java\\javaCompetition\\src\\programming\\ReadCode.java")));

while((str=br.readLine())!=null)

list.addFirst(str);

Iterator<String> ite=list.iterator();

while(ite.hasNext())

System.out.println(ite.next());

catch (FileNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();}

catch(IOException ie)

{ie.printStackTrace();}

}

}

 

T3:

package Four;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
@SuppressWarnings("serial")
public class T11 extends JFrame implements ActionListener{
    int deng=0,fu=0,i=0,point=0,n=0;
    float sum=0,equ;
    String v="";
    BorderLayout bo=new BorderLayout();
    GridLayout g=new GridLayout(4,4);
    JPanel jp=new JPanel();
    JTextField jt=new JTextField(" 0");
    JButton b1=new JButton("7");
    JButton b2=new JButton("8");
    JButton b3=new JButton("9");
    JButton b4=new JButton("+");
    JButton b5=new JButton("4");
    JButton b6=new JButton("5");
    JButton b7=new JButton("6");
    JButton b8=new JButton("-");
    JButton b9=new JButton("1");
    JButton b10=new JButton("2");
    JButton b11=new JButton("3");
    JButton b12=new JButton("*");
    JButton b13=new JButton("0");
    JButton b14=new JButton(".");
    JButton b15=new JButton("=");
    JButton b16=new JButton("/");
    @SuppressWarnings("static-access")
    public T11(String title)
    {
        super(title);
        this.setSize(300,300);
        jp.setSize(300,200);
        jt.setSize(300,100);
        jp.setLayout(g);

        jp.add(b1);
        jp.add(b2);
        jp.add(b3);
        jp.add(b4);
        jp.add(b5);
        jp.add(b6);
        jp.add(b7);
        jp.add(b8);
        jp.add(b9);
        jp.add(b10);
        jp.add(b11);
        jp.add(b12);
        jp.add(b13);
        jp.add(b14);
        jp.add(b15);
        jp.add(b16);

        b1.addActionListener(this);
        b2.addActionListener(this);
        b3.addActionListener(this);
        b4.addActionListener(this);
        b5.addActionListener(this);
        b6.addActionListener(this);
        b7.addActionListener(this);
        b8.addActionListener(this);
        b9.addActionListener(this);
        b10.addActionListener(this);
        b11.addActionListener(this);
        b12.addActionListener(this);
        b13.addActionListener(this);
        b14.addActionListener(this);
        b15.addActionListener(this);
        b16.addActionListener(this);
        this.add(jt,bo.NORTH);
        this.add(jp,bo.CENTER);
        this.setLocationRelativeTo(null);       
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true);
    }
    public void actionPerformed(ActionEvent e)
    {
        if(e.getSource()==b1)
        {
            if(deng==1||fu==1)
                v="";
            v=v+"7";
            jt.setText(v);
            deng=0;fu=0;n=1;
        }
        if(e.getSource()==b2)
        {
            if(deng==1||fu==1)
                v="";
            v=v+"8";
            jt.setText(v);
            deng=0;fu=0;n=1;
        }
        if(e.getSource()==b3)
        {
            if(deng==1||fu==1)
                v="";
            v=v+"9";
            jt.setText(v);
            deng=0;fu=0;n=1;
        }
        if(e.getSource()==b4)//+
        {
            if(n==1)
            {
                if(i==0)
                    sum=Float.parseFloat(v);
                else if(i==1){sum+=Float.parseFloat(v);}
                else if(i==2){sum-=Float.parseFloat(v);}
                else if(i==3){sum*=Float.parseFloat(v);}
                else if(i==4){sum/=Float.parseFloat(v);}
            }
            else
            {
            }
            i=1;
            fu=1;n=0;deng=0;point=0;

        }
        if(e.getSource()==b5)
        {
            if(deng==1||fu==1)
                v="";
            v=v+"4";
            jt.setText(v);
            deng=0;fu=0;n=1;
        }
        if(e.getSource()==b6)
        {
            if(deng==1||fu==1)
                v="";
            v=v+"5";
            jt.setText(v);
            deng=0;fu=0;n=1;
        }
        if(e.getSource()==b7)
        {
            if(deng==1||fu==1)
                v="";
            v=v+"6";
            jt.setText(v);
            deng=0;fu=0;n=1;
        }
        if(e.getSource()==b8)//-
        {
            if(n==1)
            {
                if(i==0)
                    sum=Float.parseFloat(v);
                else if(i==1)sum+=Float.parseFloat(v);
                else if(i==2)sum-=Float.parseFloat(v);
                else if(i==3)sum*=Float.parseFloat(v);
                else if(i==4)sum/=Float.parseFloat(v);
            }
            else
            {
            }
            i=2;
            fu=1;n=0;deng=0;point=0;

        }
        if(e.getSource()==b9)
        {
            if(deng==1||fu==1)
                v="";
            v=v+"1";
            jt.setText(v);
            deng=0;fu=0;n=1;
        }
        if(e.getSource()==b10)
        {
            if(deng==1||fu==1)
                v="";
            v=v+"2";
            jt.setText(v);
            deng=0;fu=0;n=1;
        }
        if(e.getSource()==b11)
        {
            if(deng==1||fu==1)
                v="";
            v=v+"3";
            jt.setText(v);
            deng=0;fu=0;n=1;
        }

        // ioe_gaoyong添加
        if(e.getSource()==b12)//-
        {
            if(n==1)
            {
                if(i==0)
                    sum=Float.parseFloat(v);
                else if(i==1)sum+=Float.parseFloat(v);
                else if(i==2)sum-=Float.parseFloat(v);
                else if(i==3)sum*=Float.parseFloat(v);
                else if(i==4)sum/=Float.parseFloat(v);
            }
            else
            {
            }
            i=3;
            fu=1;n=0;deng=0;point=0;

        }   // ioe_gaoyong添加结束

        if(e.getSource()==b4)//*
        {
            if(n==1)
            {
                if(i==0)
                    sum=Float.parseFloat(v);
                else if(i==1)sum+=Float.parseFloat(v);
                else if(i==2)sum-=Float.parseFloat(v);
                else if(i==3)sum*=Float.parseFloat(v);
                else if(i==4)sum/=Float.parseFloat(v);
            }
            else
            {
            }
            i=1;//这里修改
            fu=1;n=0;deng=0;point=0;

        }

        if(e.getSource()==b13)
        {
            if(deng==1||fu==1)
                v="";
            v=v+"0";
            jt.setText(v);
            deng=0;fu=0;n=1;
        }
        if(e.getSource()==b14)//.
        {
            if(deng==1||fu==1)
                v="";
            if("".equals(v))jt.setText("0.");
            else
            {
                v=v+".";
                jt.setText(v);
            }
            point=1;
            deng=0;fu=0;n=1;

        }
        if(e.getSource()==b15)//=
        {
            if(fu==1){}
            else
            {
                switch(i)
                {
                    case 0: equ=sum;break;
                    case 1: equ=sum+Float.parseFloat(v);break;
                    case 2: equ=sum-Float.parseFloat(v);break;
                    case 3: equ=sum*Float.parseFloat(v);break;
                    case 4: equ=sum/Float.parseFloat(v);break;
                }
                sum=0;v="";
                v=String.valueOf(equ);
                jt.setText(String.valueOf(equ));
                deng=1;i=0;

            }
        }
        if(e.getSource()==b16)//+
        {
            if(n==1)
            {
                if(i==0)
                    sum=Float.parseFloat(v);
                else if(i==1)sum+=Float.parseFloat(v);
                else if(i==2)sum-=Float.parseFloat(v);
                else if(i==3)sum*=Float.parseFloat(v);
                else if(i==4)sum/=Float.parseFloat(v);
            }else{
            }
            i=4;
            fu=1;n=0;deng=0;point=0;

        }
    }
    public static void main(String[]args){
        new T11("计算器");
    }
}

 

 

T4:

Client-->

package Four;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;

public class A4Client {

 private String host = "localhost";
 private int port = 5678;
 Socket socket = null;

 public A4Client() {
  try {
   socket = new Socket(host, port);
   System.out.println("客户端启动,输入End结束通话");
   talks();
  } catch (UnknownHostException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }

 public void talks() throws IOException {
  BufferedReader br = getReader(socket);
  PrintWriter bw = getWriter(socket);
  BufferedReader localin = new BufferedReader(new InputStreamReader(
    System.in));
  System.out.print("输入数据:");
  String message = null;
  while ((message = localin.readLine()) != null) {
      message+="\r\n";
   bw.write(message);
   bw.flush();
  
   System.out.println("服务器消息:" + br.readLine());
   
   if (message.equals("End\r\n")) {
    break;
   }
   System.out.print("输入数据:");
  }
  socket.close();
  System.out.println("客户端成功关闭");

 }

 public BufferedReader getReader(Socket socket) throws IOException {

  InputStream is = socket.getInputStream();
  return new BufferedReader(new InputStreamReader(is));

 }

 public PrintWriter getWriter(Socket socket) throws IOException {
  OutputStream os = socket.getOutputStream();
  return new PrintWriter(os, true);

 }
 
 public static void main(String[]args){
  new A4Client();
 }
}

 

Server-->

package Four;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

public class A4Server {
 private ServerSocket server;
 private int port = 5678;

 public A4Server() {
  System.out.println("服务器已经启动,在" + port + "端口监听多个客户,收到End客户端强制结束");
  try {
   server = new ServerSocket(port);
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  service();
 }

 public void service() {
  while (true) {
   try {
    new ServerThread(server.accept(), server).start();
   } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
  }
 }

 public static void main(String args[]) {

  new A4Server();
 }

 class ServerThread extends Thread {
  BufferedReader br = null;
  PrintWriter pw = null;
  Socket socket;
  ServerSocket serversocket;

  public ServerThread(Socket socket, ServerSocket serversocket) {
   this.socket = socket;
   System.out.println("客户" + socket.getInetAddress() + "请求连接成功");
   this.serversocket = serversocket;
  }

  public BufferedReader getReader(Socket socket) throws IOException {
   InputStream is = socket.getInputStream();
   return new BufferedReader(new InputStreamReader(is));
  }

  public PrintWriter getWriter(Socket socket) throws IOException {
   OutputStream is = socket.getOutputStream();
   return new PrintWriter(is, true);
  }

  public void run() {

   
   try {
    br = getReader(socket);
    pw = getWriter(socket);
    
   } catch (IOException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
   }

   try {
    String message = null;
    
    while ((message = br.readLine()) != null) {
     
     System.out.println("接收信息:" + message);
     pw.write("Receive\r\n");
     pw.flush();
     if (message.equals("End")) {
      break;
     }
    }
    

   } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   } finally {
    System.out.println("客户" + socket.getInetAddress() + "断开连接");
    try {
     socket.close();
    } catch (IOException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }
   }

  }
 }
}

 

 

T5:

package Four;
import java.util.LinkedList;

public class A5 {
 public static void getNumber(LinkedList<Integer> list) {
  int index = 0, num = 0;
  while (list.size() > 1) {
   num++;
   if (num % 3 == 0) {
    list.remove(index);
    num = 0;
   } else {
    index++;
   }
   if (index == list.size()) {
    index = 0;
   }

  }

 }

 public static void main(String args[]) {
  LinkedList<Integer> list = new LinkedList<Integer>();
  for (int i = 1; i <= 500; i++)
   list.add(i);
  getNumber(list);
  System.out.println("最后一个退出的编号是" + list.get(0));

 }

}

 

 

T6:

package Four;

import java.io.File;
import java.io.IOException;

public class A6 {

 public static void main(String[] args) throws IOException {
  File file = new File("");
  System.out.println("当前目录"+file.getCanonicalPath());
  if (args.length == 0) {
   showDir(new File(file.getCanonicalPath()), 1);
  } else {
   if (args[0].equals("add")) {
    addDir(new File(file.getCanonicalPath() + "\\" + args[1]));
   } else if (args[0].equals("del")) {
    delDir(new File(file.getCanonicalPath() + "\\" + args[1]));
   } else if (args[0].contains(":")) {
    showDir(new File(args[0]), 1);

   } else
    System.out.println("指令错误");
  }

 }

 public static void showDir(File file, int index) {
 /*
  String str = "";
  for (int i = 0; i < index; i++)
   str += "\t";
  if (file.exists()) {
   File[] files = file.listFiles();
   for (int i = 0; i < files.length; i++) {
    System.out.println(str + files[i].getName());
    if (files[i].isDirectory()) {
     showDir(files[i], index+1);
    }
   }

  } else
   System.out.println("该目录不存在");*/

  for(int i=1;i<index;i++){
   System.out.print("|  ");
  }
   
  if(index>0){
   System.out.print("|_");
  } 
  System.out.println(file.getName());
  
  if(file.isDirectory()){
   File files[] = file.listFiles();
   if(files == null)
   {
    return ;
   }
   for(int i=0;i<files.length;i++)
   {
    showDir(files[i],index+1);
   } 
  }
 }

 public static void addDir(File file) {

  if (!file.exists()) {
   boolean bool = file.mkdir();
   if (bool) {
    System.out.println("目录创建成功");
    showDir(file.getParentFile(), 1);
   } else
    System.out.println("Error");

  } else
   System.out.println("目录已经存在");

 }

 public static void delDir(File file) {
  if (file.exists()) {
   boolean bool = file.delete();
   if (bool) {
    System.out.println("目录已经删除");
    showDir(file.getParentFile(), 1);
   } else
    System.out.println("Error");
  } else
   System.out.println("目录不存在");
 }

}

 

 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值