Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

programming language is java Recall the text from this classic scene from Jaws: ELLEN: Do you see the kids? BRODY: Probably out in the back

programming language is java

Recall the text from this classic scene from Jaws:

ELLEN: Do you see the kids? BRODY: Probably out in the back yard. ELLEN: In Amity, you say 'Yahd.' (she gives it the Boston sound) BRODY: The kids are in the yahd, playing near the cah. How's that sound? ELLEN: Like you're from N'Yawk.

(If you don't recall, or if you've never seen the movie, stop reading and watch it immediately. It's one of the best movies of all time.)

In this assignment, you'll help Brody sound like he's from Amity, by writing a program that reads in the text of the movie's script, and transforms it so that it when read, the speaker would have a South Boston (or Southie) accent.

Some scaffolding code to read files from the web and for saving a String to a file is provided for you. These are simply two methods. Feel free to look at the contents of the methods, but they contain Java that you hava not yet learned in class. You are not yet required to understand how they work.

getWebContents( )

In order to read a file from the web, you can use the getWebContents( ) method:

import java.io.*; import java.net.*; /* url is the address of a file on the web, e.g., http://www.espn.com * If successful, the method returns the contents of the URL as a String. * On failure, it returns null */ public static String getWebContents(String url) { BufferedReader br = null; String result = null; try { URL toFetch = new URL(url); br = new BufferedReader(new InputStreamReader(toFetch.openStream())); StringBuilder ret = new StringBuilder(); String line = br.readLine(); while (line != null) { ret.append(line+' '); line = br.readLine(); } result = ret.toString(); } catch (MalformedURLException ex) { ex.printStackTrace(); System.err.println("Malformed URL: " + url); } catch (IOException ioe) { ioe.printStackTrace(); System.err.println("Error reading from URL: " + url); } finally { if(br!=null) { try { br.close(); } catch (IOException ex) { ex.printStackTrace(); System.err.println("Couldn't close connection to url properly: " + url); } } return result; } } 

You'd use this to download the Jaws script and return it as a String.

Your Job

Write a program called Accentenator.java. Your program should behave as follows:

Using the getWebContents() method described above, obtain the Jaws script.

Rewrite the text with its equivalent in a Southie accent.

Save: Finally, the program should save the entire story written in a Southie accent to a file called "brody-crib-sheet.txt". Use saveDoc() method provided.

saveSoc( )

In order to save your transformed String written in a Southie accent, you can use the saveDoc( ) method:

import java.io.*; /* Saves the String contents into a file with name filename. * * If the file did not previously exist, it is created. * If the file with the same name existed previously, it is overwritten. */ public static void saveDoc(String contents, String filename) { PrintWriter pw = null; try { pw = new PrintWriter(new FileWriter(new File(filename))); pw.print(contents); } catch (IOException ioe) { ioe.printStackTrace(); System.err.println("Error writing to file: " + filename); } finally { if(pw!=null) { pw.close(); } } } 

The Southie Accent

Handling all of the exceptions and idiosyncracies of language is difficult, so for this assignment we will use a few simple rules to approximate the Southie accent.

Basic Rules (12 points each)

If there's an 'r' following a vowel ('a', 'e', i', 'o', or 'u'), replace 'r' with 'h'. For example, "I left my car keys by the harbor" becomes "I left my cah keys by the hahboh."

