编程题
1.现在输入n个数字,以逗号,分开;
然后可选择升或者降序排序;
按提交键就在另一页面显示
按什么 排序,结果为, ,
提供reset
答案(1)
- public static String[] splitStringByComma(String source){
- if(source==null||source.trim().equals(""))
- return null;
- StringTokenizer commaToker = new StringTokenizer(source,",");
- String[] result = new String[commaToker.countTokens()];
- int i=0;
- while(commaToker.hasMoreTokens()){
- result[i] = commaToker.nextToken();
- i++;
- }
- return result;
- }
循环遍历String数组
Integer.parseInt(String s)变成int类型
组成int数组
Arrays.sort(int[] a),
a数组升序
降序可以从尾部开始输出
2.金额转换,阿拉伯数字的金额转换成中国传统的形式如:
(¥1011)->(一千零一拾一元整)输出。
3、继承时候类的执行顺序问题,一般都是选择题,问你将会打印出什么?
答:父类:
- package test;
- public class FatherClass
- {
- public FatherClass()
- {
- System.out.println("FatherClass Create");
- }
- }
- 子类:
- package test;
- import test.FatherClass;
- public class ChildClass extends FatherClass
- {
- public ChildClass()
- {
- System.out.println("ChildClass Create");
- }
- public static void main(String[] args)
- {
- FatherClass fc = new FatherClass();
- ChildClass cc = new ChildClass();
- }
- }
输出结果:
- C:>java test.ChildClass
- FatherClass Create
- FatherClass Create
- ChildClass Create
4、内部类的实现方式?
答:示例代码如下:
- package test;
- public class OuterClass
- {
- private class InterClass
- {
- public InterClass()
- {
- System.out.println("InterClass Create");
- }
- }
- public OuterClass()
- {
- InterClass ic = new InterClass();
- System.out.println("OuterClass Create");
- }
- public static void main(String[] args)
- {
- OuterClass oc = new OuterClass();
- }
- }
输出结果:
- C:>java test/OuterClass
- InterClass Create
- OuterClass Create
再一个例题:
- public class OuterClass {
- private double d1 = 1.0;
- //insert code here
- }
- You need to insert an inner class declaration at line 3. Which two inner class declarations are
- valid?(Choose two.)
- A. class InnerOne{
- public static double methoda() {return d1;}
- }
- B. public class InnerOne{
- static double methoda() {return d1;}
- }
- C. private class InnerOne{
- double methoda() {return d1;}
- }
- D. static class InnerOne{
- protected double methoda() {return d1;}
- }
- E. abstract class InnerOne{
- public abstract double methoda();
- }
说明如下:
一.静态内部类可以有静态成员,而非静态内部类则不能有静态成员。 故 A、B 错
二.静态内部类的非静态成员可以访问外部类的静态变量,而不可访问外部类的非静态变量;return d1 出错。
故 D 错
三.非静态内部类的非静态成员可以访问外部类的非静态变量。 故 C 正确
四.答案为C、E
5、Java 的通信编程,编程题(或问答),用JAVA SOCKET编程,读服务器几个字符,再写入本地显示?
答:Server端程序:
- package test;
- import java.net.*;
- import java.io.*;
- public class Server
- {
- private ServerSocket ss;
- private Socket socket;
- private BufferedReader in;
- private PrintWriter out;
- public Server()
- {
- try
- {
- ss=new ServerSocket(10000);
- while(true)
- {
- socket = ss.accept();
- String RemoteIP = socket.getInetAddress().getHostAddress();
- String RemotePort = ":"+socket.getLocalPort();
- System.out.println("A client come in!IP:"+RemoteIP+RemotePort);
- in = new BufferedReader(new
- InputStreamReader(socket.getInputStream()));
- String line = in.readLine();
- System.out.println("Cleint send is :" + line);
- out = new PrintWriter(socket.getOutputStream(),true);
- out.println("Your Message Received!");
- out.close();
- in.close();
- socket.close();
- }
- }catch (IOException e)
- {
- out.println("wrong");
- }
- }
- public static void main(String[] args)
- {
- new Server();
- }
- };
Client端程序:
- package test;
- import java.io.*;
- import java.net.*;
- public class Client
- {
- Socket socket;
- BufferedReader in;
- PrintWriter out;
- public Client()
- {
- try
- {
- System.out.println("Try to Connect to 127.0.0.1:10000");
- socket = new Socket("127.0.0.1",10000);
- System.out.println("The Server Connected!");
- System.out.println("Please enter some Character:");
- BufferedReader line = new BufferedReader(new
- InputStreamReader(System.in));
- out = new PrintWriter(socket.getOutputStream(),true);
- out.println(line.readLine());
- in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
- System.out.println(in.readLine());
- out.close();
- in.close();
- socket.close();
- }catch(IOException e)
- {
- out.println("Wrong");
- }
- }
- public static void main(String[] args)
- {
- new Client();
- }
- };
6、用JAVA实现一种排序,JAVA类实现序列化的方法(二种)? 如在COLLECTION框架中,实现比较要实现什么样的接口?
答:用插入法进行排序代码如下
- package test;
- import java.util.*;
- class InsertSort
- {
- ArrayList al;
- public InsertSort(int num,int mod)
- {
- al = new ArrayList(num);
- Random rand = new Random();
- System.out.println("The ArrayList Sort Before:");
- for (int i=0;i<num ;i++ )
- {
- al.add(new Integer(Math.abs(rand.nextInt()) % mod + 1));
- System.out.println("al["+i+"]="+al.get(i));
- }
- }
- public void SortIt()
- {
- Integer tempInt;
- int MaxSize=1;
- for(int i=1;i<al.size();i++)
- {
- tempInt = (Integer)al.remove(i);
- if(tempInt.intValue()>=((Integer)al.get(MaxSize-1)).intValue())
- {
- al.add(MaxSize,tempInt);
- MaxSize++;
- System.out.println(al.toString());
- } else {
- for (int j=0;j<MaxSize ;j++ )
- {
- if
- (((Integer)al.get(j)).intValue()>=tempInt.intValue())
- {
- al.add(j,tempInt);
- MaxSize++;
- System.out.println(al.toString());
- break;
- }
- }
- }
- }
- System.out.println("The ArrayList Sort After:");
- for(int i=0;i<al.size();i++)
- {
- System.out.println("al["+i+"]="+al.get(i));
- }
- }
- public static void main(String[] args)
- {
- InsertSort is = new InsertSort(10,100);
- is.SortIt();
- }
- }
JAVA类实现序例化的方法是实现java.io.Serializable接口
Collection框架中实现比较要实现Comparable 接口和 Comparator 接口
7、编程:编写一个截取字符串的函数,输入为一个字符串和字节数,输出为按字节截取的字符串。 但是要保证汉字不被截半个,如"我ABC"4,应该截为"我AB",输入"我ABC汉DEF",6,应该输出为"我ABC"而不是"我ABC+汉的半个"。
答:代码如下:
- package test;
- class SplitString
- {
- String SplitStr;
- int SplitByte;
- public SplitString(String str,int bytes)
- {
- SplitStr=str;
- SplitByte=bytes;
- System.out.println("The String is:′"+SplitStr+"′;SplitBytes="+SplitByte);
- }
- public void SplitIt()
- {
- int loopCount;
- loopCount=(SplitStr.length()%SplitByte==0)?(SplitStr.length()/SplitByte):(SplitStr.length()/Split
- Byte+1);
- System.out.println("Will Split into "+loopCount);
- for (int i=1;i<=loopCount ;i++ )
- {
- if (i==loopCount){
- System.out.println(SplitStr.substring((i-1)*SplitByte,SplitStr.length()));
- } else {
- System.out.println(SplitStr.substring((i-1)*SplitByte,(i*SplitByte)));
- }
- }
- }
- public static void main(String[] args)
- {
- SplitString ss = new SplitString("test中dd文dsaf中男大3443n中国43中国人
- 0ewldfls=103",4);
- ss.SplitIt();
- }
- }
8、JAVA多线程编程。 用JAVA写一个多线程程序,如写四个线程,二个加1,二个对一个变量减一,输出。
希望大家补上,谢谢
9、STRING与STRINGBUFFER的区别。
答:STRING的长度是不可变的,STRINGBUFFER的长度是可变的。如果你对字符串中的内容经常进行操作,特别是内容要修改时,那么使用StringBuffer,如果最后需要String,那么使用StringBuffer的toString()方法