Question

In: Computer Science

Please write in java: Write a fragment that reads a sequence of strings and inserts each...

Please write in java: Write a fragment that reads a sequence of strings and inserts each string that is not numeric at the front of a deque and each string that is numeric at the rear of a deque. Your fragment should also count the number of strings of each kind. Display the message "Strings that are not numeric" followed by the nonnumeric strings and then the message "Strings that are numbers" followed by the numeric strings. Do not empty the deque.

Solutions

Expert Solution

Here NonNumericAndNumericCollection is the fragment which stores the string that is not numeric at the front of a deque and each string that is numeric at the rear of a deque

NonNumericAndNumericCollection.java:

import java.util.LinkedList;

/**
 * DeQue that stores non numeric string in the start and numeric strings at the end
 */
public class NonNumericAndNumericCollection {
    public LinkedList<String> deQue = new LinkedList<>();
    private int numberOfNonNumericStrings = 0;
    private int numberOfNumericStrings = 0;

    public LinkedList<String> getDeQue() {
        return deQue;
    }

    /**
     *
     * @return Number of non numeric strings
     */
    public int getNumberOfNonNumericStrings() {
        return numberOfNonNumericStrings;
    }

    /**
     *
     * @return Number of numeric strings
     */
    public int getNumberOfNumericStrings() {
        return numberOfNumericStrings;
    }

    /**
     * If the string is non numeric then add it to the start. Otherwise add it at the end
     *
     * @param s String to be added
     */
    public void add(String s) {
        try {
            Double.parseDouble(s); //raises NumberFormatException if the string is not numeric
            deQue.addLast(s); //adds the non numeric string at end
            numberOfNumericStrings++;
        } catch (NumberFormatException e) {
            deQue.addFirst(s); //adds the non numeric string at first
            numberOfNonNumericStrings++;
        }
    }

    /**
     * Prints the strings of the DeQue
     */
    public void printNonNumericAndNumericCollection() {
        System.out.println("There are " + getNumberOfNonNumericStrings() + " strings that are not numeric:");
        for (int i = 0; i < numberOfNonNumericStrings; i++)
            System.out.println(i + 1 + ". " + deQue.get(i));
        System.out.println("There are " + getNumberOfNumericStrings() + " strings that are numeric:");
        for (int i = numberOfNonNumericStrings; i < deQue.size(); i++)
            System.out.println(i - numberOfNonNumericStrings + 1 + ". " + deQue.get(i));
    }
}

Main.java:

/**
 * Driver class to test the fragment
 */
public class Main
{
    public static void main(String[] args) {
        NonNumericAndNumericCollection collection = new NonNumericAndNumericCollection();
        collection.add("Rob");
        collection.add("1");
        collection.add("John");
        collection.add("George");
        collection.add("2");
        collection.add("Bill");
        collection.add("3");
        collection.printNonNumericAndNumericCollection();
    }

}

Output:

Please comment if you want any tweaks or clarification in the code


Related Solutions

Write a Java program that reads a list of 30 fruits from the file “fruits.txt”, inserts...
Write a Java program that reads a list of 30 fruits from the file “fruits.txt”, inserts them into a string array, and sorts the array in alphabetical order. String objects can be compared using relational operators such as <, >, or ==. For example, “abc” > “abd” is false, but “abc” < “abd” is true. Sample output: Before Sorting: Cherry, Honeydew, Cranberry, Lemon, Orange, Persimmon, Watermelon, Kiwifruit, Lime, Pomegranate, Jujube, Pineapple, Durian, Plum, Banana, Coconut, Apple, Tomato, Raisin, Mandarine, Blackberry,...
Written in JAVA Coding Write a java project that reads a sequence of up to 25...
Written in JAVA Coding Write a java project that reads a sequence of up to 25 pairs of names and postal (ZIP) codes for individuals (sample input data is attached). Store the data in an object designed to store a first name (string), last name (string), and postal code (integer). Assume each line of input will contain two strings followed by an integer value, each separated by a tab character. Then, after the input has been read in, print the...
Write a java project that reads a sequence of up to 25 pairs of names and...
Write a java project that reads a sequence of up to 25 pairs of names and postal (ZIP) codes for individuals (sample input data is attached). Store the data in an object designed to store a first name (string), last name (string), and postal code (integer). Assume each line of input will contain two strings followed by an integer value, each separated by a tab character. Then, after the input has been read in, print the list in an appropriate...
Please in C++ language Write a program that reads 10,000 words into an array of strings....
Please in C++ language Write a program that reads 10,000 words into an array of strings. The program will then read a second file that contains an undetermined number of words and search the first array for each word. The program will then report the number of words in the second list that were found on the first list.
Java: create a program that reads in a piece of DNA sequence from a sequence file...
Java: create a program that reads in a piece of DNA sequence from a sequence file (dna.seq) (alternatively you can use the getRandomSeq(long) method of the RandomSeq class to generate a piece of DNA sequence), and then print out all the codons in three forward reading frames. Design a method called codon() that can be used to find all the codons from three reading frames. The method will take in an argument, the reading frame (1, 2, or 3), and...
C program, please Write a program that reads a sequence of 10 integer inputs and prints...
C program, please Write a program that reads a sequence of 10 integer inputs and prints the smallest and largest of the inputs and the number of even and odd inputs. for a beginner please, you could use a while loop,if-else,
Please use JAVA to do this: Write a method that takes four strings as parameter. The...
Please use JAVA to do this: Write a method that takes four strings as parameter. The first string should be a pokemon name, the second a pokemon type(either fire, water, or leaf, where water beats fire, fire beats leaf and leaf beats water), the third a pokemon name, and the fourth a pokemon type. The method should print out which pokemon has the advantage over the other based on their type. Example: Pokemon X(which is the fire type) has the...
Python 8.17 LAB: Strings of a Frequency Write a program that reads whitespace delimited strings (words)...
Python 8.17 LAB: Strings of a Frequency Write a program that reads whitespace delimited strings (words) and an integer (freq). Then, the program outputs the strings from words that have a frequency equal to freq in a case insensitive manner. Your specific task is to write a function wordsOfFreqency(words, freq), which will return a list of strings (in the order in which they appear in the original list) that have a frequency same as the function parameter freq. The parameter...
In Java A palindrome is a word or sequence of characters which reads the same backward...
In Java A palindrome is a word or sequence of characters which reads the same backward and forward, such as madam, dad, racecar, 5885. In java Write a program that asks user to enter a word and prints if the word is a palindrome or not.
in .java Write a program that reads an integer with 3 digits and prints each digit...
in .java Write a program that reads an integer with 3 digits and prints each digit per line in reverse order. Hint: consider what you get from these operations: 319%10, 319/10, 31%10, ... Enter an integer of exactly 3 digits(e.g. 538): 319 9 1 3 Hint: consider what you get from these operations: 319%10 319/10 31%10
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT