So basically I have to write a simple calculator, that fine and I did it. The only problem is it can only operate on the 2 numbers which the user puts in. I want to make it like a real calculator that you carry around in your pocket.

Example:

"Calculate: "

User enters: 1+1

"The result is 2!"

"User enters again only 1 number, 2."

"The result is 4!"

The user keeps doing this until he wants to quit the program or he can clear the result and start again.

I've written a calculator for hexadecimal numbers(it converts them to decimal, calculate then converts them back). The user can ADD, SUBTRACT, MULTIPLY, DIVIDE, SQUARE and find the SQUARE ROOT of the numbers.

Here is the code written so far:

MAIN

``````package v6;

import java.util.Scanner;
import java.io.*;

public class PrviRazred {

public static void main(String[] args) throws IOException {

int a;
int b;
int x = 0;
int izbira;

Scanner vhod = new Scanner(System.in);
DrugiRazred Kalkulator = new DrugiRazred();

System.out.print("Vnesi šestnajstiško število: ");
if(Kalkulator.isHex(str)==false) {
System.out.println("Število ni šestnajstiško!");
System.exit(0);
}
a=Integer.parseInt(str,16); //pretvori hex število v decimalno za lažje računanje

System.out.print("Vnesi šestnajstiško število: ");
if(Kalkulator.isHex(str)==false) {
System.out.println("Število ni šestnajstiško!");
System.exit(0);
}
b=Integer.parseInt(str2,16);

while(x==0) {

System.out.print("Izbira: ");
izbira=vhod.nextInt();
System.out.println();

switch (izbira) {

//SEŠTEVANJE
case 1:
Kalkulator.vsota(a, b);
break;

//ODŠTEVANJE
case 2:
Kalkulator.razlika(a, b);
break;

//MNOŽENJE
case 3:
Kalkulator.zmnozek(a, b);
break;

//DELJENJE
case 4:
Kalkulator.kolicnik(a, b);
break;

case 5:
break;

//KORENJENJE
case 6:
Kalkulator.koren(a);
break;

//VNOS NOVIH ŠTEVIL
case 7:
System.out.print("Vnesi šestnajstiško število: ");
if(Kalkulator.isHex(str)==false) {
System.out.println("Število ni šestnajstiško!");
System.exit(0);
}
a=Integer.parseInt(str,16);

System.out.print("Vnesi šestnajstiško število: ");
if(Kalkulator.isHex(str)==false) {
System.out.println("Število ni šestnajstiško!");
System.exit(0);
}
b=Integer.parseInt(str2,16);
break;

case 8:
x=1;
break;

default:;
break;
}
}
}
}``````

CALCULATOR

``````package v6;

import java.util.Scanner;
import java.io.*;

public class DrugiRazred {

Scanner vhod = new Scanner(System.in);

System.out.println("***** KALKULATOR *****");
System.out.println("'1' - Seštevanje ");
System.out.println("'2' - Odštevanje ");
System.out.println("'3' - Množenje ");
System.out.println("'4' - Deljenje ");
System.out.println("'6' - Korenjenje ");
System.out.println("'7' - Ponastavitev števil ");
System.out.println();
System.out.println("'8' - Izhod ");
System.out.println("'*********************");

}

public void vsota(int a, int b) {
int rezultat;
rezultat = a + b;
System.out.println("Rezultat seštevanja je: " + Integer.toHexString(rezultat));
}

public void razlika(int a, int b) {
int rezultat;
rezultat = a - b;
System.out.println("Rezultat odštevanja je: " + Integer.toHexString(rezultat));
}

public void zmnozek(int a, int b) {
int rezultat;
rezultat = a * b;
System.out.println("Rezultat množenja je: " + Integer.toHexString(rezultat));
}

public void kolicnik(int a, int b) {
int rezultat;
rezultat = a / b;
System.out.println("Rezultat deljenja je: " + Integer.toHexString(rezultat));
}

public void kvadrat(int a, int b) {
int rezultat;
rezultat = (int) Math.pow(a, b);
System.out.println("Rezultat kvadriranja je: " + Integer.toHexString(rezultat));
}

