?
Solved

Trouble shooting...

Posted on 2011-10-25
13
Medium Priority
?
377 Views
Last Modified: 2012-05-12
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
0
Comment
Question by:Mike Eghtebas
  • 12
13 Comments
 
LVL 47

Expert Comment

by:for_yan
ID: 37028289
myInteger5.isEqual(myInteger5))  - this will always be true - no surprise it is
0
 
LVL 47

Expert Comment

by:for_yan
ID: 37028293
myInteger.isEqual(myInteger5))  -- this of course returns false
0
 
LVL 47

Expert Comment

by:for_yan
ID: 37028303

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

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 47

Expert Comment

by:for_yan
ID: 37028314
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

0
 
LVL 47

Expert Comment

by:for_yan
ID: 37028320
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
0
 
LVL 47

Expert Comment

by:for_yan
ID: 37028327
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.
0
 
LVL 47

Expert Comment

by:for_yan
ID: 37028345
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.



0
 
LVL 47

Expert Comment

by:for_yan
ID: 37028383


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.


 




0
 
LVL 47

Expert Comment

by:for_yan
ID: 37028396

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
0
 
LVL 47

Accepted Solution

by:
for_yan earned 2000 total points
ID: 37028466
This is modified program with the tests for
+getIntValue(char[]: charArray): int
+getIntValue(String: strValue) int
included:


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("\nstring converison tests:");

        System.out.println("int produced from String \"25\" is equal to int 25: " + (MyInteger.getIntValue("25") == 25) );
              char[] charArray = {'1','5'} ;
        System.out.println("int produced from charArray = {'1','5'} is equal to 15 : " + (MyInteger.getIntValue(charArray) == 15));

        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);
    }

    public static int  getIntValue(String strValue) {
        try {

            return Integer.parseInt(strValue);
        }  catch(Exception ex) {
            ex.printStackTrace();

        }
        return 0;

    }

     public static int  getIntValue(char[] charArray) {
        try {

            return Integer.parseInt(new String(charArray));
        }  catch(Exception ex) {
            ex.printStackTrace();

        }
        return 0;

    }

}

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

string converison tests:
int produced from String "25" is equal to int 25: true
int produced from charArray = {'1','5'} is equal to 15 : true

the int value of "12" is: 12

Open in new window



0
 
LVL 47

Expert Comment

by:for_yan
ID: 37028470
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
0
 
LVL 34

Author Comment

by:Mike Eghtebas
ID: 37028874
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

0
 
LVL 47

Expert Comment

by:for_yan
ID: 37028926
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
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses
Course of the Month17 days, 8 hours left to enroll

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question