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 14 of 14

Thread: Getting variable from another class

  1. #1
    Member
    Join Date
    Oct 2021
    Posts
    63
    Thanks
    10
    Thanked 0 Times in 0 Posts

    Default Getting variable from another class

    I'm working on implementing a code where the output I'm looking for is the following

    === LINKED BAG 220 JAVA ==========================================================
    [+] Creating a CSC220 LinkedBag...
    [+] Adding.... these items to the bag: A _ _ G Bb A _ u n o A o d Bb A A l l
    [>] The bag now contains 18 string(s): l l A A Bb d o A o n u _ A Bb G _ _ A
    [+] Creating... a 2D test array with the below contents:
    A A A A A A
    B A Bb B Bb B
    C B _ A
    n u l l
    [+] Removing 2D test array items from the bag...
    [-] Converting 2D array to 1D...
    [-] Removing duplicates in 1D array...
    [>] The final 1D array now contains: A B Bb C _ n u l
    [-] Removing the final 1D array items from the bag...
    [>] The bag now contains 4 string(s): G o o d
    === LINKED BAG 220 JAVA ==========================================================

    Here are the driver class (I CANNOT CHANGE ANYTHING HERE)
    public class LinkedBagCSC220JavaDriver {
     
        public static void main(String[] args) {
            System.out.println("=== LINKED BAG 220 JAVA ==========================================================");
            System.out.println("[+] Creating a CSC220 LinkedBag...");
            PrimaryDataStructureBagInterface<String> csc220Bag = new LinkedBag<>();
            testAdd(csc220Bag);
            testRemoveAllOccurrences(csc220Bag);
            System.out.println("=== LINKED BAG 220 JAVA ==========================================================");
        }
     
        private static void displayBag(PrimaryDataStructureBagInterface<String> aBag) {
            System.out.print("[>] The bag now contains " + aBag.getCurrentSize() + " string(s): \t");
            Object[] bagArray = aBag.toArray();
            for (Object bagArray1 : bagArray) {
                System.out.print(bagArray1 + " ");
            }
            System.out.println();
        }
     
        private static void testRemoveAllOccurrences(PrimaryDataStructureBagInterface<String> aBag) {
            // Removing all occurrences of the given entries from a bag
            System.out.println("[+] Creating... a 2D test array with the below contents: \t");
            String[][] testArray = {
                    { "A", "A", "A", "A", "A", "A" },
                    { "B", "A", "Bb", "B", "Bb", "B" },
                    { "C", "B", "_", "A" },
                    { "n", "u", "l", "l" }
            };
     
            for (String[] row : testArray) {
                System.out.print("\t\t\t\t\t");
                for (String col : row) {
                    System.out.print(col + " ");
                }
                System.out.println("");
            }
     
            aBag.removeAllOccurrences(testArray); // * testArray does not contain the bag with GOOD letters
            System.out.println(); // ! REMOVE later
            System.out.println(); // ! REMOVE later
            displayBag(aBag);
        }
     
        private static void testAdd(PrimaryDataStructureBagInterface<String> aBag) {
            // Adding strings
            String[] contentsOfBag = {
                    "A", "_", "_", "G", "Bb", "A", "_", "u", "n",
                    "o", "A", "o", "d", "Bb", "A", "A", "l", "l" // I'm looking for aBag variable with this content where I'll remove the common occurrence with newOneDarray from my removeAllOccurence method
            };
            System.out.print("[+] Adding.... these items to the bag: \t");
            for (String entry : contentsOfBag) {
                aBag.add(entry);
                System.out.print(entry + " ");
            }
            System.out.println();
            displayBag(aBag);
        }
    }

    Here is the removeAllOcurrence method I'm working on (this is just the method which is a part of another class that I can change)
     public boolean removeAllOccurrences(T[][] entries) {
     
            System.out.println("[+] Removing 2D test array items from the bag...");
            System.out.println("[-] Converting 2D array to 1D...");
            // convert 2D array into 1D ArrayList
            List<T> oneDArray = new ArrayList<>();
            for (int row = 0; row < entries.length; row++) {
                for (int col = 0; col < entries[row].length; col++)
                    oneDArray.add(entries[row][col]);
                // System.out.println(oneDArray);
            }
            // System.out.println("1D Array : " + oneDArray);
            System.out.println("[-] Removing duplicates in 1D array...");
     
            // using stream to remove all duplicate from 1D ArrayList
            List<T> newOneDArray = oneDArray.stream()
                    .distinct()
                    .collect(Collectors.toList());
            // System.out.println("1D Array after removing duplicate into new list : " +
            // newOneDArray);
            System.out.print("[>] The final 1D array now contains: ");
            for (int i = 0; i < newOneDArray.size(); i++) {
                System.out.print(newOneDArray.get(i) + " "); // print every element of newOneDArray (ArrayList)
            }
     
            System.out.println();
            System.out.println("[-] Removing the final 1D array items from the bag...");
            System.out.println();
            System.out.println();
            System.out.println();
            System.out.println("//DEBUGGING// array entries : " + Arrays.deepToString(entries));
            System.out.println("//DEBUGGING// oneDArray : " + oneDArray);
            System.out.println("//DEBUGGING// newDArray without duplicates : " + newOneDArray.toString());
     
            return true;
        }

    Here is my output
    === LINKED BAG 220 JAVA ==========================================================
    [+] Creating a CSC220 LinkedBag...
    [+] Adding.... these items to the bag:  A _ _ G Bb A _ u n o A o d Bb A A l l 
    [>] The bag now contains 18 string(s):  l l A A Bb d o A o n u _ A Bb G _ _ A 
    [+] Creating... a 2D test array with the below contents: 
                                            A A A A A A 
                                            B A Bb B Bb B 
                                            C B _ A 
                                            n u l l 
    [+] Removing 2D test array items from the bag...
    [-] Converting 2D array to 1D...
    [-] Removing duplicates in 1D array...
    [>] The final 1D array now contains: A B Bb C _ n u l 
    [-] Removing the final 1D array items from the bag...
     
     
    //As you can see none of those variable return me the variable with the content  l l A A Bb d o A o n u _ A Bb G _ _ A 
    //DEBUGGING// array entries : [[A, A, A, A, A, A], [B, A, Bb, B, Bb, B], [C, B, _, A], [n, u, l, l]]
    //DEBUGGING// oneDArray : [A, A, A, A, A, A, B, A, Bb, B, Bb, B, C, B, _, A, n, u, l, l]
    //DEBUGGING// newDArray without duplicates : [A, B, Bb, C, _, n, u, l]
     
     
    [>] The bag now contains 18 string(s):  l l A A Bb d o A o n u _ A Bb G _ _ A 
    === LINKED BAG 220 JAVA ==========================================================

    I'm on the part of removing all Occurrence from newOneDarray to the aBag from the driver class. I was thinking of iterating going through each newOneDArray and aBag and removing the common occurrence inside aBag. The issue is I do not know how to get access to aBag inside that method. removeAllOcurrence. Is there a way? (Knowing that I cannot modify the driver class). Or do you have any suggestions on how to achieve what I'm looking for?

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Getting variable from another class

    how to get access to aBag inside that method.
    One way is to pass a reference to aBag when the method is called.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Oct 2021
    Posts
    63
    Thanks
    10
    Thanked 0 Times in 0 Posts

    Default Re: Getting variable from another class

    but it means modify my driver right? (notice that I cannot modify it as I stated)

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Getting variable from another class

    Which aBag are you asking about? I see several places where aBag is declared as a local variable to a method as its argument:
    private static void displayBag(PrimaryDataStructureBagInterface<String > aBag) {
    private static void testRemoveAllOccurrences(PrimaryDataStructureBagIn terface<String> aBag) {
    private static void testAdd(PrimaryDataStructureBagInterface<String> aBag) {
    The only way to get access to those variables is if they are passed as an argument to the method being called.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Oct 2021
    Posts
    63
    Thanks
    10
    Thanked 0 Times in 0 Posts

    Default Re: Getting variable from another class

    within "private static void testAdd(PrimaryDataStructureBagInterface<String> aBag) {". The content of aBag in there is what I'm looking to work on in my removeAllOccurence method.
    If I pass this variable aBag into my removeAllOccurence methid, it will return "a cannot find symbol" error because remove AllOccurence is located in another class. That class is Linkedbag that you can see here : https://www.javaprogrammingforums.co...tml#post174467

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Getting variable from another class

    What class is the removeAllOccurence method in?
    What class is the aBag object?
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Member
    Join Date
    Oct 2021
    Posts
    63
    Thanks
    10
    Thanked 0 Times in 0 Posts

    Default Re: Getting variable from another class

    "public class LinkedBagCSC220JavaDriver" is the aBag object
    "public final class LinkedBag<T> implements PrimaryDataStructureBagInterface<T> {" is the removeAllOccurence class

    you can see LinkedBag class here : https://www.javaprogrammingforums.co...tml#post174467 -- I did updated this class with since this post, with the removeAllOcurrence that you see on this current post (above)

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Getting variable from another class

    "public class LinkedBagCSC220JavaDriver" is the aBag object
    Can you post the code where aBag is declared to be an instance of the LinkedBagCSC220JavaDriver class?

    The only declarations I see for aBag is as an argument to a method. To see where the actual object is created you need to find where the method is called. Its argument will be what is in aBag.
       SomeClass anInstance = new SomeClass();   // declare a variable and give it a value
       someMethod(anInstance);        // call a method passing an instance of SomeClass
       ...
       void someMethod(SomeClass theInstance) //  theInstance is the value from anInstance (an instance of SomeClass)
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Member
    Join Date
    Oct 2021
    Posts
    63
    Thanks
    10
    Thanked 0 Times in 0 Posts

    Default Re: Getting variable from another class

    Actually, when I review the code, you are right. aBag is an argument to a method.
    In fact, I think the content that I'm trying to work on comes from the csc220Bag that you can see into the void main. But in the displayBag method, they turn the csc220Bag into an object, right? I'm talking about the line in displayBag saying " Object[] bagArray = aBag.toArray();"

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Getting variable from another class

    PrimaryDataStructureBagInterface<String> csc220Bag = new LinkedBag<>();
    Here csc220Bag is declared as an instance of PrimaryDataStructureBagInterface and given a value of an instance of the LinkedBag class. That is allowed because LinkedBag implements the interface.
    If the removeAllOccurence method is in the LinkedBag class, then it does not need a reference to the class that it is already in.
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Member
    Join Date
    Oct 2021
    Posts
    63
    Thanks
    10
    Thanked 0 Times in 0 Posts

    Default Re: Getting variable from another class

    So you mean that I can call csc220Bag within my removeAllOccurence knowing that this is declared in the driver class?

    Cause if I try to call it into the removeAllOccurence, I get an cannot resolve symbol

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Getting variable from another class

    The csc220Bag variable contains a reference to the class that contains the removeAllOccurence method.

    If the method is in the class, then the 'this' variable will allow you to access all the variables in that class.
    If you don't understand my answer, don't ignore it, ask a question.

  13. #13
    Member
    Join Date
    Oct 2021
    Posts
    63
    Thanks
    10
    Thanked 0 Times in 0 Posts

    Default Re: Getting variable from another class

    Can you give me a line code example ? with the 'this'?

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Getting variable from another class

    class SomeClass {
       String aString = " a";
       void someMethod(String aString) {
          this.aString = aString;    // copy passed value to local variable
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Replies: 18
    Last Post: March 30th, 2013, 09:11 AM
  2. How do I use a variable from main class in another class?
    By El Batman in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 4th, 2013, 07:34 AM
  3. Replies: 149
    Last Post: February 19th, 2013, 10:04 PM
  4. Replies: 1
    Last Post: February 14th, 2013, 07:30 PM
  5. Must I add a Class Variable?
    By maress in forum Java Theory & Questions
    Replies: 1
    Last Post: February 24th, 2011, 04:40 AM

Tags for this Thread