Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
The Problem
Write a program that takes the words in its argument list and prints out any duplicate words, the number of distinct words, and a list of the words with duplicates eliminated.
I approached, with winning smile on my face, and the code snippet on my think pad.
import java.util.*; public class FindDups { public static void main(String args[]) { new FindDups(args); } FindDups(String[] input){ HashSet s = new HashSet(); for (int i=0; i<args.length; i++){ if (!s.add(args[i])){ System.out.println("Duplicate detected:" + args[i]); } } System.out.println(s.size()+" distinct words detected: "+s); } }
import java.util.*; public class FindDups { public static void main(String args[]) { new FindDups(args); } FindDups(String[] input){ Set s = new HashSet(); for (int i=0; i<args.length; i++){ if (!s.add(args[i])){ System.out.println("Duplicate detected:" + args[i]); } } System.out.println(s.size()+" distinct words detected: "+s); } }
I could not hide my quizzical expressions on my face. Tao continued to don the same charismatic smile. Then he asked me to ponder over the small change and decipher the underlying design principle.
I struggled through out the day to pin point the design principle. But no gain. Then I wrote down the lines of difference, in hope that I could read between the lines. HashSet s = new HashSet(); My code Set s = new HashSet(); His code It was beautiful full moon night. I kept staring, at my lap top screen. I do not remember when did I fall a sleep.
Early next morning, I hurried myself to Tao. Tao was not there. But He had left message for me.
Design to Interface.
. Now I could see the design principle I was exploring. I got the Aha! Feeling. But I was craving for the Aha! Insight.
What is the rationale behind this design mantra. And how it is making the big difference to my Java code.
I surrendered myself to Tao. Tao showed mercy on me. I know. When student is ready, Tao appears.
When I emerged from the fundamentals and looked up, Tao had left me long back.
Tao was not there. But He had left message for me.
Design to Interface.
Created by
Sachin Ramchandra Raverkar