Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7572629
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T15:58:23+00:00 2026-05-30T15:58:23+00:00

There are 4 classes: Main , GUI , Logic and Calculator . I’m having

  • 0

There are 4 classes: Main, GUI, Logic and Calculator. I’m having difficulty making the Calculator class right, the first operation/calculation is giving me the wrong answer but the next one is right. I tried to create another variable (fnum and snum) for the first and second number but it didn’t work. Do I need to modify the Logic class?

main class

class Main
{ 
  public static void main( String args[] )
  {
    GUI gui = new GUI();
    gui.display();
  }
}

GUI class

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

class GUI
{
  private static final int H = 400;       // Height of window pixels
  private static final int W = 500;       // Width  of window pixels

  private JLabel      theMes      = new JLabel();       // Message area
  private JTextArea   theOutput1  = new JTextArea();    // Input number area
  private JTextArea   theOutput2  = new JTextArea();    // Result area
  private JScrollPane theSP       = new JScrollPane();

  private ButtonPress onButtonPress = new ButtonPress();

  public void display()
  {
    JFrame rpc           = new JFrame();            // Window
    Container cp         = rpc.getContentPane();    // Content Pane
    Container rootWindow = (Container) rpc;         // Root Window
    cp.setLayout(null);                             // No layout manager
    rootWindow.setSize( W, H );                     // Size of Window

    String labels[] = {
        "7",  "8", "9", "", "CR", "+",
        "4",  "5", "6", "",  "=", "-",
        "1",  "2", "3", "",  "",  "*",
        "C",  "0", "", "",   "",  "/" };

    final int LABELS = labels.length;      // # Button Labels

    final int GAP   = 15;                  // Horizontal Gap

    final int HLAB  = 20;                  // Label
    final int SHLAB = GAP;
    final int EHLAB = SHLAB+HLAB;

    final int HTA1  = 30;                  // Output area 1
    final int SHTA1 = EHLAB + GAP;
    final int EHTA1 = SHTA1 + HTA1;

    final int HTA2  = 100;                 // Output area 2
    final int SHTA2 = EHTA1 + GAP;
    final int EHTA2 = SHTA2 + HTA2;

    final int HBUT  = 170;                 // Buttons
    final int SHBUT = EHTA2 + GAP;

    final int BNR  = 4,    BNC  = 6;       // number rows cols of buttons
    final int BW  = W/BNC, BH = HBUT/BNR;  // Size of landscape for button
    final int SBH = SHBUT, SBW = 7;        // Start position for buttons


    JButton buttons[] = new JButton[LABELS];

    Font font = new Font("Serif",Font.BOLD,20);      // Button font

    for ( int i=0; i<LABELS; i++ )
    {
      if ( labels[i].length() >= 1 )
      {
        buttons[i] = new JButton( labels[i] );
        final int col = i%BNC * BW, row = i/BNC * BH;
        buttons[i].setBounds( SBW+col, SBH+row, BW-20, BH-10 );
        buttons[i].addActionListener( onButtonPress );
        buttons[i].setFont( font );
        cp.add( buttons[i] );
      }
    }

    font = new Font("Serif",Font.BOLD,14);         // Font is

    theMes.setBounds( 10, SHLAB, W-25, HLAB );     // Message area
    theMes.setText( "" );                          // Blank
    theOutput1.setFont( font );                    //  Uses font 
    cp.add( theMes );                              //  Add to canvas

    theOutput1.setBounds( 10, SHTA1, W-25, HTA1 ); // Input Area
    theOutput1.setText("");                        // Blank
    theOutput1.setFont( font );                    //  Uses font 
    cp.add( theOutput1 );                          //  Add to canvas

    font = new Font("Serif",Font.BOLD,14);         // Font is

    theSP.setBounds( 10, SHTA2, W-25, HTA2 );      // Scrolling pane
    theOutput2.setText( "" );                      //  Blank
    theOutput2.setFont( font );                    //  Uses font  
    cp.add( theSP );                               //  Add to canvas
    theSP.getViewport().add( theOutput2 );         //  In TextArea
    rootWindow.setVisible( true );                 // Make visible

    theMes.setText( "Calculator" );                // Opening message
  }


  private Calculator calc = new Calculator();
  private Logic logic     = new Logic( calc );

  class ButtonPress implements ActionListener       // Listener
  {
    public void actionPerformed( ActionEvent ae )   // Interaction
    {
      String label = ae.getActionCommand();         // Button label  

      String info = logic.process( label);

      theOutput2.setText( "" );
      if ( info == null )
        theOutput2.append( "" + logic.getResult() );
      else
        theOutput2.append( info );

      theOutput1.setText( "" );
      theOutput1.append( "Number entered: " + logic.getNumber() );
    }
  }
}

Logic class

class Logic
{
  private enum  State { FIRST_NUMBER, SUBSEQUENT_NUMBER };
  private State state = State.FIRST_NUMBER;
  private long  number = 0;
  private char  op = ' ';
  private Calculator calc = null;


  public Logic( Calculator calculator )
  {
    calc = calculator;
  }

  public String process( String button )
  {
    String info = null;
    if ( button.length() == 1 )
    {
      char c = button.charAt(0);
      if ( c >= '0' && c <= '9' )               // Digit
      {
        number = number * 10 + c-'0';           // Build number
      } else {
        switch ( c )
        {
          case 'C' : number = 0;
                     break;
          case '=' :
          case '+' : case '-' :
          case '*' : case '/' :
            switch ( state )
            {
              case FIRST_NUMBER:
                calc.setValue( number );
                state = State.SUBSEQUENT_NUMBER;
                break;
              case SUBSEQUENT_NUMBER:
                if ( op != '=' )
                  calc.evaluate( op, number );
                break;
            }
            op = c;  number = 0;
            break;
        }
      }
    } else {
      if ( button.equals( "CR" ) )               // Clear Result
      {
        calc.reset(); number = 0; state = State.FIRST_NUMBER;
      }
    }

    return info;
  }

  public long getResult()
  {
    return calc.getValue();
  }

  public long getNumber()
  {
    return number;
  }
}

Calculator class

class Calculator
{
//Evaluate an arithmetic operation on the stored result
// E.g evaluate( '+',9) would add 9 to the stored result
// evaluate( '/',3) would divide the stored result
// by 3
// actions are '+'. '-', '*', '/'
//Note: if the operation is
// evaluate( '/',0 ) the result returned should be 0


long value;


public void evaluate( char action, long number )
 {

     if (action == '+'){
      value += Float.valueOf(number).floatValue();


     }

    else if (action == '-'){
      value -= Float.valueOf(number).floatValue();

    }

    else if (action == '*'){
      value *= Float.valueOf(number).floatValue();

    }

    else if (action == '/'){
      value /= Float.valueOf(number).floatValue();

    }

}
//Return the stored result
public long getValue()
   {
    return value;
    }

//Set the stored result to number
public void setValue( long number )
{

}

//Reset the stored number to 0
public void reset()
{
       if ( value != 0) value = 0;
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T15:58:25+00:00Added an answer on May 30, 2026 at 3:58 pm

    the problem is that you dont actually set the value in the setValue method:

    //Set the stored result to number
    public void setValue( long number )
    {
        this.value = number;
    }
    

    That should fix it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

There are many classes having this provider suffix. (Data,membership,modelmetadata,...). When should be a class
So for my current project, there are basically three main Java classes: GUI Instant
I have three classes, one main class, one GUI class which uses awt+swing to
I have those to classes: the main and GUI. In GUI there is actionListener
Are there any dictionary classes in the .NET base class library which allow duplicate
I've a DLL assembly, in which there are various classes. Each class has around
I'm making a game right now and pretty much everything has its own class.
We know that in Objective-C there are two main root classes: NSObject and NSProxy
I am having a class which uses the XmlReader and XmlReaderSettings classes in C#
I have a GUI application. In my main class I have a method (called

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.