Welcome to Dream.In.Code
Getting Java Help is Easy!

Join 109,295 Java Programmers for FREE! Ask your question and get quick answers from experts. There are 1,194 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!



Simple calculator

 
Reply to this topicStart new topic

Simple calculator, a simeple basic java calculator

qaz1134
post 27 Jun, 2008 - 03:38 AM
Post #1


New D.I.C Head

*
Joined: 21 May, 2008
Posts: 48


My Contributions


hello guys can anyone thst is soo good and soo nice that could help me with this!!!
actually the code is kinda correct but my problem is : how can i insert or add someting to make it complex

logically
the program is like this :::: 1+1 = 2

i need to make it like this :::: 1+2-1*1=2 or 1/1+1=2 somthimg like this

the program goes this way:::::

CODE

class Calculator{
    int answer;

    void addition (int a,int b) {
        answer a + b;
}
    int multiplication (int a,int b) {
        answer a * b;
}
    int subtraction (int a,int b) {
        answer a - b;
}
    int division (int a,int b) {
        answer a / b;
}

void Showanswer() {

    System.out.println("The answer is:" + answer);
}

    public static void main(String[] args) {

    Calculator calcu;
    calcu = new Calculator();
    calcu.addition(4,3);
    calcu.Showanswer();
    calcu.subtraction(4,3);
    calcu.Showanswer();
    calcu.multiplication(4,3);
    calcu.Showanswer();
    calcu.division(4,3);
    calcu.Showanswer();
    }
}



please help smile.gif
User is offlineProfile CardPM

Go to the top of the page


nick2price
post 27 Jun, 2008 - 09:13 AM
Post #2


D.I.C Head

**
Joined: 23 Nov, 2007
Posts: 130



Thanked 3 times
My Contributions


Have you checked this code out? Before you go onto somthing a bit more complex, you might want to take a look at what you have done.
CODE
void addition (int a,int b) {
        answer a + b;

Wont return anything the way you are printing out
CODE
System.out.println("The answer is:" + answer);


Also
CODE
    int multiplication (int a,int b) {
        answer a * b;
}
    int subtraction (int a,int b) {
        answer a - b;
}
    int division (int a,int b) {
        answer a / b;
}


with these methods you need to return somthing, and you are missing an = sign.
CODE
    int division (int a,int b) {
        return answer = a / b;
}
User is offlineProfile CardPM

Go to the top of the page

qaz1134
post 29 Jun, 2008 - 07:36 AM
Post #3


New D.I.C Head

*
Joined: 21 May, 2008
Posts: 48


My Contributions


QUOTE(nick2price @ 27 Jun, 2008 - 09:13 AM) *

Have you checked this code out? Before you go onto somthing a bit more complex, you might want to take a look at what you have done.
CODE
void addition (int a,int b) {
        answer a + b;

Wont return anything the way you are printing out
CODE
System.out.println("The answer is:" + answer);


Also
CODE
    int multiplication (int a,int b) {
        answer a * b;
}
    int subtraction (int a,int b) {
        answer a - b;
}
    int division (int a,int b) {
        answer a / b;
}


with these methods you need to return somthing, and you are missing an = sign.
CODE
    int division (int a,int b) {
        return answer = a / b;
}






yup iam very sorry for that.. i just have type in the code and iam kindda in a hurry o0n that time

CODE
class Calculator{
    int answer;

    void addition (int a,int b) {
        answer = a + b;
}
    void multiplication (int a,int b) {
        answer = a * b;
}
    void subtraction (int a,int b) {
        answer = a - b;
}
    void division (int a,int b) {
        answer = a / b;
}

void Showanswer() {

    System.out.println("The answer is:" + answer);
}

    public static void main(String[] args) {

    Calculator calcu;
    calcu = new Calculator();
    calcu.addition(4,3);
    calcu.Showanswer();
    calcu.subtraction(4,3);
    calcu.Showanswer();
    calcu.multiplication(4,3);
    calcu.Showanswer();
    calcu.division(4,3);
    calcu.Showanswer();
    }
}




so sorry i dont mean to return anything i just have put void to be more simpler

tnx biggrin.gif
User is offlineProfile CardPM

Go to the top of the page

RoboAlex
post 29 Jun, 2008 - 08:51 AM
Post #4


New D.I.C Head

*
Joined: 26 Jun, 2008
Posts: 39



Thanked 3 times
My Contributions


Making the methods return int will actually make everything much easier. You won't need need the variable answer anymore, and you could print the answers out like this:
System.out.println(calcu.addition(3,4));
all in one line, nice and neat.
Not to mention that it's the proper way to do it because it lets you actually DO something with your result.

So what was it you were trying to do with making the logic complicated?

This post has been edited by RoboAlex: 29 Jun, 2008 - 08:52 AM
User is offlineProfile CardPM

Go to the top of the page

qaz1134
post 1 Jul, 2008 - 10:41 AM
Post #5


New D.I.C Head

*
Joined: 21 May, 2008
Posts: 48


My Contributions


so can anyone help me how to????
User is offlineProfile CardPM

Go to the top of the page

gl3thr0
post 1 Jul, 2008 - 11:16 AM
Post #6


D.I.C Head

**
Joined: 27 Oct, 2007
Posts: 177


My Contributions


im sorry but before we provide u with any code u have to at least make an effort.. yes u have very simple functions that can be used to +,-,X,/ but you have made no effort to try and make the program even slightly complex.
first you would have to write a function to take user input like 1+2,3+4 and so on. after u get it working like that try and expand it 2 take in more complex expressions...
we are here to help u so post any errors u recieve while working on it.
User is offlineProfile CardPM

Go to the top of the page

DillonSalsman
post 1 Jul, 2008 - 04:36 PM
Post #7


D.I.C Head

**
Joined: 30 Oct, 2007
Posts: 55


My Contributions


http://www.myflex.org/books/JavaKid8x11.pdf
This guide helped me amazingly in Java developement and just so happens to have lesson on not just making a calculator but a graphic (not graphiNG) calculator. With like buttons and what not.
: ) give it a peek
User is offlineProfile CardPM

Go to the top of the page

qaz1134
post 1 Jul, 2008 - 11:51 PM
Post #8


New D.I.C Head

*
Joined: 21 May, 2008
Posts: 48


My Contributions


so can you help me put another function???? to make it multi functional??? please

User is offlineProfile CardPM

Go to the top of the page

gl3thr0
post 2 Jul, 2008 - 01:45 AM
Post #9


D.I.C Head

**
Joined: 27 Oct, 2007
Posts: 177


My Contributions


QUOTE(qaz1134 @ 1 Jul, 2008 - 11:51 PM) *

so can you help me put another function???? to make it multi functional??? please



yes but show us ur attempt at making it "multi functional" just show us what u've got
but basicly ur going to have to first take user input which can be done with "java.util.Scanner" or "java.io.Bufferedreader"and save it asa string
then u will need to be able to take the numbers and symbols out of the that string and perfrom operations on them with ur methods.. so id advise u to first get it working for expressions like 1+2 and 2-4. be4 making it more powerful.

take it in baby steps and if u cant get it show us what u have and we'll help fix it
User is offlineProfile CardPM

Go to the top of the page

qaz1134
post 10 Jul, 2008 - 09:35 AM
Post #10


New D.I.C Head

*
Joined: 21 May, 2008
Posts: 48


My Contributions


QUOTE(gl3thr0 @ 2 Jul, 2008 - 01:45 AM) *

QUOTE(qaz1134 @ 1 Jul, 2008 - 11:51 PM) *

so can you help me put another function???? to make it multi functional??? please



yes but show us ur attempt at making it "multi functional" just show us what u've got
but basicly ur going to have to first take user input which can be done with "java.util.Scanner" or "java.io.Bufferedreader"and save it asa string
then u will need to be able to take the numbers and symbols out of the that string and perfrom operations on them with ur methods.. so id advise u to first get it working for expressions like 1+2 and 2-4. be4 making it more powerful.

take it in baby steps and if u cant get it show us what u have and we'll help fix it






thankyou very much i really i jus need something like that...
its ok i dont you to type the correct code for me i just want some advice or suggestion ....
cause i dnt have any idea where iam going...


thanks 4 the help ...

User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 9/6/08 09:13AM

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month