'Java Random Sentence Generator Class not working in Runner
I am trying to make a program in VScode, utilizing Maven Java, that generates sentences randomly through randomized words for each part of speech, randomized punctuation, randomized sentence structure, etc. I have a parent class called SentenceGen which contains all of the arrays and some methods to pull random words/punctuation (e.g. getNoun, getVerb). For your convenience I've removed the words in the larger arrays:
import java.lang.Math;
public class SentenceGen{
//an array of a lot of nouns found at https://gist.github.com/ijmacdowell/8325491
protected static String n[] = {""};
//an array of verbs found at https://gist.github.com/ijmacdowell/8325491
protected static String v[] = {""};
//an array of adverbs found at https://grammaran.com/wp-content/uploads/2021/09/pp-parts-of-speech.pdf
protected static String adv[] = {""};
//an array containing each of the three existing articles
protected static String art[] = {"the", "a", "an"};
//an array containing common pronouns
protected static String pro[] = {"they", "them", "it", "he", "him", "she", "her", "their", "we", "I", "his"};
//an array containing common prepositions
protected static String prep[] = {""};
//an array containing coordinating conjuctions
protected static String coordConj[] = {"and", "or", "but", "for", "nor", "yet", "so"};
//an array containing subordinating conjunctions for even more complexity
protected static String subConj[] = {""};
//default constructor
public SentenceGen(String n[], String v[], String adj[], String adv[], String art[], String pro[], String prep[], String coordConj[], String subConj[]){
SentenceGen.n = n;
SentenceGen.v = v;
SentenceGen.adj = adj;
SentenceGen.adv = adv;
SentenceGen.art = art;
SentenceGen.pro = pro;
SentenceGen.prep = prep;
SentenceGen.coordConj = coordConj;
SentenceGen.subConj = subConj;
}
//gets a random noun from the noun array
public String getNoun(){
int a = (int)(Math.random()*(SentenceGen.n.length-1));
return SentenceGen.n[a];
}
//gets a random verb from the verb array
public String getVerb(){
int a = (int)(Math.random()*(SentenceGen.v.length-1));
return SentenceGen.v[a];
}
//gets a random adjective from the adjective array
public String getAdj(){
int a = (int)(Math.random()*(SentenceGen.adj.length-1));
return SentenceGen.adj[a];
}
//gets a random adverb from the adverb array
public String getAdv(){
int a = (int)(Math.random()*(SentenceGen.adv.length-1));
return SentenceGen.adv[a];
}
//gets a random article from the article array
public String getArt(){
int a = (int)(Math.random()*(SentenceGen.art.length-1));
return SentenceGen.art[a];
}
//gets a random pronouns from the pronouns array
public String getPronouns(){
int a = (int)(Math.random()*(SentenceGen.pro.length-1));
return SentenceGen.pro[a];
}
//gets a random prepositions from the prepositions array
public String getPrep(){
int a = (int)(Math.random()*(SentenceGen.prep.length-1));
return SentenceGen.prep[a];
}
//gets a random coordinating conjuctions from the coordinating conjuctions array
public String getCoordConj(){
int a = (int)(Math.random()*(SentenceGen.coordConj.length-1));
return SentenceGen.coordConj[a];
}
//gets a random subordinating conjunctions from the subordinating conjunctions array
public String getSubConj(){
int a = (int)(Math.random()*(SentenceGen.subConj.length-1));
return SentenceGen.subConj[a];
}
//randomizes what tense the verb is in (and does it in a grammatically correct way)
public String getTense(){
int a = (int)(Math.random()*2);
if(a == 0){
return getVerb();
} else {
String x = getVerb();
if(x.substring(x.length()-2, x.length()-1).equals("e")){
return (x + "d");
} else {
return (x + "ed");
}
}
}
//punctutation generation
public String getPunct(){
int x = (int)(Math.random()*2);
if(x == 0){
return "";
} else {
int a = (int)(Math.random()*3);
if(a == 0){
return ".";
} else if(a == 1){
return "?";
} else if(a == 2){
return "!";
} else {
return " <p>Something went wrong, please try again</p> ";
}
}
}
}
I have a child class of SentenceGen called simpleSentence (to generate several simple sentences). I know there are probably several problems with my approach to the program in general but the goal is to make an ArrayList of simple sentence structures and pull one randomly:
import java.util.ArrayList;
import java.math.*;
public class simpleSentence extends SentenceGen{
//creates an arraylist containing all simple sentence structures
private ArrayList<String> sSentence = new ArrayList<String>();
sSentence.add(getArt + " " + getNoun + " " + getTense + " " + getArt + " " + getNoun + getPunct);
sSentence.add(getPronouns + " " + getTense + getPunct);
//constructor
public simpleSentence(ArrayList<String> sSentence){
super(n, v, adj, adv, art, pro, prep, coordConj, subConj);
this.sSentence = sSentence;
}
//pulls a random sentence structure
public String toString(){
int a = (int)(Math.random()*(sSentence.size()-1));
return sSentence.get(a);
}
}
The problem is that I can't call the simpleSentence class in my runner class and I don't know how to approach this. Should I make the simpleSentence a package and import it into the runner? Another note is that they are all src files and because this is my first time using VScode I don't know if that effects the outcome or anything like that. Thanks in advance!
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
