Reputation: 25
If the two values are set to a value between 0 and 29, if the the number provided in the argument is the sum of these two numbers, and the argument number and instance number are the same.
here is the code that I have written so far:
public class TwoNumbers {
private int firstnum = 0;
private int secondnum = 0;
//Constructor that initialize the first and second number
//to a value of 0
public TwoNumbers()
{
firstnum = 0;
secondnum = 0;
}
public int getFirstNum(){
return firstnum;
}
public int getSecondNum(){
return secondnum;
}
public boolean setFirstNum (int firstnum){
if (firstnum >= 0 && firstnum <=29){
return true;
}
else {
return false;
}
}
public boolean setSecondNum (int secondnum)
{
if (secondnum >= 0 && secondnum <=29){
return true;
}
else {
return false;
}
}
public boolean checkSum (int sum){
if (sum == firstnum + secondnum){
return true;
}
else {
return false;
}
}
public boolean equals (int numbers1) {
if (firstnum == secondnum){
return true;
}
else{
return false;
}
}
}
Upvotes: 2
Views: 86
Reputation: 1674
as commented by @GlenPierce but with some changes
public boolean setFirstNum (int firstnum){
if (firstnum >= 0 && firstnum <=29){
return true;
}
else {
return false;
}
}
public boolean setSecondNum (int secondnum)
{
if (secondnum >= 0 && secondnum <=29){
return true;
}
else {
return false;
}
}
should be
public boolean setFirstNum (int firstnum)
{
if (firstnum >= 0 && firstnum <=29){
this.firstnum = firstnum; //this is to set the value for TwoNumbers class in your object
return true;
}
else {
return false;
}
}
public boolean setSecondNum (int secondnum)
{
if (secondnum >= 0 && secondnum <=29){
this.secondnum = secondnum; //this is to set the value for TwoNumbers class in your object
return true;
}
else {
return false;
}
}
and this will work fine now.
even though you get true or false from TwoNumbers class. There value will always be zero with your instance without setting them in setFirstNum and setSecondNum.
use the this keyword to set them. This is just one of the solution, there are still other ways like what @GlenPierce did.
for 14 and 15
// testing equals()
public static boolean test14() {
TwoNumbers numbers1 = new TwoNumbers();
numbers1.setFirstNum(1);
numbers1.setSecondNum(4);
TwoNumbers numbers2 = new TwoNumbers();
numbers2.setFirstNum(1);
numbers2.setSecondNum(4);
return numbers1.equals(numbers2);
}
public static boolean test15() {
TwoNumbers numbers1 = new TwoNumbers();
numbers1.setFirstNum(1);
numbers1.setSecondNum(4);
TwoNumbers numbers2 = new TwoNumbers();
numbers2.setFirstNum(4);
numbers2.setSecondNum(1);
return numbers1.equals(numbers2);
}
this will fail because your logic is wrong. in .equal method of your TwoNumbers accepts int while you are passing the whole object.
change 14 and 15 and equal method to this and everything will be fine
// testing equals()
public static boolean test14() {
TwoNumbers numbers1 = new TwoNumbers();
numbers1.setFirstNum(1);
numbers1.setSecondNum(4);
TwoNumbers numbers2 = new TwoNumbers();
numbers2.setFirstNum(1);
numbers2.setSecondNum(4);
return numbers1.equals(numbers2.secondnum);
}
public static boolean test15() {
TwoNumbers numbers1 = new TwoNumbers();
numbers1.setFirstNum(1);
numbers1.setSecondNum(4);
TwoNumbers numbers2 = new TwoNumbers();
numbers2.setFirstNum(4);
numbers2.setSecondNum(1);
return numbers1.equals(numbers2.secondnum);
}
public boolean equals (int numberYouPassed) {
//firstnum is the value from your numbers1
//numberYouPassed is the value from numbers2.secondnum that you passed
if (firstnum == numberYouPassed){
return true;
}
else{
return false;
}
}
}
Upvotes: 0
Reputation: 4841
Your setters aren't setting the new values.
public boolean setFirstNum (int newValue){
firstnum = newValue;
if (firstnum >= 0 && firstnum <=29){
return true;
}
else {
return false;
}
}
public boolean setSecondNum (int newValue)
{
secondnum = newValue;
if (secondnum >= 0 && secondnum <=29){
return true;
}
else {
return false;
}
and tests 14 and 15 are not checking the value of the numbers.
return numbers1.equals(numbers2);
should instead read
return numbers1.firstNumber == numbers2.firstNumber && numbers1.secondNumber == numbers2.secondNumber;
Upvotes: 2
Reputation: 994
I think you should check the values which you have set.
public static boolean test15() {
TwoNumbers numbers1 = new TwoNumbers();
numbers1.setFirstNum(1);
numbers1.setSecondNum(4);
TwoNumbers numbers2 = new TwoNumbers();
numbers2.setFirstNum(4);
numbers2.setSecondNum(1);
return numbers1.equals(numbers2);
}
In the case number 15, you are checking that is 1 equal to 4. Therefore, it returns false
and there is nothing wrong with this test case.
Change it with:
numbers2.setFirstNum(1);
numbers2.setSecondNum(4);
In the case number 2, you haven't set the secondNum
but you are trying to check it that's why it returns false
.
public static boolean test2() {
TwoNumbers numbers = new TwoNumbers();
boolean success = numbers.setFirstNum(1);
return success && (numbers.getFirstNum() == 1) && (numbers.getSecondNum() == 0);
}
Add the line below:
boolean success = numbers.setSecondNum(0);
In the case number 6, you haven't set the firstNum
but you are trying to check it that's why it returns false
as well.
public static boolean test6() {
TwoNumbers numbers = new TwoNumbers();
boolean success = numbers.setSecondNum(1);
return success && (numbers.getFirstNum() == 0) && (numbers.getSecondNum() == 1);
}
Add the line below:
boolean success = numbers.setFirstNum(0);
I have realised your mistake, when I looked the case number 14.. Your setter methods don't set the values. They just return false or true. Please update them as below:
public boolean setFirstNum (int firstnum) {
if (firstnum >= 0 && firstnum <=29) {
this.firstnum = firstnum;
return true;
} else {
return false;
}
}
public boolean setSecondNum (int secondnum) {
if (secondnum >= 0 && secondnum <=29) {
this.secondnum = secondnum;
return true;
} else {
return false;
}
}
Upvotes: 0
Reputation: 27986
A few suggestion for you:
assertEquals("default num1 is zero", 0, new TwoNumbers().getFirstNum())
expected
annotation key).If you start with those changes I suspect your issues will become pretty obvious pretty quickly and you'll be learning better practices from the start.
Upvotes: 0