Question

JAVA Overview Create a method public static void spongeBobify(String inputPath, String outputPath...

JAVA

Overview

Create a method public static void spongeBobify(String inputPath, String outputPath, Mode mode) that will convert a file to Mocking Sponge Bob text in 3 different modes.

  • EveryOther
    • capitalize the first letter of the string
    • capitalize every other letter (ignoring non-letter character like punctuation and spaces).
    • non-letter characters don't count toward the every other count
    • Example: mocking sponge bob! → MoCkInG sPoNgE bOb!
  • Vowels
    • capitalize every vowel (not including y)
  • Random
    • capitalize each letter with a probability of 35%.

The enum definition is shown below.

enum ConversionMode {

EveryOther, Vowels, Random

}

Input File Format

The input file will be a text on multiple lines that may contain letters, numbers, and punctuation.

Output File Format

The output file will contain the same number of lines but each line will be SpongeBob-ified.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

package stringprog;

import java.io.*;

import java.util.*;

// Creates an enumeration ConversionMode

enum ConversionMode

{

EveryOther, Vowels, Random

}// End of enumeration

// Defines a class SpongeBobifyDemo

public class SpongeBobifyDemo

{

// Method to perform spongeBobify

public static void spongeBobify(String inputPath, String outputPath,

ConversionMode mode)

{

// Scanner class object declared

Scanner readF = null;

// FileWriter class object declared

FileWriter fileWriter = null;

// PrintWriter class object declared

PrintWriter writeF = null;

// To store the data read from file

String data = "";

// try block begins

try

{

// Opens the file for reading

readF = new Scanner(new File(inputPath));

// Set true for append mode

fileWriter = new FileWriter(outputPath, true);

// Opens the file for writing

writeF = new PrintWriter(fileWriter);

// Loops till end of the file

while(readF.hasNext())

// Reads a line of data and concatenates it with data

data += readF.nextLine() + "\n";

}// End of try block

// Catch block to handle FileNotFoundException exception

catch(IOException fe)

{

System.err.println("Unable to open the file fore reading.");

}// End of catch block

switch(mode)

{

case EveryOther:

// To store the result string

String result = "";

// Loops till length of the data

for(int x = 0, y = 0; x < data.length(); x++)

{

// Checks if the current character is space or is a digit

if(data.charAt(x) == ' ' ||

Character.isDigit(data.charAt(x)))

// Decrease the y value by one

y--;

// Checks y value is even then even position

if(y % 2 == 0)

{

// Converts the current character to upper case

// and concatenates to result string

result += Character.toUpperCase(data.charAt(x));

// Increase the counter y by one

y++;

}// End of if condition

// Other odd position

else

{

// Converts the current character to lower case

// and concatenates to result string

result += Character.toLowerCase(data.charAt(x));

// Increase the counter y by one

y++;

}// End of else

}// End of for loop

// Displays the result at console

System.out.print("\n\n EveryOther: " + result);

// Writes the result to file

writeF.println("EveryOther: " + result);

// Writes new line character

writeF.println();

break;

case Vowels:

// To store the result string

result = "";

// Loops till length of the data

for(int x = 0; x < data.length(); x++)

{

// Extracts the current character

char ch = data.charAt(x);

// Converts the character to upper case

ch = Character.toUpperCase(ch);

// Checks for vowel

if(ch == 'A' || ch == 'E' || ch == 'I' || ch == 'U')

// Concatenates the character to result

result += ch;

// Otherwise not vowel

else

// Concatenates the current character of data to result

result += data.charAt(x);

}// End of for loop

// Displays the result at console

System.out.print("\n\n Vowels: " + result);

// Writes the result to file

writeF.println("Vowels: " + result);

// Writes new line character

writeF.println();

break;

case Random:

// Counter for 35%

int counter = 0;

// Creates an integer array to store random changed position

// with data length

int changedPos[] = new int[data.length()];

// Creates an character array to store random changed character

// to upper case with data length

char resultChar[] = new char[data.length()];

double len = data.length();

// Calculates 35%

int percent = (int)(len * 0.35);

// To store the result

String finalString = "";

// Loops till counter value is less than or equals to

// percent value (35%)

while(counter <= percent)

{

// Generates a random index position

// between 0 and length of the data

int pos = new Random().nextInt(data.length());

// Extracts the character at random index position

char ch = data.charAt(pos);

// Checks if the character is lower case

// and not new line character

if(Character.isLowerCase(ch) && data.charAt(pos)!= '\n')

{

// Converts the character to upper case

// and stores it at random index position

resultChar[pos] = Character.toUpperCase(ch);

// Increase the counter by one

counter++;

// Sets the random changed index position value to 1

changedPos[pos] = 1;

}// End of if condition

}// End of while loop

// Loops till length of the data

for(int x = 0, y = 0; x < data.length(); x++)

// Checks if changed position x index position

// value is 1 then character is changed

if(changedPos[x] == 1)

// Concatenates the character stored at

// x index position of resultChar with finalString

finalString += resultChar[x];

// Otherwise character is not changed

else

// Concatenates the character stored at

// x index position of data with finalString

finalString += data.charAt(x);

// Displays the result at console

System.out.print("\n\n Random: " + finalString);

// Writes the result to file

writeF.print("Random: " + finalString);

// Writes new line character

writeF.println();

break;

default:

System.out.println("Invalid choice..........");

}// End of switch case

// closes the file

readF.close();

writeF.close();

}// End of method

// main method definition

public static void main(String ss[])

{

// Calls the method for mode "EveryOther"

spongeBobify("spongeBobify.txt", "spongeBobifyTest.txt",

ConversionMode.EveryOther);

// Calls the method for mode "Vowels"

spongeBobify("spongeBobify.txt", "spongeBobifyTest.txt",

ConversionMode.Vowels);

// Calls the method for mode "Random"

spongeBobify("spongeBobify.txt", "spongeBobifyTest.txt",

ConversionMode.Random);

}// End of main method

}// End of class

Sample Output:

EveryOther: ThIs Is A dEmO tO cHeCk.
EvErY oThEr LeTtEr CaPiTaL.
tRy 2 CaTcH tHe LoGiC!


Vowels: ThIs Is A dEmo to chEck.
EvEry othEr lEttEr cApItAl.
Try 2 cAtch thE logIc!


Random: This IS A demO To cHeCK.
Every otHEr LeTtER CaPItal.
TrY 2 Catch tHE lOGIc!

spongeBobify.txt file contents

This is a demo to check.
Every other letter capital.
Try 2 catch the logic!

spongeBobifyTest.txt file contents

EveryOther: ThIs Is A dEmO tO cHeCk.
EvErY oThEr LeTtEr CaPiTaL.
tRy 2 CaTcH tHe LoGiC!


Vowels: ThIs Is A dEmo to chEck.
EvEry othEr lEttEr cApItAl.
Try 2 cAtch thE logIc!


Random: ThIs IS A dEmo to Check.
EVERy OtHeR LettER cApiTaL.
TrY 2 CAtch thE LoGic!

Add a comment
Know the answer?
Add Answer to:
JAVA Overview Create a method public static void spongeBobify(String inputPath, String outputPath...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Regular Expression processor in Java Overview: Create a Java program that will accept a regular expression...

    Regular Expression processor in Java Overview: Create a Java program that will accept a regular expression and a filename for a text file. The program will process the file, looking at every line to find matches for the regular expression and display them. Regular Expression Format The following operators are required to be accepted: + - one or more of the following character (no groups) * - zero or more of the following character (no groups) [] – no negation,...

  • Write a method that has the following header: public static void printShuffled(String filename) The method reads...

    Write a method that has the following header: public static void printShuffled(String filename) The method reads a text file, filename, sentence by sentence into an array list, shuffles the sentences, and then prints out the shuffled contents. Assume sentences end with one of these characters: ".", ":", "!" and "?". Your method should create an array list for storing the sentences. The array list should be created with approximately the correct number of sentences, instead of being gradually expanded as...

  • Submit Chapter6.java with a public static method named justifyText that accepts two parameters; a Scanner representing...

    Submit Chapter6.java with a public static method named justifyText that accepts two parameters; a Scanner representing a file as the first parameter, and an int width specifying the output text width. Your method writes the text file contents to the console in full justification form (I'm sure you've seen this in Microsoft Word full.docx ).  For example, if a Scanner is reading an input file containing the following text: Four score and seven years ago our fathers brought forth on this...

  • For Java please.Artwork. javaimport java.util.Scanner;public class ArtworkLabel {public static void main(String[] args)...

     Given main(). define the Artist class (in file Artist java) with constructors to initialize an artist's information, get methods, and a printlnfo() method. The default constructor should initialize the artist's name to "None' and the years of birth and death to 0. printinfo() should display Artist Name, born XXXX if the year of death is -1 or Artist Name (XXXX-YYYY) otherwise. Define the Artwork class (in file Artwork.java) with constructors to initialize an artwork's information, get methods, and a printinfo() method. The constructor should...

  • Write the Java code for the class WordCruncher. Include the following members:

    **IN JAVAAssignment 10.1 [95 points]The WordCruncher classWrite the Java code for the class WordCruncher. Include the following members:A default constructor that sets the instance variable 'word' to the string "default".A parameterized constructor that accepts one String object as a parameter and stores it in the instance variable. The String must consist only of letters: no whitespace, digits, or punctuation. If the String parameter does not consist only of letters, set the instance variable to "default" instead. (This restriction will make...

  • Anagram Detector Due by Friday 12 April 2019 11:59 PM Program Overview: This program will be able...

    I need a program in fortran 95 that can detect if two strings are anagrams Anagram Detector Due by Friday 12 April 2019 11:59 PM Program Overview: This program will be able to determine if two inputted texts are anagrams of one another Relevant Details and Formulas: An anagram of a text is a rearrangement of the letters such that it forms another, usually intelligible, set of words. Capitalization is not important and any white space, punctuation, or other non-letter...

  • Anagram Detector Due by Friday 12 April 2019 11:59 PM Program Overview: This program will be...

    Anagram Detector Due by Friday 12 April 2019 11:59 PM Program Overview: This program will be able to determine if two inputted texts are anagrams of one another. Relevant Details and Formulas: An anagram of a text is a rearrangement of the letters such that it forms another, usually intelligible, set of words. Capitalization is not important and any white space, punctuation, or other non-letter symbols should be ignored. Program Specification: * Prompt the user for a pair of text...

  • Pig Latin Translator in Java The goal here is to read in the characters stored in a text file and create a second text file that contains the original text but with every word converted to “Pig-Latin....

    Pig Latin Translator in Java The goal here is to read in the characters stored in a text file and create a second text file that contains the original text but with every word converted to “Pig-Latin.” The rules used by Pig Latin are as follows: • If a word begins with a vowel, just as "yay" to the end. For example, "out" is translated into "outyay". • If it begins with a consonant, then we take all consonants before...

  • Write a Java program to read customer details from an input text file corresponding to problem...

    Write a Java program to read customer details from an input text file corresponding to problem under PA07/Q1, Movie Ticketing System The input text file i.e. customers.txt has customer details in the following format: A sample data line is provided below. “John Doe”, 1234, “Movie 1”, 12.99, 1.99, 2.15, 13.99 Customer Name Member ID Movie Name Ticket Cost Total Discount Sales Tax Net Movie Ticket Cost Note: if a customer is non-member, then the corresponding memberID field is empty in...

  • import java.util.Scanner; import java.io.File; public class Exception2 {        public static void main(String[] args) {              ...

    import java.util.Scanner; import java.io.File; public class Exception2 {        public static void main(String[] args) {               int total = 0;               int num = 0;               File myFile = null;               Scanner inputFile = null;               myFile = new File("inFile.txt");               inputFile = new Scanner(myFile);               while (inputFile.hasNext()) {                      num = inputFile.nextInt();                      total += num;               }               System.out.println("The total value is " + total);        } } /* In the first program, the Scanner may throw an...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT