Q:

(The MyInteger class) Design a class named MyInteger. The class contains:

0

(The 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 getter method that returns the int value.
The methods isEven(), isOdd(), and isPrime() that return true if the value in this object is even, odd, or prime, respectively.
The static methods isEven(int), isOdd(int), and isPrime(int) that return true if the specified value is even, odd, or prime, respectively.
The static methods isEven(MyInteger), isOdd(MyInteger), and isPrime(MyInteger) that return true if the specified value is even, odd, or prime, respectively.
The methods equals(int) and equals(MyInteger) that return true if the value in this object is equal to the specified value.
A static method parseInt(char[]) that converts an array of numeric characters to an int value.
A static method parseInt(String) that converts a string into an int value.

Draw the UML diagram for the class and then implement the class. Write a client program that tests all methods in the class.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

/*********************************************************************************
* (The 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 getter method that returns the int value.                                  *
* ■ The methods isEven(), isOdd(), and isPrime() that return true if the         *
*   value in this object is even, odd, or prime, respectively.                   *
* ■ The static methods isEven(int), isOdd(int), and isPrime(int) that            *
*   return true if the specified value is even, odd, or prime, respectively.     *
* ■ The static methods isEven(MyInteger), isOdd(MyInteger), and                  *
*   isPrime(MyInteger) that return true if the specified value is even, odd,     *
*   or prime, respectively.                                                      *
* ■ The methods equals(int) and equals(MyInteger) that return true if            *
*   the value in this object is equal to the specified value.                    *
* ■ A static method parseInt(char[]) that converts an array of numeric           *
*   characters to an int value.                                                  *
* ■ A static method parseInt(String) that converts a string into an int          *
*   value.                                                                       *
*                                                                                *
* Draw the UML diagram for the class and then implement the class. Write a       *
* client program that tests all methods in the class.                            *
*********************************************************************************/
public class Exercise_10_03 {
	/** Main method */
	public static void main(String[] args) {
		// Create an array of test values
		int[] values = {5, 6, 7, 8, 9}; 

		// Test isEven(int), isOdd(int), and isPrime(int)
		System.out.println("\nTest if values are even using isEven(int):");
		for (int i = 0; i < values.length; i++) {
			System.out.println(values[i] + " " + MyInteger.isEven(values[i]));
		}

		System.out.println("\nTest if values are odd using isOdd(int):");
		for (int i = 0; i < values.length; i++) {
			System.out.println(values[i] + " " + MyInteger.isOdd(values[i]));
		}

		System.out.println("\nTest if values are prime using isPrime(int):");
		for (int i = 0; i < values.length; i++) {
			System.out.println(values[i] + " " + MyInteger.isPrime(values[i]));
		}

		// Test MyInteger(), isEven(), isOdd(), isPrime() and getValue()
		System.out.println("\nTest if values are even using isEven():");
		for (int i = 0; i < values.length; i++) {
			// Create a MyInteger
			MyInteger value = new MyInteger(values[i]);
			System.out.println(value.getValue() + " " + value.isEven());
		}

		System.out.println("\nTest if values are odd using isOdd():");
		for (int i = 0; i < values.length; i++) {
			// Create a MyInteger
			MyInteger value = new MyInteger(values[i]);
			System.out.println(value.getValue() + " " + value.isOdd());
		}

		System.out.println("\nTest if values are prime using isPrime():");
		for (int i = 0; i < values.length; i++) {
			// Create a MyInteger
			MyInteger value = new MyInteger(values[i]);
			System.out.println(value.getValue() + " " + value.isPrime());
		}

		// Test isEven(MyInteger), isOdd(MyInteger), isPrime(MyInteger)
		System.out.println("\nTest if values are even using isEven(MyInteger):");
		for (int i = 0; i < values.length; i++) {
			// Create a MyInteger
			MyInteger value = new MyInteger(values[i]);
			System.out.println(value.getValue() + " " + MyInteger.isEven(value));
		}

		System.out.println("\nTest if values are odd using isOdd(MyInteger):");
		for (int i = 0; i < values.length; i++) {
			// Create a MyInteger
			MyInteger value = new MyInteger(values[i]);
			System.out.println(value.getValue() + " " + MyInteger.isOdd(value));
		}

		System.out.println("\nTest if values are prime using isPrime(MyInteger):");
		for (int i = 0; i < values.length; i++) {
			// Create a MyInteger
			MyInteger value = new MyInteger(values[i]);
			System.out.println(value.getValue() + " " + MyInteger.isPrime(value));
		}

		// Test equals(int) and equals(MyInteger)
		int[] values2 = {5, 9, 7};
		MyInteger value = new MyInteger(9);
		System.out.println("\nTest if " + value.getValue() + 
			" is equal to the specified value:");
		for (int i = 0; i < values2.length; i++) {
			System.out.println(values2[i] + " " + value.equals(values2[i]));
		}

		System.out.println("\nTest if " + value.getValue() + 
			" is equal to the specified value:");
		for (int i = 0; i < values2.length; i++) {
			MyInteger myInteger = new MyInteger(values2[i]);	
			System.out.println(values2[i] + " " + value.equals(myInteger));
		}

		// Test parseInt(char[]) and parseInt(String)
		System.out.println("\nTest parseInt(char[]) and parseInt(String):");
		// Create a character array
		char[] numericCharacters = {'3', '4', '2'};

		// Create a string
		String numericString = "658";
		System.out.print("\'");
		for (int i = 0; i < numericCharacters.length; i++) {
		 	System.out.print(numericCharacters[i] + "");
		} 
		System.out.println("\' + "" + numericString + "" = " + 
			(MyInteger.parseInt(numericCharacters) + 
			MyInteger.parseInt(numericString)));
	}
}

MyInteger.java

/**************************************
*            MyInteger                *
*-------------------------------------*
* +value: int                         *
* +MyInteger(value: int)              *
* +getValue(): int                    *
* +isEven(): boolean                  *
* +isOdd(): boolean                   *
* +isPrime(): boolean                 *
* +isEven(value: int): boolean        *
* ----------------------------        *
* +isOdd(value: int): boolean         *
* ---------------------------         *
* +isPrime(value: int): boolean       *
* -----------------------------       *
* +isEven(value: MyInteger): boolean  *
* ----------------------------------  *
* +isOdd(value: MyInteger): boolean   *
* ---------------------------------   *
* +isPrime(value: MyInteger): boolean *
* ----------------------------------- *
* +equals(value: int): boolean        *
* +equals(value: MyInteger): boolean  *
* +parseInt(chars: char[])            *
* -----------------------             *
* +parseInt(str: String)              *
* ---------------------               *
**************************************/
// Implement MyInteger class
public class MyInteger {
	private int value;

	MyInteger(int value) {
		this.value = value;
	}

	/** Return value */
	public int getValue() {
		return value;
	}

	/** Return true if value is even */
	public boolean isEven() {
		return isEven(value); 
	}

	/** Return true if value is odd */
	public boolean isOdd() {
		return isOdd(value); 
	}

	/** Return true if value is prime */
	public boolean isPrime() {
		return isPrime(value);
	}

	/** Return true if the specified value is even */
	public static boolean isEven(int value) {
		return value % 2 == 0;
	}

	/** Return true if the specified value is odd */
	public static boolean isOdd(int value) {
		return value % 2 != 0;
	}

	/** Return true if specified value is prime */
	public static boolean isPrime(int value) {
		for (int divisor = 2; divisor <= value / 2; divisor++) {
			if (value % divisor == 0)
				return false;
		}
		return true;
	}

	/** Return true if the specified value is even */
	public static boolean isEven(MyInteger myInteger) {
		return myInteger.isEven();
	}

	/** Return true if the specified value is odd */
	public static boolean isOdd(MyInteger myInteger) {
		return myInteger.isOdd();
	}

	/** Return true if specified value is prime */
	public static boolean isPrime(MyInteger myInteger) {
		return myInteger.isPrime();
	}

	/** Return true is the value in this object 
	*   is equal to the specified value. */
	public boolean equals(int value) {
		return this.value == value;
	}

	/** Return true if the value in this object 
	*   is equal to the specified value */
	public boolean equals(MyInteger myInteger) {
		return myInteger.value == this.value;
	}

	/** converts an array of numeric
	*   characters to an int value */
	public static int parseInt(char[] chars) {
		int value = 0;
		for (int i = 0, j = (int)Math.pow(10, chars.length - 1); 
			  i < chars.length; i++, j /= 10) {
			value += (chars[i]- 48) * j;
		}
		return value;
	}

	/** Converts a string into an int value */
	public static int parseInt(String str) {
		int value = 0;
		for (int i = 0, j = (int)Math.pow(10, str.length() - 1); 
			  i < str.length(); i++, j /= 10) {
			value += (str.charAt(i) - 48) * j;
		}
		return value;
	}
}

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now