Welcome to Dream.In.Code
Become a Java Expert!

Join 150,064 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,775 people online right now. Registration is fast and FREE... Join Now!




What do these variables do?

 
Reply to this topicStart new topic

What do these variables do?, Please clarify

jakkels
8 Jun, 2008 - 03:29 AM
Post #1

New D.I.C Head
*

Joined: 21 May, 2008
Posts: 21


My Contributions
ok, so I've been working through this code a hundred times. Some of it has been given to us by our lecturer.

I'm trying to make sense of it and have a few questions before I can move forward.

This is a calculator engine.
CODE


//VERSION 0.3 with Calculator 0.3, 8-June-2008
public class Engine
{
    private int acc = 0;
    private String op = "=";
    private boolean start = true;

    public Engine()
    {
    }

    public void start()
    {
        start = true;
    }

    public void stop()
    {
        start = false;
    }

    public boolean started()
    {
        return start;
    }

    public void operation( String op )
    {
        this.op = op;
    }

    public int calculate( int n )
    {
        if ( op.equals( "+" ) )
            acc += n;
        else if ( op.equals( "-" ) )
            acc -= n;
        else if ( op.equals( "*" ) )
            acc *= n;
        else if ( op.equals( "/" ) )
            acc /= n;
        else if ( op.equals( "%" ) )
            acc %= n;
        else if ( op.equals( "=" ) )
            acc = n;
      //else if ( op.equals( "CE" ) )
      //   acc =
      else if ( op.equals( "CA" ) )
         acc = acc;
      //else if ( op.equals( "M+" ) )
      //   acc =
      //else if ( op.equals( "MC" ) )
      //   acc =
      //else if ( op.equals( "MRC" ) )
      //   acc =

        return acc;
    }
}



Is acc to store the current value in it and then n used to store the next value? And op - is used for when the "=" sign is pressed to get the answer.

This may sound like a bit of a dumb question sorry. Can someone summarise. I know this must be basic stuff to some of you.

User is offlineProfile CardPM
+Quote Post

mensahero
RE: What Do These Variables Do?
8 Jun, 2008 - 03:48 AM
Post #2

c0mput3rz Are Only Human
Group Icon

Joined: 26 May, 2008
Posts: 664



Thanked: 17 times
Dream Kudos: 75
My Contributions
I'm assuming thats not the only code.. because if it is.. it doesn't make any sense.. for me though .. im n00b.. blink.gif

the way I see it..

"acc" holds the value 0 ..

CODE

private int acc = 0;


"op" holds the string value of the operation.. ( -, +, =, *, etc )

"n" is the parameter passed to the calculate method..

so basically;

op = "+";
int answer = calculate( 2 );

the answer would be 2; n ( which is 2 ) added to acc ( which is 0 );

blink.gif

Mod Edit: Please (don't take this the wrong way please) there is no need to quote the entire post when replying. It just makes it looks cluttered and more for readers to read through smile.gif

This post has been edited by PsychoCoder: 8 Jun, 2008 - 07:36 AM
User is offlineProfile CardPM
+Quote Post

jakkels
RE: What Do These Variables Do?
8 Jun, 2008 - 07:33 AM
Post #3

New D.I.C Head
*

Joined: 21 May, 2008
Posts: 21


My Contributions
Yes, yes this is not the only code. My calculator is in a separate class. wink2.gif

Trying to get my head round the terminology. Methods and parameters and so on. I guess that comes with time.

Thanks.

User is offlineProfile CardPM
+Quote Post

mensahero
RE: What Do These Variables Do?
8 Jun, 2008 - 08:01 AM
Post #4

c0mput3rz Are Only Human
Group Icon

Joined: 26 May, 2008
Posts: 664



Thanked: 17 times
Dream Kudos: 75
My Contributions
No Problemo.. staysafe.. blink.gif
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/8/09 10:48PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month