Skip to main content

Expression Calculator in Java

Std X CA students:
Here is another assignment to be finished before Thursday 24th Apr, 2014, 8 am.
Reverse engineer the application I have posted at the FB group - Expression Caculator. i.e.
1. download the file and save in the root of c-drive
2. run it in command prompt using "java -jar c:\exprcalc.jar"
3. enter various expressions to compute like 2 + 3 = (then enter)
4. once you type '=' and a white space and enter, it displays the result.
5. You can take a long expression like 7.5562 / 7.889 + 5 - 12.67 = (then enter)
6. You can type another expression after the result for the previous is shown.
7. It also handles errors well.

Figure out the code and paste your answers here as a comment.
Hint:
It uses a blank for loop ... i.e. for (;;) {here goes the code}to repeat taking many numbers one after the other.

Comments

samyumuskday said…
public class ExprnCalc
{
public static void main()
{
InputStreamReader a = new InputStreamReader(System.in);
BufferedReader b = new BufferedReader(in);

int x; int y ; char c;
int x = Integer.parseInt(b.readLine());
int y = Integer.parseInt(b.readLine());
char d = Character.parseChar(b.readLine());

if( d == '+'){int x + int y}
else if ( char d == '-' ) {int x- int y
else if (char d == '*') { int x * int y
else if (char d == '/' ) { int x / int y}}}
else { System.out.println("not valid")}
}
}
Kedar Soni said…
You havent done the full job ... my code does a LOOOOOTTT more than this
Unknown said…
Sir my program is not running on cmd
error showing that java is not recognised as internal or external command
Kedar Soni said…
you need to install JDK
Unknown said…
import java.util.Scanner;
public class ExprnCalc
{
public static void main(String args[])throws Exception
{
float x;
Scanner in = new Scanner(System.in);
System.out.println("You can caclulate many terms with real numbers like 2+36*56-9.8 etc.");
System.out.println("Enter real numbers with a white space after each number and an operator between them.");
System.out.println("The operators should not be any other than +, -, *, /. ");
System.out.println("To compute, end it with an =, like 2+3= [enter]");
float a = in.nextFloat();
String op = in.next();
float b = in.nextFloat();
String abc = in.next();



if (op.equals("+"))
{
x = a + b;
System.out.println(x);
}
else if (op.equals("-"))
{
x = a - b;
System.out.println(x);
}
else if (op.equals("*"))
{
x = a * b;
System.out.println(x);
}
else if (op.equals( "/" ))
{
x = a / b;
System.out.println(x);
}
else
{
System.out.println("java.util.InputMismatchException" + "/n" + "This program needs to be restarted ...");
}



}
}
samyumuskday said…
import java.util.Scanner;
//enter the no. as then the answer will be shown
public class Decimal {
/**
* @param args
*/
public static void main(String[] args) {
double n1, n2;
String operation;
Scanner scannerObject = new Scanner(System.in);

System.out.println("Enter the expression as ");
n1 = scannerObject. nextDouble();

Scanner op = new Scanner(System.in);

operation = op.next();


n2 = scannerObject. nextDouble();




switch (operation) {
case "+":
System.out.println("Your answer is " + (n1 + n2));
break;

case "-":
System.out.println("Your answer is " + (n1 - n2));
break;

case "/":
System.out.println("Your answer is " + (n1 / n2));
break;

case "*":
System.out.println("Your asnwer is " + (n1 * n2));
break;

default:
System.out.println("no");

}
}
}
Unknown said…

