Fucking Java v.My Professor Sucks

Discussion in 'OT Technology' started by Macbeth, Oct 31, 2009.

  1. Macbeth

    Macbeth That's illogical... OT Supporter

    Joined:
    Mar 1, 2005
    Messages:
    18,408
    Likes Received:
    0
    Location:
    Under your bed.
    Trying to do my programming assignment in my Java class. We're working with ArrayLists right now, and I can't figure this shit out.

    I'm trying to make a program to create a bunch of ArrayLists that store numbers, to which I'm going to add numbers to them, add sets together, minus them from each other and whatnot.

    I can't even get past the basics of this ArrayList shit. If I use a separate class for the adding methods/subracting methods, and whatever, how do I access the different ArrayLists? I feel like I'm missing the basic concept behind ArrayLists, and thus don't know how to use them properly in conjunction with objects and whatnot.

    In case my post makes no sense, I'll try to clarify with the garbage I've been fucking around with (I've commented out shit I don't even want to mess with yet):

    Code:
    import java.util.ArrayList;
    
    public class IntSet {
        ArrayList<Integer> IntSet;
        private int value;
    
        
        public IntSet() {
            ArrayList<String> IntSet = new ArrayList<String>(0);
        }
        
        public IntSet(int value) {
            ArrayList IntSet = new ArrayList<Integer>(value);
        }
        
        public IntSet(ArrayList<Integer> IntSet) {
        }
    
        // public int returnSize() {
        //     return ;
        // }
        
        public int returnElement(int index) {
            int value = IntSet.get(index);
            return value;
        }
        
        public void add(int a) {
            this.IntSet.add(a);
        }
        
        public void remove(int a) {
            this.IntSet.remove(a);
        }
        
        public boolean equals(ArrayList<Integer> IntSet) {
            int check = 0;
            for (int i = 0; i < this.IntSet.size(); i++) {
                if (this.IntSet.get(i) == IntSet.get(i)) {
                    check++;
                }
                else
                    check--;
            }
            if (check == this.IntSet.size())
                return true;
            else
                return false;
        }
        /*
        public void plus(ArrayList<Integer> IntSet) {
            
        }
        
        public void minus(ArrayList<Integer> IntSet) {
            
        }
        
        public void intersect(ArrayList<Integer> IntSet) {
            
        }
        */
        
    }
    
    Code:
    import java.util.Scanner;
    import java.util.ArrayList;
    
    public class InSetDriver {
        public static void main(String[] args) {
            IntSet IntSet = new IntSet();
            Scanner stdIn = new Scanner(System.in);
            
            String inputV;
            
            System.out.println("Enter IntSet 1: ");
            IntSet.setArray(stdIn.nextInt());
            System.out.println(IntSet.IntSet.get(0));
            
            
        }
    }
     
  2. DouggieJ

    DouggieJ OT Supporter

    Joined:
    Nov 1, 2007
    Messages:
    8,126
    Likes Received:
    0
    Location:
    New York, NY USA
    I'm not really sure what you're trying to do here... can you post the exact assignment?
     
  3. Macbeth

    Macbeth That's illogical... OT Supporter

    Joined:
    Mar 1, 2005
    Messages:
    18,408
    Likes Received:
    0
    Location:
    Under your bed.
    .
     
  4. Macbeth

    Macbeth That's illogical... OT Supporter

    Joined:
    Mar 1, 2005
    Messages:
    18,408
    Likes Received:
    0
    Location:
    Under your bed.
    I feel that if I understand the basic premise of the assignment, I can get around doing the more complicated things, but as is, I barely understand how to utilize the ArrayList and Classes and Objects. I'm most likely going into the tutoring center on Monday, but I'd love any help now, as the assignment is due Sunday at 11:59 PM.
     
  5. DouggieJ

    DouggieJ OT Supporter

    Joined:
    Nov 1, 2007
    Messages:
    8,126
    Likes Received:
    0
    Location:
    New York, NY USA
    I guess you can't cheat by using TreeSet?

    I'll try to help but no guarantees... I've already had more than a few scotches tonight.
     
  6. DouggieJ

    DouggieJ OT Supporter

    Joined:
    Nov 1, 2007
    Messages:
    8,126
    Likes Received:
    0
    Location:
    New York, NY USA
    Look at what I've done so far here, it should help you:

    Code:
    import java.util.*;
    public class IntSet {
       // Data
    
       private ArrayList<Integer> theList;
    
       // constructors
    
       public IntSet () {
       	theList = new ArrayList<Integer>();
       }
    
       public IntSet (Integer newInt) {
            this();
            this.add(newInt);
       }
    
       public IntSet(int i) {
            this(new Integer(i));
       }
       
       public IntSet (IntSet theSet) {
           theList = new ArrayList<Integer>(theSet.theList);
       }
      
    
       // public methods 
       
       public int size () {
         return (theList.size());
       }
    
       public int get(int index) {
         return (theList.get(index).intValue());
       }
    
       public void add (int i) {
           this.add(new Integer(i));
       }
       
       public void add (Integer newInt) {
         if (theList.isEmpty()) {
            theList.add(newInt);
         } else if (theList.contains(newInt)) {
            return; // integer already in set
         } else { 
           for (int index = 0; index < theList.size(); index++) {
              if (theList.get(index).intValue() > newInt.intValue()) { 
                 theList.add(index, newInt);
                 return; 
              }
            }
            theList.add(newInt); // largest integer in set
         }
       }
    
       public void remove (int i) {
          int index = theList.indexOf(new Integer(i));
          if (index >= 0)
              theList.remove(index);
       }
    
    	public boolean equals (IntSet theSet) {
    	   
    		if (size() != theSet.size())
    			return false;
    		for (int index = 0; index < size(); index++)
    			if (get(index) != theSet.get(index))
    				return false;
    		return true;
    	}
    
       // DEBUG test code
    
       public void print () {
    
         System.out.println(theList.toString());
       }
    
       public static void main (String[] argv) {
          IntSet mySet = new IntSet(10);
          try {
             mySet.add(10);
             mySet.add(5);
             mySet.add(20);
             mySet.add(-5);
             mySet.add(30);
          } catch (java.lang.Exception e) {
            e.printStackTrace();
          }
          mySet.print();
          IntSet newSet = new IntSet(mySet);
          newSet.print();
    	   if (newSet.equals(mySet))
    		   System.out.println("mySet is equal to newSet");
    	   else 
    		   System.out.println("mySet and newSet differ");
    
          mySet.remove(20);
          mySet.print();
          newSet.print();
    	   if (newSet.equals(mySet))
    		   System.out.println("mySet is equal to newSet");
    	   else 
    		   System.out.println("mySet and newSet differ");
    	   
          System.out.println(newSet.get(1));
       }
    }
    
    
     
    Last edited: Oct 31, 2009
  7. hsmith

    hsmith OT Supporter

    Joined:
    Feb 24, 2002
    Messages:
    124,630
    Likes Received:
    730
    Location:
    Your mother.
    :rofl: you get to used built in data structures?

    What kind of nancy shit is this?
     
  8. ge0

    ge0 New Member

    Joined:
    Oct 31, 2005
    Messages:
    8,398
    Likes Received:
    0
    Location:
    JERSEY
    you might want to use the methods on the Collections class such as Contains, binarySearch, etc..

    Also it might be also easier to extend the List class from your main class.

    Are you allowed to sort the collection?
     
    Last edited: Oct 31, 2009

Share This Page