Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class Lab4Calculations {
- public static void main(String[] args) {
- Scanner scanner = new Scanner (System.in);
- String operation = scanner.nextLine();
- int firstNumber = Integer.parseInt(scanner.nextLine());
- int secondNumber = Integer.parseInt(scanner.nextLine());
- switch (operation){
- case"add":
- add(firstNumber,secondNumber);
- break;
- case"multiply":
- multiply(firstNumber,secondNumber);
- break;
- case"subtract":
- subtract(firstNumber,secondNumber);
- break;
- case"divide":
- divide(firstNumber,secondNumber);
- break;
- }
- }
- private static void divide(int a, int b) {
- System.out.println(a / b);
- }
- private static void subtract(int a, int b) {
- System.out.println(a-b);
- }
- private static void multiply(int a, int b) {
- System.out.println(a*b);
- }
- private static void add(int a, int b) {
- System.out.println(a+b);
- }
- }
- /*
- * Write a program that receives a string on the first line ("add", "multiply", "subtract", "divide")
- * and on the next two lines receives two numbers. Create four methods (for each calculation)
- * and invoke the right one depending on the command.
- * The method should also print the result (needs to be void).*/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement