Link to home
Start Free TrialLog in
Avatar of Mike Eghtebas
Mike EghtebasFlag for United States of America

asked on

Trouble shooting...

When MyInteger class is tested

equal tests:
4 supplied to myInteger is equal  70 y/n?: false
myInteger and myInteger5 construced using 4 and 13 are equal y/n?: true

Because 4 is not equal to 13, in the last line, false is expected not true.

Q1: What am I doing wrong in building this class?
Q2: Why the following two methods have to be static?

+isEven(int: value): boolean
+isEven(value: MyInteger): boolean

as shown in the attached UML.

Q3. The same question (similar to Q2) on:

+getIntValue(char[]: charArray): int
+getIntValue(String: strValue) int

Q4: How can I code to test for the above two lines in Q3?

This is just self study and not school work.


Thank you.
public class TestMyInteger {
    public static void main(String[] args){
        MyInteger myInteger = new MyInteger(4);
        
        System.out.println("\nprime tests:");
        System.out.println("4 supplied to myInteger, is prime y/n?: " + myInteger.isPrime());
        System.out.println("70 supplied to isPrime(), is prime y/n?: " + myInteger.isPrime(70));
        MyInteger myInteger2 = new MyInteger(5);
        System.out.println("myInteger2 construced using 5, is prime y/n?: " + myInteger2.isPrime(myInteger2));

        System.out.println("\neven tests:");
        System.out.println("4 supplied to myInteger, is even y/n?: " + myInteger.isEven());
        System.out.println("70 supplied to isEven(), is even y/n?: " + myInteger.isEven(70));
        MyInteger myInteger3 = new MyInteger(5);
        System.out.println("myInteger3 construced using 5, is even y/n?: " + myInteger3.isEven(myInteger3));
     
        System.out.println("\nodd tests:");
        System.out.println("4 supplied to myInteger, is odd y/n?: " + myInteger.isOdd());
        System.out.println("70 supplied to isEven(), is odd y/n?: " + myInteger.isOdd(70));
        MyInteger myInteger4 = new MyInteger(5);
        System.out.println("myInteger4 construced using 5, is odd y/n?: " + myInteger4.isOdd(myInteger4));
           
        System.out.println("\nequal tests:");
        System.out.println("4 supplied to myInteger is equal  70 y/n?: " + myInteger.isEqual(70));
        MyInteger myInteger5 = new MyInteger(5);
        System.out.println("myInteger and myInteger5 construced using 4 and 5 are equal y/n?: " + myInteger5.isEqual(myInteger5));
  
        System.out.println("\nthe int value of \"12\" is: " + myInteger.parseInt("12"));
        
    }
}

class MyInteger {
    private int value;
    
    MyInteger(){}
    
    // 1. Construct a MyInteger object with the specified int value.
    MyInteger(int value) {
        this.value=value;
    }
    
    // 2. Returns the value in this object.
    public int getValue() {
        return value;
    }
    
    // 3. Returns true if the value in this object is prime.
    public boolean isPrime() {
        return isPrime(value);
    }
    
    // 4. Returns true if a specified int value is prime.
    public boolean isPrime(int value) {
        for(int i=2; i<value;i++) {
            if(value % i ==0) return false;
        }
        return true;
    }
    
    // 5. Returns true if the value in a specified MyInteger object 
    public boolean isPrime(MyInteger myInteger) {
        return isPrime(myInteger.getValue());
    }  
    
    // 6. Returns true if the value in this object is even.
    public boolean isEven() {
        return isEven(value);
    }
    
    // 7. Returns true if a specified int value is even.
    public boolean isEven(int value) {
        if(value % 2 == 0) return true;
        return false;
    }
    
    // 8. Returns true if the value in a specified MyInteger object is even.
    public boolean isEven(MyInteger myInteger) {
        return isEven(myInteger.getValue());
    }
    
    // 9. Returns true if the value in this obhect is odd.
    public boolean isOdd() {
        return isOdd(value);
    }
    
    // 10. Returns true if a specified int value is odd.
    public boolean isOdd(int value) {
        if(value % 2 != 0) return true;
        return false;
    }
    
    // 11. Returns true if the value in a specified MyInteger object is odd.
    public boolean isOdd(MyInteger myInteger) {
        return isOdd(myInteger.getValue());
    }
    
    // 12. Returns true if a specified int value is equal to the value in this object.
    public boolean isEqual(int value) {
        return (this.value==value);
    }
    
    // 13. Returns true if the value in a specified MyInteger 
    // object is equal to the value in this object.
    public boolean isEqual(MyInteger myInteger) {
//        System.out.println(value);
//        System.out.println(myInteger.getValue());
        return (value==myInteger.getValue());
    }
    
    // 14. Returns the int value for the specified string. 
    public int parseInt(String value) {
        return Integer.parseInt(value);
    }    
}

Open in new window

UML-2.png
Avatar of for_yan
for_yan
Flag of United States of America image

myInteger5.isEqual(myInteger5))  - this will always be true - no surprise it is
myInteger.isEqual(myInteger5))  -- this of course returns false

I think the output (see below) is all correct:
public class TestMyInteger {
    public static void main(String[] args){
        MyInteger myInteger = new MyInteger(4);

        System.out.println("\nprime tests:");
        System.out.println("4 supplied to myInteger, is prime y/n?: " + myInteger.isPrime());
        System.out.println("70 supplied to isPrime(), is prime y/n?: " + myInteger.isPrime(70));
        MyInteger myInteger2 = new MyInteger(5);
        System.out.println("myInteger2 construced using 5, is prime y/n?: " + myInteger2.isPrime(myInteger2));

        System.out.println("\neven tests:");
        System.out.println("4 supplied to myInteger, is even y/n?: " + myInteger.isEven());
        System.out.println("70 supplied to isEven(), is even y/n?: " + myInteger.isEven(70));
        MyInteger myInteger3 = new MyInteger(5);
        System.out.println("myInteger3 construced using 5, is even y/n?: " + myInteger3.isEven(myInteger3));

        System.out.println("\nodd tests:");
        System.out.println("4 supplied to myInteger, is odd y/n?: " + myInteger.isOdd());
        System.out.println("70 supplied to isEven(), is odd y/n?: " + myInteger.isOdd(70));
        MyInteger myInteger4 = new MyInteger(5);
        System.out.println("myInteger4 construced using 5, is odd y/n?: " + myInteger4.isOdd(myInteger4));

        System.out.println("\nequal tests:");
        System.out.println("4 supplied to myInteger is equal  70 y/n?: " + myInteger.isEqual(70));
        MyInteger myInteger5 = new MyInteger(5);
        System.out.println("myInteger and myInteger5 construced using 4 and 5 are equal y/n?: " + myInteger.isEqual(myInteger5));

        System.out.println("\nthe int value of \"12\" is: " + myInteger.parseInt("12"));

    }
}

class MyInteger {
    private int value;

    MyInteger(){}

    // 1. Construct a MyInteger object with the specified int value.
    MyInteger(int value) {
        this.value=value;
    }

    // 2. Returns the value in this object.
    public int getValue() {
        return value;
    }

    // 3. Returns true if the value in this object is prime.
    public boolean isPrime() {
        return isPrime(value);
    }

    // 4. Returns true if a specified int value is prime.
    public boolean isPrime(int value) {
        for(int i=2; i<value;i++) {
            if(value % i ==0) return false;
        }
        return true;
    }

    // 5. Returns true if the value in a specified MyInteger object
    public boolean isPrime(MyInteger myInteger) {
        return isPrime(myInteger.getValue());
    }

    // 6. Returns true if the value in this object is even.
    public boolean isEven() {
        return isEven(value);
    }

    // 7. Returns true if a specified int value is even.
    public boolean isEven(int value) {
        if(value % 2 == 0) return true;
        return false;
    }

    // 8. Returns true if the value in a specified MyInteger object is even.
    public boolean isEven(MyInteger myInteger) {
        return isEven(myInteger.getValue());
    }

    // 9. Returns true if the value in this obhect is odd.
    public boolean isOdd() {
        return isOdd(value);
    }

    // 10. Returns true if a specified int value is odd.
    public boolean isOdd(int value) {
        if(value % 2 != 0) return true;
        return false;
    }

    // 11. Returns true if the value in a specified MyInteger object is odd.
    public boolean isOdd(MyInteger myInteger) {
        return isOdd(myInteger.getValue());
    }

    // 12. Returns true if a specified int value is equal to the value in this object.
    public boolean isEqual(int value) {
        return (this.value==value);
    }

