Question

8-7. Modify exercise 7-2 on comparing strings so it is now done in an applet instead...

8-7. Modify exercise 7-2 on comparing strings so it is now done in an applet instead of an application. The prompts will now be in labels and the input will now be textfields. The user will hit enter in the secondtextfield 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 init 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).

Here is the original code for 7-2:

import java.io.*;
public class ex72
{
        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 alphabetically greater than "
                                + s2);
                else            // s1 < s2
                        System.out.println(s1 + " is alphabetically less than "
                                + s2);
                System.out.println("Difference is " + s1.compareTo(s2));
        }
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

The required code is given as follows. Hope it will help you understand the code made.

cmp2str.java

import java.awt.*;
import java.applet.*;
public class cmp2str extends Applet
{
TextField tf,tf1;
public void init()
{
tf = new TextField(10);
tf1 = new TextField(10);
add(tf);
add(tf1);
tf.setText("");
tf1.setText("");
}
public void paint(Graphics g)
{
String ip1,ip2;
g.drawString("Plese enter strings for comparison: ",15,55);
ip1=tf.getText();
ip2=tf1.getText();
g.setColor(Color.black);
if(ip1.equals(ip2))
{
g.drawString(ip1+"is equal to"+ip2,15,75);
}
else if(ip1.equalsIgnoreCase(ip2))
{
g.drawString(ip1+"is a same string with different case"+ip2,20,95);
}
else if(ip1.compareTo(ip2)>0)
{
g.drawString(ip1+"is alphabetically greater than"+ip2,25,115);
}
else
{
g.drawString(ip1+"is alphabetically less than"+ip2);
}
g.drawString(Differece is: "+ip1.compareTo(ip2));
showStatus("Comparison of the two Strings.....");
}

public boolean action(Event e, Object o)
{
repaint();
return true;
}

}

Please rate the answer if it helped......Thnakyou

Hope it helps....

Add a comment
Know the answer?
Add Answer to:
8-7. Modify exercise 7-2 on comparing strings so it is now done in an applet instead...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • A Chinese restaurant wants to have a computer-based search system that will display a recipe for...

    A Chinese restaurant wants to have a computer-based search system that will display a recipe for each dish. The owner of the restaurant wants the system to be flexible in that its recipes are stored in a text file. This way, the number of dishes can be expanded by adding more entries to the text file without changing any program code. After analyzing the coding the restaurant realizes that Current coding only outputs first two line of the recipe. Goal:...

  • Project 7-3 Guessing Game import java.util.Scanner; public class GuessNumberApp {    public static void main(String[] args)...

    Project 7-3 Guessing Game import java.util.Scanner; public class GuessNumberApp {    public static void main(String[] args) { displayWelcomeMessage(); // create the Scanner object Scanner sc = new Scanner(System.in); String choice = "y"; while (choice.equalsIgnoreCase("y")) { // generate the random number and invite user to guess it int number = getRandomNumber(); displayPleaseGuessMessage(); // continue until the user guesses the number int guessNumber = 0; int counter = 1; while (guessNumber != number) { // get a valid int from user guessNumber...

  • Need help in the below question. Answer in java Start with the tree.java program (Listing 8.1)...

    Need help in the below question. Answer in java Start with the tree.java program (Listing 8.1) and modify it to create a binary tree from a string of letters (like A, B, and so on) entered by the user. Each letter will be displayed in its own node. Construct the tree so that all the nodes that contain letters are leaves. Parent nodes can contain some non-letter symbol like +. Make sure that every parent node has exactly two children....

  • Modification of original code so it can include 1) at least one array 2)*New English words...

    Modification of original code so it can include 1) at least one array 2)*New English words that are written/saved to the .txt file(s) must be in alphabetical order* (e.g, Apple cannot be in a lower line than Orange). This is what I'm really struggling with as I don't know how to organize alphabetically only the english word translation without the spanish equivalent word also getting organized. The only idea I have right now is to recreate the code using two...

  • could you please help me with this problem, also I need a little text so I...

    could you please help me with this problem, also I need a little text so I can understand how you solved the problem? import java.io.File; import java.util.Scanner; /** * This program lists the files in a directory specified by * the user. The user is asked to type in a directory name. * If the name entered by the user is not a directory, a * message is printed and the program ends. */ public class DirectoryList { public static...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT