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

Objective: You will write a program that (1) sanitizes text by hiding inappropri

ID: 3621052 • Letter: O

Question

Objective:
You will write a program that (1) sanitizes text by hiding inappropriate words and (2) reports the percentage of words that are inappropriate. The primary objective of the assignment is to use loops in a Java program. You will also learn to read input from a file and use methods on strings.


--------------------------------------------------------------------------------

Program input:
The program will read English text from a text file, which will be provided as a command line argument (use args[0]). The text contains sentences, each terminated with a period. Each sentence contains words that are separated with a single whitespace. Here are some assumptions about the input:
Two words in a sentence will be separated by only one whitespace character (blank), except for the last word in a sentence and the first word of the next sentence, which are separated by a period and a whitespace character.
Linebreaks (new lines, carriage return, or linefeed characters) may be used only at the end of the text.
Other than a period, no other punctuation will be used.
A period can only appear after a word.
Uppercase and lowercase letters may be used, but no digits will be used.
Any of the four inappropriate words can be part of a larger word, in which case it is not considered inappropriate.
Four words are considered inappropriate irrespective of whether any of the letters are in uppercase or lowercase : barking, bum, darn, gosh.




--------------------------------------------------------------------------------

Program output:
The program outputs sanitized text on the standard output. Text is output with the same whitespaces and line breaks. Inappropriate words are sanitized as follows:
The first letter of the word is kept intact.
Each remaining letter of the word is output as a single *.
If there is a period after the word, that period is printed.
After printing the sanitized text, on the next line, the program prints:
xxx.x% of the words are inappropriate.

where xxx.x is the numerical percentage with up to 3 digits before the decimal point, and exactly 1 digit after the decimal point.
Sample input: A gosh darn long time ago our darn fathers brought forth on this barking continent a new bum conceived in liberty and dedicated to the darn proposition that every bum is created equal. But we are engaged in a gosh bummer barking whether that bum or any bum so conceived and so gosh can long endure. We are met on a darn great battlefield of that bummer. We are barking to bark a portion of that darn field as a final barking place for those who here gave their lives that that nation might live. Gosh it is altogether barking and darn proper that we should do this barking.

Sample output: A g*** d*** long time ago our d*** fathers brought forth on this b****** continent a new b** conceived in liberty and dedicated to the d*** proposition that every b** is created equal. But we are engaged in a g*** bummer b****** whether that b** or any b** so conceived and so g*** can long endure. We are met on a d*** great battlefield of that bummer. We are b****** to bark a portion of that d*** field as a final b****** place for those who here gave their lives that that nation might live. G*** it is altogether b****** and d*** proper that we should do this b******.
18.3% of the words are inappropriate.

Explanation / Answer

import java.io.*; public class Main { public static void main(String args[]) { String s1 ="barking",s2="bum",s3="darn", s4="gosh"; int pos1 = 0, pos2=0, pos3=0; int init = 1; try{ // Open the file that is the first // command line parameter FileInputStream fstream = new FileInputStream(args[0]); // Get the object of DataInputStream DataInputStream in = new DataInputStream(fstream); BufferedReader br = new BufferedReader(new InputStreamReader(in)); String strLine; //Read File Line By Line while ((strLine = br.readLine()) != null) { // Print the content on the console //System.out.println (strLine); int i =0; while( pos2 != -1) { pos2 = strLine.indexOf(" ",pos1); pos3 = strLine.indexOf(".",pos1); String s; if ( pos2 != -1) { s = strLine.substring(pos1,pos2); if (init==1){ if (s.compareToIgnoreCase(s1)==0) System.out.print("B****** "); else if (s.compareToIgnoreCase(s2)==0) System.out.print("B** "); else if (s.compareToIgnoreCase(s3)==0) System.out.print("D*** "); else if (s.compareToIgnoreCase(s4)==0) System.out.print("G*** "); else System.out.print(s+" "); init =0; } else { if (s.compareToIgnoreCase(s1)==0) System.out.print("b****** "); else if (s.compareToIgnoreCase(s2)==0) System.out.print("b** "); else if (s.compareToIgnoreCase(s3)==0) System.out.print("d*** "); else if (s.compareToIgnoreCase(s4)==0) System.out.print("g*** "); else System.out.print(s+" "); } } else { s = strLine.substring(pos1);s = s.substring(0, s.length()-1); if (s.compareToIgnoreCase(s1)==0) System.out.print("b******."); else if (s.compareToIgnoreCase(s2)==0) System.out.print("b**."); else if (s.compareToIgnoreCase(s3)==0) System.out.print("d***."); else if (s.compareToIgnoreCase(s4)==0) System.out.print("g***."); else System.out.print(s+". "); init=1; } if ((pos3+1)==pos2) init=1; pos1 = pos2+1; } } //Close the input stream in.close(); }catch (Exception e){//Catch exception if any System.err.println("Error: " + e.getMessage()); } } }