$25
1 Introduction
This assignment is more complex that Programming Assignment A, but if you work methodically and ask questions along the way, it is not extremely dificult. The biggest challenge here is getting a better understanding of the mechanics of classes and objects, methods, and more generally, learning about object oriented programming and developing an OO mindset.
This won’t happen in a day: it will take multiple coding sessions for your brain to settle into this way of thinking. Start early! Starting the project a day or two before it’s due won’t su ce, and you’ll su er the reminder of the class if you do so.
Do not use Eclipse, IntelliJ, or any other IDEs: work from the command line on the linux server (or your own linux environment). This will ensure you know the syntax for de ning classes, writing a main method, and so on. We’ll get to IDEs later, but for now, you must work from the command line only (vi etc. to edit; javac to compile; java to run).
2 Description
For assignment 1, you are going to implement a new Java class for processing even integers. Even integers work exactly like regular integers, except they are all even! Addition, subtraction and multiplication of even numbers works as usual. Division might normally result in an odd number: if the result of division is odd, add 1 to make the result even.
3 Details
Your class must be named Even.
Constructor
Provide a single constructor:
Even x=new Even(2);
for example would construct the even number 2. You will need a single class variable (call it whatever you like) to store the (int) value of your Even object.
If the constructor is called with an odd number, add 1 to make it even and the construct using that even number.
Main Methods (manipulate Evens)
You must also provide the following methods (assume x and y are both objects of type Even):
x.add(y); // creates a new Even whose value is x+y
x.sub(y); // creates a new Even whose value is x-y
x.mul(y); // creates a new Even whose value is x*y
x.div(y); // creates a new Even whose value is x/y. If x/y is odd, add 1 to it
x.toInt(); // return the value of the even number as a plain int toString
You should also write a toString method for returning a String representation of the number. The string should consist of words separated by spaces, with one word for each digit of the number ( x.toString();
To do this, consider turning your (int) value into a string (by concatenating to an empty string ); using the charAt() method (from String) to read each digit one at a time (inside a loop); and using a switch statement (or a series of ifs) to append the appropriate word to a result string. Your string should consist of the following words (separated by spaces):
’-’ negative
’0’ zero
’1’ one
’2’ two
’3’ three
’4’ four
’5’ ve
’6’ six
’7’ seven
’8’ eight
’9’ nine
Remember: do not print anything from the toString method; just return a string.
4 Testing
The following sample main program might be used to test your Even class. Note that you should thoroughly test your class with your own test code. This is only an example:
// Construct some Even objects and test the methods // Also do some string concatenation to exercise toString
class Main{
// just need a (standard) main method to run some tests public static void main(String[] args) // no arguments processed {
Even a,b,c,d;
// some Even objects to play with
a=new Even(-10);
// -10
b=new Even(15);
// should round up to 16
c=a.add(b);
// expecting result of 6
d=b.div(a);
// 16/(-10) would be -1.6 which, as an int, is -1
// this should round up to 0, so d should be 0
// we’ll print these out using the toString method
System.out.println("a="+a+" b="+b+" c="+c+" d="+d); // e.g. a=negative one zero
c=new Even(2); // c is a running product
d=new Even(2); // d is our multiplier
for (int i=2;i<12;i++){
c=c.mul(d); // keep doubling c
System.out.println("2^"+i+"="+c); // and print it out in words }
}
}
Corresponding Output
a=negative one zero b=one six c=six d=zero
2^2=four
2^3=eight
2^4=one six
2^5=three two
2^6=six four
2^7=one two eight
2^8=two five six
2^9=five one two
2^10=one zero two four
2^11=two zero four eight.