+1 (315) 557-6473 

Implement The Use of Arrays and Characters in Java Assignment Solution.


Instructions

Objective
Write a java assignment program to work with arrays and characters.

Requirements and Specifications

Program to work with arrays and characters ADT in java

Screenshot

Program to work with arrays and characters ADT in java 1
Program to work with arrays and characters ADT in java 2

Source Code

QUESTION 1

package javatest;

import javatest.exception.EndOfStreamException;

import javatest.reader.CharacterReader;

import javatest.reader.SimpleCharacterReader;

import java.util.*;

public class Q1 {

    public static void main(String[] args) {

        Map map = new HashMap<>();

        StringBuilder builder = new StringBuilder();

        CharacterReader reader = new SimpleCharacterReader();

        try {

            while(true) {

                char c = reader.getNextChar();

                if (Character.isAlphabetic(c)) {

                    builder.append(builder.length() == 0 ? Character.toUpperCase(c) : Character.toLowerCase(c));

                }

                else {

                    if (builder.length() > 0) {

                        map.merge(builder.toString(), 1, Integer::sum);

                        builder = new StringBuilder();

                    }

                }

            }

        }

        catch (EndOfStreamException e) {

            if (builder.length() > 0) {

                map.merge(builder.toString(), 1, Integer::sum);

            }

        }

        List> pairs = new ArrayList<>(map.entrySet());

        pairs.sort((o1, o2) -> {

            int diff = o2.getValue() - o1.getValue();

            if (diff == 0) {

                return o1.getKey().compareTo(o2.getKey());

            }

            return diff;

        });

        for (Map.Entry pair : pairs) {

            System.out.println(pair.getKey() + " - " + pair.getValue());

        }

    }

}

QUESTION 2

package javatest;

import javatest.exception.EndOfStreamException;

import javatest.reader.CharacterReader;

import javatest.reader.SimpleCharacterReader;

import javatest.reader.SlowCharacterReader;

import java.util.*;

public class Q2 {

    private static final Map map = new HashMap<>();

    private static boolean[] completed;

    private static void printState() {

        List> pairs;

        synchronized (map) {

            pairs = new ArrayList<>(map.entrySet());

        }

        pairs.sort((o1, o2) -> {

            int diff = o2.getValue() - o1.getValue();

            if (diff == 0) {

                return o1.getKey().compareTo(o2.getKey());

            }

            return diff;

        });

        for (Map.Entry pair : pairs) {

            System.out.println(pair.getKey() + " - " + pair.getValue());

        }

    }

    public static void main(String[] args) {

        int threadNum = 10;

        Thread[] threads = new Thread[threadNum];

        completed = new boolean[threadNum];

        for (int i = 0; i < threadNum; i++) {

            final int index = i;

            threads[i] = new Thread(() -> {

                CharacterReader reader = new SlowCharacterReader();

                StringBuilder builder = new StringBuilder();

                try {

                    while (true) {

                        char c = reader.getNextChar();

                        if (Character.isAlphabetic(c)) {

                            builder.append(builder.length() == 0 ? Character.toUpperCase(c) : Character.toLowerCase(c));

                        } else {

                            if (builder.length() > 0) {

                                synchronized (map) {

                                    map.merge(builder.toString(), 1, Integer::sum);

                                }

                                builder = new StringBuilder();

                            }

                        }

                    }

                } catch (EndOfStreamException e) {

                    if (builder.length() > 0) {

                        synchronized (map) {

                            map.merge(builder.toString(), 1, Integer::sum);

                        }

                    }

                    completed[index] = true;

                }

            });

            threads[i].start();

        }

        long startTime = System.currentTimeMillis();

        long seconds = 0;

        while(true) {

            boolean stop = true;

            for (boolean b : completed) {

                if (!b) {

                    stop = false;

                    break;

                }

            }

            if (stop) {

                break;

            }

            if ((System.currentTimeMillis() - startTime) / 1000 >= (seconds + 10)) {

                seconds += 10;

                printState();

                System.out.println();

            }

        }

        for (int i = 0; i < threadNum; i++) {

            try {

                threads[i].join();

            } catch (Exception ignored) {

            }

        }

        System.out.println("Final:");

        printState();

    }

}