Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 17 of 17

Thread: Help for my homework with big numbers and ArrayList

  1. #1
    Junior Member
    Join Date
    Nov 2020
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Question Help for my homework with big numbers and ArrayList

    Hello everybody,

    I need help for a project in Java. I am a fresh beginner and I do not understand all the whys and hows.
    We were asked to create a Java Class BigNum which should enable us to calculate with very large integers, but I am already stuck at the beginning. Here is what I have so far :
    import java.util.ArrayList;
    public class BigNum {
        public static void main(String[] args) { 
        ArrayList<Integer> number = new ArrayList<Integer>();
        number.add(Integer.valueOf(3));
        number.add(Integer.valueOf(2)); 
        number.add(Integer.valueOf(1)); 
        System.out.println(number);
        }
    }
    The next step should be adding a method that can operate usign "BigNum n = new BigNum(512);" and producing automatically the ArrayList. Also it is said to use the modulo operator % to perform this task...

    I am totally lost, could someone help me with this ?
    Thank you for reading this

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Help for my homework with big numbers and ArrayList

    adding a method that can operate usign "BigNum n = new BigNum(512);"
    That statement is calling the class's constructor. See the tutorial on defining and using a constructor:
    https://docs.oracle.com/javase/tutor...structors.html

    use the modulo operator % to perform this task
    I am not sure what that means. What task is supposed to use the modulo operator?
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    Ghinou (November 15th, 2020)

  4. #3
    Junior Member
    Join Date
    Nov 2020
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Help for my homework with big numbers and ArrayList

    I do not understand either why we have to use the modulo...

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Help for my homework with big numbers and ArrayList

    why we have to use the modulo
    What is the constructor supposed to do with the number it receives?
     BigNum bn = new BigNum(512);
    What should the constructor do with the value: 512?
    If you don't understand my answer, don't ignore it, ask a question.

  6. #5
    Junior Member
    Join Date
    Nov 2020
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Help for my homework with big numbers and ArrayList

    We are supposed on the next question to add up two BigNum and finally to multiply two BigNum.
    Thank you very much for your help !

  7. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Help for my homework with big numbers and ArrayList

    You did not answer this question. It needs to be answered before you can write any code.
    What should the constructor do with the value: 512?
    If you don't understand my answer, don't ignore it, ask a question.

  8. #7
    Junior Member
    Join Date
    Nov 2020
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Help for my homework with big numbers and ArrayList

    Quote Originally Posted by Norm View Post
    You did not answer this question.
    Oh, I am sorry. I did not understand your question in that way !
    The constructor should only put the number in an ArrayList, but if it is an ArrayList, how could I multiply these two numbers ?
    I am sorry that I am so lost...

  9. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Help for my homework with big numbers and ArrayList

    constructor should only put the number in an ArrayList
    What is the purpose of putting a number in a list? A list is used to hold multiple values so they can be kept together and searched for details from each item that is in the list. If there is only one item in the list, there is no need for a list.

    What does your assignment say about the value passed to the constructor and what should go in the list?
    If you don't understand my answer, don't ignore it, ask a question.

  10. #9
    Junior Member
    Join Date
    Nov 2020
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Help for my homework with big numbers and ArrayList

    I wanted to avoid putting my asignement online, but here it is (I have not more infos about what I should do than this precisly) :

    Implement the algorithms using Java. Use test cases to verify the results on some data.
    In this project we propose to make calculations with very large integers. We assume positive integers. For that we define a BigNum class. This class models an integer as an ArrayList where each of the elements corresponds to a number:
    public class BigNum {
    ArrayList number;
    public: ...}
    The first element of the "number" list of the BigNum class always corresponds to units, the second to tens, the third to hundreds, and so on. Since a list is unbounded, we can easily imagine defining very very large integers, ie having thousands of digits.
    1.1 Write an operation that allows you to create a BigNum from an integer (of type int) passed as a parameter. For example, the call:
    BigNum n = new BigNum (512);
    will initialize the "number" list of the class to the sequence 5, 1 and 2 (the first element being 2, the second 1 and the third 5).
    Note: use for this successive divisions by 10. In Java, the remainder of an integer division is given by the operator% (modulo). For example, 14% 4 => 2.
    1.2 Write a "display" operation which displays the value of a BigNum on the screen.

  11. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Help for my homework with big numbers and ArrayList

    Ok, that looks reasonable. So you need to write code that extracts digits one at a time from the value passed to the constructor and add them to the list. This tip says how to to it:
    Note: use for this successive divisions by 10 ... remainder of an integer division is given by the operator %
    Given a value of 512 how do you get the 2? Hint: Use modulus
    If you don't understand my answer, don't ignore it, ask a question.

  12. #11
    Junior Member
    Join Date
    Nov 2020
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Help for my homework with big numbers and ArrayList

    To get the 2 from 512, I need to use %10. Is this right ?
    But then how to extract the 1 and the 5 ?

  13. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Help for my homework with big numbers and ArrayList

    To get the 2 from 512, I need to use %10.
    But then how to extract the 1 and the 5 ?
    Ok, you know how to get the right most digit (2) from 512.
    To get the next digit (1), how do you move the 1 to right most position where the 2 was? Hint: use division
    If you don't understand my answer, don't ignore it, ask a question.

  14. #13
    Junior Member
    Join Date
    Nov 2020
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Help for my homework with big numbers and ArrayList

    I may have find the solution with your help, could it be :
    int x = 512;
    System.out.println(x%10);
    int y = 512/10 ;
    System.out.println(y%10);
    int z = y/10 ;
    System.out.println(z%10);
    Is it the right logic ? It does give me
    2
    1
    5
    !!!

  15. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Help for my homework with big numbers and ArrayList

    Is it the right logic ?
    That may work for a number with 3 digits but what if the number has more or less than 3 digits: 1234 or 54? The code needs to be able to do the modulus and divide as many times as needed. That means using a loop.
    Change the code to use a loop.
    If you don't understand my answer, don't ignore it, ask a question.

  16. #15
    Junior Member
    Join Date
    Nov 2020
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Help for my homework with big numbers and ArrayList

    I think I have the answer, but I also think that it is not the straightest way to go :
    import java.util.ArrayList;
    public class BigNum {
        public static ArrayList<Integer> MakeBigNum(int a) {
            String num = Integer.toString(a);
            System.out.println(num);
            System.out.println(num.length());
            ArrayList<Integer> number = new ArrayList<Integer>();
            number.add(Integer.valueOf(a%10));
            for (int i=1; i<num.length(); i++){
                double c =(a/(Math.pow(10,i)));
                int b = (int) c;
                number.add(Integer.valueOf(b%10));
                System.out.println(b);
            }
            return number;
        }
        public static void main(String[] args) { 
        ArrayList<Integer> n = MakeBigNum(512); 
        System.out.println(n);
        ArrayList<Integer> m = MakeBigNum(1234); 
        System.out.println(m);
        ArrayList<Integer> k = MakeBigNum(54); 
        System.out.println(k);
        }
    }
    Thank you enormously for your help ! As this homework is due for mid-december, I will continue first with my other assignment and then come back to you if I still have problems for the rest of the homework !

    But either way, thank you very much for helping me, I would never be able to do this work by myself

    --- Update ---

    I will of course leave out all the prints that I made and the examples

  17. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Help for my homework with big numbers and ArrayList

    I am glad you have gotten the code to work. However, The code does not need to use String values in the makeBigNum method. All computations should be done with int variables and values.
    Also the math.pow method is not needed.

    The logic would be something like this:
    set value
    begin loop
    get rightmost digit of value using modulus
    shift value right one digit by division
    continue loop if value is > 0
    If you don't understand my answer, don't ignore it, ask a question.

  18. #17
    Junior Member
    Join Date
    Nov 2020
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Help for my homework with big numbers and ArrayList

    Thanks for helping me this much out !
    I improved the code, and here is what I ended up with, which works :
    public static ArrayList<Integer> MakeBigNum(int a) {
            ArrayList<Integer> number = new ArrayList<Integer>();
            for (int b=a; b>0;){
                number.add(Integer.valueOf(b%10));
                 b =(b/10);
            }
            return number;
        }

Similar Threads

  1. Java ArrayList Homework Help
    By Woosh in forum Object Oriented Programming
    Replies: 3
    Last Post: March 1st, 2014, 08:14 AM
  2. how do i add ID numbers to ArrayList entires
    By Amethyst in forum Java Theory & Questions
    Replies: 9
    Last Post: November 22nd, 2013, 05:54 AM
  3. Replies: 3
    Last Post: November 18th, 2013, 11:55 AM
  4. Replies: 6
    Last Post: October 26th, 2013, 01:59 PM
  5. Replies: 4
    Last Post: February 10th, 2013, 11:58 PM

Tags for this Thread