- #1
royboyz12
- 6
- 0
Hi, I have to do a homework assignment for my intro to computer porgramming class and i have to write a program that creates insult sentences. My professor gave us some code to start with and we had to fill in the functions(professor gave us the names of these functions and the name of the program loop). I need help getting the function to work. I included the code i was given and below that i put the functions and what i did so far.
This is the code I was given:
I have to fill in the functions(function names were given) and this is what I did so far(i know i probably did some stuff wrong plus i need to add some more code...but I am just not sure what):
This is the code I was given:
Code:
import java.util.Random ;
public class Generator {
public static void main(String[] args) {
for (int n = 0 ; n < 10 ; n++) {
System.out.println( generateSentence("S") ) ;
}
}
... PUT YOUR OTHER FUNCTIONS HERE ...
/// The method selectExpansion(Category) will randomly expand
/// Category according to the following grammar. Returns "error" if
/// there is no expansion.
static Random RANDOM = new Random() ;
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 aronis",
"ADJ -> stupid",
"ADJ -> geeky",
"ADJ -> ugly",
"N -> idiot",
"N -> teacher",
"N -> fool"
} ;
public static String selectExpansion(String category) {
// Variables:
String thisRule = "", ruleLHS = "", ruleRHS = "" ;
int numberOfExpansions = 0, expansionToUse = 0 ;
// Count expansions for category:
for (int n = 0 ; n < GRAMMAR.length ; n++) {
thisRule = GRAMMAR[n] ;
ruleLHS = thisRule.substring(0,thisRule.indexOf(" -> ")) ;
if ( ruleLHS.equals(category) ) numberOfExpansions++ ;
}
// Return error if no expansions:
if ( numberOfExpansions == 0 ) return "error" ;
// Randomly select which expansion to use:
expansionToUse = Math.abs(RANDOM.nextInt() % numberOfExpansions) ;
// Go through rules while counting down through expansions of category,
// and break with selected expansion:
for (int n = 0 ; n < GRAMMAR.length ; n++) {
thisRule = GRAMMAR[n] ;
ruleLHS = thisRule.substring(0,thisRule.indexOf(" -> ")) ;
ruleRHS = thisRule.substring(thisRule.indexOf(" -> ") + 4) ;
if ( ruleLHS.equals(category) && (expansionToUse == 0) ) break ;
if ( ruleLHS.equals(category) ) expansionToUse-- ;
}
return ruleRHS ;
}
}
// End-of-File
I have to fill in the functions(function names were given) and this is what I did so far(i know i probably did some stuff wrong plus i need to add some more code...but I am just not sure what):
Code:
public static void generateSentence(String insult){
string insult;
string left;
string right;
while(hasCategory(insult)){
Category = firstCategory(insult);
left = beforeFirstCategory(insult);
right = afterFirstCategory(insult);
insult = left + selectExpansion(Category) + right;
}
}
public static boolean hasCategory(String s){
for(int i=0; i<=s.length(); i++){
if(charAt(i)>='A' && charAt(i)<='Z'){
return true;
}
}
return false;
}
public static int beginningOfCategory(String s){
for(int i=0; i<s.length(); i++){
if(charAt(i)>='A' && charAt(i)<='Z'){
return i;
}
}
}
public static int endOfCategory(String s){
for(int i=0; i<s.length(); i++){
if(!(charAt(i)>'A' && charAt(i)<='A')){
return i;
}
}
}
public static String beforeFirstCategory (String s){
beginningOfCategory(String s);
return s.substring(0,beginningOfCategory(String s));
}
public static String firstCategory(String s){
beginningOfCategory(String s);
endOfcategory(String s);
return s.substring(beginningOfCategory(String s),endOfCategory(String s));
}
public static String afterFirstCategory(String s){
endCategory(String s);
s.length();
return s.substring(endOfCategory(String s),s.length());
}