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.
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
{
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")}
}
}
error showing that java is not recognised as internal or external command
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 ...");
}
}
}
//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");
}
}
}
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)}
}
}
}
}
{
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!!!");
}
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;
}
}
}
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 ...");}
}
}
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);}
}
}}
Also, what happens when you enter a different character than expected?
An un-handled exception is always a bad idea.
Post a Comment