4 Replies - 26447 Views - Last Post: 10 October 2006 - 11:30 AM Rate Topic: -----

#1 yaeli_17  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 09-October 06

Help writing a Pig Latin translator

Posted 09 October 2006 - 09:47 AM

Hi.
I am really stuck on this assignment question and the truth is i don't even know how to approach this, meaning which classes to import and what methods to invoke etc... i'm completely new in this Java thing and this is only our second assignment so far :huh:

any help on how to approach this would be very much apreciated.

the question:

Pig Latin is a language game primarily used in English which consists of appending syllables to words in order to alter them. There are multiple Pig Latin dialects but for the purposes of this question, the rules are as follows:

* For words that begin with a consonant, move this consonant to the end of the word (separated from the end of the word with "-"), and add "ay". For example, "teacher" would be translated to "eacher-tay".
* For words that begin with a vowel, simply append "-way" to the end of the word. For example, "apple" would be translated to "apple-way".
* Words that start with a non-letter character are not translated.

Write a program that consists of one Java class called PigLatinTranslator, which defines a main() method that asks the user to enter an English sentence. Your program method will then translate this sentence into Pig Latin and display the result.
Additional rules:

* You MUST only move the first consonant in a word to the end of that word; if a word starts with multiple consecutive consonants, you MUST only move the first of these consonants to the end of the word, NOT all of them. For example, "school" would be translated to "chool-say", NOT "ool-schay".
* The letter "h", when occuring at the beginning of a word, MUST always be considered as a consonant, even if it is silent. For example, "honest" would be translated to "onest-hay", NOT "honest-way".
* The letter "y", when occuring at the beginning of a word, MUST always be considered as a consonant. For example, "yellow" would be translated to "ellow-yay", NOT "yellow-way".
* The case of the letters in a word MUST NOT change during translation. That is, an upper-case consonant occuring at the beginning of a word MUST NOT become a lower-case consonant when it is moved to the end of a word. Likewise, a lower-case second letter MUST NOT become an upper-case letter when an upper-case consonant which occurs immediately before it is moved to the end of a word. For example, "September" would translate to "eptember-Say", NOT "Eptember-say".

Notes:

* As specified in the general instructions and regulations for assignment, your program MUST read all the input entered by the user before starting to compute any of the values required by the specification. The easiest way to achieve this for this question is to call the nextLine() method defined in the Scanner class, which reads an entire line of input from the keyboard instead of just one word.
* Your program method MUST be able to properly handle spaces occuring at the beginning or the end of a sentence, as well as multiple spaces occurring between any two consecutive words.
* When constructing the Pig Latin sentence, there MUST be EXACTLY one space between each Pig Latin word. Within a Pig Latin word, there MUST NOT be any spaces between the dash and a group of letters; for example, "teacher" would be translated as "eacher-tay", NOT "eacher - tay".
* The Pig Latin sentence your program constructs MUST start with a Pig Latin word and end with a Pig Latin word; in other words, the Pig Latin sentence MUST NOT start with a space nor end with a space.
* Your program MUST NOT make use of any class defined in the Java Platform API other than Scanner (which you MUST NOT use for anything other than read the input from the keyboard), System (which you MUST NOT use for anything other than display the output), String, StringTokenizer, and Character.
* Your program MUST NOT modify the input variable once it has been assigned. However, you MAY copy the input value and change the copy as many times as you need.

Is This A Good Question/Topic? 0
  • +

Replies To: Help writing a Pig Latin translator

#2 psykoprogrammer  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 2
  • View blog
  • Posts: 72
  • Joined: 09-October 06

Re: Help writing a Pig Latin translator

Posted 09 October 2006 - 04:59 PM

Well, from a high level point of view you need to loop through your input stream, converting each word found according to the rules. The key here is determining what a word is. In this case a word is any set of characters, followed by a delimiter. Delimiters would be a space, period, colon, semicolon. For example: test.. "test" is the word, and period is the delimiter. You would convert the word test, put it into the output stream. Delimiters are simply thrown into the output stream without any conversion.

I'm fairly new with Java so I cannot offer too much in the way of what classes may already be present to aide you with this. I hope this little hi level overview can at least point you in the right direction once you hit Google and such.

Cheers :)
Was This Post Helpful? 0
  • +
  • -

#3 William_Wilson  Icon User is offline

  • lost in compilation
  • member icon

Reputation: 204
  • View blog
  • Posts: 4,807
  • Joined: 23-December 05

Re: Help writing a Pig Latin translator

Posted 09 October 2006 - 05:43 PM

there are many things which could be useful, you could use char array, though easier, it is a resource hog and an issue with re-arranging, your best bet would be to loop through using the substring command, looking for spaces first, if you can split the list like this, i think the rest you can handle :)
Was This Post Helpful? 0
  • +
  • -

#4 yaeli_17  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 09-October 06

Re: Help writing a Pig Latin translator

Posted 10 October 2006 - 07:54 AM

View PostWilliam_Wilson, on 9 Oct, 2006 - 05:43 PM, said:

there are many things which could be useful, you could use char array, though easier, it is a resource hog and an issue with re-arranging, your best bet would be to loop through using the substring command, looking for spaces first, if you can split the list like this, i think the rest you can handle :)


i think my problem is that i simply don't have a plan :huh:

i'm all mixed up. what do i do first? do i extract the words using stringtokenizer ...
and then what?
once they're extracted - what do i do? or is this not how i start?

i'm really confused.
Was This Post Helpful? 0
  • +
  • -

#5 psykoprogrammer  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 2
  • View blog
  • Posts: 72
  • Joined: 09-October 06

Re: Help writing a Pig Latin translator

Posted 10 October 2006 - 11:30 AM

Ok, here is a quick and dirty app. Please note that if this is for your education, I highly recommend picking at it, changing it, and so on so you actually learn from it. Please also note that this may not be 100% complete, or even meet all the requirements. This is merely a DEMONSTATION of how one can approach this.

import java.lang.String;
import java.util.*;

class PigLatin
{
	public static void main(String[] args) 
	{
		String inStream = "I love {tokens}. They are so neat!";

		System.out.println("Original entry: " + inStream);
		System.out.println("Pig Latin: " + transformStream(inStream));
	}
	
	public static String transformStream(String input)
	{
		StringBuffer result = new StringBuffer();
		String delimiters = " ,.:;()[]{}\"'!@#$%^&*";
		StringTokenizer lexer;
		String token;

		lexer = new StringTokenizer(input, delimiters, true);
		
		while (lexer.hasMoreTokens())
		{
			token = lexer.nextToken();
			if (delimiters.indexOf(token) > -1)
				result.append(token);
			else
				result.append(transformToken(token));
		}
		
		return result.toString();
	}
	
	public static String transformToken(String original)
	{
		StringBuffer result = new StringBuffer();
		String vowels = "aeiou";
		
		String vowelRule = "-way";
		String consonantRule = "-ay";
		
		if (vowels.indexOf(original.substring(0, 1).toLowerCase()) > -1)
		{
			result.append(original);
			result.append(vowelRule);
		}
		else
		{
			result.append(original.substring(1));
			result.append(original.substring(0, 1));
			result.append(consonantRule);
		}
		
		return result.toString();
	}
}




Cheers!

This post has been edited by psykoprogrammer: 10 October 2006 - 11:33 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1