CMSI 186: Homework Assignment #6

Learning Outcomes: Students will (1) implement an independent version of arbitrary-length string arithmetic; (2) understand the concept of breaking a large problem into smaller parts; (3) get a chance to explore the use of big numbers in a real-world way; and (4) learn to document code using the javadocs tool.

Problems About Giant-sized Integer Numbers

The Problem:

Sir Richard Branson, owner of Virgin Galactic, is VERY interested in space travel. He is teaming up with quite a few companies to make a spacecraft that will carry humans to space, but he knows there are several other companies that are trying to do that as well. He wants to beat Bezos and Musk so he can be first at something, and has set his sights on being the first company to offer round-trip rides to the planet Neptune, some 4.4 Billion Kilometers away from Earth. The round-trip will thus be over 8.8 Billion Km. However, to save costs, Branson has decided on instruments that can only measure meters, not Kilometers, so that means the distance will be some 8.8 Trillion meters.

Another factor is the amount of time it will take. If we can travel at the speed of light, which is some 299,792,458 meters/second, we can get there in a mere 8.15 hours. However, since travel and that speed isn't possible, and since we can't instantaneously jump to light speed, we will have to make do by ramping up linearly to half the speed of light for the trip, then decelerate when we get there, and reverse that process on the way back.

For this homework, you will develop public class BrobdingnagianInt, [also known as BrobInt], whose objects model arbitrarily-large integers. Basically, you are being tasked with re-inventing some of the material of java.Math.BigInteger to accomplish the following activities. Don't forget to javadoc your code…

Ground Rules

  1. You must implement your class from first principles; in particular, you may not make use of java.Math.BigInteger or any related device, except for testing purposes if you want to check on the correctness of your test results for your BrobdingnagianInt class.
  2. In the descriptions that follow, the word this refers to an instantiated object of your BrobdingnagianInt class.
  3. It is recommended for testing purposes that you abbreviate BrobdingnagianInt as BrobInt which will be much easier to type. You will be typing this over and over and over and over, so it is best if you get used to the shorter name from the start.
  4. Although it can contain more methods [see java.Math.BigInteger for more ideas], your class must include at least the following items:
    1. public BrobInt( String value )
      // mimics one of the several java.Math.BigInteger constructors
    2. public BrobInt add( BrobInt value )
      // returns a BrobInt whose value is the sum of this plus the argument
    3. public BrobInt subtract( BrobInt value )
      // returns a BrobInt whose value is the difference of this minus the argument
    4. public BrobInt multiply( BrobInt value )
      // returns a BrobInt whose value is the product of this times the argument
    5. public BrobInt divide( BrobInt value )
      // returns a BrobInt whose value is the quotient of this divided by the argument
    6. public BrobInt remainder( BrobInt value )
      // returns a BrobInt whose value is the remainder of this divided by the argument
    7. public String toString()
      // returns the decimal string represention of this BrobInt
    8. public int compareTo( BrobInt value )
      // returns -1/0/1 as this BrobInt is numerically less than/equal to/greater than the value passed as the argument
    9. public boolean equals( Object x )
      // returns true iff x is a BrobInt whose value is numerically equal to this BrobInt
    10. public static BrobInt valueOf( long value )
      // a BrobInt "static factory" for constructing BrobInt out of longs
    11. public static final BrobInt ZERO
      // a BrobInt classwide constant for zero
    12. public static final BrobInt ONE
      // a BrobInt classwide constant for one
    13. public static final BrobInt TEN
      // a BrobInt classwide constant for ten
  5. Following the programming practice known as Test Driven Development [TDD] you must first construct unit tests for the above items before coding the methods themselves. Use the main() method of your class to invoke these unit tests. [In other words, the command java BrobIntTester will run your unit tests.] Try using Java's assert statement in the tests you write; or even better, use JUnit.
  6. Complete as many of the items as possible. All incomplete items must throw an exception; use UnsupportedOperationException.
  7. For part two of this assignment, you will write the program Neptune.java which will help Sir Richard achieve his goal. Your program will take one command line argument, which is the acceleration factor for the trip. Your program with then use that number to speed the spacecraft up to 1/2 the speed of light from a starting speed of 11,186 meters/second [the escape velocity of Earth], and slow it down again in time to reach Neptune at a speed of 23,500 meters/second. We then need to reverse the calculation to get back to Earth.
  8. Try several different command line values to see what happens. If you enter something too large, you run the risk of reaching 1/2 the speed of light at too far of a distance to be able to slow down again, and you'll miss Neptune. Don't worry, this isn't the same as the soccer balls [it's not 1% of the current speed per second], it's a constant addition [or subtraction] for every second.
  9. Your program should report the TOTAL DURATION OF THE JOURNEY in hours, minutes, and seconds, along with the VALUE OF ACCELERATION USED.

Notes and Suggestions:

  1. Do not be fooled by appearances: this assignment is a lot more challenging than it looks. The good news is that for each operation, you already have detailed knowledge of some algorithm — although you will discover that expressing the algorithms in a computer programming language is a whole new can of worms!
  2. There will be at least three Java files for this assignment:
    1. The BrobInt.java file containing the methods listed above, as well as any other helper methods or extra things you may want to implement
    2. The BrobIntTester.java file containing your tests, which may end up in your main program of your BrobInt.java file, but it is highly recommended that this be a separate file
    3. the Neptune.java program file in which you implement your BrobInt to simulate a flight to the planet Neptune and back
  3. There are alternatives to some of the time-honored algorithms, [e.g., Russian Peasant multiplication], which tend to be easier to program. [This is especially true if you have taken CMSI 185 with Dr. Johnson. You may even come to decide that they are easier to use in practice!] Feel free to implement them, instead of the time-honored techniques.
  4. Similarly, arithmetic is easier in some bases than others, e.g., binary vs. decimal. You should consider this when you decide on an internal representation for your objects.
  5. Feel free to create versions of your methods that can act recursively; possessing such facility can be a handy tool for all kinds of things involving repetitive calculations.
  6. NEW: Here is a test program that you can use/adapt for your purposes. Note that this program simply tests several of the methods in the class, it DOES NOT catch any exceptions thrown, so it will stop with the first un-implemented method test. You may want to modify this to allow the test program to continue and recover gracefully from those exceptions, more like what happens in the real world.
  7. Don't forget the three most important rules for software success:
    1. Commit early and commit often
    2. Test, test, and test again
    3. Start assignments early, using an iterative approach to your development
  8. Here are the links I showed in class about different algorithms. If you do any searching for things on your own, feel free to share them with me and with each other, AS LONG AS THEY ARE ALGORITHMIC DESCRIPTIONS AND NOT ACTUAL CODE.
  9. Here are some image files that will help explain chunification and Russian Peasant Multiplication [RPM]
  10. The escape velocity of Earth is 11,186 meters per second; the escape velocity of Neptune is 23,500 meters per second.

Submission Guidelines: Make a sub-directory in your repository as mentioned above, called homework06 and commit your source code into it. DON'T FORGET TO ADD A COMMIT COMMENT!