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;
BufferedReader vhod2 = new BufferedReader(new InputStreamReader(System.in));
Scanner vhod = new Scanner(System.in);
DrugiRazred Kalkulator = new DrugiRazred();
System.out.print("Vnesi šestnajstiško število: ");
String str= vhod2.readLine();
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: ");
String str2= vhod2.readLine();
if(Kalkulator.isHex(str)==false) {
System.out.println("Število ni šestnajstiško!");
System.exit(0);
}
b=Integer.parseInt(str2,16);
while(x==0) {
Kalkulator.glavniMenu();
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;
//KVADRIRANJE
case 5:
Kalkulator.kvadrat(a, b);
break;
//KORENJENJE
case 6:
Kalkulator.koren(a);
break;
//VNOS NOVIH ŠTEVIL
case 7:
System.out.print("Vnesi šestnajstiško število: ");
str= vhod2.readLine();
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: ");
str2= vhod2.readLine();
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 {
BufferedReader vhod2 = new BufferedReader(new InputStreamReader(System.in));
Scanner vhod = new Scanner(System.in);
public void glavniMenu() {
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("'5' - Kvadriranje ");
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!