Examine the following two classes. There are five errors that cause this program
ID: 3685074 • Letter: E
Question
Examine the following two classes. There are five errors that cause this program to malfunction. Find and correct these errors.
import java.io.*;
import java.net.*;
public class NewClient
{
static Socket s;
static DataInputStream reader;
static DataOutputStream writer;
static String userN = “user”;
static String userP = “password”;
static int permissions;
static final String IP =
“Localhost”;
static final int PORT = 9999;
public static void main
(String[] args)
{
try
{
s = new Socket(IP,PORT);
reader = new
DataInputStream
(s.getInputStream());
writer = new
DataOutputStream
(s.getOutputStream());
permissions = getLogin();
}
catch(Exception ex){}
}
public int getLogin()
{
writer.writeUTF(userN);
writer.writeUTF(userP);
int perm = reader.read();
return perm;
}
}
import java.io.*;
import java.net.*;
public class NewServer
{
static ServerSocket s;
static Socket cl;
static DataInputStream reader;
static DataOutputStream writer;
static String userN;
static String userP;
static final int P_FULL = 1054;
static final int P_GUEST = 1051;
static final int PORT = 9876;
public static void main
(String[] args)
{
try
{
s = new ServerSocket
(PORT);
cl = s.accept();
reader = new
DataInputStream
(cl.getInputStream());
writer = new
DataOutputStream
(cl.getOutputStream());
readUser();
}
catch (Exception ex){}
}
public void readUser()
{
userN = reader.readLine();
userP = reader.readLine();
int perm;
if (userN.equals(“user”))
perm = P_FULL;
if (userN.equals(“guest”))
perm = P_GUEST;
writer.writeInt(perm);
}
}
import java.io.*;
import java.net.*;
public class NewClient
{
static Socket s;
static DataInputStream reader;
static DataOutputStream writer;
static String userN = “user”;
static String userP = “password”;
static int permissions;
static final String IP =
“Localhost”;
static final int PORT = 9999;
public static void main
(String[] args)
{
try
{
s = new Socket(IP,PORT);
reader = new
DataInputStream
(s.getInputStream());
writer = new
DataOutputStream
(s.getOutputStream());
permissions = getLogin();
}
catch(Exception ex){}
}
public int getLogin()
{
writer.writeUTF(userN);
writer.writeUTF(userP);
int perm = reader.read();
return perm;
}
}
import java.io.*;
import java.net.*;
public class NewServer
{
static ServerSocket s;
static Socket cl;
static DataInputStream reader;
static DataOutputStream writer;
static String userN;
static String userP;
static final int P_FULL = 1054;
static final int P_GUEST = 1051;
static final int PORT = 9876;
public static void main
(String[] args)
{
try
{
s = new ServerSocket
(PORT);
cl = s.accept();
reader = new
DataInputStream
(cl.getInputStream());
writer = new
DataOutputStream
(cl.getOutputStream());
readUser();
}
catch (Exception ex){}
}
public void readUser()
{
userN = reader.readLine();
userP = reader.readLine();
int perm;
if (userN.equals(“user”))
perm = P_FULL;
if (userN.equals(“guest”))
perm = P_GUEST;
writer.writeInt(perm);
}
}
Explanation / Answer
/*If are giving userN then it is showing errors like illegal character.the reason is because it is taking some illegal characters insteadd of double quotes.This is one error.
Another error is calling function from static main.*/
NewClient.java:
import java.io.*;
import java.net.*;
public class NewClient
{
static Socket s;
static DataInputStream reader;
static DataOutputStream writer;
static String userN = "user";
static String userP = "password";
static int permissions;
static final String IP ="localhost";
static final int PORT = 9999;
public static void main
(String[] args)
{
try
{
s = new Socket(IP,PORT);
reader = new
DataInputStream
(s.getInputStream());
writer = new
DataOutputStream
(s.getOutputStream());
permissions = getLogin();
}
catch(Exception ex){}
}
public static int getLogin() throws IOException
{
writer.writeUTF(userN);
writer.writeUTF(userP);
int perm = reader.read();
return perm;
}
}
NewServer.java:
import java.io.*;
import java.net.*;
public class NewServer
{
static ServerSocket s;
static Socket cl;
static DataInputStream reader;
static DataOutputStream writer;
static String userN;
static String userP;
static final int P_FULL = 1054;
static final int P_GUEST = 1051;
static final int PORT = 9876;
public static void main
(String[] args)
{
try
{
s = new ServerSocket
(PORT);
cl = s.accept();
reader = new
DataInputStream
(cl.getInputStream());
writer = new
DataOutputStream
(cl.getOutputStream());
readUser();
}
catch (Exception ex){}
}
public static void readUser() throws IOException
{
userN = reader.readLine();
userP = reader.readLine();
int perm = 0;
if (userN.equals("user"))
perm = P_FULL;
if (userN.equals("guest"))
perm = P_GUEST;
writer.writeInt(perm);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.