If a word ends in 'a', append an 'r'. For example "tuna" becomes "tunar", "Cuba" becomes "Cubar", and "idea" becomes "idear". (Don't change this to an 'h' based on the previous rule; leave it as an 'r'.) Do not apply this rule to the word "a", so "a tuna" should become "a tunar", not "ar tunar".

Replace the word "very" with the word "wicked". So "very hard" becomes "wicked hahd".

Make sure that these rules apply to capitalized words/letters as well as lower-case.

Exceptions (12 points each)

If 'r' is at the end of a word and is preceded by "ee" or 'i' replace 'r' with "yah" instead of 'h'. For example, "deer" becomes "deeyah" instead of "deeh", but "veneers" still becomes "veneehs".

If 'r' is at the end of a word and is preceded by "oo", replace 'r' with "wah". For example, "door" becomes "doowah" instead of "dooh" (but "doors" still becomes "doohs").

Suggestions

Don't try to program everything all at once. Do it in parts. Start by getting the web page, and saving it to a file. You can then open the file to see if your code worked. As you add more functionality to your program, you can open this file after running the program to see how well the program worked.

Try to break the rest of the program down into manageable parts, and implement each part as a method. For instance, you might create a method that is passed a single word as an argument, and returns the word converted into a Boston accent. You might also write a method which is passed a String which could consist of several words (i.e., a sentence or paragraph), and returns a new String, which is just like the original, but converted into a Boston accent. (It could do this by calling the previous method repeatedly.) Other helpful methods would be ones to find the beginning and ending of quotes (if you're attempting the extra credit), and an isVowel method, that takes a char value as argument, and returns true if the char is a vowel.

The Character class contains useful methods like Character.isLetter(char c) that returns true if the char c is a letter ('A' through 'Z' or 'a' through 'z'), and false otherwise. This could help you tell whether a char is at the end of the word (by checking whether the next char is a letter or not).

Until you're sure that all of your methods are working properly, don't try to make the main() method do everything. Instead, use the main() method to check whether one of your methods is working properly. For instance, if I just wrote a definition of the isVowel method, I could add a line to main() that reads

System.out.println(isVowel('u'));

to make sure that my method returns true for the vowel 'u'. Once you've tested all of your individual methods, then you can start replacing all of those println statements in main() with real code that does what the assignment is asking.

String methods

If you find something useful, you're welcome to use any of the methods in Java's String class, even if you find some that we have not covered in class or are mentioned in the textbook.

Scanner

We've used Scanner to read text from the user at the keyboard, but it's much more versatile. Scanners actually read and parse text from any source. Soon, we'll see how to use them to read from files. You can also use them to read text from network connections. In this assignment, you might find it useful to attach a Scanner to read an parse Strings. Here are some examples:

This program attaches a Scanner to a String, and reads it word-by-word:

import java.util.Scanner; public class AttachScannerToString { public static void main(String args[]) { String words = "There's always money in the banana stand"; Scanner in = new Scanner(words); int i=1; while (in.hasNext()) { String word = in.next(); System.out.println("word " + i + " is " + word); i++; } } } 

This does the same, separating by commas:

import java.util.Scanner; public class ScannerDifferentDelimiters { public static void main(String args[]) { String movies = "Toy Story,Bug's Life,Toy Story 2,Monsters Inc,Finding Nemo,Incredibles"; Scanner in = new Scanner(movies); in.useDelimiter(","); // separate based on commas int i=1; while (in.hasNext()) { String movie = in.next(); System.out.println("movie " + i + " is " + movie); i++; } } } 

and this separates based on whitespace or punctuation:

import java.util.Scanner; import java.util.regex.Pattern; public class ScannerWhitespacePunctuation { public static void main(String args[]) { String conversation = "ELLEN: Do you see the kids? " + "BRODY: Probably out in the back yard. " + "ELLEN: In Amity, you say 'Yahd.' " + "(she gives it the Boston sound) " + "BRODY: The kids are in the yahd, playing near the cah. How's that sound? " + "ELLEN: Like you're from N'Yawk. "; Scanner in = new Scanner(conversation); in.useDelimiter("[^\\p{Alpha}']+"); int i=1; while (in.hasNext()) { String word = in.next(); if (word.equals("'")) // ignore lone apostrophes continue; System.out.println("word " + i + " is " + word); i++; } } } 

You might consider not using Scanner at all, and writing code that parses long Strings into individual words by reading character by character, noting that when we read a letter that's preceded by a non-letter (a space, punctuation mark, etc.), we have the start of a new word. Similarly, when we're in the middle of a word and reach a non-letter, we've hit the end of the word.

You are not required to use any of these. Feel free to go about solving the problem any way you choose (provided, of course, that your solution is your own).

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

5. List the forces that shape a groups decisions

Answered: 1 week ago

Question

4. Identify how culture affects appropriate leadership behavior

Answered: 1 week ago