Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 04-16-2008, 01:46 AM
Member
 
Join Date: Feb 2008
Posts: 5
cart1443 is on a distinguished road
Java calculator decimal
I need to have my calculator be able to work with decimals. I've changed all the pertinent numbers to be doubles, but I can't figure out the code to make things fit together. As of now I have it formatted to work the same way as the calculations (+-*/...) but this is backwards of what I need it to do. I understand the general idea that it needs to divide the number by 10 to the power of how many digits it is away from the decimal. So, for instance. .001 would be 1/(10^3). I understand the concept, it's just the code I am having problems with. Any help would be appreciated.

What I have thus far is a little way down the code under "public void decimal()".

PHP Code:
import java.math.*;
import java.util.Scanner;
/**
 * The main part of the calculator doing the calculations.
 */

//Ok this is what I have so far.. Basically I have just finished the plus operation on the calculator, and tried to fix as many problems as I could find, let me know if you find any other errors in the calculator
//One person can help out gettin the Strings to display, and the other person can do the minus operation.. Ill try to finish the divide and *(times)..  I hope this is ok..
public class CalcEngine
{
   
double displayValue// this is what is displayed on the screen
   
double store// Used to store values, that later will be added etc...
   
int identify= -1;// this variable will be used to let the calculator know whether it should add or subtract etc..
   
int keep// This variable is used that when the user adds etc.. the initial value can stay on the screen.
   
int clear// this variable is used, so that after the calculator computes an equation, it will clear the screen if another number is pushed instead of clear..
    /**
     * Create a CalcEngine instance. Initialize its state so that it is ready 
     * for use.
     */
    
public CalcEngine()// starts off 
    
{
    
// sets values to these at the start
   
displayValue0;
   
store0;
    
identify = -1;
    
clear = -1;
    
keep = -1;
    }

    
/**
     * Return the value that should currently be displayed on the calculator
     * display.
     */
    
public double getDisplayValue()
    {
return 
displayValue;// returns the display value if this method is called.....
    
}

   
     
/**
     * A number button was pressed. Do whatever you have to do to handle it.
     * The number value of the button is given as a parameter.
     */
    
public void numberPressed(int number)// if a number is pressed then...
    
{
     if (
keep == 0)// if keep is zero when a button is pressed, it will now reset the display value to zero, so that their new value will show on the calculator
     // Example say 2+2, if keep was not zero when you tried to put the second 2, the first one would still be displayed and it would look like this 2+22, not 2+2.
     //Go ahead and cut these if statments out of the program and run it, to see what problems they fix, then you can paste them back in...
     
{
   
    
displayValue0;
     
keep = -1;
    }
        if (
clear == 0)// First it checks to see if clear is -1 if it is that means that the users last input was an equals, This makes it so that if the user does 2 + 2 = 4, and then decides to type in new numbers it will reset all the values for the user whithout having to hit clear...
     
{
   
    
displayValue0;
     
identify = -1;
     
clear = -1;
    }
    
displayValue displayValue *10 number;// makes it so you can input multi digit values.
   
   
    
}

      
/**
     * The 'plus' button was pressed. 
     */
    
public void decimal()
    {
    if(
identify == 0)// this is so that if two numbers are being added before the equals sign is pushed,it will automatically add them, display the value, and reset store..
        
{
         
displayValue store displayValue;
         
store 0;
        }
        if(
identify == 1)
        {
         
displayValue store displayValue;
         
store 0;
        }
        if(
identify == 2)// this is so that if two  numbers are being *'ed before the equals sign is pushed,it will automatically times them, display the value, and reset store..
        
{
         
displayValue store displayValue;
         
store 0;
        }
        if(
identify == 3)// this is so that if two  numbers are being /ed before the equals sign is pushed,it will automatically divide them, display the value, and reset store..
        
{
         
displayValue store displayValue;
         
store 0;
        }
        if(
identify == 4)// this is so that if two  numbers are being /ed before the equals sign is pushed,it will automatically divide them, display the value, and reset store..
        
{
         
displayValue displayValue 10;
         
store 0;
        }
        if(
keep == 0)// if this is zero it means that plus was already pressed and was the last thing pressed
        
{
         
displayValue displayValue;//This if statement makes it so that if the user presses the plus button a bunch of times in a row, it wont do any calculations, it will just keep the display value the same...
        
}
        else
        {
       
clear = -1;// this is needed to set clear to -1 in case the plus button is hit after the equals button, now it wont reset the values once a number is pressed
       // example 2+2=4+4=8 it will now give us 8 correctly instead of an incorrect value 4...
     
     
displayValue displayValue 10;//
     
      
keep 0;// sets keep to zero,this makes it so we can keep their input on the screen for them to see until another button is pressed...
      
identify 0// sets identify to zero this lets the equals sign know we want to add....
     
    
}
    }
    
    public 
void plus()// plus button is pressed
    
{
        if(
identify == 0)// this is so that if two numbers are being added before the equals sign is pushed,it will automatically add them, display the value, and reset store..
        
{
         
displayValue store displayValue;
         
store 0;
        }
        if(
identify == 1)
        {
         
displayValue store displayValue;
         
store 0;
        }
        if(
identify == 2)// this is so that if two  numbers are being *'ed before the equals sign is pushed,it will automatically times them, display the value, and reset store..
        
{
         
displayValue store displayValue;
         
store 0;
        }
        if(
identify == 3)// this is so that if two  numbers are being /ed before the equals sign is pushed,it will automatically divide them, display the value, and reset store..
        
{
         
displayValue store displayValue;
         
store 0;
        }
        if(
identify == 4)// this is so that if two  numbers are being /ed before the equals sign is pushed,it will automatically divide them, display the value, and reset store..
        
{
         
displayValue displayValue 10;
         
store 0;
        }
        if(
keep == 0)// if this is zero it means that plus was already pressed and was the last thing pressed
        
{
         
displayValue displayValue;//This if statement makes it so that if the user presses the plus button a bunch of times in a row, it wont do any calculations, it will just keep the display value the same...
        
}
        else
        {
       
clear = -1;// this is needed to set clear to -1 in case the plus button is hit after the equals button, now it wont reset the values once a number is pressed
       // example 2+2=4+4=8 it will now give us 8 correctly instead of an incorrect value 4...
     
store =store displayValue;//this will take the display value and add it to the store value and set it to store..
    
     
      
keep 0;// sets keep to zero,this makes it so we can keep their input on the screen for them to see until another button is pressed...
      
identify 0// sets identify to zero this lets the equals sign know we want to add....
     
    
}
    }
    
/**
     * The 'minus' button was pressed.
     */
public void minus()// ONE Person could try to do the Minus 
    
{
        if(
identify == 0)// this is so that if two numbers are being added before the equals sign is pushed,it will automatically add them, display the value, and reset store..
        
{
         
displayValue store displayValue;
         
store 0;
        }
        if(
identify == 1)
        {
         
displayValue store displayValue;
         
store 0;
        }
        if(
identify == 2)// this is so that if two  numbers are being *'ed before the equals sign is pushed,it will automatically times them, display the value, and reset store..
        
{
         
displayValue store displayValue;
         
store 0;
        }
        if(
identify == 3)// this is so that if two  numbers are being /ed before the equals sign is pushed,it will automatically divide them, display the value, and reset store..
        
{
         
displayValue store displayValue;
         
store 0;
        }
        if(
identify == 4)// this is so that if two  numbers are being /ed before the equals sign is pushed,it will automatically divide them, display the value, and reset store..
        
{
         
displayValue displayValue 10;
         
store 0;
        }
        if(
keep == 0)// if this is zero it means that plus was already pressed and was the last thing pressed
        
{
         
displayValue displayValue;//This if statement makes it so that if the user presses the plus button a bunch of times in a row, it wont do any calculations, it will just keep the display value the same...
        
}
        else
        {
       
clear = -1;// this is needed to set clear to -1 in case the plus button is hit after the equals button, now it wont reset the values once a number is pressed
       // example 2+2=4+4=8 it will now give us 8 correctly instead of an incorrect value 4...
     
store displayValue store;//this will take the display value and add it to the store value and set it to store..
    
     
      
keep 0;// sets keep to zero,this makes it so we can keep their input on the screen for them to see until another button is pressed...
      
identify 1// sets identify to zero this lets the equals sign know we want to add....
     
    
}
    }
    
    public 
void product()//  and the *
    
{
        if(
identify == 4)// this is so that if two  numbers are being /ed before the equals sign is pushed,it will automatically divide them, display the value, and reset store..
        
{
         
displayValue displayValue 10;
         
store 0;
        }
        if(
identify == 3)// this is so that if two  numbers are being /ed before the equals sign is pushed,it will automatically divide them, display the value, and reset store..
        
{
         
displayValue store displayValue;
         
store 0;
        }
         if(
identify == 1)
        {
         
displayValue store displayValue;
         
store 0;
        }
        if(
identify == 2)// this is so that if two numbers are being *'d before the equals sign is pushed,it will automatically times them, display the value, and reset store..
        
{
         
displayValue store displayValue;
         
store 0;
        }
        if(
identify == 0)// this is so that if two numbers are being added before the equals sign is pushed,it will automatically add them, display the value, and reset store..
        
{
         
displayValue store displayValue;
         
store 0;
        }
        if(
keep == 0)// if this is zero it means that plus was already pressed and was the last thing pressed
        
{
         
displayValue displayValue;//This if statement makes it so that if the user presses the plus button a bunch of times in a row, it wont do any calculations, it will just keep the display value the same...
        
}
        else
        {
       
clear = -1;
      if(
store == 0)// if store is at zero then it will just add it to the display value, that way we dont come up with a zero by timesing it...
      
{
       
store =store displayValue;//this will take the display value and add it to the store value and set it to store..
      
}
     else
     {
       
store store displayValue//if store is not zero then we can times it no problem. 
        
}
      
keep 0;// sets keep to zero,this makes it so we can keep their input on the screen for them to see until another button is pressed...
      
identify 2// sets identify to two this lets the equals sign know we want to times....
      
    
}
    }
    
    public 
void divide()//  divide...
    
{
       if(
identify == 4)// this is so that if two  numbers are being /ed before the equals sign is pushed,it will automatically divide them, display the value, and reset store..
        
{
         
displayValue displayValue 10;
         
store 0;
        }
        if(
identify == 3)
        {
          
displayValue store displayValue;  
        }
         if(
identify == 2)// this is so that if two numbers are being *'d before the equals sign is pushed,it will automatically times them, display the value, and reset store..
        
{
         
displayValue store displayValue;
         
store 0;
        }
         if(
identify == 1)
        {
         
displayValue store displayValue;
         
store 0;
        }
        if(
identify == 0)// this is so that if two numbers are being added before the equals sign is pushed,it will automatically add them, display the value, and reset store..
        
{
         
displayValue store displayValue;
         
store 0;
        }
        if(
keep == 0)// if this is zero it means that plus was already pressed and was the last thing pressed
        
{
         
displayValue displayValue;//This if statement makes it so that if the user presses the plus button a bunch of times in a row, it wont do any calculations, it will just keep the display value the same...
        
}
        else
        {
       
clear = -1;
      if(
store == 0)// if store is at zero then it will just add it to the display value, that way we dont come up with a zero..
      
{
       
store =store displayValue;//this will take the display value and add it to the store value and set it to store..
      
}
     else
     {
       
store store displayValue//if store is not zero then we can divide no problem. 
        
}
      
keep 0;// sets keep to zero,this makes it so we can keep their input on the screen for them to see until another button is pressed...
      
identify 3// sets identify to two this lets the equals sign know we want to times....
    
}
}
    
/**
     * The '=' button was pressed.
     */
    
public void equals()
    {
        if(
identify == 0)// if Identify is zero,(meaning the plus button was pressed) we want the calculator to add the two values they inputed..
        
{
        
displayValue displayValue store;// add those values and displays the new awnser..
        
}
        if(
identify == 1)
        {
         
displayValue store displayValue;
         
store 0;
        }
        if(
identify == 2)
        {
            
displayValue = (displayValue store);
        }
         if(
identify == 3)
        {
            
displayValue = (store displayValue);
        }
        if(
identify == 4)// this is so that if two  numbers are being /ed before the equals sign is pushed,it will automatically divide them, display the value, and reset store..
        
{
         
displayValue displayValue 10;
         
store 0;
        }
        
store=0;//sets store to zero, because we are done with that value now..
         
clear 0//sets clear to zero, so that if the user inputs new numbers it will clear the previous entry...
        
identify = -1;// its done adding so we want this set back to -1...
    
}

    
/**
     * The 'C' (clear) button was pressed.
     */
    
public void clear()
    {
     
store 0;// clears everything.. 
     
displayValue0;
     
identify = -1;
     
clear = -1;
     
keep = -1;
    }

    
/**
     * Return the title of this calculation engine.
     */
    //ONE PERSON COULD WORK ON THE These returning the strings....  Im not sure how we are supposed to do them.....  IF we need to make buttons for returning the strings or what.....
    //Or it looks like from the other code, these may be displayed apon opening the program.. or the ? mark button...
    
public String getTitle()
    {
        return 
"Calculator";
    }

    
/**
     * Return the author of this engine. This string is displayed as it is,
     * so it should say something like "Written by H. Simpson".
     */
    
public String getAuthor()
    {
        return 
"Groucho, Harpo, Chico" ;
    }

    
/**
     * Return the version number of this engine. This string is displayed as 
     * it is, so it should say something like "Version 1.1".
     */
    
public String getVersion()
    {
        return 
"Version 1.0";
    }

Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 04-16-2008, 02:13 PM
sukatoa's Avatar
Senior Member
 
Join Date: Jan 2008
Location: Cebu City, Philippines
Posts: 518
sukatoa is on a distinguished road
Send a message via Yahoo to sukatoa
can you post an example input and the expected correct output?

regards,
sukatoa
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 04-16-2008, 02:19 PM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 2,875
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
And also, when you post a code next time please use tags. And also used multiple lines for comment as much as possible. It's really difficult to read your thread.

It can be caused to people avoid replaying to your question at all
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Has someone helped you? Then you can
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
their helpful post.

Want to make your IDE the best?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
(Close on September 4, 2008)

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Java Calculator aapanju New To Java 3 04-17-2008 06:33 AM
calculator not working Renegade85 New To Java 5 03-10-2008 04:27 PM
Capping decimal places Rageagainst20 New To Java 1 12-20-2007 10:28 PM
Create a Calculator in Java Albert New To Java 2 07-04-2007 09:01 AM
Swing Calculator nemo AWT / Swing 1 05-28-2007 12:07 PM


All times are GMT +3. The time now is 02:46 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org