2021. 4. 4. 20:01ㆍJava programming
Write a class with the name SimpleCalculator. The class needs two fields with names firstNumber and secondNumber both of type double.
Write the following methods:
*Method named getFirstNumber without any parameters, it needs to return the value of firstNumber field.
*Method named getSecondNumber without any parameters, it needs to return the value of secondNumber field.
*Method named setFirstNumber with one parameter of type double, it needs to set the value of the firstNumberfield.
*Method named setSecondNumber with one parameter of type double, it needs to set the value of the secondNumberfield.
*Method named getAdditionResult without any parameters, it needs to return the result of adding the field values of firstNumber and secondNumber.
*Method named getSubtractionResult without any parameters, it needs to return the result of subtracting the field values of secondNumber from the firstNumber.
*Method named getMultiplicationResult without any parameters, it needs to return the result of multiplying the field values of firstNumber and secondNumber.
*Method named getDivisionResult without any parameters it needs to return the result of dividing the field values of firstNumber by the secondNumber. In case the value of secondNumber is 0 then return 0.
public class SimpleCalculator {
private double firstNumber = 0;
private double secondNumber = 0;
public double getFirstNumber(){
return firstNumber;
}
public double getSecondNumber(){
return secondNumber;
}
public void setFirstNumber(double firstNumber){
this.firstNumber = firstNumber;
}
public void setSecondNumber(double secondNumber){
this.secondNumber = secondNumber;
}
public double getAdditionResult(){
return firstNumber + secondNumber;
}
public double getSubtractionResult(){
return firstNumber - secondNumber;
}
public double getMultiplicationResult(){
return firstNumber * secondNumber;
}
public double getDivisionResult(){
if (secondNumber == 0){
return 0;
}
return firstNumber / secondNumber;
}
}
'this.' is to distinguish between instance variables and local variables when the names both of variables are the same.
A variable with 'this.' ahead of a variable name is an instance variable whereas a variable without 'this.' is a local variable. (My tip to easily memorise them is, since 'this.' has an 'i', it should be an instance variable.)
For instance, on the line 13 we've got a method:
public void setFirstNumber(double firstNumber){
this.firstNumber = firstNumber;
In this method, the variable 'this.firstNumber' indicates the variable outside of the method called 'private double firstNumber' while 'firstNumber' on the left side of the equals sign is a parameter of the method, 'public void setFirstNumber'.
Another significant lesson from this chapter is to access private fields like 'private double firstNumber = 0;' and 'private double secondNumber = 0;' in this exercise.
The primary reason we use private fields, access modifier, is to limit access for certain variables so prevent them from changing data without being authorised to do so.
In order to get access to private fields, getter and setter should be executed by clicking 'Generate Getters and Setters' on the 'Source' tab in whatever such Java compliers as Eclipse IDE, IntelliJ etc.
'Java programming' 카테고리의 다른 글
Coding Exercise 31: Wall Area (0) | 2021.04.06 |
---|---|
Coding Exercise 30: Person (0) | 2021.04.05 |
Coding Exercise 28: Paint Job (0) | 2021.04.04 |
Coding Exercise 27: Input Calculator (0) | 2021.04.03 |
Coding Exercise 26: Diagonal Star (0) | 2021.04.03 |