Results 1 to 9 of 9
  1. #1
    Join Date
    Jul 2008
    Location
    Phoenix, AZ
    Posts
    6
    Rep Power
    0

    Question Trouble with static methods and boolean equals() methods with classes

    Hello all. I'm currently taking a Java I class and i've trudged through well enough so far, but I'm totally stuck on the concepts surrounding Objects and Classes. Specifically, I have a problem that reads thus:

    Design a class named MyInteger. The class contains:
    • an int data field named 'value' that stores the int value represented by this object.
    • a constructor that creates a MyInteger object for the specified int value.
    • a get method that returns the int value.
    • methods isEven(), isOdd(), and isPrime() that return true if the value is even, odd, or prime, respectively.
    • static methods is Even(MyInteger), is Odd(MyInteger), and isPrime(MyInteger) that return true if the specified value is even, odd, or prime, respectively.
    • methods equals(int) and equals(MyInteger) that return true if the value in the object is equal to the specified value.
    • a static method parseInt(int) that converts a string to an int value.


    The part I'm currently having trouble with is in red. The book does not give any examples of how to set up an equals method comparing "the value in the object is equal to the specified value". I'm not even sure I understand quite what it means, to be honest! Also, the book is asking me to create a static method parseInt(int) that converts a string to an int value, but from the way it looks, it doesn't even accept String values. Wouldn't it have a string data type within the parentheses it was supposed to accept strings??

    I'm certainly not looking for "the answers", but if anyone has any insight as to how to approach the last two problems in the bullet list, I will grealy appreciate it! Thanks in advance!

    Here's the work I have done so far:

    /*TestMyInteger.java:
    */

    package testmyinteger; //name of program package

    public class TestMyInteger {//begin class TestMyInteger

    //main method begins Java application
    public static void main(String[] args)
    {
    MyInteger integer1 = new MyInteger(1);
    MyInteger integer2 = new MyInteger(2);
    MyInteger integer3 = new MyInteger(2);

    System.out.print("The statement integer1 equals integer2 is: "
    + integer1.equals(integer2));
    System.out.print("The statement integer1 equals integer3 is: "
    + integer1.equals(integer3));
    System.out.print("The statement integer2 equals integer3 is: "
    + integer2.equals(integer3));

    }//end of method main

    }//end of class TestMyInteger

    class MyInteger {

    static int value;


    MyInteger(int newValue) {

    value = newValue;
    }

    public int getValue() {

    return value;
    }

    public boolean isEven() {

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

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

    int factor = 0;
    for (factor = 2; factor <= value / 2; factor++)
    {
    if (value % factor != 0)
    return true;
    }
    return false;
    }
    public static boolean isEven(int value) {

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

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

    int factor = 0;
    for (factor = 2; factor <= value / 2; factor++)
    {
    if (value % factor != 0)
    return true;
    }
    return false;
    }
    public static boolean isEven(MyInteger newValue) {

    if (value % 2 == 0)
    return true;
    }
    public static boolean isOdd(MyInteger newValue) {

    if (value % 2 != 0)
    return true;
    }
    public static boolean isPrime(MyInteger newValue) {

    int factor = 0;
    for (factor = 2; factor <= value / 2; factor++)
    {
    if (value % factor != 0)
    return true;
    }
    return false;
    }
    public boolean equals(int value, MyInteger newValue) {

    return (value == newValue);
    }
    public boolean equals(MyInteger newValue) {

    return (value == newValue);
    }
    public static int parseInt(int value) {

    String s;
    int intValue = Integer.parseInt(s);

    }

    }

    The last three methods at the end are, of course, incorrect, but I have no idea of how to set them up...

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Java Code:
    public class TestMyInt {
        public static void main(String[] args) {
            MyInt integer1 = new MyInt(1);
            MyInt integer2 = new MyInt(2);
            MyInt integer3 = new MyInt(2);
    
            System.out.println("The statement integer1 equals integer2 is: "
                              + integer1.equals(integer2));
            System.out.println("The statement integer1 equals integer3 is: "
                              + integer1.equals(integer3));
            System.out.println("The statement integer2 equals integer3 is: "
                              + integer2.equals(integer3));
        }
    }
    
    class MyInt {
    //    static int value;
        int value;
    
        MyInt(int newValue) {
            value = newValue;
        }
    
        public int getValue() {
            return value;
        }
    
        public boolean isEven() {
            if (value % 2 == 0)
                return true;
            return false;
        }
    
        public boolean isOdd() {
            if (value % 2 != 0)
                return true;
            return false;
        }
    
        public boolean isPrime() {
            int factor = 0;
            for (factor = 2; factor <= value / 2; factor++) {
                if (value % factor != 0)
                    return true;
            }
            return false;
        }
    
        public boolean isEven(int value) {
            if (value % 2 == 0)
                return true;
            return false;
        }
    
        public boolean isOdd(int value) {
            if (value % 2 != 0)
                return true;
            return false;
        }
    
        public static boolean isPrime(int value) {
            return isPrime(value);
        }
    
        public static boolean isEven(MyInt myInt) {
            return myInt.isEven(myInt.getValue());
        }
    
        public static boolean isOdd(MyInt myInt) {
            return myInt.isOdd(myInt.getValue());
        }
    
        public static boolean isPrime(MyInt myInt) {
            return myInt.isPrime(myInt.getValue());
        }
    
        public boolean equals(int intValue) {
            return value == intValue; 
        }
    
        public boolean equals(MyInt myInt) {
            return equals(myInt.getValue());
        }
    
        public static int parseInt(String s) {
            return Integer.parseInt(s);
        }
    }

  3. #3
    Join Date
    Jul 2008
    Location
    Phoenix, AZ
    Posts
    6
    Rep Power
    0

    Default

    WOW! Thank you so much hardwired!!!

    It's amazing how I understand the program once I see it actually work!

    Thank you so much again! I cannot express my gratitude enough..

    How did you come up with the answer so fast? Amazing!

  4. #4
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    How did you come up with the answer so fast?
    Just playing with it. After some more testing, some changes:
    remove this method
    Java Code:
        public static boolean isPrime(int value) {
            return isPrime(value);
        }
    and change this one
    Java Code:
        public static boolean isPrime(MyInt myInt) {
    //        return myInt.isPrime(myInt.getValue());
            return myInt.isPrime();
        }

  5. #5
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Looks like you can do the same for the isOdd and isEven methods.

  6. #6
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Here are a few more tests for the test class:
    Java Code:
            MyInt mi5 = new MyInt(5);
            MyInt mi6 = new MyInt(6);
            System.out.printf("mi5.equals(mi6) = %b%n", mi5.equals(mi6));
            System.out.printf("mi5.equals(5) = %b%n", mi5.equals(5));
            System.out.printf("MyInt.parseInt(12)= %d%n", MyInt.parseInt("12"));
            System.out.printf("mi5.isPrime = %b%n", mi5.isPrime());
            System.out.printf("mi5.isEven = %b%n", mi5.isEven());
            System.out.printf("MyInt.isOdd(mi5) = %b%n", MyInt.isOdd(mi5));
            MyInt mi101 = new MyInt(101);
            System.out.printf("MyInt.isPrime(mi101) = %b%n", MyInt.isPrime(mi101));
    Do everything you can to use/test your methods to see how well they work/where more work is needed.

  7. #7
    tdub is offline Member
    Join Date
    Apr 2012
    Posts
    15
    Rep Power
    0

    Default Re: Trouble with static methods and boolean equals() methods with classes

    Quote Originally Posted by hardwired View Post
    Java Code:
    public class TestMyInt {
        public static void main(String[] args) {
            MyInt integer1 = new MyInt(1);
            MyInt integer2 = new MyInt(2);
            MyInt integer3 = new MyInt(2);
    
            System.out.println("The statement integer1 equals integer2 is: "
                              + integer1.equals(integer2));
            System.out.println("The statement integer1 equals integer3 is: "
                              + integer1.equals(integer3));
            System.out.println("The statement integer2 equals integer3 is: "
                              + integer2.equals(integer3));
        }
    }
    
    class MyInt {
    //    static int value;
        int value;
    
        MyInt(int newValue) {
            value = newValue;
        }
    
        public int getValue() {
            return value;
        }
    
        public boolean isEven() {
            if (value % 2 == 0)
                return true;
            return false;
        }
    
        public boolean isOdd() {
            if (value % 2 != 0)
                return true;
            return false;
        }
    
        public boolean isPrime() {
            int factor = 0;
            for (factor = 2; factor <= value / 2; factor++) {
                if (value % factor != 0)
                    return true;
            }
            return false;
        }
    
        public boolean isEven(int value) {
            if (value % 2 == 0)
                return true;
            return false;
        }
    
        public boolean isOdd(int value) {
            if (value % 2 != 0)
                return true;
            return false;
        }
    
        public static boolean isPrime(int value) {
            return isPrime(value);
        }
    
        public static boolean isEven(MyInt myInt) {
            return myInt.isEven(myInt.getValue());
        }
    
        public static boolean isOdd(MyInt myInt) {
            return myInt.isOdd(myInt.getValue());
        }
    
        public static boolean isPrime(MyInt myInt) {
            return myInt.isPrime(myInt.getValue());
        }
    
        public boolean equals(int intValue) {
            return value == intValue; 
        }
    
        public boolean equals(MyInt myInt) {
            return equals(myInt.getValue());
        }
    
        public static int parseInt(String s) {
            return Integer.parseInt(s);
        }
    }
    Hey I have a similar problem.
    This is what I need to do.
    The methods to be implemented are listed below:
    1. boolean find(int x, int[] numbers): If x is an element of numbers, then return true, otherwise return false.
    2. int indexOf(int x, int[] numbers): Returns the first location of x in numbers. If x is not an element, return -1.
    3. int lastIndexOf(int x, int[] numbers): Returns the last location of x in numbers. If x is not an element, return -1.
    4. int elementsGreater(int x, int[] numbers): Returns the number of elements that are strictly greater than x in the numbers array. Returns 0 if no element is greater than x.
    5. int elementsSmaller(int x, int[] numbers): Returns the number of elements that are strictly smaller than x in the numbers array. Returns 0 if no element is smaller than x.
    6. int elementsEqual(int x, int[] numbers): Returns the number of times x occurs in the numbers array. Returns 0 if x does not occur in numbers.
    7. int[] indexOfElementsEqual(int x, int[] numbers): Returns an array containing the indices of the numbers array, which have the value x. For example, if the array is {2, 4, 2, 3, 4, 2}, and x=2, the array returned is {0, 2, 5}. If x is not present in numbers, then null is returned.

    Then we must test the program. Here is the code I have come up with to do that

    import java.util.Arrays

    public static void main(String[] args) {

    int[] testArray = {6, 1, 4, 9, 2, 4, 2, 3, 2, 1};

    // Find an element that does exist
    System.out.print("Test 1...find...");
    if(find(2, testArray))
    System.out.println("passed");
    else
    System.out.println("failed");

    // Find an element that does not exist
    System.out.print("Test 2...find...");
    if(find(10, testArray))
    System.out.println("failed");
    else
    System.out.println("passed");

    // Last index of an element that exists multiple times
    System.out.print("Test 3...lastIndexOf...");
    if(lastIndexOf(1, testArray)==9)
    System.out.println("passed");
    else
    System.out.println("failed");

    // Last index of an element that does not exist
    System.out.print("Test 4...lastIndexOf...");
    if(lastIndexOf(10, testArray)==-1)
    System.out.println("passed");
    else
    System.out.println("failed");

    // Array containing indexes of an element that exists multiple times
    System.out.print("Test 5...indexOfElementsEqual...");
    int[] expectedArray = {1, 9};
    int[] resultArray = indexOfElementsEqual(1, testArray);
    if(Arrays.equals(expectedArray, resultArray))
    System.out.println("passed");
    else
    System.out.println("failed");
    }

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,508
    Blog Entries
    7
    Rep Power
    20

    Default Re: Trouble with static methods and boolean equals() methods with classes

    @hardwired: please no spoonfeeding; the OP doesn't learn much (if anything) from it and if the school/university finds out the OP just copied and pasted the solution the OP might be in trouble (and possibly this forum too).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  9. #9
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,229
    Rep Power
    19

    Default Re: Trouble with static methods and boolean equals() methods with classes

    Zombie. closing.

    db

    THREAD CLOSED
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Newbie question about Static methods
    By SCS17 in forum New To Java
    Replies: 9
    Last Post: 02-06-2008, 08:03 AM
  2. Static methods - not working
    By Echilon in forum New To Java
    Replies: 2
    Last Post: 12-21-2007, 01:31 PM
  3. Why methods in an interface cannot be static?
    By cbalu in forum Advanced Java
    Replies: 2
    Last Post: 12-12-2007, 07:57 PM
  4. Static methods
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-04-2007, 05:56 PM
  5. significance of static variables and methods
    By imran_khan in forum New To Java
    Replies: 4
    Last Post: 08-02-2007, 09:52 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •