Question

In: Computer Science

Write a program called Assignment3 (saved in a file Assignment3 .java) that asks a user to...

Write a program called Assignment3 (saved in a file Assignment3 .java) that asks a user to enter two strings.

First, the program prompts: Please enter a string.

The program should read in the string, and prompts: Please enter another string.

The program reads in two strings and it prints a menu to the user.

The program asks for user to enter an option and performs one of the following:

Here are the options on the menu:

Option a: checks if the length of the two strings are the same.

Option b: checks if two strings are same or different.

Option c: checks which string is lexically larger, or if they are same

Option d: prints out the first character (index 0) of each string

Option e: prints out a new string consisting of the first string concatenated (appended) with the second string.

Option f: prints out two strings using upper case letters.

Option q: quit the program.

Solutions

Expert Solution

// Please find the required solution

import java.util.Scanner;

// Assignment class performs string operations with menu
public class Assignment3 {
    public static void main(String[] args) {

        // instantiate scanner to read input from keyboard
        Scanner input = new Scanner(System.in);
        char option;

        // read string 1
        System.out.println("Enter String:");
        String s1 = input.nextLine();

        // read string 1
        System.out.println("Enter Another String:");
        String s2 = input.nextLine();

        // print menu as given in question
        System.out.println("-------Menu-------");
        System.out.println("Option a: checks if the length of the two strings are the same");
        System.out.println("Option b: checks if two strings are same or different");
        System.out.println("Option c: checks which string is lexically larger, or if they are same");
        System.out.println("Option d: prints out the first character (index 0) of each string");
        System.out.println("Option e: prints out a new string consisting of the first string concatenated (appended) with the second string");
        System.out.println("Option f: prints out two strings using upper case letters");
        System.out.println("Option q: quit the program");

        // perform above operations on string
        do {
            System.out.println("Enter the option:");
            option = input.nextLine().charAt(0);
            switch (option) {
                case 'a':
                    System.out.println("Two String Lengths are " + (s1.length() == s2.length() ? "same" : "different"));
                    break;
                case 'b':
                    System.out.println("Two strings are " + (s1.equals(s2) ? "same" : "different"));
                    break;
                case 'c':
                    int value = s1.compareTo(s2);
                    if (value == 0)
                        System.out.println("Two strings are Lexically same.");
                    else if (value > 0)
                        System.out.println(s1 + " is lexically larger");
                    else
                        System.out.println(s2 + " is lexically larger");
                    break;
                case 'd':
                    System.out.println("First characters :");
                    System.out.println(s1.charAt(0) + "  " + s2.charAt(0));
                    break;
                case 'e':
                    String newString = s1.concat(s2);
                    System.out.println(newString);
                    break;
                case 'f':
                    System.out.println("Upper case of s1:" + s1.toUpperCase());
                    System.out.println("Upper case of s2:" + s2.toUpperCase());
                    break;
                case 'q':
                    System.out.println("you have selected to quit:");
                    break;
                default:
                    System.out.println("Invalid option Selected please try again");
            }
        }
        while (option != 'q');


        input.close();
    }
}

sample screenshot:


Related Solutions

In a file called LengthSum.java, write a program that: - Asks the user to enter a...
In a file called LengthSum.java, write a program that: - Asks the user to enter a string. - Asks the user to enter a second string. - Prints out the length of the first string, the length of the second string, and the sum of the two lengths, using EXACTLY the same format as shown below. For example: if the user enters strings "UT" and "Arlington", your program output should look EXACTLY like this: Please enter a string: UT Please...
Write a program that asks the user to enter the name of a file, and then...
Write a program that asks the user to enter the name of a file, and then asks the user to enter a character. The program should count and display the number of times that the specified character appears in the file. Use Notepad or another text editor to create a sample file that can be used to test the program. Sample Run java FileLetterCounter Enter file name: wc4↵ Enter character to count: 0↵ The character '0' appears in the file...
Create a Java program that asks a user to enter two file names. The program will...
Create a Java program that asks a user to enter two file names. The program will read in two files and do a matrix multiplication. Check to make sure the files exist. first input is the name of the first file and it has 2 (length) 4 5 6 7 Second input is the name of the second file and it has 2 (length) 6 7 8 9 try catch method
Write a program that asks the user for a file name. The file contains a series...
Write a program that asks the user for a file name. The file contains a series of scores(integers), each written on a separate line. The program should read the contents of the file into an array and then display the following content: 1) The scores in rows of 10 scores and in sorted in descending order. 2) The lowest score in the array 3) The highest score in the array 4) The total number of scores in the array 5)...
Tail of a File, C++ Program. write a program that asks the user for the name...
Tail of a File, C++ Program. write a program that asks the user for the name of a text file. The program should display the last 10 lines, or all lines if less than 10. The program should do this using seekg Here is what I have so far. #include<iostream> #include<fstream> #include<string> using namespace std; class File { private:    fstream file;    string name; public:    int countlines();    void printlines(); }; int File::countlines() {    int total =...
Write a program IN JAVA that asks the user for a number. The program should check...
Write a program IN JAVA that asks the user for a number. The program should check the number to ensure that it is valid (if not, the user should enter a valid number to continue.) The program should print out all of the prime numbers from 2 up to the number, with up to 10 numbers per line. (Recall: A prime number is a number that is only divisible by itself and 1.) The code should ask the user if...
Program should be written in Java a) Write a program that asks the user to enter...
Program should be written in Java a) Write a program that asks the user to enter the approximate current population of India. You should have the computer output a prompt and then YOU (as the user should enter the population.)  For testing purposes you may use the value of 1,382,000,000 from August 2020. Assume that the growth rate is 1.1% per year. Predict and print the predicted population for 2021 and 2022. The printout should include the year and the estimated...
Write a Java program that asks the user to enter an integer that is used to...
Write a Java program that asks the user to enter an integer that is used to set a limit that will generate the following four patterns of multiples of five using nested loops •Ascending multiples of five with ascending length triangle •Ascending multiples of five with descending length (inverted) triangle •Descending multiples of five with ascending length triangle •Descending multiples of five with descending length (inverted) triangle Use error checking to keep asking the user for a positive number until...
IN JAVA write a program that asks a user for a maximum of 20 user-inputted elements...
IN JAVA write a program that asks a user for a maximum of 20 user-inputted elements and create an array. Then, write a Merge Sort function with recursion (in the main) that takes the user inputted elements in the array, sorts them, and prints them back.
Write a java program that asks the user for a number n and gives them the...
Write a java program that asks the user for a number n and gives them the possibility to choose between computing the sum and computing the product of 1,…,n. Example of running this program: Enter an integer number n: __7________ Enter Sum or Product: __Sum__________________________________ Program output: Sum of 1 ... 7 Sum or Product: Sum Sum = 28 Now second sample of second execution Enter an integer number n: __5__________________________________ Enter Sum or Product: __Product__________________________________ Program output:  Product of 1...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT