Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Omar Natour
- * 4/28/16
- * Csc-112 Java 2
- * Hw# 16
- * Create a server to go on cs.stcc.edu that calculates quadratic roots
- * Ojnatour0001@student.stcc.edu
- */
- import java.net.*;
- import java.io.*;
- public class QuadServer {
- public static void main(String[] args) {
- QuadServer q = new QuadServer();
- q.runServer();
- }
- void runServer() {
- ServerSocket server;
- Socket connection;
- try {
- System.out.println("Creating server socket 5009 .....");
- server = new ServerSocket(5009);
- System.out.println("It's Alive!!");
- while (true) {
- System.out.println("Wating for connection");
- connection = server.accept();
- System.out.println("Connected");
- ThreadConnect t = new ThreadConnect(connection);
- t.start();
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- public class ThreadConnect extends Thread {
- Socket connection;
- ThreadConnect(Socket x) {
- connection = x;
- }
- public void run() {
- DataOutputStream output = null;
- DataInputStream input = null;
- try {
- input = new DataInputStream(connection.getInputStream());
- output = new DataOutputStream(connection.getOutputStream());
- double a = 0, b = 0, c = 0;
- int status;
- a = input.readDouble();
- b = input.readDouble();
- c = input.readDouble();
- status = getStatus(a, b, c);
- output.writeInt(status);
- if (status != -1) {
- if (status == 1) {
- double[] roots = QuadraticFormula(a, b, c, status);
- output.writeDouble(roots[0]);
- } else if (status == 2) {
- double[] roots = QuadraticFormula(a, b, c, status);
- output.writeDouble(roots[0]);
- output.writeDouble(roots[1]);
- }
- System.out.println("Output given");
- }
- }
- catch (Exception e) {
- //System.out.println(e.getMessage());
- } finally {
- try {
- input.close();
- } catch (Exception e) {
- }
- try {
- output.close();
- } catch (Exception e) {
- }
- try {
- connection.close();
- } catch (Exception e) {
- }
- }
- }
- }
- private int getStatus(double a, double b, double c) {
- int status = -1;
- double radicand = (Math.pow(b, 2)) - (4 * (a) * (c));
- if (radicand < 0)
- status = 0;
- else if (radicand == 0)
- status = 1;
- else if (radicand > 0)
- status = 2;
- if(a == 0)
- status =-1;
- return status;
- }
- private double[] QuadraticFormula(double a, double b, double c, int status) {
- double[] root = new double[2];
- double radicand = (Math.pow(b, 2)) - (4 * (a) * (c));
- if (status == 1) {
- root[0] = ((-1 * b) + (Math.sqrt(radicand))) / (2 * a);
- // root[1] = 0;
- } else if (status == 2) {
- root[0] = ((-1 * b) + (Math.sqrt(radicand))) / (2 * a);
- root[1] = ((-1 * b) - (Math.sqrt(radicand))) / (2 * a);
- }
- return root;
- }
- }
Add Comment
Please, Sign In to add comment