2

Hey guys that are linking me to the other thread on this site. I read that but I still don't understand :( And also is everything actually right besides the static problems i'm running into?

Here is the assignment

  1. (MyInteger class) 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 isEven(int), isOdd(int), and isPrime(int) that return true if the specified value is even, odd, or prime, respectively.
    • Static methods isEven(MyInteger), isOdd(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(String) that converts a string to an int value. Implement the method without using Integer.parseInt(x). Draw the UML diagram for the class. Implement the class. Write a client program that tests all methods in the class.

here is the code

public class MyInteger {
private int value=0;

public int getValue(){
    return value;
}

public boolean isEven(){
if(value%2==0);
    return true;
}
public boolean isOdd(){
if (value%2==1);
    return true;
}
public boolean isPrime(){
for (int i = 2; i<value-1; i++)
if (value % i == 0)
    return false;

    return true;
}

public static boolean isEven(int x){
if (isEven(x))
    return true;
return false;
}

public static boolean isOdd(int x){
if (isOdd(x))
    return true;
return false;
}

public static boolean isPrime(int x){
if (isPrime(x))
    return true;
return false;
}

public static boolean isPrime(MyInteger x){
if (isPrime(x))
    return true;
return false;
} 

public static boolean isEven(MyInteger x){
if (isEven(x))
    return true;
return false;
}

public static boolean isOdd(MyInteger x){
if (isOdd(x))
    return true;
return false;
} 

public boolean equals (int x){
int integer;
MyInteger y = new MyInteger(integer);
if (x==MyInteger.getValue())
    return true;

return false;

}

public boolean equals (MyInteger x){
int integer;
MyInteger y = new MyInteger(integer);
if (x==MyInteger.getValue())
    return true;

return false;

}

public static int parseInt (String str){

int answer = 0, factor = 1;
for (int i = str.length()-1; i >= 0; i--) {
    answer += (str.charAt(i) - '0') * factor;
    factor *= 10;
}
return answer;
}


public MyInteger(int integer){

}


 }

Now I already know this code won't compile because in both equals methods I get the error "cannot make a static reference to the non-static method" on the if() line. Can anybody tell me why that is happening and what it means? I tried googling it but I don't really understand.

Natantantan
  • 47
  • 2
  • 8
  • The first answer in the posted question should give you a good explanation. If you're still having difficulty, described exactly what about the topic is eluding you; that way we can focus on the specific problem you're having. – Paul Richter Oct 16 '14 at 16:53
  • your `static` `isEven(int)` should do more or less what your instance method does: `if(x%2==0) return true;` – Sam I am says Reinstate Monica Oct 16 '14 at 16:55
  • Hey man I read the thing but I still don't understand. – Natantantan Oct 16 '14 at 16:56
  • @sam just the isEven or all of them? And if I should just copy and paste the exact same code then whats the point of the extra methods? – Natantantan Oct 16 '14 at 17:01
  • @user3367451 all of the `static` methods that have an `int` parameter – Sam I am says Reinstate Monica Oct 16 '14 at 17:05
  • 1
    @user3367451 if you don't understand anything that is said in the referenced question, then we may not be able to help you. I would assume you understand some things, but not others, or are not sure of others -- there are lots of variations. So you need to analyze your confusion enough to tell us what to explain to you. If you're not willing to go to that effort, then you may be out of luck. We cannot read your mind, and we dislike repeating what's already been explained. – arcy Oct 16 '14 at 17:09

0 Answers0