Write a program that simulates a simple calculator. It reads two integers and a
ID: 642577 • Letter: W
Question
Write a program that simulates a simple calculator. It reads two integers and a character. If the character is a +, the sum is printed; if it is a -, the difference is printed; if it is a *, the product is printed; if it is a /, the quotient is printed; and if it is a %, the remainder is printed.
In the 2- dimensional plane, a point is described by its two coordinates x and y. It supports these operations :
A constructor allowing initialization of both coordinates
Accessors and mutators to its coordinates
Translation of a point
Write a java class, called MyPoint, that corresponds to such an abstraction of points in the dimensional plane.
Provide a tester class that creates one point, then translates and displays its new coordinates.
Explanation / Answer
SIMPLE CALCULATOR
import java.util.Scanner;
public class cal {
/**
* @param args
*/
public static void main(String[] args) {
double press;
double n1, n2;
String operation;
Scanner scannerObject = new Scanner(System.in);
do{
System.out.println("Enter first number");
n1 = scannerObject. nextDouble();
System.out.println("Enter second number");
n2 = scannerObject. nextDouble();
Scanner op = new Scanner(System.in);
System.out.println("Enter your operation");
operation = op.next();
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("Invalid Operation");
}
System.out.println("Plz press 1 for continue Otherwise press 0");
press=scannerObject. nextDouble();
}while(press==1);
}
}
2- dimensional plane, a point
import java.util.Scanner;
public class Point {
private double x;
private double y;
public Point(double x, double y) {
this.x = x;
this.y = y;
}
public double getX() {
return x;
}
public double getY() {
return y;
}
@Override
public String toString() {
return "(" + this.x + "," + this.y + ")";
}
}
public class Test
{
public static void main(String[] args){
Point point= new Point(3,2);
System.out.println(point.toString());
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.