public void koren(int a) {
int rezultat;
rezultat = (int) Math.sqrt(a);
System.out.println("Rezultat korenjenja je: " + Integer.toHexString(rezultat));
}

public boolean isHex(String s)
{
return s.matches("[0-9A-Fa-f]+");
}

}``````

I'm sorry its not in English and if this bothers anyone I can translate the code into english.

Help needed!

@ lines 29 and 86 you check if the first input is hexadecimal... you should check the value of the second input

I'm sorry its not in English and if this bothers anyone I can translate the code into english

If you still have problem …

well, you don't really have a question in there, so what do you need help with?
also, how far do you want your code to be able to go? just
1 + 1
or also, for instance:
1 + (2 * 3)

it would also help if you add …

## All 11 Replies

@ lines 29 and 86 you check if the first input is hexadecimal... you should check the value of the second input

I'm sorry its not in English and if this bothers anyone I can translate the code into english

If you still have problem with the program I'd appreciate that or you can use comments in English on the important parts of the code

well, you don't really have a question in there, so what do you need help with?
also, how far do you want your code to be able to go? just
1 + 1
or also, for instance:
1 + (2 * 3)

it would also help if you add a bit of comments that translate the method names.
in the part

``````public void koren(int a) {
int rezultat;
rezultat = (int) Math.sqrt(a);
System.out.println("Rezultat korenjenja je: " + Integer.toHexString(rezultat));
}``````

it's quite clear what you mean by koren, but in your menu and in your switch statement it could be easier to follow your code if we don't have to go to the Kalkulator code every time to check what we are looking at.

he didn't include a question mark, but he did ask a question.
he wants to make his app capable of adding (or performing some other operation) a number to the existing result... anyways.

suchanewb,
you see, when you have such a calculator the equal sign, square root, and squaring lead directly to the result and output it on screen.

the other possible tasks that your calculator should be capable of performing are the arithmetic operations.

if the user clicks the equal sign, square root, or square button, I mentioned what needs to be done, else, he must have clicked an arithmetic operation button on your calculator. in that case, your app should perform the operation with operands being the current result (stored on some variable in your app) and the user input. since inputting a number is the natural followup when the user clicks on a button that represents an arithmetic operation, then it would be equally natural to clean the text input part of your calculator every time such a button is clicked and refresh the current result by performing the previous operation in case it has not been performed (you know it was performed if equal, square root, or square button was clicked last)...

Exactly what bibiki said, I'm having difficulties implementing the last part (+,-,/,*)..

You need to keep track of your state. If you have a results from previous input, then that can be used as the first operand for the next input if the next token entered is an operator.

You need to keep track of your state. If you have a results from previous input, then that can be used as the first operand for the next input if the next token entered is an operator.

Yes, I know that now and that is why I'm asking for help...because I don't know how to code it in my case...it has been bugging me for a few days now and I really hope someone could help me :)

One way to keep track of state is with a boolean variable.
Set it true if you have a result from a previous input.
If its true allow the next input to be an operator.
If the next input is an operand not an operator, set it false.

Also you need to keep the previous result so it can be used again.

Yes, I know that now and that is why I'm asking for help...because I don't know how to code it in my case...it has been bugging me for a few days now and I really hope someone could help me :)

Something similar to what you're asking was done in this application.Perhaps it will be useful for you.

Something similar to what you're asking was done in this application.Perhaps it will be useful for you.

this doesn't do anything similar. the OP said 'the user inputs 1 + 1' and gets as result: '2'
that's quite a bit different from saying: the user inputs 1, clicks a button for + and enters another 1, it 's also a lot easier to write than what the OP is trying to do.

this doesn't do anything similar. the OP said 'the user inputs 1 + 1' and gets as result: '2'
that's quite a bit different from saying: the user inputs 1, clicks a button for + and enters another 1, it 's also a lot easier to write than what the OP is trying to do.

If you click on an entry of the listbox which contains things like "32+64=96", the program uses StringTokenizer to split according to + and = and parse the first two operators and the result. I'm sorry for not being more specific in my first post.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.