    // 13. Returns true if the value in a specified MyInteger
    // object is equal to the value in this object.
    public boolean isEqual(MyInteger myInteger) {
//        System.out.println(value);
//        System.out.println(myInteger.getValue());
        return (value==myInteger.getValue());
    }

    // 14. Returns the int value for the specified string.
    public int parseInt(String value) {
        return Integer.parseInt(value);
    }
}

Open in new window


Output:
prime tests:
4 supplied to myInteger, is prime y/n?: false
70 supplied to isPrime(), is prime y/n?: false
myInteger2 construced using 5, is prime y/n?: true

even tests:
4 supplied to myInteger, is even y/n?: true
70 supplied to isEven(), is even y/n?: true
myInteger3 construced using 5, is even y/n?: false

odd tests:
4 supplied to myInteger, is odd y/n?: false
70 supplied to isEven(), is odd y/n?: false
myInteger4 construced using 5, is odd y/n?: true

equal tests:
4 supplied to myInteger is equal  70 y/n?: false
myInteger and myInteger5 construced using 4 and 5 are equal y/n?: false

the int value of "12" is: 12

Open in new window

I think isEven(value) does not have to be static - as it is not static and it works
But it makes sense to make it static because it does nlot use any instance variable of your class.
No matter what is your instance vraibe "value" the result of isEven(in num) method
does not depned on that; so it is in fact static method not instance mathosd
which ueses and instance variables

If you want to make it instance you rather make it without any parameters

public boolean isEven() {
if((value%2) == 0) return true;
else return false;

}

This methhod will check the instance variable value and will be truly instance method
The same with isEven(MyInteger i) - this method is not connected
with any instance field of your calss ansd therefore will give the same result for all instances of your class,
so it is in fact static method.
UInstance methods are those which depend on the instance variables of your class and may give different results
for different instances of your class.
This method wioll gicve different results depending on the argument, but on on the object on which it is invoked
becuase it is not using instance variable value of the object on which it is invoked.
The same applies to all your methods which take arguments

this is instance method, beacause it depneds on instance field value
public boolean isOdd() {
        return isOdd(value);
    }

and this is effectively static method:

  public boolean isOdd(int value) {
        if(value % 2 != 0) return true;
        return false;
    }

because the argument value here has nothing to do with your instnce variable
and this method will give the same result for all instances of your class, provided
tyou feed to the method the same argument, therefore this is
in effect not an instance method, but rather a static method by nature,
even though compiler will not consider this method to bestatic unless
you declare explicitle that this method is static.





read here about duisticntion between statiic and instance methods:

http://download.oracle.com/javase/tutorial/java/javaOO/classvars.html

we read:
Static methods, which have the static modifier in their declarations, should be invoked with the class name, without the need for creating an instance of the class, as in

ClassName.methodName(args)

The reason static methods can be attched to class name rather than to the varaible specifying the
the instnce of a class - is because static methods do not depend on any particvular instance
of the class. Internally in doing their operatioons those methods which are declared static
will not be alolowed to use static variables - and compiler will write compilation error if you try it.

Opposite is not truye - that measn that compiler will not throw error
if in instance method you would not be using any instance field,
but logically in fact this method would act as a static
method becuase it would produce the same result
irrespective of aparticular instance of the class on which it was invoked.
In your case it means that no matter if
your private vlaue = 3
or if your private value = 5
still your method
isEven(7) will always return false
So in its nature this method is static.
It is still good idea to declare those methods which are not using instance variables
static - it makes it easier to understand your code.


 





the same applies to these - their resulst do not depend
on any instance variables of your class
(in your case on the only instance varibale value that you have in this class):
+getIntValue(char[]: charArray): int
+getIntValue(String: strValue) int
ASKER CERTIFIED SOLUTION
Avatar of for_yan
for_yan
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
If you still feel any uncertainties about static vs instance, please do ask questions - this is of course one of the most important issues in java
and you should not have any doubts about it
Avatar of Mike Eghtebas

ASKER

re:> myInteger.isEqual(myInteger5))  -- this of course returns false

I see my mistake. thx

re:> read here about duisticntion between statiic and instance methods:

Appreciate the link and the explanations. I am fairly comfortable with use of static vs. instance variables/methods. In this particular case I guess I just got confused.

Regards,

Mike

Yes, because in this case you just used methods which are static by nature but not by declaration.
They usually do not show such examples and are not lllooking at it from this angle in static explanations.
So it is not prohibited by compiler but just does not make sense