CS211 Accel Stream H/W 1 - 9/6/2000
See instructions for HW #1 in the Regular Stream.
Exercies 1 and 2 are same as those for the regular stream.
Exercise 3:
In this exercise, we'll encapsulate the type "int" in a class. This
exercise, along with the next homework will show some advantages of
abstract data types.
BE SURE TO RETAIN THE CODE YOU WRITE FOR THIS EXERCISE. YOU WILL
REQUIRE IT IN THE NEXT HOMEWORK.
Implement a class called MyInteger, which stores the value of a single
integer, and provides the following methods:
public boolean lessThan(MyInteger i) {
/* returns true if 'this' is less than 'i' */
...
}
public void showValue() {
/* Displays the stored integer's value on the terminal */
...
}
public boolean divides(MyInteger i) {
/* returns true if 'this' divides 'i', without leaving any remainder
*/
/* [the divides method's behavior is undefined when 'this' is zero] */
...
}
public void plus(MyInteger i) {
/* replace the value of 'this' with 'this + i' */
...
}
public MyInteger squareOf() {
/* returns a new object holding the square of the value of 'this' */
...
}
The integer value stored should be private.
Be sure to implement a suitable constructor.
Write a class to find a factor of a given object of the class
MyInteger, (excluding one or the number itself). If a factor is
found, it should be printed on the terminal. Otherwise, your
program should print "Prime". You may assume that the number to
be factorized is odd, and greater than 100.
Note: You are not required to factorize the number
completely. Finding one non-trivial factor is adequate.
The factorization should be done using only the methods described
above. You should use the template:
class Factorize {
public static void getFactor(MyInteger i) {
...
}
}
Test your program with the numbers 899, 401, 769, 68983 .
Points to Ponder:
What are the largest numbers your program can handle ?
What are the ways your program can be modified to handle larger
numbers -
say, of 100 to 1000 digits ?
You should get some answers in the next homework. Happy coding!