Skip to content

Instantly share code, notes, and snippets.

@tliggett
Created December 13, 2016 20:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tliggett/f113af492b88a258acbbaa054b8cd70a to your computer and use it in GitHub Desktop.
Save tliggett/f113af492b88a258acbbaa054b8cd70a to your computer and use it in GitHub Desktop.
// Creates an interface that can store both doubles and Strings in the same ArrayList
interface SuperVariable {
String getString();
// returns String if String is stored
boolean isString();
// denotes whether SuperVariable is a SuperString or a SuperDouble
double getDouble();
// returns double if double is stored
String getName();
// requires each variable to have a name (this is for tracking purposes and sorting within multiple ArrayLists)
}
---------------------------------------------
class SuperString implements SuperVariable {
String dataText;
double dataNum;
String name;
// Creates a SuperString with a name and a stored String
SuperString(String n, String data) {
dataText = data;
this.name = n;
}
// because a String is stored, it returns the string data
String getString() {
return dataText;
}
// because it is a String, returns true
boolean isString() {
return true;
}
// returns null because dataNum is never instantiated
double getDouble() {
return dataNum;
}
//returns the name of the String Data
String getName() {
return name;
}
}
---------------------------------------------
// utilized for double variables.
class SuperDouble implements SuperVariable {
String dataText;
double dataNum;
String name;
//Creates a SuperDouble with name n and stored
SuperDouble(String n, double d) {
dataNum = d;
name = n;
}
// returns null because String is never instantiated
String getString() {
return dataText;
}
//returns false because Double is stored
boolean isString() {
return false;
}
//returns stored double
double getDouble() {
return dataNum;
}
//returns name of variable
String getName() {
return name;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment