Results 1 to 17 of 17
  1. #1
    fatabass is offline Senior Member
    Join Date
    Nov 2011
    Location
    Turkey
    Posts
    380
    Blog Entries
    24
    Rep Power
    3

    Default Question on HashTable, and methods.

    Java Code:
    package myPackage5;
    
    import java.util.Hashtable;
    
    public class Comic extends Hashtable
    {
    	String title;
    	String condition;
    	float basePrice;
    	float price;
    	
    	public static void main(String[] args) 
    	{
    		Hashtable quailtyPriceTable = new Hashtable();
    		quailtyPriceTable.put("fine", 3);
    		
    		Comic myComic = new Comic();
    		
    		myComic.title = "Spider - Man";
    		myComic.condition = "fine";
    		myComic.basePrice = 15F;
    		
    		myComic.setPrice(quailtyPriceTable.getPriceForCondition("fine"));
    		
    		System.out.println(myComic.price);
    		
    	}
    	
    	public void setPrice(int i)
    	{
    		this.price = i*this.basePrice;
    	}
    	
    	public Integer getPriceForCondition(Object key)
    	{
    		Integer o = (Integer) super.get(key);
    		return o;
    	}
    
    }
    The compiler is telling me:


    Exception in thread "main" java.lang.ClassCastException: java.util.Hashtable cannot be cast to myPackage5.Comic
    at myPackage5.Comic.main(Comic.java:23)

    Any help ?

  2. #2
    fatabass is offline Senior Member
    Join Date
    Nov 2011
    Location
    Turkey
    Posts
    380
    Blog Entries
    24
    Rep Power
    3

    Default Re: Question on HashTable, and methods.

    Answering my own question:

    You need to call that method (getPriceForCondition) on a Comic object, you are trying to call it on a HashTable..

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Question on HashTable, and methods.

    You're calling a Comic method "getPriceForCondition(...)" on a HashTable variable, quailtyPriceTable. Why not simply use HashTable's get(...) method?

  4. #4
    fatabass is offline Senior Member
    Join Date
    Nov 2011
    Location
    Turkey
    Posts
    380
    Blog Entries
    24
    Rep Power
    3

    Default Re: Question on HashTable, and methods.

    What I am trying to do is: instead of saying get() method, I am trying to create a method that does exactly the same job, but is called: "getPriceForCondition", so it looks nicer in my code.

    Is it possible?

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Question on HashTable, and methods.

    Quote Originally Posted by fatabass View Post
    What I am trying to do is: instead of saying get() method, I am trying to create a method that does exactly the same job, but is called: "getPriceForCondition", so it looks nicer in my code.

    Is it possible?
    Sure it's possible, but again you have to call the method on a Comic object, not a HashTable object. As an aside, why are you using HashTable and not HashMap? As another aside, I see no reason to use inheritance here but rather composition.

  6. #6
    fatabass is offline Senior Member
    Join Date
    Nov 2011
    Location
    Turkey
    Posts
    380
    Blog Entries
    24
    Rep Power
    3

    Default Re: Question on HashTable, and methods.

    Nevermind..
    Last edited by fatabass; 01-29-2012 at 09:54 PM. Reason: Very stupid answer to my own question.

  7. #7
    fatabass is offline Senior Member
    Join Date
    Nov 2011
    Location
    Turkey
    Posts
    380
    Blog Entries
    24
    Rep Power
    3

    Default Re: Question on HashTable, and methods.

    Java Code:
    package myPackage5;
    
    import java.util.Hashtable;
    
    public class myHashTable extends Hashtable 
    {
    	public Integer getPriceForCondition(Object key)
    	{
    		Integer returnedValue = (Integer) get(key);
    		return returnedValue;
    		
    	}
    }
    Is this an ok solution ?

  8. #8
    fatabass is offline Senior Member
    Join Date
    Nov 2011
    Location
    Turkey
    Posts
    380
    Blog Entries
    24
    Rep Power
    3

    Default Re: Question on HashTable, and methods.

    Of course now quailtyPriceTable is declared as myHashTable.

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Question on HashTable, and methods.

    Quote Originally Posted by fatabass View Post
    Java Code:
    package myPackage5;
    
    import java.util.Hashtable;
    
    public class myHashTable extends Hashtable 
    {
    	public Integer getPriceForCondition(Object key)
    	{
    		Integer returnedValue = (Integer) get(key);
    		return returnedValue;
    		
    	}
    }
    Is this an ok solution ?
    Again, why HashTable and not HashMap? Why not use generics? Again, consider composition over inheritance.

  10. #10
    fatabass is offline Senior Member
    Join Date
    Nov 2011
    Location
    Turkey
    Posts
    380
    Blog Entries
    24
    Rep Power
    3

    Default Re: Question on HashTable, and methods.

    I am just learning JAVA and I have no idea what a HashMap is and what composition means.

    I am learning HashTables now, and this is just an example I am working on, I tried to spiceup it alittle bit with my inheritance knowledge.

  11. #11
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Question on HashTable, and methods.

    Quote Originally Posted by fatabass View Post
    I am just learning JAVA and I have no idea what a HashMap is
    Think of HashMap as the more up-to-date version of HashTable, one created from the get-go to be part of the Collections framework. If you look at the HashTable API, you'll see this quote:

    As of the Java 2 platform v1.2, this class was retrofitted to implement the Map interface, making it a member of the Java Collections Framework. Unlike the new collection implementations, Hashtable is synchronized. If a thread-safe implementation is not needed, it is recommended to use HashMap in place of Hashtable. If a thread-safe highly-concurrent implementation is desired, then it is recommended to use ConcurrentHashMap in place of Hashtable
    This is equivalent to the difference between using Vector and using ArrayList.

    ...and what composition means.
    This is where you build functionality into a class by having it contain objects rather than by having it extend classes. There's a good discussion of this here: Inheritance versus composition: Which one should you choose?

  12. #12
    fatabass is offline Senior Member
    Join Date
    Nov 2011
    Location
    Turkey
    Posts
    380
    Blog Entries
    24
    Rep Power
    3

    Default Re: Question on HashTable, and methods.

    Thank you, I will look at these.

  13. #13
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,755
    Rep Power
    19

    Default Re: Question on HashTable, and methods.

    Put simply, in the world of OO most things are composed rather than inherited.
    For example, a Car does not extend an Engine...it has an Engine.
    Your use of Hashtable above is more like the former than the latter.
    You want a Hashtable (or HashMap) that holds Comics...yet you have declared a Comic that is-a Hashtable.

  14. #14
    fatabass is offline Senior Member
    Join Date
    Nov 2011
    Location
    Turkey
    Posts
    380
    Blog Entries
    24
    Rep Power
    3

    Default Re: Question on HashTable, and methods.

    But then I fixed it. Is my second approach ok ?

    I have a new class : MyHashTableClass which extends HashTable. And there I have that method (method name) I want.

  15. #15
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,755
    Rep Power
    19

    Default Re: Question on HashTable, and methods.

    That's where Generics comes in.
    And, in any case, I'd still say you'd have a class that had a HashMap as an attribute, and then methods that interacted with it. It would be something like a wrapper, which was one of the ways of handling the lack of generics in pre 1.5 Java.

  16. #16
    fatabass is offline Senior Member
    Join Date
    Nov 2011
    Location
    Turkey
    Posts
    380
    Blog Entries
    24
    Rep Power
    3

    Default Re: Question on HashTable, and methods.

    This HashMap sounds really important, I better do some reading on it.
    Thanks.

  17. #17
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,755
    Rep Power
    19

    Default Re: Question on HashTable, and methods.

    I meant to say "Hashtable"...:)
    Shows how little Hashtable gets used.

Similar Threads

  1. Hashtable question. Getting info from a hashtable
    By sindrem in forum New To Java
    Replies: 4
    Last Post: 01-19-2012, 05:00 PM
  2. Question about ResultSetMetadata methods
    By mDennis10 in forum New To Java
    Replies: 5
    Last Post: 10-14-2011, 08:30 AM
  3. Question concerning methods
    By jim01 in forum New To Java
    Replies: 6
    Last Post: 05-18-2011, 02:46 PM
  4. 2 methods with same name Question
    By dienesh77 in forum New To Java
    Replies: 4
    Last Post: 03-22-2011, 01:58 PM
  5. question about abstract methods
    By osval in forum New To Java
    Replies: 1
    Last Post: 07-25-2007, 08:36 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
  •