Professional Documents
Culture Documents
2018 Assigment 2
2018 Assigment 2
2018 Assigment 2
a. Write code for a multi-threaded server that would echo system time to a telnet
client on port 23.
[10]
import java.net.Socket;
import java.net.ServerSocket;
while (true) {
// open up IO streams
In in = new In (clientSocket);
// readLine() blocks until the server receives a new line from client
String s;
out.println(s);
out.close();
in.close();
clientSocket.close();
Question 2
a. Create a serializable Class called BankAccount with the following fields account
name, account number, account type and balance. You should provide getters
and setters for the variables you define
[10]
import java.text.NumberFormat;
import java.lang.Comparable;
{
// Data fields that every BankAccount object will maintain
*/
my_ID = initID;
my_balance = initBalance;
*/
{
boolean result = true;
else
result = false;
return result;
return my_ID;
*/
return my_balance;
*/
{
NumberFormat nf = NumberFormat.getCurrencyInstance( );
*/
return leftID.compareTo(rightID);
*/
else
return false;
}
Write a Java class method called saveToFile ( ) that persists the calling object to a
file called bankaccounts.dat [10]
throws IOException
out.write(text);
out.close();
Question 4
ROT13 Algorithm
ROT13 (rotate by 13 places) replaces a letter with the letter 13 letters after it in the
alphabet.
For example :
Becomes
a. Write source code for a client and server application. The client is supposed to send text
message input from the user and then encrypts it using the ROT13 algorithm. The server
is supposed to be listening to client messages on port 2018. When the client connects to
the server, the server prints the encrypted and decrypted message using the ROT13
Algorithm and prompts user for a response message. The response is ROT13 encrypted
before sending it back to the client. Your client is able to decrypt the message from the
server and display both the encrypted and decrypted messages. The server and client
processes terminate when either of them sends a message “bye” or “olr” its ROT13
equivalent. [20]
import java.nio.CharBuffer;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.nio.charset.CharsetEncoder;
import java.nio.charset.CharsetDecoder;
import java.nio.charset.CoderResult;
import java.util.Map;
import java.util.Iterator;
import java.io.Writer;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.io.OutputStreamWriter;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.FileReader;
Charset baseCharset;
return (false);
char a = '\u0000';
a = 'a';
a = 'A';
if (a != '\u0000') {
baseEncoder.maxBytesPerChar());
this.baseEncoder = baseEncoder;
while (cb.hasRemaining()) {
tmpcb.put (cb.get());
tmpcb.rewind();
rot13 (tmpcb);
baseEncoder.reset();
baseDecoder.maxCharsPerByte());
this.baseDecoder = baseDecoder;
baseDecoder.reset();
rot13 (cb);
return (result);
BufferedReader in;
if (argv.length > 0) {
} else {
String s = null;
out.println (s);
out.flush();