📄 question.java
字号:
/*Server's implementation of the Question class. This contains a vector of DataSets.Copyright (C) 2005-2006 Igor Partola, Michael J. Krikonis, Clark UniversityThis program is free software; you can redistribute it and/ormodify it under the terms of the GNU General Public Licenseas published by the Free Software Foundation; either version 2of the License, or (at your option) any later version.This program is distributed in the hope that it will be useful,but WITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See theGNU General Public License for more details.You should have received a copy of the GNU General Public Licensealong with this program; if not, write to the Free SoftwareFoundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.*/package SharedClasses;import java.net.*;import java.io.*;import java.util.*;public class Question { private String QuestionName; private String Question; private String AnswerA; private String AnswerB; private String AnswerC; private String AnswerD; private String AnswerE; private String AnswerF; private int Timeout; private int QuestionNumber; private int NumOfAnswers = 6; private Vector DataSetVector = new Vector(); public Question() { } public Question(String q, String a, String b, String c, String d, String e, String f, int t, int n, int na) { Question = q; AnswerA = a; AnswerB = b; AnswerC = c; AnswerD = d; AnswerE = e; AnswerF = f; Timeout = t; QuestionNumber = n; NumOfAnswers = na; QuestionName = "Question " + QuestionNumber; //newDataSet(); } public Question(byte[] bytes) { /* * question name size = 0..3 * question size = 4..7 * answer A size = 8..11 * answer B size = 12..15 * answer C size = 16..19 * answer D size = 20..23 * answer E size = 24..27 * answer F size = 28..31 * timeout = 32..35 * number of answers = 36..39 * data = 40.... */ int qns = bytesToInt(bytes, 0); int qs = bytesToInt(bytes, 4); int as = bytesToInt(bytes, 8); int bs = bytesToInt(bytes, 12); int cs = bytesToInt(bytes, 16); int ds = bytesToInt(bytes, 20); int es = bytesToInt(bytes, 24); int fs = bytesToInt(bytes, 28); int t = bytesToInt(bytes, 32); int n = bytesToInt(bytes, 36); String qn = new String(bytes, 40, qns); String q = new String(bytes, 40 + qns, qs); String a = new String(bytes, 40 + qns + qs, as); String b = new String(bytes, 40 + qns + qs + as, bs); String c = new String(bytes, 40 + qns + qs + as + bs, cs); String d = new String(bytes, 40 + qns + qs + as + bs + cs, ds); String e = new String(bytes, 40 + qns + qs + as + bs + cs + ds, es); String f = new String(bytes, 40 + qns + qs + as + bs + cs + ds + es, fs); QuestionName = qn; Question = q; AnswerA = a; AnswerB = b; AnswerC = c; AnswerD = d; AnswerE = e; AnswerF = f; Timeout = t; NumOfAnswers = n; } public void setQuestion(String s) { Question = s; } public void setAnswerA(String s) { AnswerA = s; } public void setAnswerB(String s) { AnswerB = s; } public void setAnswerC(String s) { AnswerC = s; } public void setAnswerD(String s) { AnswerD = s; } public void setAnswerE(String s) { AnswerE = s; } public void setAnswerF(String s) { AnswerF = s; } public void setTimeout(int t) { Timeout = t; } public void setQuestionNumber(int n) { QuestionNumber = n; QuestionName = "Question " + QuestionNumber; } public String getQuestionName() { return QuestionName; } public String getQuestion() { return Question; } public String getAnswerA() { return AnswerA; } public String getAnswerB() { return AnswerB; } public String getAnswerC() { return AnswerC; } public String getAnswerD() { return AnswerD; } public String getAnswerE() { return AnswerE; } public String getAnswerF() { return AnswerF; } public int getTimeout() { return Timeout; } public int getQuestionNumber() { return QuestionNumber; } public String toString() { return QuestionName; } public Vector getDataSetListing() { DataSet d; int i; int size = DataSetVector.size(); Vector v = new Vector(); for (i = 0; i < size; i++) { d = (DataSet) DataSetVector.elementAt(i); v.addElement(d.toString()); } return v; } public DataSet getDataSetAt(int index) { if (DataSetVector.size() <= 0) return null; return (DataSet) DataSetVector.elementAt(index); } public void newDataSet() { DataSet ds = new DataSet(DataSetVector.size() + 1); DataSetVector.addElement(ds); } public byte[] getBytes() { byte[] buf; String qn = getQuestionName(); String q = getQuestion(); String a = getAnswerA(); String b = getAnswerB(); String c = getAnswerC(); String d = getAnswerD(); String e = getAnswerE(); String f = getAnswerF(); int t = getTimeout(); int n = getNumOfAnswers(); String total; int qns = qn.getBytes().length; int qs = q.getBytes().length; int as = a.getBytes().length; int bs = b.getBytes().length; int cs = c.getBytes().length; int ds = d.getBytes().length; int es = e.getBytes().length; int fs = f.getBytes().length; total = qn + q + a + b + c + d + e + f; buf = new byte[total.getBytes().length + 4 * 10]; System.arraycopy(intToBytes(qns), 0, buf, 0, 4); System.arraycopy(intToBytes(qs), 0, buf, 4, 4); System.arraycopy(intToBytes(as), 0, buf, 8, 4); System.arraycopy(intToBytes(bs), 0, buf, 12, 4); System.arraycopy(intToBytes(cs), 0, buf, 16, 4); System.arraycopy(intToBytes(ds), 0, buf, 20, 4); System.arraycopy(intToBytes(es), 0, buf, 24, 4); System.arraycopy(intToBytes(fs), 0, buf, 28, 4); System.arraycopy(intToBytes(t), 0, buf, 32, 4); System.arraycopy(intToBytes(n), 0, buf, 36, 4); System.arraycopy(total.getBytes(), 0, buf, 40, total.getBytes().length); /* * question name size = 0..3 * question size = 4..7 * answer A size = 8..11 * answer B size = 12..15 * answer C size = 16..19 * answer D size = 20..23 * answer E size = 24..27 * answer F size = 28..31 * timeout = 32..35 * number of answers = 36..39 * data = 40.... */ return buf; } public void setNumOfAnswers(int i) { int j = i; if (i < 2) j = 2; if (i > 6) j = 6; NumOfAnswers = j; } public int getNumOfAnswers() { return NumOfAnswers; } private byte[] intToBytes(int value) { byte [] bytes = new byte [4]; bytes[0] = (byte)(value >> 24); bytes[1] = (byte)(value >> 16); bytes[2] = (byte)(value >> 8); bytes[3] = (byte)(value); return bytes; } private static int bytesToInt(byte[] b, int offset) { int value = 0; for (int i = 0; i < 4; i++) { int shift = (4 - 1 - i) * 8; value += (b[i + offset] & 0x000000FF) << shift; } return value; } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -