Java Homework- Random Insult Generator (outline of code inside)

In summary: This is a very poor example of code and does not follow Java conventions. I am sorry you have to deal with this.In summary, the conversation is about a person who is new to Java and needs help with their homework assignment to create a program that generates 10 random insults using a given outline and grammar rules. The program includes a function to select and expand different parts of a sentence, and the person helping suggests using multiple arrays to simplify the code. The code provided is not following Java conventions and may be a difficult task for a beginner.
  • #1
d33bo
2
0
Hey I'm new to this java stuff, I am taking my first class in it at the university of pittsburgh, and for my homework I need to write code to make a thing that will generate 10 random insults from a list of words. I have this outline to it and I was wondering if someone could help me fill in the blanks (i.e. I know i need to make a while/for loop that says something along the lines of while x < 10 blah blah x++). Here's the outline:
import java.util.Random ;


public class Generator {


////////////////////////////////////////////////////////////////////
//
// Generate 10 sentences.
//

public static void main(String[] args) {
for (int n = 0 ; n < 10 ; n++) {
System.out.println( generate("S") ) ;
}
}


////////////////////////////////////////////////////////////////////
//
// Completely expand category (usually S).
//

public static String generate(String category) {
... YOUR DEFINITION OF GENERATE() HERE ...
}


... YOUR OTHER FUNCTIONS HERE ...


////////////////////////////////////////////////////////////////////
//
// Select_Expansion(Category) will randomly expand Category
// according to the following grammar. Returns "error" if there is
// no expansion.
//

static String[] Grammar = {
"S -> NAME VP",
"VP -> V NP",
"NP -> ART NN",
"NN -> ADJ NN",
"NN -> N",
"ART -> a",
"V -> is",
"V -> looks like",
"V -> acts like",
"NAME -> dr aronis",
"NAME -> mr aronis",
"NAME -> john",
"ADJ -> nice",
"ADJ -> mean",
"ADJ -> smart",
"ADJ -> stupid",
"ADJ -> hip",
"ADJ -> geeky",
"ADJ -> normal",
"ADJ -> strange",
"ADJ -> great",
"ADJ -> terrible",
"N -> person",
"N -> teacher",
"N -> fool"
} ;

static Random generator = new Random() ;

public static String Select_Expansion(String Category) {
// Variables:
String This_Rule = "", Rule_LHS = "", Rule_RHS = "" ;
int Number_of_Expansions = 0, Expansion_to_Use = 0 ;
// Count expansions for Category:
for (int n = 0 ; n < Grammar.length ; n++) {
This_Rule = Grammar[n] ;
Rule_LHS = This_Rule.substring(0,This_Rule.indexOf( " -> ")) ;
if ( Rule_LHS.equals(Category) ) Number_of_Expansions++ ;
}
// Return error if no expansions:
if ( Number_of_Expansions == 0 ) return "error" ;
// Randomly select which expansion to use:
Expansion_to_Use = Math.abs(generator.nextInt() % Number_of_Expansions) ;
// Go through rules while counting down through expansions of Category,
// and break with selected expansion:
for (int n = 0 ; n < Grammar.length ; n++) {
This_Rule = Grammar[n] ;
Rule_LHS = This_Rule.substring(0,This_Rule.indexOf( " -> ")) ;
Rule_RHS = This_Rule.substring(This_Rule.indexOf(" -> ") + 4) ;
if ( Rule_LHS.equals(Category) && (Expansion_to_Use == 0) ) break ;
if ( Rule_LHS.equals(Category) ) Expansion_to_Use-- ;
}
return Rule_RHS ;
}


////////////////////////////////////////////////////////////////////

}

// End-of-File
 
Technology news on Phys.org
  • #2
thats interesting...its almost like BSP. I haven't done Java in a few years, but I think my

general knowledge of programming might help. I might need some help filling in the java

blanks though.

Code:
import java.util.Random ;public class Generator {////////////////////////////////////////////////////////////////////
//
// Generate 10 sentences.
//

public static void main(String[] args) {
for (int n = 0 ; n < 10 ; n++) {
System.out.println( generate("S") ) ; [COLOR="SeaGreen"] ok so here we print out whats

returned from generate.  we do this 10 times so generate will return a string with the insult

[/COLOR]
}
}////////////////////////////////////////////////////////////////////
//
// Completely expand category (usually S).
//

public static String generate(String category) {
[COLOR="SeaGreen"]ok having looked at the Select_Expansion function, it seems that all you have to do is call the function with the correct category.  This leads me to believe I've underestimated the function.  have you tried calling Select_Expansion() here with the category "S"?[/COLOR]
}... YOUR OTHER FUNCTIONS HERE ...////////////////////////////////////////////////////////////////////
//
// Select_Expansion(Category) will randomly expand Category
// according to the following grammar. Returns "error" if there is
// no expansion.
//

static String[] Grammar = {
"S -> NAME VP",
"VP -> V NP",
"NP -> ART NN",
"NN -> ADJ NN",
"NN -> N",
"ART -> a",
"V -> is",
"V -> looks like",
"V -> acts like",
"NAME -> dr aronis",
"NAME -> mr aronis",
"NAME -> john",
"ADJ -> nice",
"ADJ -> mean",
"ADJ -> smart",
"ADJ -> stupid",
"ADJ -> hip",
"ADJ -> geeky",
"ADJ -> normal",
"ADJ -> strange",
"ADJ -> great",
"ADJ -> terrible",
"N -> person",
"N -> teacher",
"N -> fool"
} ;

static Random generator = new Random() ;

public static String Select_Expansion(String Category) 
{
// Variables:
String This_Rule = "", Rule_LHS = "", Rule_RHS = "" ;

int Number_of_Expansions = 0, Expansion_to_Use = 0 ;
// Count expansions for Category:

for (int n = 0 ; n < Grammar.length ; n++) 
{
This_Rule = Grammar[n] ;
Rule_LHS = This_Rule.substring(0,This_Rule.indexOf( " -> ")) ;

if ( Rule_LHS.equals(Category) ) 
{
            Number_of_Expansions++ ;
}

}
// Return error if no expansions:
if ( Number_of_Expansions == 0 )
{
     return "error" ;
}

// Randomly select which expansion to use:
Expansion_to_Use = Math.abs(generator.nextInt()Number_of_Expansions);

// Go through rules while counting down through expansions of Category,
// and break with selected expansion:
for (int n = 0 ; n < Grammar.length ; n++) 
{
This_Rule = Grammar[n] ;
Rule_LHS = This_Rule.substring(0,This_Rule.indexOf( " -> ")) ;
Rule_RHS = This_Rule.substring(This_Rule.indexOf(" -> ") + 4) ;
if ( Rule_LHS.equals(Category) && (Expansion_to_Use == 0) ) break ;
if ( Rule_LHS.equals(Category) ) Expansion_to_Use-- ;
}

return Rule_RHS ;
}////////////////////////////////////////////////////////////////////

}

// End-of-File
 
  • #3
First, the code you've shown looks moderately smooth, so I'm going to guess it was given to you as part of the assignment (am I wrong?). What you need to fill in, in the generate function, is the following:

--select the next expansion using the function they gave you
--split up that expansion into some number of words, and do generate for each of those words

You'll need to do a bunch of other stuff too but this should get you started.
 
  • #4
A bit complex task for first lessons, grammar and stuff, anyway I do not know who teaches you Java but

Code:
////////////////////////////////////////////////////////////////////
//
// Select_Expansion(Category) will randomly expand Category
// according to the following grammar. Returns "error" if there is
// no expansion.
//

THIS IS SOMETHING YOU DO NOT DO IN JAVA

Use JavaDocs! And conventionaly you use _ for naming only in constants.

"S -> NAME VP",
"VP -> V NP",
"NP -> ART NN",
"NN -> ADJ NN",
"NN -> N",
"ART -> a",

That means you want a sentance in the form of

NAME + VERB + ARTICLE + ADJECTIVE + NOUN?

If possible maybe you can simplify a little and make 5 arrays of Strings
one array for names one for verb one for article... and then you just randomly select one entry from the each array when making the String. Use StringBuffer or StringBuilder when making a long String. You would have something in the form:

Code:
String generateRandomSentance(){
StringBuilder builder = new StringBuilder();
builder.append(getRandomName());
builder.append(getRandomVerb());
...
return builder.toString();
}

P.S.: Only a certified Java programmer should be allowed to teach Java.
 
Last edited:
  • #5
I thought it wasn't a bad way to write that program. It introduces him to context-free grammars, which is useful. Also, the sentences according to the grammar do not necessarily take the form you stated, since they may have more than one adjective. Certainly it could be made faster by using a bunch of arrays (or a HashMap<String, String[]>) instead of string parsing to extract the grammar, but speed isn't a concern.

By the way, I'm unsure what you think should be done in JavaDoc instead of Java. The String[] Grammar is part of the program.
 
Last edited:
  • #6
JavaDoc comment is a Java Style Comment

e.g.
Code:
/**
 * Adds the two numbers together and returns the result.
 * @param a first int to add
 * @param b second int to add
 * @return returns the value of a + b
 */
public int add(int a, int b){
return a + b;
}
the you can use the JavaDoc utility to make html style documents from the source code as I am sure you have came across this page

http://java.sun.com/j2se/1.5.0/docs/api/

all the pages are generated from the JavaDoc comments in the sources of the standard library. Therefore one is supose to write JavaDoc comments and not

Code:
////////////////////
// My own
// non Java style
// comment
//////////////////
// Methods returns a + b
///////////////////
public int add(int a, int b){
return a + b;
}

I would not introduce advanced things such as context-free grammars and complex data structures such as a HashMap in the beginning of the course. First you need to know how to crawl, then to walk and then finally to run, but ofcorse then you also need to learn how to take the bus, but for first lessons let's first crawl:)
 
  • #7
I know what javadoc is, having written a fair amount of it myself. I guess it would have been a bit better to use that for this, though it's such a small program it doesn't make too much difference. The only thing that annoyed me a little was the capitalized method and variable names.

Context free grammars are easy, just a bunch of replacement rules--I didn't know what they were until a discussion in Theory of Computation, and it took about 5 minutes to get the basic concept (it wasn't even really explained, just learning by example).

But maybe for a beginning programmer it is hard to learn them just from reading code. d33bo, do you understand what Select_Expansion does? Can you give an example of what might be its output if you called Select_Expansion("ADJ")? How about an example of what its output might be if you called Select_Expansion("NP")? Select_Expansion("S")?

A pretty good introduction to context-free grammars can be found at Wikipedia, by looking up context-free grammar. Alternatively, here's one with more pictures of parse trees, which make it intuitive:
http://www.coli.uni-saarland.de/~kris/nlp-with-prolog/html/node37.html
 
Last edited by a moderator:
  • #8
OK I'm pretty lost, I'm not going to lie, but to answer the question about what Select_Expansion would do for things. I understand that select_expansion for let's say S could return dr.aronis or john etc, and the VP part of it could return any V and N combo. I understand that, but what I don't understand is what you're telling me to do lol. Looking at the thing Bucky posted, is that code actually doing anything or did you just add comments? Am I adding these expansions etc into the place in my outline that says "YOUR FUNCTIONS HERE" or what? I'm truly lost.
 
  • #9
d33bo said:
OK I'm pretty lost, I'm not going to lie, but to answer the question about what Select_Expansion would do for things. I understand that select_expansion for let's say S could return dr.aronis or john etc, and the VP part of it could return any V and N combo. I understand that, but what I don't understand is what you're telling me to do lol.

No, Select_Expansion only goes one level down. For example:
Select_Expansion("S") returns "NAME VP"
Select_Expansion("NP") returns "ART NN"
Select_Expansion("ADJ") returns one word chosen from "nice", "mean", "smart", "stupid", "hip", "geeky", "normal", "strange", "great", or "terrible".
 
  • #10
I have a couple of questions:

1. For a beginning course in Java, couldn't you write a much simpler program? I understand if you want to make it more complex, I'm just wondering if all the grammar stuff is required. Personally, I'd create a.) a string[] array containing insulting nouns, b.) a string[] array of insulting adjectives, and c.) a method that takes a random element of each array and returns a String that's something like "you " + adjective + noun + "!".

2. Some of your code looks complicated, especially the Select_Expansion method. Have you tested this section of the code separately?
 

1. What is a random insult generator?

A random insult generator is a computer program that randomly generates and displays insults or offensive remarks.

2. How does a random insult generator work?

A random insult generator uses a combination of pre-written insults and randomly generated words or phrases to create a unique insult output each time it is run.

3. What is the purpose of a random insult generator?

The purpose of a random insult generator is usually for entertainment or humor. It can also be used as a tool to practice programming skills or to add some fun elements to a game or application.

4. Can a random insult generator be customized?

Yes, a random insult generator can be customized by adding or removing insults, changing the language or tone of the insults, and adjusting the frequency of certain words or phrases.

5. Is Java the only language that can be used to create a random insult generator?

No, a random insult generator can be created using any programming language. However, Java is a popular and versatile language that is well-suited for this type of project.

Similar threads

  • Programming and Computer Science
Replies
2
Views
6K
  • Programming and Computer Science
Replies
9
Views
2K
  • Programming and Computer Science
Replies
4
Views
1K
  • Programming and Computer Science
Replies
5
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
  • Programming and Computer Science
Replies
14
Views
5K
  • Programming and Computer Science
Replies
1
Views
2K
  • Programming and Computer Science
Replies
22
Views
5K
  • Programming and Computer Science
Replies
4
Views
1K
  • Programming and Computer Science
Replies
2
Views
1K
Back
Top