I want to build an app for my pupil which generates tasks out of sentence blocks. For example I created two classes, apple and pear, with nearly the same structure. They return a question built out of the sentence blocks which are defined in the classes. In both classes is a GenerateQuestion()-function for that. Now I want to build some kind of overclass, which picks a random class of i.e. apple or pear and then returns the strings from the functions. The functions names are the same, but I can't figure out how to get data from a random choosen class. Hoping for help. Thanks in advance.
Update: Here is the code I wrote so far (I tried to translate it properly):
import 'dart:math';
int randomminmax1 = 0;
int randomminmax2 = 0;
int randomminmax3 = 0;
List classes = [apple, pear];
class overClass {
static pickClass(){
int randomClassItem = Random().nextInt(classes.length);
print(classes[randomClassItem]);
return classes[randomClassItem];
}
}
class apple {
static String giveQuestion() {
randomminmax1 = 2 Random().nextInt(15 - 2);
randomminmax2 = randomminmax1 * (2 Random().nextInt(12 - 2));
randomminmax3 = 2 Random().nextInt(30 - 2);
List value_1 = [" boxes", " bags", " bucket"];
List verbs = ["cost","have a price of", "are offered for"];
List value_2 = ["Euro"];
List questionWords = [""];
int randomIndexValue1 = Random().nextInt(value_1.length);
int randomIndexVerbs = Random().nextInt(verbs.length);
int randomIndexValue2 = Random().nextInt(value_2.length);
String value = randomminmax1.toString() value_1[randomIndexValue1].toString() " apples" verbs[randomIndexVerbs].toString() " " randomminmax2.toString() " " value_2[randomIndexValue2].toString() ".\n";
String question = "How much are " randomminmax3.toString() value_1[randomIndexValue1].toString() "?\n";
return value question;
}
static String giveAnswer(){
double result = (randomminmax2/randomminmax1)*randomminmax3;
return result.toStringAsFixed(2) " Euro.";
}
}
class pear {
static String giveQuestion() {
randomminmax1 = 2 Random().nextInt(15 - 2);
randomminmax2 = randomminmax1 * (2 Random().nextInt(12 - 2));
randomminmax3 = 2 Random().nextInt(30 - 2);
List value_1 = [" boxes", " bags", " bucket"];
List verbs = ["cost","have a price of", "are offered for"];
List value_2 = ["Euro"];
List questionWords = [""];
int randomIndexValue1 = Random().nextInt(value_1.length);
int randomIndexVerbs = Random().nextInt(verbs.length);
int randomIndexValue2 = Random().nextInt(value_2.length);
String value = randomminmax1.toString() value_1[randomIndexValue1].toString() " pears" verbs[randomIndexVerbs].toString() " " randomminmax2.toString() " " value_2[randomIndexValue2].toString() ".\n";
String question = "How much are " randomminmax3.toString() value_1[randomIndexValue1].toString() "?\n";
return value question;
}
static String giveAnswer(){
double result = (randomminmax2/randomminmax1)*randomminmax3;
return result.toStringAsFixed(2) " Euro.";
}
}
static String giveAnswer(){
double result = (randomminmax2/randomminmax1)*randomminmax3;
return result.toStringAsFixed(2) " Euro.";
}
}
CodePudding user response:
Can you try to create a superclass that all question classes will inherit and then get the subclasses and programmatically call functions. Try to see here
CodePudding user response:
At least I managed to give my values directly from the classes into the list. But then I got problems with the int-variables, which were built wrong.
My "solution": I converted my classes into isolated flutter widgets, which works fine, except the problem that I'm not able to put the widgets into a list, from where they are picked randomly...but therefore I'll write a new post.
Thank you Moustapha for your help (the superclass idea sounds good, but way too hard to code for me) ;)!