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
- (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.