Page 201 - CTS - CSA TP - Volume 2
P. 201
COMPUTER SOFTWARE APPLICATION - CITS
public static int subtract(int a, int b) {
return a - b;
}
}
2 Calculator.java
// Inside the ‘myPackage’ folder
package myPackage;
import java.util.Scanner;
public class Calculator {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print(“Enter the first number: “);
int num1 = scanner.nextInt();
System.out.print(“Enter the second number: “);
int num2 = scanner.nextInt();
int sum = MyMath.add(num1, num2);
int difference = MyMath.subtract(num1, num2);
System.out.println(“Sum: “ + sum);
System.out.println(“Difference: “ + difference);
scanner.close();
}
}
Explanation:
• The MyMath class provides simple mathematical operations (addition and subtraction).
• The Calculator class takes user input, calls methods from MyMath, and displays the results.
Execution:
• The program prompts the user to enter two numbers, performs addition and subtraction using methods from
MyMath, and displays the results.
• Compile the code:
186
CITS : IT & ITES - Computer Software Application - Exercise 115