Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

V1: Change the original program to alphabetically sort the outputted list of wor

ID: 3863821 • Letter: V

Question

V1: Change the original program to alphabetically sort the outputted list of words – first executable.

V2 + V1: Change the logic so that the words within each line are identified – this would then result in a full word frequency functionality and if the sort has been implemented, it will now sort the entire list – second executable.

V3 +V1+V2: Change the program so that the data is read from input.txt and written to output.tex – third executable.

This is the original ada program that needs to be modified:

-- Read one word per line and print list of unique words and their frequencies
-- Case sensitive
-- This is a minimalist version. No bells or whistles.
with ada.integer_text_io; use ada.integer_text_io;
with ada.text_io; use ada.text_io;

procedure u_words_min is
type Word is record
s: String(1 .. 120); -- The string. Assume 120 characters or less
wlen: Natural; -- Length of the word
count: Natural := 0; -- Total number of occurrences of this word
end record;

type Word_Array is array(1 .. 1000) of Word;

type Word_List is record
words: Word_Array; -- The unique words
num_words: Natural := 0; -- How many unique words seen so far
end record;

procedure get_words(wl: out Word_List) is
begin
wl.num_words := 0; -- only to get rid of a warning
while not End_of_File loop
declare
s: String := Get_Line;
found: Boolean := false;
begin
for i in 1 .. wl.num_words loop
if s = wl.words(i).s(1 .. wl.words(i).wlen) then
wl.words(i).count := wl.words(i).count + 1;
found := true;
end if;
exit when found;
end loop;

if not found then -- Add word to list
wl.num_words := wl.num_words + 1;
wl.words(wl.num_words).s(1 .. s'last) := s;
wl.words(wl.num_words).wlen := s'length;
wl.words(wl.num_words).count := 1;
end if;
end; -- declare
end loop;
end get_words;

procedure put_words(wl: Word_List) is
begin
for i in 1 .. wl.num_words loop
put(wl.words(i).count);
put(" " & wl.words(i).s(1 .. wl.words(i).wlen));
new_line;
end loop;
end put_words;

the_words: Word_List;
begin
get_words(the_words);
put_words(the_words);
end u_words_min;

Explanation / Answer

//reading the fixed number of woords from a string array

package sample;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class sort {

public static void main(String[] args) throws Exception {

String inputFile = "input.txt";
String outputFile = "output.txt";
// reading the contents of the input file
Path filePath = new File("fileName").toPath();
Charset charset = Charset.defaultCharset();
List<String> stringList = Files.readAllLines(filePath, charset);
String[] strWords = stringList.toArray(new String[]{});

String[] strWords = new String[]{"hello", "haii", "happy", "writing", "questions", "answers"};

//sorting the array of words   

Arrays.sort(strWords);

System.out.println("String array sorted (case sensitive)");

for(int i=0; i < strWords.length; i++){
System.out.println(strWords[i]);
}
//writing the contents of the input file to outputfile
FileWriter fileWriter = new FileWriter(outputFile);
PrintWriter out = new PrintWriter(fileWriter);
for (String outputLine : lineList) {
out.println(outputLine);
}
out.flush();
out.close();
fileWriter.close();
}
}

//same program but contents from a file to file