Results 1 to 12 of 12
  1. #1
    Jack_Tauson_Sr is offline Senior Member
    Join Date
    Nov 2011
    Posts
    103
    Rep Power
    0

    Default Need to convert it into Java !

    Hi ,

    May I know what does the following Python code do. I need to convert it into java.


    Java Code:
    import random
    
    howMany = random.randint(0,1000)
    stats = {}
    for i in range(howMany):
       value = random.randint(0,500)
       stats.setdefault(value,0)
       stats[value]+=1
    for item in stats:
       if stats[item] > 1:
           print item
    Thanks

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,005
    Blog Entries
    7
    Rep Power
    20

    Default Re: Need to convert it into Java !

    It poplates a map with pseudo random numbers and the times the numbers were generated. Finally it prints those numbers that were generated more than once.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    Jack_Tauson_Sr is offline Senior Member
    Join Date
    Nov 2011
    Posts
    103
    Rep Power
    0

    Default Re: Need to convert it into Java !

    What does

    Java Code:
    stats[value]+=1
    do? Can you tell me it's expanded form? Is it gonna be

    Java Code:
    stats[value]= value + 1
    ?

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

    Default Re: Need to convert it into Java !

    It adds 1 to stats[value].
    Please do not ask for code as refusal often offends.

  5. #5
    Jack_Tauson_Sr is offline Senior Member
    Join Date
    Nov 2011
    Posts
    103
    Rep Power
    0

    Default Re: Need to convert it into Java !

    So, stats[4] will be stats[5] after addition?


    Quote Originally Posted by Tolls View Post
    It adds 1 to stats[value].

  6. #6
    Jack_Tauson_Sr is offline Senior Member
    Join Date
    Nov 2011
    Posts
    103
    Rep Power
    0

    Default Re: Need to convert it into Java !

    Well, I have written the following code so far:

    Java Code:
    
    import java.util.Random;
    import java.util.*;
    
    
    public class PythonToJava {
    
    	
    	public static void main(String[] args) {
    		
    		
    		Random rm = new Random();
    		
    		int i = rm.nextInt(1000);
    		
    		HashMap<Integer,Integer> stats = new HashMap<Integer,Integer>();
    		
    		
    		
    		System.out.println("Random Number Generated is: " + i);
    		
    				for (int j = 0; j<i; j++){
    			      
    					int value = rm.nextInt(500);
    					
    					System.out.println("The value of VALUE is " + value);
    					
    					DefaultingMap<Integer,Integer> defaultvalue = new DefaultingMap<Integer,Integer>();
    					defaultvalue.put(value,0);
    					
    			
    				}
    		
    
    	}
    
    }
    
    public class DefaultingMap<Integer, Integer> implements Map<Integer, Integer>
    {
        private final Map<Integer, Integer> map;
        private final Integer defaultValue;
    
        public DefaultingMap(Map<Integer, Integer> map, Integer defaultValue)
        {
            this.map = map;
            this.defaultValue = defaultValue;
        }
    
        @Override public Integer get(Object key)
        {
            Integer ret = map.get(key);
            if (ret == null)
            {
                ret = defaultValue;
            }
            return ret;
        }
    
        @Override public int size()
        {
            return map.size();
        }
    
        // etc
    }
    But getting error at the following line:

    Java Code:
    DefaultingMap<Integer,Integer> defaultvalue = new DefaultingMap<Integer,Integer>(); and at public class DefaultingMap<Integer, Integer> implements Map<Integer, Integer>
    Can anyone explain why?

    Basically I'm trying to set a default value somehow just like setdefault thing works in python. I would appreciate if someone can help me here.

    Thanks
    Last edited by Jack_Tauson_Sr; 05-09-2013 at 06:33 AM. Reason: Updated Source Code !

  7. #7
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    732
    Rep Power
    2

    Default Re: Need to convert it into Java !

    DefaultingMap has no default contructor but you are trying to use one:
    line 27: new DefaultingMap<Integer,Integer>();
    line 43: public DefaultingMap(Map<Integer, Integer> map, Integer defaultValue)

    These two don't match.

  8. #8
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,005
    Blog Entries
    7
    Rep Power
    20

    Default Re: Need to convert it into Java !

    The tricky lines are lines #7 and #8 in the original Python code (see above); you can't translate it without understanding what the Python code actually does. The following Java code does the job:

    Java Code:
    Integer count= stats.get(value);
    if (count == null) 
       count= 0;
    stats.put(value, ++count);
    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

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

    Default Re: Need to convert it into Java !

    Quote Originally Posted by Jack_Tauson_Sr View Post
    So, stats[4] will be stats[5] after addition?
    No.
    stats is an array, so the value of stats[4] will be incremented.
    Please do not ask for code as refusal often offends.

  10. #10
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,005
    Blog Entries
    7
    Rep Power
    20

    Default Re: Need to convert it into Java !

    Quote Originally Posted by Tolls View Post
    No.
    stats is an array, so the value of stats[4] will be incremented.
    Not really, see line #4 in the original post; it's a map or 'associative array' '{ }'; if it were an 'ordinary' array it would've been initialized as '[ ]'; Java is much more verbose than Python (and much cleaner) ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

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

    Default Re: Need to convert it into Java !

    Quote Originally Posted by JosAH View Post
    Not really, see line #4 in the original post; it's a map or 'associative array' '{ }'; if it were an 'ordinary' array it would've been initialized as '[ ]'; Java is much more verbose than Python (and much cleaner) ...

    kind regards,

    Jos
    A bit like Javascript, which is what I was assuming.
    In either case stats[4] += 1, does not become stats[5].
    Please do not ask for code as refusal often offends.

  12. #12
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,005
    Blog Entries
    7
    Rep Power
    20

    Default Re: Need to convert it into Java !

    Quote Originally Posted by Tolls View Post
    A bit like Javascript, which is what I was assuming.
    In either case stats[4] += 1, does not become stats[5].
    Only C++ and its diabolical operator overloading can do that (you need at least a couple of overloaded operators to achieve this ;-)

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Convert Java to PHP
    By jop007 in forum JavaServer Pages (JSP) and JSTL
    Replies: 5
    Last Post: 08-22-2013, 09:21 AM
  2. How Do I: Convert to Java
    By mexists in forum New To Java
    Replies: 3
    Last Post: 11-29-2011, 02:50 AM
  3. how to convert * into : using java
    By Ricks in forum New To Java
    Replies: 2
    Last Post: 02-02-2010, 04:57 PM
  4. Convert .java to .exe
    By susan in forum New To Java
    Replies: 6
    Last Post: 02-11-2009, 06:47 AM
  5. Convert java to .exe
    By heartysnowy in forum New To Java
    Replies: 13
    Last Post: 11-21-2008, 08:16 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •