In: Computer Science
In Java
Define the EvenNumber class for representing an even number. The class contains:
Write a test program that creates an EvenNumber object for value 16 and invokes the getNext() and getPrevious() functions to obtain and displays these numbers.
|
Your code for this problem |
|
-- Copy and paste your code here |
Run the code and insert the result in the following box.
|
The result of the query |
|
Copy and paste the result here (e.g. the screen shot of the result you get by running the code). |
// Define the EvenNumber class for representing an even number
class EvenNumber {
private int value;
public EvenNumber() {
value = 0;
}
public EvenNumber(int value) {
this.value = value;
}
public int getValue() {
return value;
}
public EvenNumber getNext() {
return new EvenNumber(value + 2);
}
public EvenNumber getPrevious() {
return new EvenNumber(value - 2);
}
}
// Write a test program that creates an EvenNumber object for value 16 and invokes the getNext() and getPrevious()
// functions to obtain and displays these numbers.
class TestEvenNumber {
public static void main(String[] args) {
EvenNumber evenNumber = new EvenNumber(16);
System.out.println(evenNumber.getNext().getValue());
System.out.println(evenNumber.getPrevious().getValue());
}
}
