Advertisement
CoineTre

JF-LabMethods04.Calculations

Feb 5th, 2021
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.46 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class Lab4Calculations {
  4.     public static void main(String[] args) {
  5.         Scanner scanner = new Scanner (System.in);
  6.         String operation = scanner.nextLine();
  7.         int firstNumber = Integer.parseInt(scanner.nextLine());
  8.         int secondNumber = Integer.parseInt(scanner.nextLine());
  9.         switch (operation){
  10.             case"add":
  11.                 add(firstNumber,secondNumber);
  12.                 break;
  13.             case"multiply":
  14.                 multiply(firstNumber,secondNumber);
  15.                 break;
  16.             case"subtract":
  17.                 subtract(firstNumber,secondNumber);
  18.                 break;
  19.             case"divide":
  20.                 divide(firstNumber,secondNumber);
  21.                 break;
  22.         }
  23.  
  24.     }
  25.  
  26.     private static void divide(int a, int b) {
  27.         System.out.println(a / b);
  28.     }
  29.  
  30.     private static void subtract(int a, int b) {
  31.         System.out.println(a-b);
  32.     }
  33.  
  34.     private static void multiply(int a, int b) {
  35.         System.out.println(a*b);
  36.     }
  37.  
  38.     private static void add(int a, int b) {
  39.         System.out.println(a+b);
  40.     }
  41. }
  42.  
  43. /*
  44. * Write a program that receives a string on the first line ("add", "multiply", "subtract", "divide")
  45. *  and on the next two lines receives two numbers. Create four methods (for each calculation)
  46. * and invoke the right one depending on the command.
  47. * The method should also print the result (needs to be void).*/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement