d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Need Help With Hw (lots Done But Errors) > Reverse Polish Calculator W/ Linked List
12Next
Add Reply New Topic New Poll
Member
Posts: 6,036
Joined: Nov 15 2012
Gold: 130.00
Sep 11 2015 07:18pm
Heyo, I would like to send someone my source files of my code or just copy and paste my code to them.

It's in Java.
I use NetBeans as my compiler.

This is for my hw due Sunday. I'm not paying for someone to help me do it, but looking for someone able to point me in the right direction.

So basically, here's what I have so far and what I need to do:

User inputs for example:
5 10 +

My program should be able to print:
answer = 15

I need to use a LinkedList that I create myself and not import from a library.
But for this assignment, it's ok to make the LinkedList act more like a stack (as my teacher called it, but we haven't really learned stacks yet either).

What I have now:
I have my LinkedList class and Node class.
I created a "push" method that pushes doubles into the list and it's working correctly. (I am testing it using a "get" function that I copied from online)
However, I would like a "pop" method that returns the value at the top of a list and returns the value of the data it carried.

My terminology might be way off since I'm still a noob at this stuff. I apologize if I say anything incorrectly, but will clarify if need be.
Member
Posts: 1,995
Joined: Jun 28 2006
Gold: 7.41
Sep 11 2015 07:34pm
sure
Member
Posts: 6,036
Joined: Nov 15 2012
Gold: 130.00
Sep 11 2015 09:48pm
Quote (Minkomonster @ Sep 11 2015 08:34pm)
sure

Do you have a skype I could msg you on?

I'd like to show you what I have now and maybe you can help me with why it isn't working or guide me on how to remake a pop method that works.

Thanks!
Member
Posts: 1,995
Joined: Jun 28 2006
Gold: 7.41
Sep 11 2015 09:48pm
ill pm you
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Sep 11 2015 09:57pm
Quote (Minkomonster @ Sep 11 2015 10:48pm)
ill pm you


waaay too informal. You need to send a proper business email + cover letter before you begin working on an extensive solution to his problem
Member
Posts: 6,036
Joined: Nov 15 2012
Gold: 130.00
Sep 11 2015 10:51pm
Quote (Minkomonster @ Sep 11 2015 10:48pm)
ill pm you

xD <3 Thanks so much for the help.
I'll be working through it again and commenting my code to make sure I understand it.

Quote (Eep @ Sep 11 2015 10:57pm)
waaay too informal. You need to send a proper business email + cover letter before you begin working on an extensive solution to his problem

Come back to PoE.
I enjoyed playing with the stuff you sold me ^-^
Member
Posts: 1,995
Joined: Jun 28 2006
Gold: 7.41
Sep 12 2015 04:11am
We talked a bit about thinking about objects. I decided to throw together something to show how even a simple assignment like this can be built with objects in mind. While I was doing this, I fell into old trollish habits. But I just went with it. I figured it kills two birds. I get to show you how to encapsulate the calculator as an object, and everyone else can get some sort of kick out of me turning something simple into something ridiculous

What is a Calculator? It is a set of operations that can be applied to a group of operands. Given an expression, the calculator can evaluate it to produce a result.

Properties: A set of operations
Behaviors: evaluating expression

Code

public interface Calculator {
double evaluate(String expression);
}


A reverse polish notation calculator is a specific implementation of a calculator which evaluates using RPN.

Code
import java.util.LinkedList;
import java.util.Optional;
import java.util.Scanner;
import java.util.Set;

public class ReversePolishNotationCalculator implements Calculator {

private Set<Operation> operations;

public ReversePolishNotationCalculator()
{
//TODO: implement dependency injection framework to decouple
//implementation from specifics of operation creation
this(new BasicOperationSetFactory());
}

public ReversePolishNotationCalculator(OperationSetFactory operationSetFactory)
{
if(operationSetFactory == null)
throw new IllegalArgumentException("operationSetFactory is null");

this.operations = operationSetFactory.createOperationSet();
}

public double evaluate(String expression) {

Scanner parser = new Scanner(expression);
LinkedList<Double> register = new LinkedList<Double>();

while(parser.hasNext())
{
String parsed = parser.next();
Optional<Operation> operation = getOperation(parsed);

if(operation.isPresent())
{
if(register.size() < 2)
throw new IllegalArgumentException("Too many operators");

Double operand1 = register.pop();
Double operand2 = register.pop();
Double result = operation.get().execute(operand1, operand2);
register.push(result);
}
else
{
register.push(Double.parseDouble(parsed));
}
}

if(register.size() != 1)
throw new IllegalArgumentException("Too many operands");

return register.pop();
}

private Optional<Operation> getOperation(String operator)
{
return operations
.stream()
.filter(o -> o.getOperator().equals(operator))
.findFirst();
}

}


Our calculator doesn't care about specifics of its operations. It just cares about applying the rules specified by RPN. For this reason, we can abstract the implementation of Operations behind an interface, and the construction of the Set to a Factory. Fuhhtermore, the factory can be dependency injected into the calculator to further separate concerns and decouple our solution. That would be S, I, and D of SOLID design principles of OOP.
Member
Posts: 1,995
Joined: Jun 28 2006
Gold: 7.41
Sep 12 2015 04:14am
For shits and giggles, here is the rest of my ridiculous implementation:

Code

public interface Operation {
String getOperator();
double execute(Double operand1, Double operand2) throws ArithmeticException;
}


public class AddOperation implements Operation {

public String getOperator() {
return "+";
}

public double execute(Double operand1, Double operand2)
throws ArithmeticException {
return operand1 + operand2;
}

}


public class SubtractOperation implements Operation {

public String getOperator() {
return "-";
}

public double execute(Double operand1, Double operand2)
throws ArithmeticException {
return operand2 + operand1;
}

}


public class MultiplyOperation implements Operation {

public String getOperator() {
return "*";
}

public double execute(Double operand1, Double operand2)
throws ArithmeticException {
return operand1 * operand2;
}

}


public class DivideOperation implements Operation {

public String getOperator() {
return "/";
}

public double execute(Double operand1, Double operand2)
throws ArithmeticException {
if(operand1 == 0)
throw new ArithmeticException("Divison by zero");
return operand2 / operand1;
}

}



Code
import java.util.Set;

public interface OperationSetFactory {
Set<Operation> createOperationSet();
}

import java.util.HashSet;
import java.util.Set;


public class BasicOperationSetFactory implements OperationSetFactory {

public Set<Operation> createOperationSet() {
return new HashSet<Operation>()
{{
add(new AddOperation());
add(new SubtractOperation());
add(new MultiplyOperation());
add(new DivideOperation());
}};

}
}


Code
import java.util.Scanner;

public class ReversePolishNotationCalculatorDriver {
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);

ReversePolishNotationCalculator calculator = new ReversePolishNotationCalculator();
String expression;
while(!(expression = input.nextLine()).equals("0"))
{
try
{
System.out.println(calculator.evaluate(expression));
}
catch(RuntimeException ex)
{
System.out.println(String.format("Error: %s", ex.getMessage()));
}
}
}
}
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Sep 12 2015 11:10am
Quote (lxpandaxl @ Sep 11 2015 11:51pm)
xD <3 Thanks so much for the help.
I'll be working through it again and commenting my code to make sure I understand it.


Come back to PoE.
I enjoyed playing with the stuff you sold me ^-^


Something about the current season bothers me honestly.

I think they need to refine A4 a bit, and possibly rebalance some other aspects.

I tried like 6 different builds this season but couldn't find a single one that didn't just RIP incredibly easily, even when I built multiple layers of defenses
Member
Posts: 7,755
Joined: Sep 26 2011
Gold: 129.34
Sep 20 2015 12:10am
looks like that calculator will add 2 numbers when asked to subtract em minko
Go Back To Programming & Development Topic List
12Next
Add Reply New Topic New Poll