5 Replies - 23418 Views - Last Post: 08 June 2009 - 06:35 PM Rate Topic: -----

#1 legz01  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 08-June 09

Java - Calculator Program using Interface & GUI

Post icon  Posted 08 June 2009 - 10:40 AM

I am a beginner at Java. I am working on a calculator program. Instructor onlys wants 3 classes (calculator class, scientific class and calculator GUI). Calculator class has no data members and 4 interfaces (add, sub, div, mult). The GUI class is a calculator setup for the user to enter two values then click (+ - / *); the action performed should invoke the interface to process the calculation accordingly and place the results in the results field back in GUI.

I've completed the majority of the program however is stuck at interfaces. I was able to starting the interface coding identifying the two doubles; however where would i put the actual calculation. And where would the result be placed?

How do i connect the approriate interface based on the button selection?

Whatever you can assist would be most helpful. I have been trying to review different examples all weekend and not really finding anything that can assist me.

Is This A Good Question/Topic? 0
  • +

Replies To: Java - Calculator Program using Interface & GUI

#2 computerfox  Icon User is offline

  • straight vegetarian kid

Reputation: 50
  • View blog
  • Posts: 3,772
  • Joined: 29-January 09

Re: Java - Calculator Program using Interface & GUI

Posted 08 June 2009 - 10:44 AM

public static void yourAnswer(){
while(0==0){
System.out.println("I'm sorry, but we don't do your work for you here...");
System.out.println("We only help with code presented...");
System.out.println("Please edit your post with your code and issues...");
System.out.println("Thank you...");
}
}


This post has been edited by computerfox: 08 June 2009 - 10:44 AM

Was This Post Helpful? 0
  • +
  • -

#3 Tanira  Icon User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 102
  • Joined: 30-May 09

Re: Java - Calculator Program using Interface & GUI

Posted 08 June 2009 - 10:46 AM

View Postlegz01, on 8 Jun, 2009 - 09:40 AM, said:

I am a beginner at Java. I am working on a calculator program. Instructor onlys wants 3 classes (calculator class, scientific class and calculator GUI). Calculator class has no data members and 4 interfaces (add, sub, div, mult). The GUI class is a calculator setup for the user to enter two values then click (+ - / *); the action performed should invoke the interface to process the calculation accordingly and place the results in the results field back in GUI.

I've completed the majority of the program however is stuck at interfaces. I was able to starting the interface coding identifying the two doubles; however where would i put the actual calculation. And where would the result be placed?

How do i connect the approriate interface based on the button selection?

Whatever you can assist would be most helpful. I have been trying to review different examples all weekend and not really finding anything that can assist me.


You can put the result in the input box. Many computer calculators work in that way.

The calculation could be done in a method called by the buttonPressed.

Just if(buttonPressed.getText() == "+"){
}
or something like that :P

Hope that helps!
Was This Post Helpful? 0
  • +
  • -

#4 n00bc0der  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 62
  • Joined: 27-November 08

Re: Java - Calculator Program using Interface & GUI

Posted 08 June 2009 - 10:49 AM

I'm assuming he wants you to use inheritance (because it's an object oriented language), but I'm going to have to agree with computerfox, I'm not helping you without seeing your original code.

GOoD LuCk
Was This Post Helpful? 0
  • +
  • -

#5 legz01  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 08-June 09

Re: Java - Calculator Program using Interface & GUI

Posted 08 June 2009 - 03:00 PM

View Postlegz01, on 8 Jun, 2009 - 09:40 AM, said:

I am a beginner at Java. I am working on a calculator program. Instructor onlys wants 3 classes (calculator class, scientific class and calculator GUI). Calculator class has no data members and 4 interfaces (add, sub, div, mult). The GUI class is a calculator setup for the user to enter two values then click (+ - / *); the action performed should invoke the interface to process the calculation accordingly and place the results in the results field back in GUI.

I've completed the majority of the program however is stuck at interfaces. I was able to starting the interface coding identifying the two doubles; however where would i put the actual calculation. And where would the result be placed?

How do i connect the approriate interface based on the button selection?

Whatever you can assist would be most helpful. I have been trying to review different examples all weekend and not really finding anything that can assist me.


Here is my GUICalculator Class:Sorry Everyone. This is my first time using this and I thought by adding my attachment it woul be helpuful. I've posted by code below. I am really confused. Thanks

import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Component;
import javax.swing.JButton;
import javax.swing.JTextField;
import javax.swing.JFrame;
import javax.swing.JLabel;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
//import javax.swing.JOptionPane;


public class CalculatorGUI1 extends JFrame implements ActionListener
{

    private static final int TXT_FIELD_SZ= 10;
    private GridBagLayout calcLayout;               // layout for frame
    private GridBagConstraints constraints;
    private Object[] buttons;
    private Object txtDisplay;

    // Setting up GUI
    public CalculatorGUI1()
    {
        super("Calculator");
        calcLayout = new GridBagLayout ();
        setLayout (calcLayout);                     // Set Layout Frame
        constraints = new GridBagConstraints();     // Determine loc & size of components

        // SETTING UP GUI COMPONENTS
        JLabel lbval1 = new JLabel("Value 1");
        JTextField val1entryfield = new JTextField (CalculatorGUI1.TXT_FIELD_SZ);
        JLabel lbval2 = new JLabel("Value 2");
        JTextField val2entryfield = new JTextField (CalculatorGUI1.TXT_FIELD_SZ);
        JLabel lbresults = new JLabel ("Results");
        JTextField displayresults = new JTextField (" ");
        displayresults.setEnabled(false);

        String btnNames[] = { "ADD", "SUBTRACT", "MULTIPLY", "DIVIDE", "POWER"};
        JButton buttons[] = new JButton[5];

        for ( int count = 0; count < 5; count++)
        {
          buttons[count] = new JButton( btnNames [count]);
          buttons[count].addActionListener((ActionListener) this);

        }
        // DEFINE GUI Components & contraints for textfield
        constraints.weightx = 1;
        constraints.weighty = 1;
        constraints.fill = GridBagConstraints.BOTH;
        constraints.gridwidth = GridBagConstraints.REMAINDER;
        addComponent (lbval1);
        addComponent (val1entryfield);
        addComponent (lbval2);
        addComponent (val2entryfield);
        addComponent (lbresults);
        addComponent (displayresults);

        val1entryfield.addActionListener ((ActionListener) this);
        val2entryfield.addActionListener ((ActionListener) this);
        displayresults.addActionListener ((ActionListener) this);

        // Define Buttons
        constraints.gridwidth = 1;
        addComponent( buttons [0] );

        constraints.gridwidth = GridBagConstraints.RELATIVE;
        addComponent( buttons [1] );

        constraints.gridwidth = GridBagConstraints.REMAINDER;
        addComponent( buttons [2] );

        constraints.gridwidth = 2;
        addComponent( buttons [3] );

        constraints.gridwidth = GridBagConstraints.REMAINDER;
        addComponent( buttons [4] );
    }  // end of GridBag Constructor

    // Add A Component to the Container
    private void addComponent (Component component)
    {
        calcLayout.setConstraints( component, constraints );
        add (component);
    }
    [i][b]public void actionPerformed (ActionEvent ae)
    {
        for ( int count = 0; count < 5; count++)
        {
            if (ae.getSource() == buttons[count])
            {
                txtDisplay.setText(btnNames[count]);
            }
        }
   // JOptionPane.showMessage(displayresults);[/b]\[/i]
    }

    public static void main (String args [] )
    {
        CalculatorGUI1 gridBagFrame = new CalculatorGUI1();
        gridBagFrame.setDefaultCloseOperation ( JFrame.EXIT_ON_CLOSE);
        gridBagFrame.setSize( 400 , 300);
        gridBagFrame.setVisible( true );
    }// end main

    }


This is my Calculator Class: [u]
public abstract class Calculator
{
    public Calculator ()
    {
        double results = 0;
    }

    public void setResults (double results)
    {

    }

    public double getResults()
    {
        double results = 0;
        return results;
    }


    public void setAdd( double x, double y,double Add)
    {
        Add = x + y;
    }
    
    public double getAdd(double Add,double results)
    {
        return results;
    }
      
    
    public interface Subtract
    {
        double subtract( double x, double y);
        double getSubtract();
    }
    public static double Subtract(double x, double y)
    {
        double subtract;
        subtract = x - y;
        return subtract;    
    }

    public interface Multiply
    {
        double multiply (double x, double y);
        double getMultiply();
    }
    public static double Multiply(double x, double y)
    {
        double multiply;
        multiply = x * y;
        return multiply;
    }
    
    
    public interface Divide
    {
        double divide (double x, double y);
        double getDivide();
    }
    public static double Divide(double x, double y)
    {
        double divide;
        divide = x / y;
        return divide;
    }
        
    public String toString(String results)
    {
      return results;
    }
    
    
    
  
}

Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8334
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Java - Calculator Program using Interface & GUI

Posted 08 June 2009 - 06:35 PM

View PostTanira, on 8 Jun, 2009 - 09:46 AM, said:

The calculation could be done in a method called by the buttonPressed.

Just
if(buttonPressed.getText() == "+"){
}


surely not... you are going into problems

if(buttonPressed.getText().equals("+")){

may be :)

anyway a lot more efficient with event.,getSource()
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1