Interpreter Design pattern

This is what I came up with so far it has issues and wrongly used
111 Replies
JavaBot
JavaBotā€¢10mo ago
āŒ› This post has been reserved for your question.
Hey @Rag...JN šŸŒŒ šŸ¦” šŸ‘½ šŸ’°! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
Rag...JN šŸŒŒ šŸ¦” šŸ‘½ šŸ’° šŸŠ
public interface Expression {
public boolean interpret(String context);
}

class TerminalExpression implements Expression {

private List<String> expectedData;

public TerminalExpression(List<String> expectedData) {
this.expectedData = expectedData;
}

@Override
public boolean interpret(String context) {
// instead of returning the boolean you should do an actual task
return expectedData.contains(context);
}
}

class Test{
public static void main(String[] args) {
List<String> commandList = new LinkedList<>();
commandList.add("Extra Cheese");
commandList.add("Extra Spice");
commandList.add("Extra Salt");
commandList.add("Lower Salt");
commandList.add("Medium Sauce");
commandList.add("Less Oil");

Expression expression = new TerminalExpression(commandList);

System.out.println("Customer ordered Extra Cheese "+ expression.interpret("Extra Cheese"));
}
}
public interface Expression {
public boolean interpret(String context);
}

class TerminalExpression implements Expression {

private List<String> expectedData;

public TerminalExpression(List<String> expectedData) {
this.expectedData = expectedData;
}

@Override
public boolean interpret(String context) {
// instead of returning the boolean you should do an actual task
return expectedData.contains(context);
}
}

class Test{
public static void main(String[] args) {
List<String> commandList = new LinkedList<>();
commandList.add("Extra Cheese");
commandList.add("Extra Spice");
commandList.add("Extra Salt");
commandList.add("Lower Salt");
commandList.add("Medium Sauce");
commandList.add("Less Oil");

Expression expression = new TerminalExpression(commandList);

System.out.println("Customer ordered Extra Cheese "+ expression.interpret("Extra Cheese"));
}
}
:hidethepain: what would be the Non-terminal class would look like and its functionality here
Unknown User
Unknown Userā€¢10mo ago
Message Not Public
Sign In & Join Server To View
Rag...JN šŸŒŒ šŸ¦” šŸ‘½ šŸ’° šŸŠ
I have did research on interpreter pattern now I need some help with implementing it, like I would appreciate if you can give me some directions like we(Daniest and tjoener) had in the general chat So the purpose of that question is to add the extra details to the food order right? So when you click Extra Spice, and Extra Salt these two data will be attached to the Ordered Food Item ChatGPT generated this code give a look
// Step 1: Define the Expression interface
interface OrderExpression {
void interpret(Order order);
}

// Step 2: Create terminal expressions for specific customizations
class ExtraCheeseExpression implements OrderExpression {
@Override
public void interpret(Order order) {
order.addCustomization("Extra Cheese");
}
}

class ExtraSpiceExpression implements OrderExpression {
@Override
public void interpret(Order order) {
order.addCustomization("Extra Spice");
}
}

// Step 3: Create a non-terminal expression for parsing commands
class CustomizationInterpreter {
private OrderExpression expression;

public CustomizationInterpreter(OrderExpression expression) {
this.expression = expression;
}

public void interpret(Order order) {
expression.interpret(order);
}
}

// Step 4: Context class representing the order
class Order {
private String foodItem;
private StringBuilder customizations;

public Order(String foodItem) {
this.foodItem = foodItem;
this.customizations = new StringBuilder();
}

public void addCustomization(String customization) {
customizations.append(customization).append(" ");
}

public void printOrder() {
System.out.println("Food Item: " + foodItem);
System.out.println("Customizations: " + customizations.toString().trim());
}
}
// Step 1: Define the Expression interface
interface OrderExpression {
void interpret(Order order);
}

// Step 2: Create terminal expressions for specific customizations
class ExtraCheeseExpression implements OrderExpression {
@Override
public void interpret(Order order) {
order.addCustomization("Extra Cheese");
}
}

class ExtraSpiceExpression implements OrderExpression {
@Override
public void interpret(Order order) {
order.addCustomization("Extra Spice");
}
}

