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

Modify the following exercise 7-2 on comparing strings so it is now done in a GU

ID: 3906845 • Letter: M

Question

Modify the following exercise 7-2 on comparing strings so it is now done in a GUI application frame class definition with a main instead of a console application. The GUI components will be data fields of the class definition. The prompts will now be in labels and the input will now betextfields. The user will hit enter in the second textfield to get the results in two other answer labels. The results (which is greater and the difference amount) will be in these two other answer labels. Declare everything at the top, use the constructor to add the components to your container (use background and foreground colors and the flow layout), and use actionPerformed to do three things: get the text out of the textfields for the two strings (instead of readLines), to perform the nested if else with the comparisons, and the setting of the answers in the two answer labels - which is greater and the difference (instead of printlns). Use a little main method to construct yourself. Take a picture of the output GUI frame with <Alt><PrtScr> which will copy the frame, paste into paint, and save as a .jpg file. Attach the . java program file .jpg output file and submit.

ex7-2

import java.io.*;
public class ex72a
{
public static void main(String[] args) throws IOException
{
String s1, s2;
BufferedReader br = new
BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter a string: ");
s1 = br.readLine();
System.out.print("Enter a string: ");
s2 = br.readLine();
if (s1.equals(s2)) // (s1 == s2)
System.out.println("Same string");
else if (s1.equalsIgnoreCase(s2))
System.out.println("Same string - different case");
else if (s1.compareTo(s2) > 0) // s1 > s2
System.out.println(s1 + " is greater than " + s2);
else // s1 < s2
System.out.println(s1 + " is less than " + s2);
System.out.println("Difference is " + s1.compareTo(s2));
}
}

Explanation / Answer

here is your solution : -------------->>>>>>>>>

import java.awt.*;
import java.awt.event.*;


public class ex72a extends Frame implements ActionListener{
private Label lin1;
private Label lin2;
private Label res1;
private Label res2;
private TextField f1;
private TextField f2;
public ex72a(){
  setTitle("String Difference");
  setSize(500,500);
  addWindowListener(new WindowAdapter(){
            public void windowClosing(WindowEvent we)
        {
                System.exit(0);
             }
        });
        f1 = new TextField(7);
        f2 = new TextField(7);
        f2.addActionListener(this);
        lin1 = new Label("Enter a String : ");
        lin2 = new Label("Enter Another String : ");
        res1 = new Label("Results Shows Here                                       ");
        res2 = new Label("Difference Shows Here ");
        setLayout(new FlowLayout());
        add(lin1);
        add(f1);
        add(lin2);
        add(f2);
        add(res1);
        add(res2);
  setVisible(true);
}
public static void main(String[] args) {
  ex72a a = new ex72a();
}

public void actionPerformed(ActionEvent e){
  String s1 = f1.getText();
  String s2 = f2.getText();
  if (s1.equals(s2)) // (s1 == s2)
  res1.setText("Same string");
  else if (s1.equalsIgnoreCase(s2))
  res1.setText("Same string - different case");
  else if (s1.compareTo(s2) > 0) // s1 > s2
  res1.setText(s1 + " is greater than " + s2);
  else // s1 < s2
  res1.setText(s1 + " is less than " + s2);
  res2.setText("Difference is " + s1.compareTo(s2));
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote