Create a class titled MyInteger. The class will contain: An int data field title
ID: 3673392 • Letter: C
Question
Create a class titled MyInteger. The class will contain: An int data field titled value that stores the int value for each instance of this class. A constructor that creates an instance object for the int value specified. A getter method that returns the int value. A setter method that changes the int value. A method titled isEven() that returns true if the int value is even. A method titled isOdd() that returns true if the int value is odd. Write a main method in a class titled TestMyInteger to test your code.
Explanation / Answer
MyInteger.java
package my.integer;
public class MyInteger {
private int data;
public MyInteger(int data) {
super();
this.data = data;
}
public int getData() {
return data;
}
public void setData(int data) {
this.data = data;
}
public boolean isEven() {
return data % 2 == 0;
}
public boolean isOdd() {
return !isEven();
}
}
TestMyInteger.java
package my.integer;
public class TestMyInteger {
public static void main(String[] args) {
MyInteger myOddInteger = new MyInteger(5);
MyInteger myEvenInteger = new MyInteger(8);
System.out.println(myOddInteger.getData());
System.out.println(myEvenInteger.getData());
System.out.println(myOddInteger.isOdd());
System.out.println(myEvenInteger.isEven());
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.