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

you’re going to create two Java classes: A Server class and a Client class. The

ID: 3676920 • Letter: Y

Question

you’re going to create two Java classes: A Server class and a Client class. The server will run first, set up a local connection, and wait for the client. The client will then connect to the server and send some data to be processed. The server will work as a conversion app. The client will ask for the user to enter a temperature in the form “80F” or “16C” to be converted. Note that the client should be able to enter either Fahrenheit or Celsius temperatures. Invalid entries (being anything not in the format “xF” or “xC” where x is any real float value) should be turned down by the client and the program should ask for new entry until the user enters a valid input. The server should then receive the user’s entry by any means and convert the given temperature into its counterpart, sending the converted temperature back to the client. The formula for converting temperatures can be found below.

When writing this program, here’s some things to keep in mind:

The nature of the app is to collect an entry from the user, send the data to the server, have the server convert the data, send the data back to the client, and display the converted data to the user. The program should run through only once, and both the client and server should terminate once finished. Keep in mind that it’s possible for either the server or the client to terminate while the other is sending data – if this happens it’ll result in a Connection Reset error. If you encounter this error, make sure that both your classes stay connected beyond sending data.

For this assignment, the program must be entirely console-based. No GUI applications or stand-alone executables will be accepted for this submission.

Explanation / Answer

import java.io.IOException;

import java.io.PrintStream;

import java.net.Socket;

import java.net.UnknownHostException;

import java.util.Scanner;

public class Client {

@SuppressWarnings("resource")

public static void main(String args[]) throws UnknownHostException, IOException

{

double number, store;

Scanner scan = new Scanner(System.in);

Socket clientSocket = new Socket("localhost", 9999);

Scanner scan2 = new Scanner(clientSocket.getInputStream());

System.out.println("Enter a temperature in C");

number = scan.nextInt();

PrintStream output = new PrintStream(clientSocket.getOutputStream());

output.println(number);

store = scan2.nextInt();

System.out.println(store);

}

}

import java.io.IOException;
import java.io.PrintStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;

public class Client {

  
   @SuppressWarnings("resource")
   public static void main(String args[]) throws UnknownHostException, IOException
   {
          
           double number, store;
           Scanner scan = new Scanner(System.in);
           Socket clientSocket = new Socket("localhost", 9999);
           Scanner scan2 = new Scanner(clientSocket.getInputStream());
           System.out.println("Enter a temperature in C");
           number = scan.nextInt();
           PrintStream output = new PrintStream(clientSocket.getOutputStream());
           output.println(number);
           store = scan2.nextInt();
           System.out.println(store);
          
   }          
       }

import java.io.IOException;

import java.io.PrintStream;

import java.net.ServerSocket;

import java.net.Socket;

import java.util.Scanner;

public class Server {

public static void main(String args[]) throws IOException

{

double number, store;

ServerSocket serverSocket = new ServerSocket(9999);

Socket acc = serverSocket.accept();

Scanner scan = new Scanner(acc.getInputStream());

number = scan.nextInt();

store = 9/5.0*number+32;

PrintStream output = new PrintStream(acc.getOutputStream());

output.println(store);

try {

} finally {

serverSocket.close();

scan.close();

}

}

}

import java.io.IOException;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;

public class Server {


   public static void main(String args[]) throws IOException
   {
           double number, store;
           ServerSocket serverSocket = new ServerSocket(9999);
           Socket acc = serverSocket.accept();
           Scanner scan = new Scanner(acc.getInputStream());
           number = scan.nextInt();
          
           store = 9/5.0*number+32;
          
           PrintStream output = new PrintStream(acc.getOutputStream());
           output.println(store);
           try {
           } finally {
               serverSocket.close();
           scan.close();   
           }
   }
}