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 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 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.
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 a C++ or Java program that reads an input graph data from a user. Then,...
Write a C++ or Java program that reads an input graph data from a user. Then, it should present a path for the travelling salesman problem (TSP). In the assignment, you can assume that the maximum number ofvertices in the input graph is less than or equal to 20. Input format: This is a sample input from a user. 4 12 0 1 2 0 3 7 0 2 5 1 0 2 1 2 8 1 3 3 2...
In Java: Write a program called F2C that allows the user to convert from degrees Fahrenheit...
In Java: Write a program called F2C that allows the user to convert from degrees Fahrenheit to degrees Celsius. The program should prompt for a temperature in Fahrenheit and output a temperature in Celsius. All calculations should be done in in ints, so be careful of truncation.
Write out code for a nested if statement that allows a user to enter in a...
Write out code for a nested if statement that allows a user to enter in a product name, store the product into a variable called product name and checks to see if that product exists in your nested if statement. You must include 5 product names to search for. If it is then assign the price of the item to a variable called amount and then print the product name and the cost of the product to the console. If...
You have been asked to write program that allows the user to input a first name, middle initial (without the period)
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:Solve a...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT