Java Program – Create a new class Calculation with add() and subtract methods to add two numbers and to subtract two numbers.
Write down a Java Program to Design a Calculation Class with the following features:
- Define two instance variables num1, num2 of type double.
- Define two constructors – default constructor to initialize num1 and num2 with 0.0. One parameterized constructor to initialize num1, num2 with the given values passed by the user at the time of the object creation.
- Define 2 methods add() and subtract(). These methods will add num1, num2 and perform subtraction as well. The two methods will return the resultant value.
- Write down a class TestCalculation with main() method.
- Create two objects obj1 and obj2 using default constructor and parameterized constructor by passing values(700.0,86.0).
- Call add() and subtract methods and show results.
Source Code for Program Create new Java Calculation Class
Output of a Sample Program Execution
Object1 Results Addition=0.0 Subtraction=0.0 Object2 Results Addition=786.0 Subtraction=614.0
You may also like to read a Java Program to define Rectangle class.
122 total views, 1 views today