Skip to content

Instantly share code, notes, and snippets.

View luisenriquecorona's full-sized avatar
😎
I may be slow to respond.

TextKi JS luisenriquecorona

😎
I may be slow to respond.
View GitHub Profile
@luisenriquecorona
luisenriquecorona / Deprec.java
Last active April 14, 2019 22:59
Java.util.date
import java.util.Date;
/** Demonstrate deprecation warning */
public class Deprec {
public static void main(String[] av) {
// Create a Date object for May 5, 1986
// EXPECT DEPRECATION WARNING
Date d = new Date(86, 04, 05); // May 5, 1986
System.out.println("Date is " + d);
}
}
@luisenriquecorona
luisenriquecorona / Buggy.java
Created April 14, 2019 23:29
This program exhibits some bugs, so we can use a debugger
/** This program exhibits some bugs, so we can use a debugger */
public class Buggy {
static String name;
public static void main(String[] args) {
int n = name.length( ); // bug # 1
System.out.println(n);
name += "; The end."; // bug #2
System.out.println(name); // #3
}
}
@luisenriquecorona
luisenriquecorona / Person.java
Created April 16, 2019 00:01
A simple class used to demostrate unit testing
/** A simple class used to demonstrate unit testing. */
public class Person {
protected String fullName;
protected String firstName, lastName;
/** Construct a Person using his/her first+last names. */
public Person(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
/** Get the person's full name */
@luisenriquecorona
luisenriquecorona / Debug.java
Created April 16, 2019 00:49
Program: Debug
package com.darwinsys.util;
/** Utilities for debugging
*/
public class Debug {
/** Static method to see if a given category of debugging is enabled.
* Enable by setting e.g., -Ddebug.fileio to debug file I/O operations.
* Use like this:<BR>
* if (Debug.isEnabled("fileio"))<BR>
* System.out.println("Starting to read file " + fileName);
*/
@luisenriquecorona
luisenriquecorona / Traversal.js
Created April 16, 2019 23:36
Portable Document Traversal Fuctions
/**
* Return the nth ancestor of e, or null if there is no such ancestor
* or if that ancestor is not an Element (a Document or DocumentFragment e.g.).
* If n is 0 return e itself. If n is 1 (or
* omitted) return the parent. If n is 2, return the grandparent, etc.
*/
function parent(e, n) {
if (n === undefined) n = 1;
while(n-- && e) e = e.parentNode;
if (!e || e.nodeType !== 1) return null;
@luisenriquecorona
luisenriquecorona / GetOptSimple.java
Created April 17, 2019 00:17
Trivial demostration of GetOpt. If -h present, print help.
import com.darwinsys.util.GetOpt;
/** Trivial demonstration of GetOpt. If -h present, print help.
*/
public class GetOptSimple {
public static void main(String[] args) {
GetOpt go = new GetOpt("h");
char c;
while ((c = go.getopt(args)) !=GetOpt.DONE) {
switch(c) {
case 'h':
@luisenriquecorona
luisenriquecorona / GetOpt.java
Created April 17, 2019 00:34
A class to implement Unix-style (single-character) command-line argument parsing. Originally patterned after (but not using code from) the Unix getopt(3) program, this has been redesigned to be more Java-friendly.
package com.darwinsys.lang;
import com.darwinsys.util.Debug;
import java.util.Map;
import java.util.HashMap;
import java.util.List;
import java.util.ArrayList;
import java.util.Iterator;
/** A class to implement Unix-style (single-character) command-line argument
* parsing. Originally patterned after (but not using code from) the Unix
* getopt(3) program, this has been redesigned to be more Java-friendly.
@luisenriquecorona
luisenriquecorona / TestJDK11.java
Created April 17, 2019 23:11
Test for JDK >= 1.1
/** Test for JDK >= 1.1 */
public class TestJDK11 {
public static void main(String[] a) {
// Check for JDK >= 1.1
try {
Class.forName("java.lang.reflect.Constructor");
} catch (ClassNotFoundException e) {
String failure =
"Sorry, but this version of MyApp needs \n" +
"a Java Runtime based on Java JDK 1.1 or later";
@luisenriquecorona
luisenriquecorona / StrTokDemo4.java
Created April 17, 2019 23:25
Show using a StringTokenizer including getting the delimiters back
import java.util.*;
/** Show using a StringTokenizer including getting the delimiters back */
public class StrTokDemo4 {
public final static int MAXFIELDS = 5;
public final static String DELIM = "|";
/** Processes one String; returns it as an array of Strings */
public static String[] process(String line) {
String[] results = new String[MAXFIELDS];
// Unless you ask StringTokenizer to give you the tokens,
// it silently discards multiple null tokens.
@luisenriquecorona
luisenriquecorona / StringBufferDemo.java
Created April 17, 2019 23:36
Construct the same String three different ways
/**
* StringBufferDemo: construct the same String three different ways.
*/
public class StringBufferDemo {
public static void main(String[] argv) {
String s1 = "Hello" + ", " + "World";
System.out.println(s1);
// Build a StringBuffer, and append some things to it.
StringBuffer sb2 = new StringBuffer( );
sb2.append("Hello");