habibalbi 0 Newbie Poster

I already created a class Calculator that performs basic arithmetic,

now I need to create a class called ScientificCalculator that the functionalities of "Calculator" class through use of inheritance. Scientific Calculator has additional operstions of 1) taking a log of a number to any base 2) taking any number to any power. The operational keys for log, and power are "LogX_BaseY", and "X^Y" respectively.

This is my code for the Calculator:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.text.*;

public class CalculatorApplication extends JFrame implements ActionListener {

    JTextField resultField;
	int operandOne;
	int operandTwo;
    int operator;

    private final int WINDOW_WIDTH = 280;
    private final int WINDOW_HEIGHT = 240;

    JButton addButton;
    JButton subtractButton;
    JButton multiplyButton;
    JButton divideButton;
    JButton equalsButton;
    JButton dotButton;
    JButton cancelButton;
    JButton backButton;

    JButton one1;
    JButton two2;
    JButton three3;
    JButton four4;
    JButton five5;
    JButton six6;
    JButton seven7;
    JButton eight8;
    JButton nine9;
    JButton zero0;

    Object source;


    public CalculatorApplication() {


        super("Calculator");
		setSize(WINDOW_WIDTH, WINDOW_HEIGHT);

        JPanel operandPanel;
        JPanel buttonPanel1;
        JPanel buttonPanel2;
        JPanel buttonPanel3;
        JPanel buttonPanel4;

        Container cp;

        cp = getContentPane();

        cp.setLayout(new GridLayout(6, 2));


        operandPanel = new JPanel();
        operandPanel.setLayout(new FlowLayout());

        resultField = new JTextField("0", 23);
        resultField.setHorizontalAlignment(SwingConstants.RIGHT);

        operandPanel.add(resultField);
        cp.add(operandPanel);


        buttonPanel1 = new JPanel();
        buttonPanel1.setLayout(new FlowLayout());
        seven7 = new JButton(" 7 ");
        seven7.addActionListener(this);
        buttonPanel1.add(seven7);
        eight8 = new JButton(" 8 ");
        eight8.addActionListener(this);
        buttonPanel1.add(eight8);
        nine9 = new JButton(" 9 ");
        nine9.addActionListener(this);
        buttonPanel1.add(nine9);
        cancelButton = new JButton(" C ");
        cancelButton.addActionListener(this);
        buttonPanel1.add(cancelButton);
        cp.add(buttonPanel1);

        buttonPanel2 = new JPanel();
        buttonPanel2.setLayout(new FlowLayout());
        four4 = new JButton(" 4 ");
        four4.addActionListener(this);
        buttonPanel2.add(four4);
        five5 = new JButton(" 5 ");
        five5.addActionListener(this);
        buttonPanel2.add(five5);
        six6 = new JButton(" 6 ");
        six6.addActionListener(this);
        buttonPanel2.add(six6);
        divideButton = new JButton(" / ");
		divideButton.addActionListener(this);
        buttonPanel1.add(divideButton);
        cp.add(buttonPanel2);


        buttonPanel3 = new JPanel();
        buttonPanel3.setLayout(new FlowLayout());
        one1 = new JButton(" 1 ");
        one1.addActionListener(this);
        buttonPanel3.add(one1);
        two2 = new JButton(" 2 ");
        two2.addActionListener(this);
        buttonPanel3.add(two2);
        three3 = new JButton(" 3 ");
        three3.addActionListener(this);
        buttonPanel3.add(three3);
        multiplyButton = new JButton(" * ");
		multiplyButton.addActionListener(this);
        buttonPanel2.add(multiplyButton);
        cp.add(buttonPanel3);

        buttonPanel4 = new JPanel();
        buttonPanel4.setLayout(new FlowLayout());
        zero0 = new JButton(" 0 ");
        zero0.addActionListener(this);
        buttonPanel4.add(zero0);
        addButton = new JButton(" + ");
        addButton.addActionListener(this);
        buttonPanel4.add(addButton);
       	subtractButton = new JButton(" - ");
		subtractButton.addActionListener(this);
        buttonPanel3.add(subtractButton);
        equalsButton = new JButton(" = ");
        equalsButton.addActionListener(this);
        buttonPanel4.add(equalsButton);
        cp.add(buttonPanel4);

        setVisible(true);

    }

    public void showResult() {

        boolean validOperands;

        validOperands = true;


        if (validOperands) {
			if (source == one1) {
				if (resultField.getText().equals("0"))
					resultField.setText("1");
				else
					resultField.setText(resultField.getText() + "1");
			    }

			else if (source == two2) {
				if (resultField.getText().equals("0"))
					resultField.setText("2");
				else
					resultField.setText(resultField.getText() + "2");
		   		 }

			else if (source == three3) {
				if (resultField.getText().equals("0"))
					resultField.setText("3");
				else
					resultField.setText(resultField.getText() + "3");
		    	}

			else if (source == four4) {
				if (resultField.getText().equals("0"))
					resultField.setText("4");
				else
					resultField.setText(resultField.getText() + "4");
			    }

			else if (source == five5) {
				if (resultField.getText().equals("0"))
					resultField.setText("5");
				else
					resultField.setText(resultField.getText() + "5");
	    		}

			else if (source == six6) {
				if (resultField.getText().equals("0"))
					resultField.setText("6");
				else
					resultField.setText(resultField.getText() + "6");
		    }

			else if (source == seven7) {
				if (resultField.getText().equals("0"))
					resultField.setText("7");
				else
					resultField.setText(resultField.getText() + "7");
			    }

			else if (source == eight8) {
				if (resultField.getText().equals("0"))
					resultField.setText("8");
				else
					resultField.setText(resultField.getText() + "8");
			    }

			else if (source == nine9) {
				if (resultField.getText().equals("0"))
					resultField.setText("9");
				else
					resultField.setText(resultField.getText() + "9");
			    }

			else if (source == zero0) {
				if (resultField.getText().equals("0"))
					resultField.setText("0");
				else
					resultField.setText(resultField.getText() + "0");
			    }

	    	else if (source == cancelButton) {
				resultField.setText("0");
				}

			else if (source == backButton) {
				if (!(resultField.getText().equals("0") || resultField.getText().equals("0.0")) && resultField.getText().length() > 1){
							resultField.setText(resultField.getText().substring(0,resultField.getText().length()-1));
					}
	    		}

            else if (source == addButton) {
                operandOne = Integer.parseInt(resultField.getText());
				resultField.setText("0");
				operator = 1;
            }

            else if (source == subtractButton) {
             	operandOne = Integer.parseInt(resultField.getText());
				resultField.setText("0");
				operator = 2;
            }

            else if (source == multiplyButton) {
	        	operandOne = Integer.parseInt(resultField.getText());
				resultField.setText("0");
				operator = 3;
            }

            else if (source == divideButton) {
             	operandOne = Integer.parseInt(resultField.getText());
				resultField.setText("0");
				operator = 4;
            }

            else if (source == equalsButton) {
             	operandTwo = Integer.parseInt(resultField.getText());

					switch(operator){
					case 1:
						resultField.setText(Integer.toString(operandOne + operandTwo));
						operandOne = 0;
						operandTwo = 0;
						break;

					case 2:
						resultField.setText(Integer.toString(operandOne - operandTwo));
						operandOne = 0;
						operandTwo = 0;
						break;

					case 3:
						resultField.setText(Integer.toString(operandOne * operandTwo));
						operandOne = 0;
						operandTwo = 0;
						break;

					case 4:
               			 if (operandTwo != 0){
                   			resultField.setText(Integer.toString(operandOne / operandTwo));
							operandOne = 0;
							operandTwo = 0;
						}
              			  else{
							 resultField.setText("Error");
						}
						break;
					}
				}
			}
		}


    public void actionPerformed(ActionEvent ae) {

        source = ae.getSource();
        showResult();
    }



   public static void main(String[] args) {

        CalculatorApplication app = new CalculatorApplication();

        app.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent we) {
                System.exit(0);
            }
        });


    }
}

This is what I have for the Scientific calculator, but I can't even get it to display the new buttons..I'm extending it wrong? Or is there something wrong with my constructor? I'm stuck on just getting the buttons to be displayed, if I can do that, I think I might know how to do the rest.

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.text.*;



public class ScientificCalculator extends CalculatorApplication {

	JButton logButton;
	JButton powerButton;




	public ScientificCalculator(){

		super();

		JPanel buttonPanel5;

               Container cp;

               cp = getContentPane();


		

		buttonPanel5 = new JPanel();
		buttonPanel5.setLayout(new FlowLayout());
		logButton = new JButton("LogX_Base_Y");
		logButton.addActionListener(this);
		buttonPanel5.add(logButton);
		powerButton = new JButton("X^Y");
		powerButton.addActionListener(this);
		buttonPanel5.add(powerButton);
		cp.add(buttonPanel5);

		setVisible(true);
	}
}

And just to be sure, for the log operation I would use Math.log(operand1)/java.Lang.Math.log(operand2)

For power: Math.pow(operand1, operand2);

Does that seem correct?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.