Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Currently working on my integer java class, need help with the last step, My Ins

ID: 3682677 • Letter: C

Question

Currently working on my integer java class, need help with the last step, My Instuctor told me to

to add more tests for example invoke the method to see if 7 is a prime number. Here is what I have so far

class MyInteger {
int value;
MyInteger(int newValue) {
value = newValue;
}
public int getValue() {
return value;
}
public static boolean isEven(int n) {
return (n % 2 == 0);
}
public static boolean isOdd(int n) {
return !isEven(n);
}
public static boolean isPrime(int n) {
for (int f = 2; f < n / 2; f++) {
if (n % f == 0) {
return false;
}
  
}
return true;
}
public static boolean isEven(MyInteger n) {
return n.isEven();
}
public static boolean isOdd(MyInteger n) {
return n.isOdd();
}
public static boolean isPrime(MyInteger n) {
return n.isPrime();
}
public boolean isEven() {
return isEven(value);
}
public boolean isOdd() {
return isOdd(value);
}
public boolean isPrime() {
return isPrime(value);
}
public boolean equals(int n) {
return (value == n);
}
public boolean equals(MyInteger n) {
return equals(n.getValue());
}
public static int parseInt(String s) {
return Integer.parseInt(s);
}
public static int parseInt(char[] s) {
return parseInt(new String(s));
}
}

Explanation / Answer

Hi, Please find below class implemented for test your methods.

TestMyInteger.java

public class TestMyInteger {

   /**
   * @param args
   */
   public static void main(String[] args) {
       // TODO Auto-generated method stub
       java.util.Scanner in = new java.util.Scanner(System.in);
       System.out.println("Please enter an integer value : ");
       int n = in.nextInt();
       MyInteger a = new MyInteger(n);
       System.out.println("Given Number is Even : "+MyInteger.isEven(a));
       System.out.println("Given Number is Odd : "+MyInteger.isOdd(a));
       System.out.println("Given Number is Prime : "+MyInteger.isPrime(a));
       System.out.println("Given Number is Even : "+MyInteger.isEven(n));
       System.out.println("Given Number is Odd : "+MyInteger.isOdd(n));
       System.out.println("Given Number is Prime : "+MyInteger.isPrime(n));      
       System.out.println("Given Number is Even : "+a.isEven());
       System.out.println("Given Number is Odd : "+a.isOdd());
       System.out.println("Given Number is Prime : "+a.isPrime());
       System.out.println("Given Number is Equal : "+a.equals(n));
   }

}

Output:

Please enter an integer value :
7
Given Number is Even : false
Given Number is Odd : true
Given Number is Prime : true
Given Number is Even : false
Given Number is Odd : true
Given Number is Prime : true
Given Number is Even : false
Given Number is Odd : true
Given Number is Prime : true
Given Number is Equal : true

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote