I am testing a Class in Java and I have written an if-else statement to ensure that the value is between 0-4.5. If it is not, the accessor method should return 0 as a value. However, when I test a number that is out of range, whether above or below, it still prints that value instead of 0.
I am very new to Java, I appreciate any insight into where I may have gone wrong.
Below, I have my accessor and mutator method, as well as a test where the value is above the specified range:
public double getNumber()
{
return this.num;
}
public void setNumber(double num)
{
if(num >= 0.0 && num <= 4.5)
{
this.num = num;
}
else
this.num = 0.0;
}
static void Test_setNumber()
{
ClassName test = new ClassName(400.0);
applicant.setNumber(460.5);
double expected = 460.5;
double actual = applicant.getNumber();
}
CodePudding user response:
package test_numaboverange;
public class Test_numAboveRange {
public static class ClassName{
private double num ;
public ClassName() {
}
public double getNumber() {
return num;
}
public void setNumber(double num) {
if(num >= 0.0 && num <= 4.5) {
this.num = num;
}
else
this.num = 0.0;
}
}
public static void main(String[] args) {
ClassName test = new ClassName();
test.num = 5.5;
double expected = 0.0;
double actual = test.getNumber();
System.out.println(actual);
test.setNumber(5.5);
System.out.println(test.getNumber());
}
}
You should know when set and get the value num . result :
5.5
0.0
CodePudding user response:
static void Test_numAboveRange()
{
ClassName test = new ClassName(5.0);
double expected = 0.0;
double actual = test.getNumber();
}
You have to sent in ClassName class's full code to let us see how you set in the value. I dont see how the class ClassName(double) call the setNum.