Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * APNum class
- * You must use basic ADTs in your implementation.
- * The number of digits in an APnum should only be limited by the availability of memory
- *
- * Don't forget about other classes you need to write.
- * Please make sure your code is readable. Code style will be marked as well as code correctness.
- */
- int num;
- int x = 0; //Used in APnextdigit
- public APnum(){
- this.num=0;
- }
- private int getNum(APnum n){
- return n.num;
- }
- public class APnum {
- /**
- * Adds a digit represented by the character supplied in the second argument as the next
- * least significant digit to the value of the fi rst argument.
- * (Thus, the value of the first argument is updated.)
- * @param n ADT argument
- * @param c one of the characters '0', '1', '2', '3', '4', '5', '6', '7', '8' or '9'.
- */
- public static void APin(APnum n, char c){
- num = getNum(n)*10 + (Integer.parseInt(Character.toString(c))).intValue();
- }
- /**
- * Takes as its input a reference to an APnum and complements i
- * @param n ADT argument
- */
- public static void APneg(APnum n){
- num = getNum( n) * (-1);
- }
- /**
- * Adds the APnum supplied in the first argument to the APNumber supplied in the second
- * argument. The value of the first APNumber is updated with the new value while the value
- * of the second APNumber remains unchanged
- * @param operand1 first ADT argument
- * @param operand2 second ADT argument
- */
- public static void APadd(APnum operand1, APnum operand2){
- num = getNum(operand1) + getNum(operand2);
- // Fill your code in here, required time O(n)
- }
- /**
- * Compares the values of the two APnums supplied as arguments and returns true if
- * they are equal and false otherwise. The values of the supplied APnums must not change
- * @param operand1 first ADT argument
- * @param operand2 second ADT argument
- * @return true if operand1 and operand 2 are equal and false otherwise
- */
- public static boolean APeq(APnum operand1, APnum operand2){
- if (getNum(operand1) == getNum(operand2)){
- return true;
- }
- return false;
- }
- /**
- * Compares the values of the two APnums supplied as arguments and returns true if
- * the first is strictly less than the second and false otherwise. The values of the supplied
- * APnums must not change
- * @param operand1 first ADT argument
- * @param operand2 second ADT argument
- * @return true if first operand is strictly less than the second operand and false otherwise
- */
- public static boolean APless(APnum operand1, APnum operand2){
- if (getNum(operand1) < getNum(operand2)){
- return true;
- }
- return false;
- }
- /**
- *
- * @param n ADT argument
- * @return the most significant digit of the absolute value of its argument if the value is non-zero.
- * Otherwise, returns the blank character ' '
- */
- public static char APfirstdigit(APnum n){
- num=Integer.toString(getNum(n)).charAt(0);
- // Fill your code in here, required time O(1)
- }
- /**
- *
- * @param n ADT argument
- * @return the next most significant digit of the absolute value of its argument if it exists.
- * Otherwise, returns the blank character ' '
- */
- public static char APnextdigit(APnum n){
- if(x < Integer.toString(getNum(n)).length()){
- return Integer.toString(getNum(n)).charAt(x);
- }
- return " ";
- }
- public class APsystem {
- /**
- * Place for class members
- * (not necessary, depending on implementation there can be members and there can be no members)
- */
- /**
- * Will be invoked at the start of the tests, initializer of APSystem contents.
- * Can be empty if you don't need to initialize anything in APsystem
- * Tests will start according to the following example:
- * APsystem aps = new APsystem();
- * aps.APinit();
- * APnum n = aps.APzero();
- * APnum.APin(n,'1');
- */
- public void APinit(){
- // Fill your code in here
- }
- /**
- *
- * @return a new APNumber with the value 0
- */
- public APnum APzero(){
- APnum n = new APnum();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement