1

Ok so here is the code for my calculator I started with.

import java.awt.*;
import java.applet.*;

public class Calculator extends Applet
{

    private Label calculatorL; //Sets the label
    private boolean firstDigit = true; //Sets Boolean firstDigit to be true
    private float savedValue = 0.0f;//Sets saved value to 0
    private String operator = "="; //Sets Default operator

    public void init ()
    {
        setLayout (new BorderLayout()); //Sets the layout
        add ("North", calculatorL = new Label ("0", Label.RIGHT));
        Panel calculatorPanel = new Panel(); //Adding a panel for the buttons
        calculatorPanel.setLayout (new GridLayout (4, 4)); //Adding a 4 * 4 grid for the layout of the buttons
        calculatorPanel.setBackground(Color.CYAN);
        calculatorPanel.setForeground(Color.BLUE);
        addButtons (calculatorPanel, "123-");
        addButtons (calculatorPanel, "456*");
        addButtons (calculatorPanel, "789/");
        addButtons (calculatorPanel, ".0=+");
        add("Center", calculatorPanel);
    }

    public boolean action (Event event, Object inputobject)
    {
        if (event.target instanceof Button)
        {
            String inputstring = (String)inputobject;
            if ("0123456789.".indexOf (inputstring) != -1)
            {
                if (firstDigit)
                {
                    firstDigit = false;
                    calculatorL.setText (inputstring);
                }
                else
                {
                    calculatorL.setText(calculatorL.getText() + inputstring);
                }
            }  
            else
            {
                if(!firstDigit)
                {
                    solve(calculatorL.getText());
                    firstDigit = true;
                }
                operator = inputstring;
            }
            return true;
        }
        return false;
    }

    public void solve (String valueToBeComputed)
    {
        float sValue = new Float (valueToBeComputed).floatValue();
        char c = operator.charAt (0);
        switch (c)
        {
            case '=': savedValue = sValue;
                break;
            case '+': savedValue += sValue;
                break;
            case '-': savedValue -= sValue;
                break;
            case '*': savedValue *= sValue;
                break;
            case '/': savedValue /= sValue;
                break;
        }
        calculatorL.setText (Float.toString(savedValue));
    }

    public void addButtons (Panel panel, String labels)
    {
        int count = labels.length();
        for (int i = 0; i<count; i ++)
        {
            panel.add (new Button (labels.substring(i,i+1)));
        }
    }
}

What I'm trying to do is transfer my solver code into its own separate class.

This is what I have so far

import java.awt.*;

public class Solver
{

    private String operator = "="; //Sets Default operator
    private float savedValue = 0.0f;//Sets saved value to 0

    public void solve (String valueToBeComputed)
    {
        float sValue = new Float (valueToBeComputed).floatValue();
        char c = operator.charAt (0);
        switch (c)
        {
            case '=': savedValue = sValue;
                break;
            case '+': savedValue += sValue;
                break;
            case '-': savedValue -= sValue;
                break;
            case '*': savedValue *= sValue;
                break;
            case '/': savedValue /= sValue;
                break;
        }
        calculatorL.setText (Float.toString(savedValue));
    }
}

Really new to using/creating my own custom classes. Could use some help transfering and seting up the solver class.

Alexander Farber
  • 21,519
  • 75
  • 241
  • 416
  • 1
    What is your question? – Alexander Farber Oct 26 '16 at 08:20
  • I'm looking for help to create a custom class. I'm not to sure on how they work. Just looking for someone to help me move my solve code into a separate class. – Dylan Bassett Oct 26 '16 at 08:27
  • 1) Why use AWT? See [this answer](http://stackoverflow.com/questions/6255106/java-gui-listeners-without-awt/6255978#6255978) for many good reasons to abandon AWT components in favor of Swing. 2) Why code an applet? If it is due to the teacher specifying it, please refer them to [Why CS teachers should **stop** teaching Java applets](http://programmers.blogoverflow.com/2013/05/why-cs-teachers-should-stop-teaching-java-applets/). .. – Andrew Thompson Oct 26 '16 at 12:50
  • .. 3) See [Java Plugin support deprecated](http://www.gizmodo.com.au/2016/01/rest-in-hell-java-plug-in/) and [Moving to a Plugin-Free Web](https://blogs.oracle.com/java-platform-group/entry/moving_to_a_plugin_free). 4) See also this [calculator example](http://stackoverflow.com/a/7441804/418556). It uses `ScriptEngine` to evaluate the expression in the text field. – Andrew Thompson Oct 26 '16 at 12:51

1 Answers1

1

Moving code around is sometimes called refactoring...

You could either make the solve method static in your new class and then call it like

Solver.solve(calculatorL.getText());

Or instantiate a Solve object with

Solve mySolve = new Solve();

and then call that object's method with:

mySolve.solve(calculatorL.getText());

Finally, to return the calculated value you should change the method type from void to String or float and then pass it to the calculatorL.setText().

Alexander Farber
  • 21,519
  • 75
  • 241
  • 416