I have the following code in java, I need to •build a Graphical User Interface t
ID: 3717095 • Letter: I
Question
I have the following code in java, I need to
•build a Graphical User Interface to achieve the same process.
•Four text boxes for receiving numbers
•A button to process the numbers
•Display the steps and total number of steps in the GUI
•Make use of layouts and borders so that the four text fields and button sit horizontal above the output
•Consider using a panel with scroll bars for output to handle any number of lines in the answer
import java.util.Scanner;
public class FinalBonous {
public static void main(String args[]){
Scanner scan= new Scanner(System.in);
System.out.print("Enter a: " );
int a=scan.nextInt();
System.out.print("Enter b: ");
int b=scan.nextInt();
System.out.print("Enter c: ");
int c=scan.nextInt();
System.out.print("Enter d: ");
int d=scan.nextInt();
System.out.println(a+" "+b+" "+c+" "+d);
int a1=Math.abs(a-b),b1=Math.abs(b-c),c1=Math.abs(c-d),d1=Math.abs(d-a);
int i=1,temp=0;
while(!(a1==b1 && b1==c1 && c1==d1 && d1==a1)){
System.out.println("Step "+(i++)+":"+" "+a1+" "+b1+" "+c1+" "+d1);
temp=a1;
a1=Math.abs(a1-b1);
b1=Math.abs(b1-c1);
c1=Math.abs(c1-d1);
d1=Math.abs(d1-temp);
}
System.out.println("Step "+(i)+":"+" "+a1+" "+b1+" "+c1+" "+d1);
System.out.println(i+" Steps");
}
}
Explanation / Answer
import java.util.Scanner;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class FinalBonus extends JFrame implements ActionListener
{
JTextArea ta;//Textarea to show output
JButton submit;//submit button
JTextField valA,valB,valC,valD;//textfield to take inputs
JLabel labA,labB,labC,labD;//labels
JPanel pl1,pl2;//pl1 contains labels,textfields and submit button and pl2 contains textarea
JScrollPane sp;//scrolling option
FinalBonus()
{
setLayout(new BorderLayout());
//initializing and adding to frame
valA = new JTextField();
valB = new JTextField();
valC = new JTextField();
valD = new JTextField();
labA = new JLabel("Enter a");
labB = new JLabel("Enter b");
labC = new JLabel("Enter c");
labD = new JLabel("Enter d");
submit = new JButton("Submit");
ta = new JTextArea();
sp = new JScrollPane(ta);
pl1 = new JPanel(new GridLayout(5,2));
pl2 = new JPanel(new BorderLayout());
pl1.add(labA);
pl1.add(valA);
pl1.add(labB);
pl1.add(valB);
pl1.add(labC);
pl1.add(valC);
pl1.add(labD);
pl1.add(valD);
pl1.add(submit);
pl2.add(sp);
add(pl1,BorderLayout.NORTH);
add(pl2,BorderLayout.CENTER);
submit.addActionListener(this);
}
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource()==submit)
{
//your code goes here
try
{
long a = Long.parseLong(valA.getText());
long b = Long.parseLong(valB.getText());
long c = Long.parseLong(valC.getText());
long d = Long.parseLong(valD.getText());
ta.setText("");
ta.append(a+" "+b+" "+c+" "+d+" ");
long a1=Math.abs(a-b),b1=Math.abs(b-c),c1=Math.abs(c-d),d1=Math.abs(d-a);
int i=1;
long temp=0;
while(!(a1==b1 && b1==c1 && c1==d1 && d1==a1))
{
ta.append("Step "+(i++)+":"+" "+a1+" "+b1+" "+c1+" "+d1+" ");
temp=a1;
a1=Math.abs(a1-b1);
b1=Math.abs(b1-c1);
c1=Math.abs(c1-d1);
d1=Math.abs(d1-temp);
}
ta.append("Step "+(i)+":"+" "+a1+" "+b1+" "+c1+" "+d1+" ");
ta.append(i+" Steps ");
}
catch(Exception e)
{
JOptionPane.showMessageDialog(this,"Enter All Values");
}
}
}
public static void main(String args[])
{
FinalBonus ob = new FinalBonus();
ob.setVisible(true);
ob.setSize(400,400);
}
}