Results 1 to 2 of 2
  1. #1
    bengiles89 is offline Member
    Join Date
    Mar 2010
    Posts
    7
    Rep Power
    0

    Default 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!

    Java 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 */
    }

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,543
    Rep Power
    5

    Default 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
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

Similar Threads

  1. Type cast in case of classes
    By bigo005 in forum New To Java
    Replies: 4
    Last Post: 12-10-2013, 08:04 PM
  2. Java, MySQL and Data Type Float
    By digidigdj in forum AWT / Swing
    Replies: 0
    Last Post: 03-11-2010, 07:42 PM
  3. Replies: 6
    Last Post: 02-21-2010, 12:54 AM
  4. compile classes with no main type
    By jan2321 in forum Eclipse
    Replies: 2
    Last Post: 07-20-2009, 08:01 PM
  5. Replies: 0
    Last Post: 04-07-2008, 07:56 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •