Help creating new data type VeryLargeInteger w/out premade Java classes

• 01-22-2014, 08:27 PM
bengiles89
Help creating new data type VeryLargeInteger w/out premade Java classes
This is a homework help post, but I'm not expecting anyone to do my work for me. I really do want to learn.
Any insight would be greatly appreciated as would any pertinant reading you could send my way!

Tried my hand at the humanities at University, got distracted by school and the city, and eventually took a few semesters off to work full time. I'm hungry to succeed now, but I'm getting frustrated by my hazy memory of Java's syntax and programming logic.

What I'm doing about it: googling the shit out of my problems, consulting you fine readers, consulting my friends, and yesterday I signed up for Lynda.com. I'm hoping 30hrs+ or so of watching, rewatching, and analyzing the example code will catch me up before I get too behind in CS302

** Assignment Prompt **

Integer types are very convenient, but their limited width and precision makes them unsuitable for some applications where precision is more important than speed. Develop a class VeryLargeInteger that can handle arbitrary long integer numbers (both negative and positive) and the basic arith- metic operations (addition, subtraction, multiplication, division, and remainder).
Hint: The number could be represented as string, the sign could be represented either as boolean or as part of the string.
Note: Implementations of addition/subtraction through repeated use of a constant incremen- t/decrement will not be accepted. Implementations of multiplication and division that rely only on addition and subtraction will not be accepted.

** Where I'm at **

I'm honestly pretty lost. I know I'm going to have to create a separate tester to call on the VeryLargeInteger class and it's math methods. For the new data type, should I convert the integer/string into an array in order to handle the large length of the number? I know he wants us to use recursion for the math methods. My gut tells me addition and subtraction will be slightly easier than multiplication and division. I know I'll have to reference the other methods for division. We aren't allowed to use the BigInteger class.

Once again, I would sincerely appreciate any replies (even insults). I know everyone's busy, and this is my problem. Any relevant Java links you can send my way would be great! Or any barebones examples of how I sould construct any of the methods. Thanks!

Code:

```import java.util.ArrayList;   /** ∗ VeryLargeInteger (VLI) is a class for arbitrary precision integer computation */ public class VeryLargeInteger {     private int[] num1;     private int[] num2;     private int[] num3;   /** ∗ Constructs a new VLI object from a long integer */ VeryLargeInteger(long) {     VeryLargeInteger veryLargeInteger = new VeryLargeInteger();     }   /** ∗ Constructs a new VLI object from a String */ VeryLargeInteger(String) {         }   /** ∗ Computes this+other and returns the result in a new object */ VeryLargeInteger add(VeryLargeInteger other) {     int carry = 0, pos = 0;     while(pos<num1.size() && num2.size()) {         int value = num2[pos] + num1[pos] + carry;         num3[pos++] = value % base;         carry = value / base;     }     // a couple extra whiles     if (carry!=0) {         num3[pos++] = carry;     }     }   /** ∗ Computes this−other and returns the result in a new object */ VeryLargeInteger sub(VeryLargeInteger other) {     /* YOUR CODE */     }   /** ∗ Computes this∗other and returns the result in a new object */ VeryLargeInteger mul(VeryLargeInteger other) {     /* YOUR CODE */     }   /** ∗ Computes this/other and returns the result in a new object */ VeryLargeInteger div(VeryLargeInteger other) {     /* YOUR CODE */     }   /** ∗ Computes this%other and returns the result in a new object */ VeryLargeInteger mod(VeryLargeInteger other) {     /* YOUR CODE */     }   /** ∗ Returns the textual representation of this VLI */ String toString() {     /* YOUR CODE */     }   /* YOUR CODE */ }```
• 01-22-2014, 10:34 PM
jim829
Re: Help creating new data type VeryLargeInteger w/out premade Java classes
This is not trivial. But you might want to consider that multiplication is just repeated addition and division is repeated subtraction. There are certainly more direct methods that are more efficient. And you can't use the BigInteger class for your assignment. But I assume you could use it to check the correctness of your own methods.

Regards,
Jim