I successfully wrote code for the scenario below. I would like someone from your
ID: 3683139 • Letter: I
Question
I successfully wrote code for the scenario below. I would like someone from your team to rewrite the code using a different approach. This will allow me to see how someone else may go about coding this program and ultimately coming out with the same results. Please code using Java and verify it will run in a Java IDE.My code can be found below. Thanks.
This is my code for the program. It runs successfully, no issues. Please re-code using a different approach so I can compare and contrast a different way to write the same program.
//Server.java: The server accepts data from the client, processes it
//and returns the result back to the client
import java.io.*;
import java.net.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Server extends JFrame {
//Text area for displaying contents
private JTextArea jta = new JTextArea();
public static void main(String[] args){
new Server();
}
public Server() {
//Place text area on the frame
getContentPane().setLayout(new BorderLayout());
getContentPane().add(new JScrollPane(jta),BorderLayout.CENTER);
setTitle("Server");
setSize(500,300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true); //It is necessary to show the frame here!
try{
//Create a server socket
ServerSocket serverSocket = new ServerSocket(8000);
jta.append("Server started at" +new Date()+ ' ');
//Listen for a connection request
Socket connectToClient = serverSocket.accept();
//Create data input and output streams
DataInputStream isFromClient = new DataInputStream(
connectToClient.getInputStream());
DataOutputStream osToClient = new DataOutputStream(
connectToClient.getOutputStream());
while (true) {
//Receive annual interest rate from the client
double annualInterestRate = isFromClient.readDouble();
//Receive number of years from the client
int numOfYears = isFromClient.readInt();
//Receive loan amount from the client
double loanAmount = isFromClient.readDouble();
//Obtain monthly interest rate
double monthlyInterestRate = annualInterestRate / 1200;
//Compute total payment
double totalPayment=(loanAmount*annualInterestRate/100*numOfYears)+loanAmount;
//Compute monthly payment
double monthlyPayment = totalPayment/(numOfYears*12);
//Send monthly payment back to the client
osToClient.writeDouble(monthlyPayment);
//Send total payment back to the client
osToClient.writeDouble(totalPayment);
jta.append("The Annual Interest Rate received from client is "+ annualInterestRate+' ');
jta.append("The Number Of Years received from client is "+ numOfYears+' ');
jta.append("The Loan Amount received from client is "+ loanAmount+' ');
jta.append("The Monthly Payment is "+ monthlyPayment+' ') ;
jta.append("The Total Payment is"+ totalPayment+' ');
}
}
catch(IOException ex){
System.err.println(ex);
}
}
}
----------------------------------------------------------------------------------------------------------------
//Client.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 Client extends JFrame implements ActionListener{
//Text field for receiving annual interest rate,number of years, loan amount
private JTextField jtfAnnualInterestRate = new JTextField();
private JTextField jtfNumOfYears = new JTextField();
private JTextField jtfLoanAmount = new JTextField();
private JButton jbtSubmit = new JButton("Submit");
//Text area for displaying contents
private JTextArea jta = new JTextArea();
//IO streams
DataOutputStream osToServer;
DataInputStream isFromServer;
public static void main(String[] args){
new Client();
}
public Client(){
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 = new Panel();
p2.setLayout(new GridLayout(3,1));
p2.add(jtfAnnualInterestRate);
p2.add(jtfNumOfYears);
p2.add(jtfLoanAmount);
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);
getContentPane().setLayout(new BorderLayout());
getContentPane().add(p,BorderLayout.NORTH);
getContentPane().add(new JScrollPane(jta),BorderLayout.CENTER);
jbtSubmit.addActionListener(this); //Register listener
setTitle("Client");
setSize(500,300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
try{
//Create a socket to connect to the server
Socket connectToServer = new Socket("localhost",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();
//Display to the text area
jta.append("Annual Interest Rate: " + annualInterestRate +" ");
jta.append("Number Of Years: " + numOfYears +" ");
jta.append("Loan Amount: " + loanAmount +" ");
jta.append("The Monthly Payment is: "+ monthlyPayment +" ");
jta.append("The Total Payment is: "+ totalPayment +" ");
}
catch(IOException ex){
System.err.println(ex);
}
}
}
}
Explanation / Answer
modifications: instead of creating multiple panel's and adding to the frame
i have just created a frame and added all components (buttons,textarea etc) to it
we can add multiple panels to frame.
i have removed all panels and added all components to the jframe
Server.java
import java.io.*;
import java.net.*;
import java.util.*;
import java.awt.*;
import javax.swing.*;
public class Server extends JFrame {
//Text area for displaying contents
public static void main(String[] args) {
JFrame frame = new JFrame("JFrame Example");//creating a frame
JTextArea jta = new JTextArea();//text area button
frame.getContentPane().setLayout(new BorderLayout());//adding layout to framr
frame.getContentPane().add(new JScrollPane(jta), BorderLayout.CENTER);//adding text area to frame
frame.setTitle("Server");//adding title server
frame.setSize(500, 300);//frame size
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//clse peration methd
frame.setVisible(true); //It is necessary to show the frame here!
//Place text area on the frame
try {
//Create a ServerDemo socket
ServerSocket ServerDemoSocket = new ServerSocket(8000);
jta.append("ServerDemo started at" + new Date() + ' ');
//Listen for a connection request
Socket connectToClient = ServerDemoSocket.accept();
//Create data input and output streams
DataInputStream isFromClient = new DataInputStream(
connectToClient.getInputStream());
DataOutputStream osToClient = new DataOutputStream(
connectToClient.getOutputStream());
while (true) {
//Receive annual interest rate from the client
double annualInterestRate = isFromClient.readDouble();
//Receive number of years from the client
int numOfYears = isFromClient.readInt();
//Receive loan amount from the client
double loanAmount = isFromClient.readDouble();
//Obtain monthly interest rate
double monthlyInterestRate = annualInterestRate / 1200;
//Compute total payment
double totalPayment = (loanAmount * annualInterestRate / 100 * numOfYears) + loanAmount;
//Compute monthly payment
double monthlyPayment = totalPayment / (numOfYears * 12);
//Send monthly payment back to the client
osToClient.writeDouble(monthlyPayment);
//Send total payment back to the client
osToClient.writeDouble(totalPayment);
jta.append("The Annual Interest Rate received from client is " + annualInterestRate + ' ');
jta.append("The Number Of Years received from client is " + numOfYears + ' ');
jta.append("The Loan Amount received from client is " + loanAmount + ' ');
jta.append("The Monthly Payment is " + monthlyPayment + ' ');
jta.append("The Total Payment is" + totalPayment + ' ');
}
} catch (IOException ex) {
System.err.println(ex);
}
}
}
Client.java
import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Client extends JFrame implements ActionListener{
//Text field for receiving annual interest rate,number of years, loan amount
private JTextField jtfAnnualInterestRate = new JTextField();//creating jtextfield
private JTextField jtfNumOfYears = new JTextField();
private JTextField jtfLoanAmount = new JTextField();
private JButton jbtSubmit = new JButton("Submit");//button creation
//Text area for displaying contents
private JTextArea jta = new JTextArea();
//IO streams
DataOutputStream osToServer;
DataInputStream isFromServer;
public static void main(String[] args){
new Client();//calling constructor
}
public Client(){
JFrame frame=new JFrame();//creating frame andd adding componenets
frame.setLayout(new GridLayout(3,1));
frame.add(new JLabel("Annual Interest Rate"));
frame.add(new JLabel("Number Of Years"));
frame.add(new JLabel("Loan Amount"));
frame.setLayout(new GridLayout(3,1));
frame.add(jtfAnnualInterestRate);
frame.add(jtfNumOfYears);
frame.add(jtfLoanAmount);
frame.add(jbtSubmit);
jtfAnnualInterestRate.setHorizontalAlignment(JTextField.RIGHT);
jtfNumOfYears.setHorizontalAlignment(JTextField.RIGHT);
jtfLoanAmount.setHorizontalAlignment(JTextField.RIGHT);
jbtSubmit.addActionListener(this); //Register listener
frame.setTitle("Client");
frame.setSize(700,200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
try{
//Create a socket to connect to the server
Socket connectToServer = new Socket("localhost",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();
//Display to the text area
jta.append("Annual Interest Rate: " + annualInterestRate +" ");
jta.append("Number Of Years: " + numOfYears +" ");
jta.append("Loan Amount: " + loanAmount +" ");
jta.append("The Monthly Payment is: "+ monthlyPayment +" ");
jta.append("The Total Payment is: "+ totalPayment +" ");
}
catch(IOException ex){
System.err.println(ex);
}
}
}
}