Questions regarding class


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 5 of 5

Thread: Questions regarding class

  1. #1
    Join Date
    Jan 2005
    Posts
    45

    Questions regarding class

    Here is a problem that I found in a book:

    Assume that a class C has one instance variable: a 1-dimensional array named list that can hold objects
    of class T. Assume class T includes an equals method that returns true if a pair of variables refer to the
    same object, and false otherwise. Write an instance method called allSame that returns true if all the
    objects in list refer to the same object and false otherwise. You should assume that the array is not
    null, but items in it may be.

    Here's my code

    public class T {

    public boolean equals(T x) {
    return this == x;
    }
    }

    ----------------------------------------------------------------------------------------------

    public class C {

    private T[] list;

    public C(int size) {
    T[]list = new T[size];
    }

    public boolean allSame() {
    boolean same = false;
    for (int i = 1; i < list.length && list[i] != null; i++) {
    same = list[i].equals(list[i-1]);
    }
    return same;
    }

    public void assign(T x, int n) {
    list[n] = x;
    }

    public T get(int n) {
    return list[n];
    }

    }

    My question is: how can I assign values of type T to the array.of type C? I keep having these errors:

    > T x= new T();

    > C var = new C(22)

    > var.get(5).assign(5)

    Error: No 'assign' method in 'T'
    > var.get(5).assign(x,5)

    Error: No 'assign' method in 'T'
    > var.assign(x,5)

    java.lang.NullPointerException:
    at C.assign(C.java:18)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)

  2. #2
    Join Date
    Jan 2005
    Posts
    45
    Anyone want to answer my question?

  3. #3
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560
    A statement like:

    var.get(5).assign(5); assumes that the get(5) retrieves a not null object (class T) that has a method called assign(TObject, int). But that is the method of your C class, not th T class.

    Also, your equals and allSame methods shoud be coded like:

    Code:
      public boolean equals(Object x) { // compares on address
        return this == x;
      }
    -----
    // if same address then same address in hashset
    //  this logic also imples that nulls are not considered when comparing elements 
    public boolean allSame() {
        HashSet hs=new HashSet();
        for (int i=1; i<list.length; i++) {
          if (list[i] != null) hs.add(list[i]);
        }
        return hs.size()==1;
      }
    eschew obfuscation

  4. #4
    Join Date
    Jan 2005
    Posts
    45
    Hi. Thanks for replying

    What is Hashset, anyway?

  5. #5
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560
    A HashSet is a collection of objects. When u add an object to the HashSet then that object's equals() -method is used for comparing the new object w. the ones already there. If there already is an object in the set that equals() the new one then that object is replaced by the new one.
    So, when the equals method is comparing the objects addresses, then you know that if you add, say, 100 elements to the HashSet and the HashSet still has a size() of 1, that all the added elements are the same element.

    You should look into java's Collection class and its descendants. By implementing these you can often reduce and simplify your code considerably.
    eschew obfuscation

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center