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

UPDATE : How many files doe this need to be? I have saved the Server as one clas

ID: 3680173 • Letter: U

Question

UPDATE: How many files doe this need to be? I have saved the Server as one class, and Client as another class. I then run the Server and it works. Then I try to run the Client program, but I get a message stating '"Run is active. OK to end run and continue?". I then click End Run, and enter the values of the Client program. That is when I get the Exception Thread error message. What am I doing wrong? If this code work please let me know how to correctly run it.

Please code using Java and show an example that it will run successfully in a JAVA IDE. Code so it is formatted as in the images below. Also please DO NOT use the code from my previous 2 questions answered by Anonymous answered this 8 hours later 231 answers or 1234567 answered this 14 minutes later 64 answers. These are the same exact programs…..The code is not formatted correctly and does not runs correctly.

Please let me know if I have to save the files as one program or separate programs for it to run successfully. Please code using fresh and not use recycled code.

DO NOT send me the code below it DOES NOT WORK. I keep getting the error below when running the Client Program:

After I click submit I get this error:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
   at Client.actionPerformed(Client.java:101)
   at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
   at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348)
   at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
   at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
   at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
   at java.awt.Component.processMouseEvent(Component.java:6535)
   at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
   at java.awt.Component.processEvent(Component.java:6300)
   at java.awt.Container.processEvent(Container.java:2236)
   at java.awt.Component.dispatchEventImpl(Component.java:4891)
   at java.awt.Container.dispatchEventImpl(Container.java:2294)
   at java.awt.Component.dispatchEvent(Component.java:4713)
   at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888)
   at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4525)
   at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466)
   at java.awt.Container.dispatchEventImpl(Container.java:2280)
   at java.awt.Window.dispatchEventImpl(Window.java:2750)
   at java.awt.Component.dispatchEvent(Component.java:4713)
   at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
   at java.awt.EventQueue.access$500(EventQueue.java:97)
   at java.awt.EventQueue$3.run(EventQueue.java:709)
   at java.awt.EventQueue$3.run(EventQueue.java:703)
   at java.security.AccessController.doPrivileged(Native Method)
   at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
   at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
   at java.awt.EventQueue$4.run(EventQueue.java:731)
   at java.awt.EventQueue$4.run(EventQueue.java:729)
   at java.security.AccessController.doPrivileged(Native Method)
   at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
   at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
   at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
   at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
   at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
   at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
   at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
   at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

DO NOT SEND ME THE BELOW CODE, it gives me the errors above, it does not run correctly:

Server Program

Bad Client code:

Bad Action Performed Code:

Explanation / Answer

Exercise30_1Client.java:
//The client sends the input to the server and receives
// result back from the server
import java.io.*;
import java.net.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Exercise30_1Client extends JFrame implements ActionListener {
// Text field for receiving radius
private JTextField jtfAnnualInterestRate = new JTextField();
private JTextField jtfNumOfYears = new JTextField();
private JTextField jtfLoanAmount = new JTextField();
private JButton jbtSubmit= new JButton("Submit");

// Text area to display contents
private JTextArea jta = new JTextArea();

// IO streams
DataOutputStream osToServer;
DataInputStream isFromServer;

public static void main(String[] args) {
    new Exercise30_1Client();
}

public Exercise30_1Client() {
    // Panel p1 to hold the labels
    JPanel p1 = new JPanel();
    p1.setLayout(new GridLayout(3, 1));
    p1.add(new JLabel("Annual Interest Rate"));
    p1.add(new JLabel("Number Of Years"));
    p1.add(new JLabel("Loan Amount"));

    // Panel p2 to hold the text fields
    Panel p2 = new Panel();
    p2.setLayout(new GridLayout(3, 1));
    p2.add(jtfAnnualInterestRate);
    p2.add(jtfNumOfYears);
    p2.add(jtfLoanAmount);

    // Panel p to hold p1, p2 and a button
    JPanel p = new JPanel();
    p.setLayout(new BorderLayout());
    p.add(p1, BorderLayout.WEST);
    p.add(p2, BorderLayout.CENTER);
    p.add(jbtSubmit, BorderLayout.EAST);

    jtfAnnualInterestRate.setHorizontalAlignment(JTextField.RIGHT);
    jtfNumOfYears.setHorizontalAlignment(JTextField.RIGHT);
    jtfLoanAmount.setHorizontalAlignment(JTextField.RIGHT);

    setLayout(new BorderLayout());
    add(p, BorderLayout.NORTH);
    add(new JScrollPane(jta), BorderLayout.CENTER);

    jbtSubmit.addActionListener(this); // Register listener

    jta.setWrapStyleWord(true);
    jta.setLineWrap(true);

    setTitle("Exercise30_1Client");
    setSize(500, 300);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setLocationRelativeTo(null); // Center the frame
    setVisible(true); // It is necessary to show the frame here!

    try {
      // Create a socket to connect to the server
      Socket connectToServer = new Socket("localhost", 8000);
      //Socket connectToServer = new Socket("130.254.204.36", 8000);
      //Socket connectToServer = new Socket(
      // "drake.Armstrong.edu", 8000);

      // Create an input stream to receive data from the server
      isFromServer = new DataInputStream(
        connectToServer.getInputStream());

      // Create an output stream to send data to the server
      osToServer =
        new DataOutputStream(connectToServer.getOutputStream());
    }
    catch (IOException ex) {
      jta.append(ex.toString() + ' ');
    }
}

public void actionPerformed(ActionEvent e) {
    String actionCommand = e.getActionCommand();
    if (e.getSource() instanceof JButton) {
      try {
        // Get the annual interest rate from the text field
        double annualInterestRate =
          Double.parseDouble(jtfAnnualInterestRate.getText().trim());

        // Get the number of years from the text field
        int numOfYears =
          Integer.parseInt(jtfNumOfYears.getText());

        // Get the loan amount from the text field
        double loanAmount =
          Double.parseDouble(jtfLoanAmount.getText().trim());

        // Send the annual interest rate to the server
        osToServer.writeDouble(annualInterestRate);

        // Send the number of years to the server
        osToServer.writeInt(numOfYears);

        // Send the loan amount to the server
        osToServer.writeDouble(loanAmount);

        osToServer.flush();

        // Get monthly payment from the server
        double monthlyPayment = isFromServer.readDouble();

        // Get total payment from the server
        double totalPayment = isFromServer.readDouble();

          jta.append("Annual Interest Rate: " + annualInterestRate +
            " Number of Years: " + numOfYears + " Loan Amount: " +
            loanAmount + " ");
          jta.append(" monthlyPayment: " + monthlyPayment + " " +
            " totalPayment: " + totalPayment + ' ');
      }
      catch (IOException ex) {
        System.err.println(ex);
      }
    }
}
}