import java.io.*;
public class ExprnCalc
{
public static void main(String args[]) throws Exception
{
InputStreamReader in = new InputStreamReader(System.in);
BufferedReader b = new BufferedReader(in);
int x;
int y;
int d;
if{(d == '+');
System.out.println( x+ y);}
else{if(d == '-');
System.out.println(x - y);{
else {if(d == '*');
System.out.println(x * y);{
else {if(d == '/');
System.out.println(x / y)}{
else {if(d == '^');
System.out.println(x ^ y)}{
else {if(d == '%');
System.out.println(x % y)}{
else {if(d == '&');
System.out.println(x & y)}{
else {if(d == '!');
System.out.print!n(x ! y)}{
else {if(d == '||');
System.out.println(x || y)}




}
}
}
}
Unknown said…
try
{
InputStreamReader a = new InputStreamReader(System.in);
BufferedReader b = new BufferedReader(a);
for (;;)
{
System.out.print("---->");

double x = 0; double y = 0;
String operator;
double res = 0;


String usrinput = b.readLine();
String[] parts = usrinput.split(" ");
x = Integer.parseInt(parts[0]);
operator = parts[1];
y = Integer.parseInt(parts[2]);

if( operator.equals("+")){res = x + y;}
else if ( operator.equals("-")) {res = x- y;}
else if (operator.equals("*")) { res = x * y;}

System.out.println(res);

}
}
catch (Exception e)
{
System.out.println("Invalid Entry!!!");
}
Unknown said…
import java.util.Scanner;
public class ExprnCalc
{
public static void main(String args[])throws Exception
{
float x;
Scanner in = new Scanner(System.in);
System.out.println("You can caclulate many terms with real numbers like 2+36*56-9.8 etc.");
System.out.println("Enter real numbers with a white space after each number and an operator between them.");
System.out.println("The operators should not be any other than +, -, *, /. ");
System.out.println("To compute, end it with an =, like 2+3= [enter]");
for(;;)
try
{
float a = in.nextFloat();
String op = in.next();
float b = in.nextFloat();
String abc = in.next();
if (op.equals("+"))
{
x = a + b;
System.out.println(x);
}
else if (op.equals("-"))
{
x = a - b;
System.out.println(x);
}
else if (op.equals("*"))
{
x = a * b;
System.out.println(x);
}
else if (op.equals( "/" ))
{
x = a / b;
System.out.println(x);
}
else
{
System.out.println("java.util.InputMismatchException" + "\n" + "This program needs to be restarted ...");
}
}
catch(Exception e){
System.out.println("java.util.InputMismatchException" + "\n" + "This program needs to be restarted ...");
break;
}
}
}
Kedar Soni said…
My Code

import java.util.*;
public class Calculator {
public static void main(String args[]) throws Exception {
try {
Scanner sc = new Scanner(System.in);
System.out.println("You can caclulate many terms with real numbers like 2+36*56-9.8 etc.\nEnter real numbers with a white space " +
"after each number and an operator between them.\nThe operators should not be any other than +, -, *, /. \nTo compute, end " +
"it with an =, like 2+3= [enter] \n");
double ans; String s; Exception e1 = new Exception("You gave a wrong input or sequence of inputs");
ans = sc.nextDouble();
for (;;) {
s = sc.next();
if (s.equals("+")){ans = ans + sc.nextDouble();}
else if (s.equals("-")){ans = ans - sc.nextDouble();}
else if (s.equals("*")){ans = ans * sc.nextDouble();}
else if (s.equals("/")){ans = ans / sc.nextDouble();}
else if (s.equals("=")){System.out.println(ans); ans = sc.nextDouble();}
else {throw e1;}
}
} catch(Exception e) {System.out.println(e + "\nThis program needs to be restarted ...");}
}
}
Ankit Pandey said…
import java.util.Scanner;
public class calculate
{
public static void main(String args[])
{double n;String op;
Scanner in=new Scanner(System.in);
System.out.println("enter the numbers");
n=in.nextDouble();
for(;;){
op=in.next();
if(op.equals("+"))
{n=n+in.nextDouble();}
else if(op.equals("-"))
{n=n-in.nextDouble();}
else if(op.equals("*"))
{n=n*in.nextDouble();}
else if(op.equals("/"))
{n=n/in.nextDouble();}
if(op.equals("="))
{System.out.println("The answer is "+n);}
}

}}
Kedar Soni said…
How does your program stop?
Also, what happens when you enter a different character than expected?
An un-handled exception is always a bad idea.

All Time Popular Posts

Annual Day 2023

 After a long hiatus due to covid we finally got most activities on track. The Annual Day function is always a big deal for everyone at Abhinav. It has been a tradition my mother started that the Annual Day will be not a Teacher-managed event, but a Student Initiative. Since when I was a teenager, I was part of this and today 10 years after she passed, I still strive to make it work as she would have. After some hesitation, we began working in early January. I wanted to use an outside choreographer just so that teachers will not be burdened. (They are already struggling with post-covid learning difficulties) But then, an outside guy could never do justice to the Abhinav style of doing things. We always keep in mind that the cultural program should be enjoyable to everyone in the audience and at the same time should display as many of the diverse talents that our students have, as possible.  It was a great relief that my G3 students came to the rescue. Almost all of std 9 and m...

Are Self-Driving Cars Taking Over Humans?

- Arya Dharmadhikari(Std X, Abhinav Vidyalay) The 21st century has played an important role in the advancement of technologies. This development has even revolutionized the Automobile industry. We have developed from fuel-efficient vehicles to Hybrids and EVs and now we are enhancing self-driving vehicles. Tesla, Waymo, Volvo, GM, BMW, Mercedes are among the few companies that have started testing self-driving cars. The leading among them is Waymo, which has developed level 4 autonomy which we will discuss in the next section. So before reaching levels of autonomy we should understand what self-driving cars are and why we require them. What is a self-driving car? A self-driving car (also called an autonomous vehicle) is a car that is competent in driving itself without any human intervention. The car has sensors, cameras, lasers, radars, GPS, LIDAR through which the Artificial Intelligence senses the surrounding environment, this helps the car to navigate on its path and to detect sig...

Dark Matter

  ~Anvi Patil(X th A Abhinav Vidyalay) In the universe, there is about 25% of dark matter and 70% of dark energy but only 5% of it is visible. What is Dark Matter? It is a non-luminous material, which holds two galaxies. This cannot be called a black hole because it does not bend light. Then the question arrives, as it is non-luminous, how did we detect it? As it is not visible, scientists have found indirect methods to find more about it. One such method is by using the Fermi Gamma-Ray Space Telescope . Gamma rays are released when two particles of dark matter collide, Fermi telescope can be used to detect this collision. This topic was discovered by Fritz Zwicky in the 1930s, he discovered that galaxies were rotating at a faster rate than usual. A dark region seen in the foreground of a star field. This dark region could be a dark cloud of gases like hydrogen, left over from the formation of our galaxy Dark matter is called ‘dark’ not because it is ‘black’ but because it does...