// Step 3: Create a non-terminal expression for parsing commands
class CustomizationInterpreter {
private OrderExpression expression;

public CustomizationInterpreter(OrderExpression expression) {
this.expression = expression;
}

public void interpret(Order order) {
expression.interpret(order);
}
}

// Step 4: Context class representing the order
class Order {
private String foodItem;
private StringBuilder customizations;

public Order(String foodItem) {
this.foodItem = foodItem;
this.customizations = new StringBuilder();
}

public void addCustomization(String customization) {
customizations.append(customization).append(" ");
}

public void printOrder() {
System.out.println("Food Item: " + foodItem);
System.out.println("Customizations: " + customizations.toString().trim());
}
}
and the Client Usage
// Step 5: Client code that uses the interpreter for customizations
public class Client {
public static void main(String[] args) {
// Creating an order for a burger
Order burgerOrder = new Order("Burger");

// Creating customizations using interpreter
CustomizationInterpreter extraCheeseInterpreter = new CustomizationInterpreter(new ExtraCheeseExpression());
extraCheeseInterpreter.interpret(burgerOrder);

CustomizationInterpreter extraSpiceInterpreter = new CustomizationInterpreter(new ExtraSpiceExpression());
extraSpiceInterpreter.interpret(burgerOrder);

// Printing the final order
burgerOrder.printOrder();
}
}
// Step 5: Client code that uses the interpreter for customizations
public class Client {
public static void main(String[] args) {
// Creating an order for a burger
Order burgerOrder = new Order("Burger");

// Creating customizations using interpreter
CustomizationInterpreter extraCheeseInterpreter = new CustomizationInterpreter(new ExtraCheeseExpression());
extraCheeseInterpreter.interpret(burgerOrder);

CustomizationInterpreter extraSpiceInterpreter = new CustomizationInterpreter(new ExtraSpiceExpression());
extraSpiceInterpreter.interpret(burgerOrder);

// Printing the final order
burgerOrder.printOrder();
}
}
Unknown User
Unknown Userā€¢10mo ago
Message Not Public
Sign In & Join Server To View
Rag...JN šŸŒŒ šŸ¦” šŸ‘½ šŸ’° šŸŠ
oh wait, my general discussion were about intepreter patterns I was struggling to understand the concepts and in one example I didn't understand what postfix is Those were related to interpreter pattern examples This is the main question, I have to use Interpreter pattern So I started to study about interpreter pattern now it's time to implement interpreter pattern to that question
Unknown User
Unknown Userā€¢10mo ago
Message Not Public
Sign In & Join Server To View
Unknown User
Unknown Userā€¢10mo ago
Message Not Public
Sign In & Join Server To View
Rag...JN šŸŒŒ šŸ¦” šŸ‘½ šŸ’° šŸŠ
do you want to see the full question so you will get the context?
Unknown User
Unknown Userā€¢10mo ago
Message Not Public
Sign In & Join Server To View
Rag...JN šŸŒŒ šŸ¦” šŸ‘½ šŸ’° šŸŠ
so I guess the Customer will say "Extra Cheese" "Extra Spice" "Low Oil" "Low Spice"
Rag...JN šŸŒŒ šŸ¦” šŸ‘½ šŸ’° šŸŠ
These are the commands that needed to be interpreted if I understood it correctly This is just part 1, second part is I have to actually build the Food Ordering system app in Java Swing with these patterns applied
Unknown User
Unknown Userā€¢10mo ago
Message Not Public
Sign In & Join Server To View
Rag...JN šŸŒŒ šŸ¦” šŸ‘½ šŸ’° šŸŠ
so in Java We will have a Expression, TerminalExpression, NonTerminalExpression, and Context right that's the structure right Where does the rules/guidelines/etc comes into
Rag...JN šŸŒŒ šŸ¦” šŸ‘½ šŸ’° šŸŠ
can you explain with this diagram so I can easily grasp it
No description
Rag...JN šŸŒŒ šŸ¦” šŸ‘½ šŸ’° šŸŠ
sorry if I am being rude
Unknown User
Unknown Userā€¢10mo ago
Message Not Public
Sign In & Join Server To View
Unknown User
Unknown Userā€¢10mo ago
Message Not Public
Sign In & Join Server To View
Unknown User
Unknown Userā€¢10mo ago
Message Not Public
Sign In & Join Server To View
Rag...JN šŸŒŒ šŸ¦” šŸ‘½ šŸ’° šŸŠ
damn so extra, low, high, medium is like adjectives right and Cheese, Fat, Sugar, Oil, Salt are nouns so this my syntax correct? adjective and noun
Unknown User
Unknown Userā€¢10mo ago
Message Not Public
Sign In & Join Server To View
Rag...JN šŸŒŒ šŸ¦” šŸ‘½ šŸ’° šŸŠ
what I am expecting is much simpler expectations we can just stick to adjectives and nouns for now? Because I am trying to think how the user will give commands like extra cheese to an selected pizza or extra salt to a selected burger in the Swing GUI So if the user clicks "Extra Cheese" button
Unknown User
Unknown Userā€¢10mo ago
Message Not Public
Sign In & Join Server To View
Rag...JN šŸŒŒ šŸ¦” šŸ‘½ šŸ’° šŸŠ
hmm this is my friend's code how he implemented the interpreter pattern to his application
package patterns;

import ingredient.BBQChicken;
import ingredient.MayonnaiseCheese;
import java.util.ArrayList;
import java.util.HashMap;

interface Expression {

public String interpret();
}

class TerminalExpression implements Expression {

ArrayList<String> commandlist = new ArrayList();
int count;
String command;

TerminalExpression(String[] split) {

for (int i = 0; i < split.length; i++) {
commandlist.add(split[i]);
if (commandlist.get(i).contains("extra")) {
count++;
}
}
}

@Override
public String interpret() {
for (String value : commandlist) {

if (value.contains("cheese")) {
command = count + " " + new MayonnaiseCheese().getName();
}

if (value.contains("chicken")) {
command = count + " " + new BBQChicken().getName();
}

}
return command;
}

}
//+

public class CommandInterpretor {

public static String expression = "extra extra cheese";

public static void main(String[] args) {

String[] split = expression.split(" ");

TerminalExpression terminalCommand = new TerminalExpression(split);
String interpret = terminalCommand.interpret();
System.out.println(interpret);

}
}
package patterns;

import ingredient.BBQChicken;
import ingredient.MayonnaiseCheese;
import java.util.ArrayList;
import java.util.HashMap;

interface Expression {

public String interpret();
}

class TerminalExpression implements Expression {

ArrayList<String> commandlist = new ArrayList();
int count;
String command;

TerminalExpression(String[] split) {

for (int i = 0; i < split.length; i++) {
commandlist.add(split[i]);
if (commandlist.get(i).contains("extra")) {
count++;
}
}
}

@Override
public String interpret() {
for (String value : commandlist) {

if (value.contains("cheese")) {
command = count + " " + new MayonnaiseCheese().getName();
}

if (value.contains("chicken")) {
command = count + " " + new BBQChicken().getName();
}

}
return command;
}

}
//+

public class CommandInterpretor {

public static String expression = "extra extra cheese";

public static void main(String[] args) {

String[] split = expression.split(" ");

TerminalExpression terminalCommand = new TerminalExpression(split);
String interpret = terminalCommand.interpret();
System.out.println(interpret);

}
}
I hope this gives you what I am trying to interpret here :hidethepain:
Unknown User
Unknown Userā€¢10mo ago
Message Not Public
Sign In & Join Server To View
Rag...JN šŸŒŒ šŸ¦” šŸ‘½ šŸ’° šŸŠ
can you explain? a little bit more thank you so much for your time and kindness šŸ™Œ šŸ™
JavaBot
JavaBotā€¢10mo ago
If you are finished with your post, please close it. If you are not, please ignore this message. Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts.
Unknown User
Unknown Userā€¢10mo ago
Message Not Public
Sign In & Join Server To View
Unknown User
Unknown Userā€¢10mo ago
Message Not Public
Sign In & Join Server To View
Unknown User
Unknown Userā€¢10mo ago
Message Not Public
Sign In & Join Server To View
Unknown User
Unknown Userā€¢10mo ago
Message Not Public
Sign In & Join Server To View
Rag...JN šŸŒŒ šŸ¦” šŸ‘½ šŸ’° šŸŠ
what do you mean by squares can hav 3 values? yeah right? if you had to create a game with System.out.printlns (with the Terminal) how would you do it? you enter values which are strings
Rag...JN šŸŒŒ šŸ¦” šŸ‘½ šŸ’° šŸŠ
this is what I meant
No description
Unknown User
Unknown Userā€¢10mo ago
Message Not Public
Sign In & Join Server To View
Unknown User
Unknown Userā€¢10mo ago
Message Not Public
Sign In & Join Server To View
Rag...JN šŸŒŒ šŸ¦” šŸ‘½ šŸ’° šŸŠ
u just REPRESENTED the user input in a differant way btw please have some water if you haven't
Unknown User
Unknown Userā€¢10mo ago
Message Not Public
Sign In & Join Server To View
Rag...JN šŸŒŒ šŸ¦” šŸ‘½ šŸ’° šŸŠ
being able to click on the blocks means that typing numbers is not an incorrect way to play the game did I get it wrong? do I have ADHD? I literally processing every thing you have said. I know your concept is simple for some reason my brain can't process it
Unknown User
Unknown Userā€¢10mo ago
Message Not Public
Sign In & Join Server To View
Rag...JN šŸŒŒ šŸ¦” šŸ‘½ šŸ’° šŸŠ
So I literally get it wrong? no
Unknown User
Unknown Userā€¢10mo ago
Message Not Public
Sign In & Join Server To View
Rag...JN šŸŒŒ šŸ¦” šŸ‘½ šŸ’° šŸŠ
btw I don't have ADHD like this could be a symptom yep Like you have the details of the car
Unknown User
Unknown Userā€¢10mo ago
Message Not Public
Sign In & Join Server To View
Rag...JN šŸŒŒ šŸ¦” šŸ‘½ šŸ’° šŸŠ
no, the picture comes under the details of the car
Unknown User
Unknown Userā€¢10mo ago
Message Not Public
Sign In & Join Server To View
Unknown User
Unknown Userā€¢10mo ago
Message Not Public
Sign In & Join Server To View
Rag...JN šŸŒŒ šŸ¦” šŸ‘½ šŸ’° šŸŠ
ok ok yah the photo is a representation of a car
Unknown User
Unknown Userā€¢10mo ago
Message Not Public
Sign In & Join Server To View
Rag...JN šŸŒŒ šŸ¦” šŸ‘½ šŸ’° šŸŠ
what is the question here? yah it also represents the car but it represents the exact details
Unknown User
Unknown Userā€¢10mo ago
Message Not Public
Sign In & Join Server To View
Rag...JN šŸŒŒ šŸ¦” šŸ‘½ šŸ’° šŸŠ
Photo I mean number plate or VIN will you some unique details but it's not enough to show it to your friend hey this is my car and you show the Photo of the car not the number plate
Unknown User
Unknown Userā€¢10mo ago
Message Not Public
Sign In & Join Server To View
Rag...JN šŸŒŒ šŸ¦” šŸ‘½ šŸ’° šŸŠ
no it's subjective to show it to a friend online you represent the car with a Photo, to government work you must need a VIN and Number plate even that some government related papers ask for vehicle color and stuff Photo is not only 'correct' way to represent a car
Unknown User
Unknown Userā€¢10mo ago
Message Not Public
Sign In & Join Server To View
Rag...JN šŸŒŒ šŸ¦” šŸ‘½ šŸ’° šŸŠ
The interpreter pattern is you defining how you will represent things So I am representing the Car with Number Plate and Photo Others can Communicate with your program regarding those things They can read and get data from my photo and number plate
Unknown User
Unknown Userā€¢10mo ago
Message Not Public
Sign In & Join Server To View
Rag...JN šŸŒŒ šŸ¦” šŸ‘½ šŸ’° šŸŠ
alright šŸ§  you can represent it in many ways but others has to understand how you represented the thing? so that's why in order to use/communicate your program they have to learn the syntax ok where are we going finally? you can hit me up if you are tired I can wait
Rag...JN šŸŒŒ šŸ¦” šŸ‘½ šŸ’° šŸŠ
=================== is this pattern does interpret? https://www.tutorialspoint.com/design_pattern/interpreter_pattern.htm
Design Patterns - Interpreter Pattern
Design Patterns Interpreter Pattern - Interpreter pattern provides a way to evaluate language grammar or expression. This type of pattern comes under behavioral pattern. This pattern involves implementing an expression interface which tells to interpret a particular context. This pattern is used in SQL parsing, symbol processing engine
Unknown User
Unknown Userā€¢10mo ago
Message Not Public
Sign In & Join Server To View
Unknown User
Unknown Userā€¢10mo ago
Message Not Public
Sign In & Join Server To View
Rag...JN šŸŒŒ šŸ¦” šŸ‘½ šŸ’° šŸŠ
any reviews on this example
public interface Expression {
public boolean interpret(String context);
}

public class TerminalExpression implements Expression {

private String data;

public TerminalExpression(String data){
this.data = data;
}

@Override
public boolean interpret(String context) {

if(context.contains(data)){
return true;
}
return false;
}
}
public class OrExpression implements Expression {

private Expression expr1 = null;
private Expression expr2 = null;

public OrExpression(Expression expr1, Expression expr2) {
this.expr1 = expr1;
this.expr2 = expr2;
}

@Override
public boolean interpret(String context) {
return expr1.interpret(context) || expr2.interpret(context);
}
}
public class AndExpression implements Expression {

private Expression expr1 = null;
private Expression expr2 = null;

public AndExpression(Expression expr1, Expression expr2) {
this.expr1 = expr1;
this.expr2 = expr2;
}

@Override
public boolean interpret(String context) {
return expr1.interpret(context) && expr2.interpret(context);
}
}
public interface Expression {
public boolean interpret(String context);
}

public class TerminalExpression implements Expression {

private String data;

public TerminalExpression(String data){
this.data = data;
}

@Override
public boolean interpret(String context) {

if(context.contains(data)){
return true;
}
return false;
}
}
public class OrExpression implements Expression {

private Expression expr1 = null;
private Expression expr2 = null;

public OrExpression(Expression expr1, Expression expr2) {
this.expr1 = expr1;
this.expr2 = expr2;
}

@Override
public boolean interpret(String context) {
return expr1.interpret(context) || expr2.interpret(context);
}
}
public class AndExpression implements Expression {

private Expression expr1 = null;
private Expression expr2 = null;

public AndExpression(Expression expr1, Expression expr2) {
this.expr1 = expr1;
this.expr2 = expr2;
}

@Override
public boolean interpret(String context) {
return expr1.interpret(context) && expr2.interpret(context);
}
}
public class InterpreterPatternDemo {

//Rule: Robert and John are male
public static Expression getMaleExpression(){
Expression robert = new TerminalExpression("Robert");
Expression john = new TerminalExpression("John");
return new OrExpression(robert, john);
}

//Rule: Julie is a married women
public static Expression getMarriedWomanExpression(){
Expression julie = new TerminalExpression("Julie");
Expression married = new TerminalExpression("Married");
return new AndExpression(julie, married);
}

public static void main(String[] args) {
Expression isMale = getMaleExpression();
Expression isMarriedWoman = getMarriedWomanExpression();

System.out.println("John is male? " + isMale.interpret("John"));
System.out.println("Julie is a married women? " + isMarriedWoman.interpret("Married Julie"));
}
}
public class InterpreterPatternDemo {

//Rule: Robert and John are male
public static Expression getMaleExpression(){
Expression robert = new TerminalExpression("Robert");
Expression john = new TerminalExpression("John");
return new OrExpression(robert, john);
}

//Rule: Julie is a married women
public static Expression getMarriedWomanExpression(){
Expression julie = new TerminalExpression("Julie");
Expression married = new TerminalExpression("Married");
return new AndExpression(julie, married);
}

public static void main(String[] args) {
Expression isMale = getMaleExpression();
Expression isMarriedWoman = getMarriedWomanExpression();

System.out.println("John is male? " + isMale.interpret("John"));
System.out.println("Julie is a married women? " + isMarriedWoman.interpret("Married Julie"));
}
}
Unknown User
Unknown Userā€¢10mo ago
Message Not Public
Sign In & Join Server To View
Rag...JN šŸŒŒ šŸ¦” šŸ‘½ šŸ’° šŸŠ
I have no idea what it interprets tbh ok it tries interpret John a male name in general so if I inserted a lot of male names
Unknown User
Unknown Userā€¢10mo ago
Message Not Public
Sign In & Join Server To View
Unknown User
Unknown Userā€¢10mo ago
Message Not Public
Sign In & Join Server To View
Rag...JN šŸŒŒ šŸ¦” šŸ‘½ šŸ’° šŸŠ
ok It would be like
Expression ragbag = new TerminalExpression("RagBag");
Expression rag = new TerminalExpression("Rag");
Expression combination1 = new OrExpression(ragbag, rag)
Expression robert = new TerminalExpression("Robert");
Expression john = new TerminalExpression("John");
Expression combination2 = new OrExpression(robert, john)
return new OrExpression(combination1, combination2);
Expression ragbag = new TerminalExpression("RagBag");
Expression rag = new TerminalExpression("Rag");
Expression combination1 = new OrExpression(ragbag, rag)
Expression robert = new TerminalExpression("Robert");
Expression john = new TerminalExpression("John");
Expression combination2 = new OrExpression(robert, john)
return new OrExpression(combination1, combination2);
Unknown User
Unknown Userā€¢10mo ago
Message Not Public
Sign In & Join Server To View
Rag...JN šŸŒŒ šŸ¦” šŸ‘½ šŸ’° šŸŠ
it's just a fixed method right? the user has the option to enter any of the male names that are mentioned there?
Unknown User
Unknown Userā€¢10mo ago
Message Not Public
Sign In & Join Server To View
Rag...JN šŸŒŒ šŸ¦” šŸ‘½ šŸ’° šŸŠ
alright can we try another approach? would you try out your solution for original question in abstract way not too much detail then we can discuss it?
Unknown User
Unknown Userā€¢10mo ago
Message Not Public
Sign In & Join Server To View
Rag...JN šŸŒŒ šŸ¦” šŸ‘½ šŸ’° šŸŠ
So if I type "3x + 4 = 28" it should print out the value? 8 let's start with the interface
public interface Expression {
void interpret();
}
public interface Expression {
void interpret();
}
which is obvious
Unknown User
Unknown Userā€¢10mo ago
Message Not Public
Sign In & Join Server To View
Rag...JN šŸŒŒ šŸ¦” šŸ‘½ šŸ’° šŸŠ
4,3 are the terminal expressions
Unknown User
Unknown Userā€¢10mo ago
Message Not Public
Sign In & Join Server To View
Rag...JN šŸŒŒ šŸ¦” šŸ‘½ šŸ’° šŸŠ
class TerminalExpression implements Expression{

private int value;

public TerminalExpression(int value) {
this.value = value;
}

@Override
public int interpret() {
return value;
}
}
class TerminalExpression implements Expression{

private int value;

public TerminalExpression(int value) {
this.value = value;
}

@Override
public int interpret() {
return value;
}
}
Unknown User
Unknown Userā€¢10mo ago
Message Not Public
Sign In & Join Server To View
Rag...JN šŸŒŒ šŸ¦” šŸ‘½ šŸ’° šŸŠ
it does not take a context either right?
Unknown User
Unknown Userā€¢10mo ago
Message Not Public
Sign In & Join Server To View
Rag...JN šŸŒŒ šŸ¦” šŸ‘½ šŸ’° šŸŠ
The interface's interpret method should've taken a context in our case "3x + 4 = 28"
Rag...JN šŸŒŒ šŸ¦” šŸ‘½ šŸ’° šŸŠ
context will have something like this?
No description
Rag...JN šŸŒŒ šŸ¦” šŸ‘½ šŸ’° šŸŠ
can you see it btw? shall I send the code?
Unknown User
Unknown Userā€¢10mo ago
Message Not Public
Sign In & Join Server To View
Rag...JN šŸŒŒ šŸ¦” šŸ‘½ šŸ’° šŸŠ
this is the full example code for it but where is the context?
Rag...JN šŸŒŒ šŸ¦” šŸ‘½ šŸ’° šŸŠ
jt
Spring Framework Guru
Interpreter Pattern - Spring Framework Guru
Interpreter Pattern ā€œGiven a language, define a representation for its grammar along with an interpreter that uses the representation to interpret sentences in the language.ā€ Design Patterns: Elements of Reusable Object-Oriented Software dInterpreter pattern is part of the Behavioral pattern family of the Gang of Four design patterns. Behavioral...
From jt
From An unknown user
Unknown User
Unknown Userā€¢10mo ago
Message Not Public
Sign In & Join Server To View
Rag...JN šŸŒŒ šŸ¦” šŸ‘½ šŸ’° šŸŠ
ok ok I am going to take some break
JavaBot
JavaBotā€¢10mo ago
šŸ’¤ Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived. If your question was not answered yet, feel free to re-open this post or create a new one. In case your post is not getting any attention, you can try to use /help ping. Warning: abusing this will result in moderative actions taken against you.
Rag...JN šŸŒŒ šŸ¦” šŸ‘½ šŸ’° šŸŠ
/open @borgrel 3x + 4 = 28 for this the NonTerminal ones are AdditionExpression? SubstractionExpression also I have to follow the BODMAS right? The non terminal expressions we need
class AdditionExpression implements Expression{
private Expression firstExpression, secondExpression;

public AdditionExpression(Expression firstExpression, Expression secondExpression) {
this.firstExpression = firstExpression;
this.secondExpression = secondExpression;
}

@Override
public int interpret() {
return firstExpression.interpret()+secondExpression.interpret();
}
}
class SubstractionExpression implements Expression{
private Expression firstExpression, secondExpression;

public SubstractionExpression(Expression firstExpression, Expression secondExpression) {
this.firstExpression = firstExpression;
this.secondExpression = secondExpression;
}

@Override
public int interpret() {
return firstExpression.interpret() - secondExpression.interpret();
}
}
class MultiplyExpression implements Expression{
private Expression firstExpression, secondExpression;

public MultiplyExpression(Expression firstExpression, Expression secondExpression) {
this.firstExpression = firstExpression;
this.secondExpression = secondExpression;
}

@Override
public int interpret() {
return firstExpression.interpret() * secondExpression.interpret();
}
}
class DivisionExpression implements Expression{
private Expression firstExpression, secondExpression;

public DivisionExpression(Expression firstExpression, Expression secondExpression) {
this.firstExpression = firstExpression;
this.secondExpression = secondExpression;
}

@Override
public int interpret() {
return firstExpression.interpret() / secondExpression.interpret();
}
}
class AdditionExpression implements Expression{
private Expression firstExpression, secondExpression;

public AdditionExpression(Expression firstExpression, Expression secondExpression) {
this.firstExpression = firstExpression;
this.secondExpression = secondExpression;
}

@Override
public int interpret() {
return firstExpression.interpret()+secondExpression.interpret();
}
}
class SubstractionExpression implements Expression{
private Expression firstExpression, secondExpression;

public SubstractionExpression(Expression firstExpression, Expression secondExpression) {
this.firstExpression = firstExpression;
this.secondExpression = secondExpression;
}

@Override
public int interpret() {
return firstExpression.interpret() - secondExpression.interpret();
}
}
class MultiplyExpression implements Expression{
private Expression firstExpression, secondExpression;

public MultiplyExpression(Expression firstExpression, Expression secondExpression) {
this.firstExpression = firstExpression;
this.secondExpression = secondExpression;
}

@Override
public int interpret() {
return firstExpression.interpret() * secondExpression.interpret();
}
}
class DivisionExpression implements Expression{
private Expression firstExpression, secondExpression;

public DivisionExpression(Expression firstExpression, Expression secondExpression) {
this.firstExpression = firstExpression;
this.secondExpression = secondExpression;
}

@Override
public int interpret() {
return firstExpression.interpret() / secondExpression.interpret();
}
}
JavaBot
JavaBotā€¢10mo ago
šŸ’¤ Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived. If your question was not answered yet, feel free to re-open this post or create a new one. In case your post is not getting any attention, you can try to use /help ping. Warning: abusing this will result in moderative actions taken against you.
john
johnā€¢10mo ago
I guess I figured out the Interpreter Pattern for my application
tjoener
tjoenerā€¢10mo ago
That's not what terminal and non terminal mean
Rag...JN šŸŒŒ šŸ¦” šŸ‘½ šŸ’° šŸŠ
Non-terminal is a composite
tjoener
tjoenerā€¢10mo ago
You got it šŸ™‚
JavaBot
JavaBotā€¢10mo ago
šŸ’¤ Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived. If your question was not answered yet, feel free to re-open this post or create a new one. In case your post is not getting any attention, you can try to use /help ping. Warning: abusing this will result in moderative actions taken against you.
Want results from more Discord servers?
Add your server