Question

In: Computer Science

Write a java code that allows the user to input any word/name from the console (10...

Write a java code that allows the user to input any word/name from the console (10 words/names only) and then sorts and alphabetizes them into an array without using predefined methods (.compareTo(), or any sorter functions) and then print out the results.

you must have three methods that can:

Compare multiple strings

Swap elements

Sort and alphabetize string

***REMEMBER DO NOT USE ANY PRE-DEFINED METHODS***

Example of how the code should work:

Enter names/words (10 only):

"james, john, alex, aaron, brandon, nick, gian, deavan, daniel, jake"

*sorted output*:

"Aaron, Alex, Brandon, Daniel, Deavan, Gian, Jake, James, John, Nick"

Solutions

Expert Solution

JAVA CODE:

import java.util.*;
class Jav{
    //method to compare strings
    static int Compare(String a,String b)
    {
        int l1 = a.length();
        int l2 = b.length();
        int len = Math.min(l1, l2);
        int i = 0;
        while (i < len) {
            int x = (int) a.charAt(i);
            int y = (int) b.charAt(i);
            if (x > y)
                return 1; 
            else if (x < y)
                return 0;
            i++;
        }
        if (l2 < l1)
            return 1;
        else
            return 0;
    }
    //method to sort, alphabetize and print strings
    static void Sort(String str[],int count)
    {
        String temp;
        //sorting strings
        for (int i = 0; i < count; i++) 
        {
            for (int j = i + 1; j < count; j++) {
                //comparing strings to sort
                if (Compare(str[i], str[j]) == 1) {
                    //swapping the strings
                    temp = str[i];
                    str[i] = str[j];
                    str[j] = temp;
                }
            }
        }
        //alphabetize strings
        for (int i = 0; i < count; i++)
        {
            str[i] = str[i].substring(0, 1).toUpperCase() + str[i].substring(1, str[i].length());
        }
        //printing strings
        System.out.print("Sorted String: ");
        for (int i = 0; i <= count - 1; i++) 
        {
            System.out.print(str[i]);
            if (i!=count-1)
            System.out.print(", ");
        }
    }
    //main method
    public static void main(String[] args) {
        int count;
        Scanner scan = new Scanner(System.in);
        
        //prompt to take input 
        System.out.print("Enter number of strings you would like to enter:\n");
        count = scan.nextInt();

        String str[] = new String[count];
        //enter inputs line by line
        System.out.println("Enter the Strings one by one:");
        scan.nextLine();
        
        for(int i = 0; i < count; i++)
        {
            str[i] = scan.nextLine();
        }
        scan.close();
        //function call for Sorting the strings
        Sort(str, count);
    }
}

SAMPLE RUN:

code:

output:


Related Solutions

Java Code: Write an application that takes in user input. (Name, Age, and Salary) ------ Write...
Java Code: Write an application that takes in user input. (Name, Age, and Salary) ------ Write an application that includes a constructor, user input, and operators.
Please code C# 10. Write a program that allows a user to input names and corresponding...
Please code C# 10. Write a program that allows a user to input names and corresponding heights (assumed to be in inches). The user can enter an indefinite number of names and heights. After each entry, prompt the user whether they want to continue. If the user enters true, ask for the next name and height. If the user enters false, display the name of the tallest individual and their height. Sample run: “Name?” James “Height?” 50 “Continue?” True “Name?”...
Please write the code JAVA Write a program that allows the user to enter the last...
Please write the code JAVA Write a program that allows the user to enter the last names of five candidates in a local election and the number of votes received by each candidate. The program should then output each candidate’s name, the number of votes received, and the percentage of the total votes received by the candidate. Your program should also output the winner of the election. A sample output is: Candidate      Votes Received                                % of Total Votes...
Write a Java program that allows the user to specify a file name on the command...
Write a Java program that allows the user to specify a file name on the command line and prints the number of characters, words, lines, average number of words per line, and average number of characters per word in that file. If the user does not specify any file name, then prompt the user for the name.
Write the code in Java: 1. Create a method that displays your name in the console....
Write the code in Java: 1. Create a method that displays your name in the console. This method is void and takes no parameters. Make an app that runs the method in response to a button press. 2. Create a version of the method in #1 that takes the text (String) to be displayed as a parameter. Allow the user to enter the text in a dialog box or text field and display that text in the console. Be sure...
Write a java program to reverse element of a stack. For any given word (from input),...
Write a java program to reverse element of a stack. For any given word (from input), insert every character (from the word) into a stack. The output from the stack should be the same as the input. Your program should be using a stack and a queue to complete this process. 1. Push into stack 2. Pop from stack 3. Enqueue into queue 4. Dequeue from queue 5. Push into stack 6. Pop from stack and display java
Write Java code that allows a user to repeatedly enter numbers. Each time the user enters...
Write Java code that allows a user to repeatedly enter numbers. Each time the user enters a number, the program should print out the average of the last 3 numbers (or all numbers if 3 or less have been entered). I would like a detailed explanation so that a beginner level Java programmer could understand.
Write a Java program that prompts the user to input a word (String). The program must...
Write a Java program that prompts the user to input a word (String). The program must print the reversed word with all consecutive duplicate characters removed. The program must contain the following classes: - The StackX class (you can use the Java Stack class). - The Reverse class which must contain a private data field called “word” of type string, a constructor, and a void method called revNoDup(). The revNoDup() method must reverse the word and remove the consecutive duplicate...
You have been asked to write program that allows the user to input a first name,...
You have been asked to write program that allows the user to input a first name, middle initial (without the period), and last name of a user and then display that person’s name with the first, middle initial followed by a period, and last name last. BEFORE creating the program, use structured programming principles to document how you are going to develop the program. Use Microsoft Word to complete this part of the assessment. Answer each of the following areas:...
Write Java program that asks a user to input a letter, converts the user input to...
Write Java program that asks a user to input a letter, converts the user input to uppercase if the user types the letter in lowercase, and based on the letter the user the user enters, display a message showing the number that matches the letter entered. For letters A or B or C display 2 For letter D or E or F display 3 For letter G or H or I display 4 For letter J or K or L...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT