cfoley's Profile User Rating: -----

Reputation: 2195 Grandmaster
Group:
Mentors
Active Posts:
4,633 (1.66 per day)
Joined:
11-December 07
Profile Views:
66,476
Last Active:
User is online 4 minutes ago
Currently:
Viewing Topic: I have a feeling that Codecademy...

Previous Fields

Country:
GB
OS Preference:
Who Cares
Favorite Browser:
Who Cares
Favorite Processor:
Who Cares
Favorite Gaming Platform:
Who Cares
Your Car:
Who Cares
Dream Kudos:
225
Expert In:
Java

Latest Visitors

Icon   cfoley has not set their status

Posts I've Made

  1. In Topic: I have a feeling that Codecademy is, umm, crap...

    Posted 30 Jul 2015

    Quote

    not useful repetition like you're describing.


    Quote

    Code Academy is specifically crap because it's not useful for any imaginable user.


    I stand corrected. :) That'll teach me to write about a site I have never used.
  2. In Topic: I have a feeling that Codecademy is, umm, crap...

    Posted 30 Jul 2015

    Everything is crap in the sense that nothing is perfect, and there is no single resource for "all you need to learn programming".

    Books are great for the basics if you choose a good one. But I find them lacking in the number and variety of exercises. Remember maths textbooks where you had a 30 very simple factorisation (for example) exercises, another 20 that had some complication or other, and maybe 10 text descriptions of (contrived) real world examples that required factorization and some other stuff that you had already learned about? Programming textbooks really need this level of stuff: repetition to drill it into your head, then ramping up in difficulty all the way to problem solving. I just don't see it in many textbooks.

    This is the niche that sites like Code Academy seem to be usefully filling (at least as far as I can tell. I've never used Code Academy.) It could be a powerful learning tool in conjunction with a good teaching book.

    In this specific case it has worked well. Your code did turn out to be wrong. It asked you to print something. You wrote the correct expression but then did nothing with the result. That's pedantic, I know but so are compilers and interpretors. What if they had given you the answer? Did you learn more/better by researching what might have gone wrong? Did you benefit more by using it to participate in an online coding community? Or was it all just a waste of time? Only you can say.

    Code Academy is probably fine as part of a learning experience.
  3. In Topic: Getting sub-objects recursively

    Posted 29 Jul 2015

    It's a lambda. For example:

    v -> v != null
    


    If you were to write a method to do the same it might lok like this:

    boolean isNotNull(Object v) {
      return v != null;
    }
    


    The important difference between methods and lambdas is that lambdas can be "stored" in variables or passed as arguments. In these two lines, I filter the stream in two different ways by passing different lambdas to the filter method:

    .filter(v -> v != null)
    .filter(v -> !visited.contains(v))
    


    Lambdas were new in Java 8. To do something similar in earlier versions you would have had to use anonymous inner classes.
  4. In Topic: Getting sub-objects recursively

    Posted 29 Jul 2015

    I can see you are using getDeclaredMethods() but that won't get you the fields that contribute to the size of the object. In any case, my snippet shows you how to iterate through all the objects and count them up, avoiding counting them twice. You could use that or modify it to estimate the object size. Remember to count references. Not sure how many bits they take. Maybe it varies between 64 bit and 32 bit JVMs, or maybe it relies on maximum heap size.

    Edit: a small refactoring

    	static int countObjects(Object root) {
    		return allObjects(root).size();
    	}
    
    	static Collection<Object> allObjects(Object root) {
    		Queue<Object> queue = new LinkedList<>();
    		Set<Object> result = new HashSet<>();
    		queue.add(root);
    		while (!queue.isEmpty()) {
    			Object next = queue.remove();
    			result.add(next);
    			queue.addAll(unvisitedValues(result, next));
    		}
    		return result;
    	}
    
    
  5. In Topic: Getting sub-objects recursively

    Posted 29 Jul 2015

    I had a play with this and it turned out to be more complex than I expected. Arrays need special treatment and you need to filter out static fields if you're using my method. It was fun to play with the streams API too. I ended up with this.

    import java.lang.reflect.*;
    import java.util.*;
    import java.util.stream.*;
    
    
    public class ObjctCounting {
    
    	public static void main(String[] args) {
    		reportObjectsIn("Hello World!");
    		reportObjectsIn(5);
    		String[] arr = {"a", "b", null};
    		reportObjectsIn(arr);
    		reportObjectsIn(Arrays.asList(arr));
    		reportObjectsIn(new ArrayList<>(Arrays.asList(arr)));
    		reportObjectsIn(new LinkedList<>(Arrays.asList(arr)));
    		String[][] nested = {{"a", "b"}, {"c", "d", "e"}};
    		reportObjectsIn(nested);
    	}
    
    	private static void reportObjectsIn(Object o) {
    		System.out.println(countObjects(o) + " objects in " + o);
    	}
    
    	static int countObjects(Object root) {
    		Queue<Object> queue = new LinkedList<>();
    		Set<Object> visited = new HashSet<>();
    		int result = 0;
    		queue.add(root);
    		while (!queue.isEmpty()) {
    			result++;
    			Object next = queue.remove();
    			visited.add(next);
    			queue.addAll(unvisitedValues(visited, next));
    		}
    		return result;
    	}
    
    	private static List<Object> unvisitedValues(Set<Object> visited, Object next) {
    		return valueStream(next)
    				.filter(v -> v != null)
    				.filter(v -> !visited.contains(v))
    				.collect(Collectors.toList());
    	}
    
    	private static Stream<Object> valueStream(Object next) {
    		if (next instanceof Object[]) {
    			return Arrays.asList((Object[]) next).stream();
    		} else {
    			Field[] fields = next.getClass().getDeclaredFields();
    			return Arrays.asList(fields).stream()
    					.filter(f -> (f.getModifiers() & Modifier.STATIC) == 0)
    					.map(f -> valueFromField(next, f));
    		}
    	}
    
    	private static Object valueFromField(Object next, Field f) {
    		try {
    			f.setAccessible(true);
    			return f.get(next);
    		} catch (IllegalArgumentException | IllegalAccessException e) {
    			throw new RuntimeException(e);
    		}
    	}
    
    }
    

My Information

Member Title:
Cabbage
Age:
33 years old
Birthday:
April 7, 1982
Gender:
Location:
Glasgow
Forum Leader:
Java
Years Programming:
20

Contact Information

E-mail:
Click here to e-mail me
Website URL:
Website URL  http://

Comments

Page 1 of 1
  1. Photo

    burakaltr Icon

    06 Mar 2013 - 18:18
    Thanks for Your Precious Input. I Found the character counting thing very bedazzling. I have the code to it that I wrote Myself, but it took me Long to find a subtle Algorithm :)
  2. Photo

    cfoley Icon

    18 Oct 2011 - 03:29
    Cheers! The next one is in progress, but I'm having to learn and write some programs first. It's going to take a little time...
  3. Photo

    Dogstopper Icon

    18 Oct 2011 - 03:24
    Nice blog. Can't wait to see more
  4. Photo

    ayaz 786123 Icon

    25 Feb 2011 - 06:49
    have a nice day sir
  5. Photo

    cfoley Icon

    16 Feb 2011 - 17:26
    Oooh thanks! Your comment is the first I heard!
  6. Photo

    m-e-g-a-z Icon

    16 Feb 2011 - 15:34
    Congrats on becoming a Forum Leader! :)
  7. Photo

    Dogstopper Icon

    08 Jan 2011 - 23:49
    "Cabbage" is much better! :D
  8. Photo

    cfoley Icon

    06 Jan 2011 - 16:19
    Cheers dude!
  9. Photo

    Dogstopper Icon

    06 Jan 2011 - 14:36
    "Purple DIC-headed Warrior" doesn't quite apply now that you got the promotion. Well done!
  10. Photo

    DaneAU Icon

    08 Sep 2010 - 09:19
    "cfoley has no profile comments yet. Why not say hello?"
    I don't have many comments either, so i am only doing what the thingy below said to do, hello cfoley and thanks for the thanks :)
Page 1 of 1