Created
October 6, 2016 15:53
-
-
Save Noble-Mushtak/31b8e4925784dee2d5116fea6c51903d to your computer and use it in GitHub Desktop.
APCS Unit 3 Design a Class Project
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* A class that models mathematical expressions. | |
* | |
* @author Noble H. Mushtak | |
* @version 1.0 (6 Oct 2016) | |
*/ | |
public class Expression | |
{ | |
/** | |
* These are the five operations allowed in our expressions. | |
* | |
* @since 1.0 (6 Oct 2016) | |
*/ | |
public static enum Operation { PLUS, MULTIPLY, POWER }; | |
/** | |
* These are the three types of expression. | |
* | |
* @since 1.0 (6 Oct 2016) | |
*/ | |
public static enum Type { NUMBER, VARIABLE, OPERATION }; | |
/** | |
* This is the type of this expression. | |
* | |
* @since 1.0 (6 Oct 2016) | |
*/ | |
public Type type; | |
/** | |
* This is null if type != NUMBER. | |
* This is the integer this Expression represents otherwise. | |
* | |
* @since 1.0 (6 Oct 2016) | |
*/ | |
public Double value; | |
/** | |
* This is null if type != VARIABLE. | |
* This is the name of the variable this Expression represents otherwise. | |
* | |
* @since 1.0 (6 Oct 2016) | |
*/ | |
public String name; | |
/** | |
* This is null if type != OPERATION. | |
* This is the Operation used in this expression otherwise. | |
* | |
* @since 1.0 (6 Oct 2016) | |
*/ | |
public Operation op; | |
/** | |
* This is null if type != OPERATION. | |
* This is the list of operands that are being operated on | |
* by the above op. | |
* | |
* If op is PLUS or MULTIPLY, then operands.length >= 2. | |
* Otherwise, operands.length == 2. | |
* | |
* @since 1.0 (6 Oct 2016) | |
*/ | |
public Expression[] operands; | |
/** | |
* This constructor takes an double and | |
* returns an Expression representing a number. | |
* | |
* @since 1.0 (6 Oct 2016) | |
*/ | |
public Expression(Double realValue) | |
{ | |
type = Expression.Type.NUMBER; | |
value = realValue; | |
} | |
/** | |
* This constructor takes a String and | |
* return an Expression representing a variable. | |
* | |
* Also, this takes out all of the non-letters out of the variable name. | |
* | |
* @since 1.0 (6 Oct 2016) | |
*/ | |
public Expression(String variableName) { | |
//This is the variable name after we take out all of the non-letters. | |
String realVariableName = ""; | |
type = Expression.Type.VARIABLE; | |
name = variableName; | |
} | |
/** | |
* This tells us if this is a valid expression | |
* where the operations satisfy the properties told above. | |
* | |
* @returns boolean -- Is this expression valid? | |
* @since 1.0 (6 Oct 2016) | |
*/ | |
public boolean validExpression() | |
{ | |
//This is what we're going to return. | |
boolean isValid = true; | |
//Based on the type: | |
switch (type) { | |
//If number, everything null except value. | |
case NUMBER: | |
isValid &= (name == null); | |
isValid &= (op == null); | |
isValid &= (operands == null); | |
break; | |
//If variable, everything null except variableName. | |
case VARIABLE: | |
isValid &= (value == null); | |
isValid &= (op == null); | |
isValid &= (operands == null); | |
break; | |
//If operation, everything null except op and operands. | |
case OPERATION: | |
isValid &= (value == null); | |
isValid &= (name == null); | |
break; | |
} | |
return isValid; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment