Skill-1 Java
Skill-1 Java
1. Expression Evaluation
Implement Interpreter Design pattern to solve postfix expression in Java.
package interpreterpattern;
Package interpreterpattern;
package interpreterpattern;
public Substract(Expression
leftExpression,Expression rightExpression )
{
this.leftExpression = leftExpression;
this.rightExpression = rightExpression;
}
package interpreterpattern;
public Number(int n)
{
this.n = n;
}
public int interpret()
{
return n;
}
package interpreterpattern;
}
package interpreterpattern;
import java.util.Stack;
Input:
73-21+*
Output:12
2. Assume you are going to buy some items from the Departmental store. You want to calculate the
Total cost of the items which you want to purchase from the store. Following items to be purchased:
• Parker pen
• Pilet pen
• One Notebook with more than 250 pages ( Ex: Price is Rs. 50)
• One Notebook with less than 250 pages ( If pages < 250, reduce the cost by Rs.5)
Find the Total cost by implementing Visitor pattern
Package visitorpattern;
interface Item
interface Visitor
this.price = price;
this.model = model;
return price;
return model;
return visitor.visit(this);
this.price = price;
this.numberOfPages = numberOfPages;
return price;
return numberOfPages;
returnvisitor.visit(this);
return price;
{
Int price = 0;
price = notebook.getPrice()-5;
else
price = notebook.getPrice();
return price;
new Pen(10, "Parker"), new Pen(5, "Pilet"), new Notebook(50, 150), new Notebook(75, 300);
inttotal = getTotalPrice(items);
privatestaticintgetTotalPrice(Item[] items)
int result = 0;
return result;
}
3. Implement BubbleSort sorting algorithm using Strategy pattern
Ans)
Public interface SortingStrategy
{
int[] sort( int[] inputArray );
}
import java.util.Scanner;
context.sort();
}
}
Input: 34,12,67,10,7
Output: 7,10,12,34,67