Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The following server-client program takes a username and a password and returns

ID: 3690363 • Letter: T

Question

The following server-client program takes a username and a password and returns the user’s current balance. Write the client-side of this program. Allow the user to enter their own Username and Password (assume all possible input is valid / no data validation) to send to the client and print the balance received from the server to the client’s output. Remember to catch necessary exceptions. DO NOT modify the Server class. (40 points)

import java.io.*;

import java.net.*;

import java.util.*;

public class NewClient

{

    //Write your code here!

   

}

import java.io.*;

import java.net.*;

import java.util.*;

public class NewServer

{

    static ServerSocket server;

    static Socket client;

    static PrintStream writer;

    static Scanner reader;

   

    static String username;

    static String password;

    static double balance;

   

    public static void main

                (String[] args)

    {

        try

        {

            server = new

                ServerSocket(9999);

            client = server.accept();

            writer = new PrintStream

          (client.getOutputStream());

            reader = new Scanner

           (client.getInputStream());

           

            getBalance();

    }

    catch (IOException IOex)

    {

            IOex.printStackTrace();

    }

    }

   

    public void getBalance()

    {

        username = reader.next();

        password = reader.next();

        if (password.equals(“guest”))

            balance = 100.00;

        else

            balance = 10.00;

        writer.println(balance);

    }

}

import java.io.*;

import java.net.*;

import java.util.*;

public class NewClient

{

    //Write your code here!

   

}

import java.io.*;

import java.net.*;

import java.util.*;

public class NewServer

{

    static ServerSocket server;

    static Socket client;

    static PrintStream writer;

    static Scanner reader;

   

    static String username;

    static String password;

    static double balance;

   

    public static void main

                (String[] args)

    {

        try

        {

            server = new

                ServerSocket(9999);

            client = server.accept();

            writer = new PrintStream

          (client.getOutputStream());

            reader = new Scanner

           (client.getInputStream());

           

            getBalance();

    }

    catch (IOException IOex)

    {

            IOex.printStackTrace();

    }

    }

   

    public void getBalance()

    {

        username = reader.next();

        password = reader.next();

        if (password.equals(“guest”))

            balance = 100.00;

        else

            balance = 10.00;

        writer.println(balance);

    }

}

Explanation / Answer

import java.io.*;
import java.net.*;
import java.util.*;
public class NewClient
{
static ServerSocket server;
static Socket client;
static PrintStream writer;
static Scanner reader;
static String username;
static String password;
static double balance;
public static void main
(String[] args)
{
try
{

client = new Socket(9999);
writer = new PrintStream (server.getOutputStream());
reader = new Scanner (server.getInputStream());
System.out.println("Please Enter username");
String username = writer.next();
System.out.println("Please Enter password");
String password = writer.next();
System.out.println("Your Current balance is: " + balance());
}
catch (IOException IOex)
{
IOex.printStackTrace();
}
}