Create a class “Calculator” with two variables, a constructor with no parameter and
two functions for add and multiply. Create a class TestApp with a main function and
create object of Calculator class to call functions.
public class Calculator {
private long var1;
private long var2;
public Calculator() {
}
public long multiply() {
return var1 * var2;
}
public long add() {
return var1 + var2;
}
public void setVar1(long var1) {
this.var1 = var1;
}
public void setVar2(long var2) {
this.var2 = var2;
}
public long getVar1() {
return var1;
}
public long getVar2() {
return var2;
}
}
public class TestApp {
public static void main(String[] args) {
Calculator calculator = new Calculator();
//init variables
calculator.setVar1(-10);
calculator.setVar2(72);
System.out.println("The sum of " + calculator.getVar1() + " and " + calculator.getVar2() + " is " + calculator.add());
System.out.println("The multiplication of " + calculator.getVar1() + " and " + calculator.getVar2() + " is " + calculator.multiply());
}
}
Comments
Leave a comment