"Programming so that you can feel like a hero is a whole lot different than programming so that you have a program"
Fragments: July 21
3 days ago
"Programming so that you can feel like a hero is a whole lot different than programming so that you have a program"
Design, by nature, is a series of trade-offs. Every choice has a good and bad side, and you make your choice in the context of overall criteria defined by necessity. Good and bad are not absolutes, however. A good decision in one context might be bad in another. If you don't understand both sides of an issue, you cannot make an intelligent choice; in fact, if you don't understand all the ramifications of your actions, you're not designing at all. You're stumbling in the dark
// Version 1
public static String[] findClasses(String jarFile)
throws Exception
{
List<String> list = new ArrayList<String>();
ZipFile f = new ZipFile(jarFile);
Enumeration<? extends ZipEntry> entries
= f.entries();
while(entries.hasMoreElements())
{
ZipEntry ze = entries.nextElement();
String name = ze.getName();
list.add(name);
}
for(Iterator<String> i = list.iterator();
i.hasNext(); )
{
String name = i.next();
int pos = name.lastIndexOf('.');
String suffix = "";
if(pos >= 0)
suffix = name.substring(pos + 1);
if(!suffix.equals("class"))
i.remove();
}
String[] arr = new String[list.size()];
for(int i = 0; i < arr.length; ++i)
{
String name = list.get(i);
name = name.replace('/', '.');
arr[i] = name;
}
return arr;
}
// Version 2
public static String[] findClasses(String jarFile)
throws Exception {
List<String> list = new ArrayList<String>();
Enumeration<? extends ZipEntry> entries
= new ZipFile(jarFile).entries();
while(entries.hasMoreElements()) {
String name = entries.nextElement().getName();
if(name.endsWith(".class"))
list.add(name.replace('/', '.');
}
return list.toArray(new String[0]);
}
if(n == 1)
return 1;
if(n == 2)
return 2;
if(n == 3)
return 6;
if(n == 4)
return 1*2*3*4;
if(n == 5)
return 1*2*3*4*5;
...
int r = 1;
for(int i = 1; i <= n; ++i)
r *= i;
return r;
function factorial(n) {
if (n==1)
return 1
else
return n * factorial(n-1);
}
"x=" + x; "x=" + x.toString();
ClassType t = ....
if(t.interfaces_field.size() > 0) {
... // Do something
}
public static String toCsv(String[] line) {
String result = "";
final Separator comma = new Separator(",");
for (String s : line)
result += comma + s;
return result;
}
public class Separator {
private final String s;
boolean first = true;
public Separator(String s) { this.s = s; }
@Override public String toString() {
if (!first)
return s;
first = false;
return "";
}
}
import java.awt.event.*;
import javax.swing.*;
public class Win extends JFrame
{
private static final long
serialVersionUID = -9206602636016215477L;
public Win()
{
JPanel panel = new JPanel();
final JButton b = new JButton("A Button");
b.setVisible(false);
JButton t = new JButton("Toggle");
t.addActionListener(new ActionListener()
{
boolean show = false;
public void actionPerformed(ActionEvent e)
{
show = !show;
b.setVisible(show);
}
});
panel.add(t);
panel.add(b);
getContentPane().add(panel);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
import org.junit.Test;
import org.uispec4j.*;
import org.uispec4j.assertion.UISpecAssert;
public class Win_Test
{
static
{
UISpec4J.init();
}
@Test
public void test1() throws Exception
{
Panel panel = new Panel(new Win());
Button b = panel.getButton("A Button");
Button t = panel.getButton("Toggle");
t.click();
UISpecAssert.assertTrue(b.isVisible());
t.click();
UISpecAssert.assertFalse(b.isVisible());
t.click();
UISpecAssert.assertTrue(b.isVisible());
}
}
Java: Developing On The Streets © 2008. Blog design by Lucian E. Marin — Converted by Randomness!