1: 设计一个方法要求检查一个指定字符是否允许作为 Java 标识符中的首字符,并测试?
2: 设计一个方法,接收一个字符和字符串,然后返回这个字符在这个字符串出现的次数,并测试?
3: 设计一个类并提供一个方法,将整型值转换为二进制、十六进制和八进制值?
4: 设计一个方法,接收一个byte然后将其转换字母并要求是大写的,再设计一个方法判断其是元音还是辅音。最后在main方法中声明一个数组,要求随机产生10个符合字母范围的数据放在数组内,并使用一个方法转成大写,另个方法判断其是元音还是辅音,并打印出来。
5: 设计一个方法,接收三个参数分别是年,月,日,然后返加一个Calender类型的对象,再设计一个方法接收Calender类型对象,在方法内分别打印此日期是一个月的第几天,一年的第几天,一周的第几天,此天是周几,这个月的第一天是周几,是一周的第几天。
这些方法很多都已经有了
[code="java"]
/*
import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.io.Reader;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.xml.transform.TransformerFactoryConfigurationError;
/**
@author Joe Walker [joe at getahead dot ltd dot uk]
/
public final class LocalUtil
{
/*
/**
/**
/**
@return replaced text or null if any args are null
*/
public static String replace(String text, String repl, String with)
{
if (text == null || repl == null || with == null || repl.length() == 0)
{
return text;
}
StringBuffer buf = new StringBuffer(text.length());
int searchFrom = 0;
while (true)
{
int foundAt = text.indexOf(repl, searchFrom);
if (foundAt == -1)
{
break;
}
buf.append(text.substring(searchFrom, foundAt)).append(with);
searchFrom = foundAt + repl.length();
}
buf.append(text.substring(searchFrom));
return buf.toString();
}
/**
@see java.lang.Character#isJavaIdentifierStart(char)
*/
public static boolean isJavaIdentifier(String test)
{
if (test == null || test.length() == 0)
{
return false;
}
if (!Character.isJavaIdentifierStart(test.charAt(0)) && test.charAt(0) != '_')
{
return false;
}
for (int i = 1; i < test.length(); i++)
{
if (!Character.isJavaIdentifierPart(test.charAt(i)) && test.charAt(i) != '_')
{
return false;
}
}
return true;
}
/**
@see Character#isLetterOrDigit(char)
*/
public static boolean isLetterOrDigitOrUnderline(String test)
{
if (test == null || test.length() == 0)
{
return false;
}
for (int i = 0; i < test.length(); i++)
{
if (!Character.isLetterOrDigit(test.charAt(i)) && test.charAt(i) != '_')
{
return false;
}
}
return true;
}
/**
@return true if the classes are equivalent
*/
public static boolean isEquivalent(Class c1, Class c2)
{
if (c1 == Boolean.class || c1 == Boolean.TYPE)
{
return c2 == Boolean.class || c2 == Boolean.TYPE;
}
else if (c1 == Byte.class || c1 == Byte.TYPE)
{
return c2 == Byte.class || c2 == Byte.TYPE;
}
else if (c1 == Character.class || c1 == Character.TYPE)
{
return c2 == Character.class || c2 == Character.TYPE;
}
else if (c1 == Short.class || c1 == Short.TYPE)
{
return c2 == Short.class || c2 == Short.TYPE;
}
else if (c1 == Integer.class || c1 == Integer.TYPE)
{
return c2 == Integer.class || c2 == Integer.TYPE;
}
else if (c1 == Long.class || c1 == Long.TYPE)
{
return c2 == Long.class || c2 == Long.TYPE;
}
else if (c1 == Float.class || c1 == Float.TYPE)
{
return c2 == Float.class || c2 == Float.TYPE;
}
else if (c1 == Double.class || c1 == Double.TYPE)
{
return c2 == Double.class || c2 == Double.TYPE;
}
else if (c1 == Void.class || c1 == Void.TYPE)
{
return c2 == Void.class || c2 == Void.TYPE;
}
return false;
}
/**
@return The non-privitive version of the class
*/
public static Class getNonPrimitiveType(Class type)
{
if (!type.isPrimitive())
{
return type;
}
else if (type == Boolean.TYPE)
{
return Boolean.class;
}
else if (type == Byte.TYPE)
{
return Byte.class;
}
else if (type == Character.TYPE)
{
return Character.class;
}
else if (type == Short.TYPE)
{
return Short.class;
}
else if (type == Integer.TYPE)
{
return Integer.class;
}
else if (type == Long.TYPE)
{
return Long.class;
}
else if (type == Float.TYPE)
{
return Float.class;
}
else if (type == Double.TYPE)
{
return Double.class;
}
else if (type == Void.TYPE)
{
return Void.class;
}
return null;
}
/**
@param resp The response to add headers to
*/
public static void addNoCacheHeaders(HttpServletResponse resp)
{
// Set standard HTTP/1.1 no-cache headers.
resp.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");
// Set IE extended HTTP/1.1 no-cache headers (use addHeader).
resp.addHeader("Cache-Control", "post-check=0, pre-check=0");
// Set standard HTTP/1.0 no-cache header.
resp.setHeader("Pragma", "no-cache");
// Set to expire far in the past. Prevents caching at the proxy server
resp.setHeader("Expires", "Sat, 6 May 1995 12:00:00 GMT");
}
/**
/**
@return The decoded string
*/
public static String decode(String value)
{
if (!testedDecoder)
{
try
{
decode14 = URLDecoder.class.getMethod("decode", new Class[] { String.class, String.class });
}
catch (Exception ex)
{
if (!warn13)
{
log.warn("URLDecoder.decode(String, String) is not available. Falling back to 1.3 variant.");
warn13 = true;
}
}
testedDecoder = true;
}
if (decode14 != null)
{
try
{
return (String) decode14.invoke(null, new Object[] { value, "UTF-8" });
}
catch (Exception ex)
{
log.warn("Failed to use JDK 1.4 decoder", ex);
}
}
return URLDecoder.decode(value);
}
/**
Note only the warning is skipped, we still try the setter
*/
public static void setParams(Object object, Map params, List ignore)
{
for (Iterator it = params.entrySet().iterator(); it.hasNext();)
{
Map.Entry entry = (Map.Entry) it.next();
String key = (String) entry.getKey();
Object value = entry.getValue();
try
{
setProperty(object, key, value);
}
catch (NoSuchMethodException ex)
{
if (ignore != null && !ignore.contains(key))
{
log.warn("No property '" + key + "' on " + object.getClass().getName());
}
}
catch (InvocationTargetException ex)
{
log.warn("Error setting " + key + "=" + value + " on " + object.getClass().getName(), ex.getTargetException());
}
catch (Exception ex)
{
log.warn("Error setting " + key + "=" + value + " on " + object.getClass().getName(), ex);
}
}
}
/**
@throws InvocationTargetException Passed on from reflection code
*/
public static void setProperty(Object object, String key, Object value) throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException
{
Class real = object.getClass();
String setterName = "set" + key.substring(0, 1).toUpperCase(Locale.ENGLISH) + key.substring(1);
try
{
// Can we work with whatever type we were given?
Method method = real.getMethod(setterName, new Class[] { value.getClass() });
method.invoke(object, new Object[] { value });
return;
}
catch (NoSuchMethodException ex)
{
// If it is a string then next we try to coerce it to the right type
// otherwise we give up.
if (!(value instanceof String))
{
throw ex;
}
}
Method[] methods = real.getMethods();
for (int i = 0; i < methods.length; i++)
{
Method setter = methods[i];
if (setter.getName().equals(setterName) && setter.getParameterTypes().length == 1)
{
Class propertyType = setter.getParameterTypes()[0];
try
{
Object param = LocalUtil.simpleConvert((String) value, propertyType);
setter.invoke(object, new Object[] { param });
return;
}
catch (IllegalArgumentException ex)
{
// The conversion failed - it was speculative anyway so we
// don't worry now
}
}
}
throw new NoSuchMethodException("Failed to find a property called: " + key + " on " + object.getClass().getName());
}
/**
/**
@return The converted object.
*/
public static Object simpleConvert(String value, Class paramType)
{
if (paramType == String.class)
{
return value;
}
if (paramType == Character.class || paramType == Character.TYPE)
{
if (value.length() == 1)
{
return new Character(value.charAt(0));
}
else
{
throw new IllegalArgumentException("Can't more than one character in string - can't convert to char: '" + value + "'");
}
}
String trimValue = value.trim();
if (paramType == Boolean.class)
{
if (trimValue.length() == 0)
{
return null;
}
return Boolean.valueOf(trimValue);
}
if (paramType == Boolean.TYPE)
{
return Boolean.valueOf(trimValue);
}
if (paramType == Integer.class)
{
if (trimValue.length() == 0)
{
return null;
}
return Integer.valueOf(trimValue);
}
if (paramType == Integer.TYPE)
{
if (trimValue.length() == 0)
{
return new Integer(0);
}
return Integer.valueOf(trimValue);
}
if (paramType == Short.class)
{
if (trimValue.length() == 0)
{
return null;
}
return Short.valueOf(trimValue);
}
if (paramType == Short.TYPE)
{
if (trimValue.length() == 0)
{
return new Short((short) 0);
}
return Short.valueOf(trimValue);
}
if (paramType == Byte.class)
{
if (trimValue.length() == 0)
{
return null;
}
return Byte.valueOf(trimValue);
}
if (paramType == Byte.TYPE)
{
if (trimValue.length() == 0)
{
return new Byte((byte) 0);
}
return Byte.valueOf(trimValue);
}
if (paramType == Long.class)
{
if (trimValue.length() == 0)
{
return null;
}
return Long.valueOf(trimValue);
}
if (paramType == Long.TYPE)
{
if (trimValue.length() == 0)
{
return new Long(0);
}
return Long.valueOf(trimValue);
}
if (paramType == Float.class)
{
if (trimValue.length() == 0)
{
return null;
}
return Float.valueOf(trimValue);
}
if (paramType == Float.TYPE)
{
if (trimValue.length() == 0)
{
return new Float(0);
}
return Float.valueOf(trimValue);
}
if (paramType == Double.class)
{
if (trimValue.length() == 0)
{
return null;
}
return Double.valueOf(trimValue);
}
if (paramType == Double.TYPE)
{
if (trimValue.length() == 0)
{
return new Double(0);
}
return Double.valueOf(trimValue);
}
throw new IllegalArgumentException("Unsupported conversion type: " + paramType.getName());
}
/**
@return the class name of the class without the package name
*/
public static String getShortClassName(Class clazz)
{
String className = clazz.getName();
char[] chars = className.toCharArray();
int lastDot = 0;
for (int i = 0; i < chars.length; i++)
{
if (chars[i] == '.')
{
lastDot = i + 1;
}
else if (chars[i] == '$')
{
chars[i] = '.';
}
}
// This might come up in scans for locale/charset issues. It's not an
// issue since we are talking about chars.
return new String(chars, lastDot, chars.length - lastDot);
}
/**
@return true if the name is simple
*/
public static boolean isSimpleName(String name)
{
if (name.length() == 0)
{
return false;
}
if (JavascriptUtil.isReservedWord(name))
{
return false;
}
boolean isSimple = Character.isLetter(name.charAt(0));
for (int i = 1; isSimple && i < name.length(); i++)
{
if (!Character.isLetterOrDigit(name.charAt(i)))
{
isSimple = false;
}
}
return isSimple;
}
/**
The initial implementation makes use of the context classloader for
className
is not valid */ public static Class classForName(String className) throws ClassNotFoundException { // Class.forName(className); return Thread.currentThread().getContextClassLoader().loadClass(className); }/**
@throws IllegalStateException If anything goes wrong
*/
public static Object invoke(Object object, Method method, Object[] params) throws IllegalStateException
{
Object reply = null;
if (method != null)
{
try
{
reply = method.invoke(object, params);
}
catch (InvocationTargetException ex)
{
throw new IllegalStateException("InvocationTargetException calling " + method.getName() + ": " + ex.getTargetException().toString());
}
catch (Exception ex)
{
throw new IllegalStateException("Reflection error calling " + method.getName() + ": " + ex.toString());
}
}
return reply;
}
/**
@return The class if it is safe or null otherwise.
*/
public static Class classForName(String name, String className, Class impl)
{
Class clazz;
try
{
clazz = classForName(className);
}
catch (ClassNotFoundException ex)
{
// We expect this sometimes, hence debug
log.debug("Skipping '" + name + "' due to ClassNotFoundException on " + className + ". Cause: " + ex.getMessage());
return null;
}
catch (NoClassDefFoundError ex)
{
// We expect this sometimes, hence debug
log.debug("Skipping '" + name + "' due to NoClassDefFoundError on " + className + ". Cause: " + ex.getMessage());
return null;
}
catch (TransformerFactoryConfigurationError ex)
{
// We expect this sometimes, hence debug
log.debug("Skipping '" + name + "' due to TransformerFactoryConfigurationError on " + className + ". Cause: " + ex.getMessage());
log.debug("Maybe you need to add xalan.jar to your webserver?");
return null;
}
// Check it is of the right type
if (!impl.isAssignableFrom(clazz))
{
log.error("Class '" + clazz.getName() + "' does not implement '" + impl.getName() + "'.");
return null;
}
// Check we can create it
try
{
clazz.newInstance();
}
catch (InstantiationException ex)
{
log.error("InstantiationException for '" + name + "' failed:", ex);
return null;
}
catch (IllegalAccessException ex)
{
log.error("IllegalAccessException for '" + name + "' failed:", ex);
return null;
}
catch (NoClassDefFoundError ex)
{
// We expect this sometimes, hence debug
log.debug("Skipping '" + name + "' due to NoClassDefFoundError on " + className + ". Cause: " + ex.getMessage());
return null;
}
catch (TransformerFactoryConfigurationError ex)
{
// We expect this sometimes, hence debug
log.debug("Skipping '" + name + "' due to TransformerFactoryConfigurationError on " + className + ". Cause: " + ex.getMessage());
log.debug("Maybe you need to add xalan.jar to your webserver?");
return null;
}
catch (Exception ex)
{
// For some reason we can't catch this?
if (ex instanceof ClassNotFoundException)
{
// We expect this sometimes, hence debug
log.debug("Skipping '" + name + "' due to ClassNotFoundException on " + className + ". Cause: " + ex.getMessage());
return null;
}
else
{
log.error("Failed to load '" + name + "' (" + className + ")", ex);
return null;
}
}
return clazz;
}
/**
@return The new instance if it is safe or null otherwise.
*/
public static Object classNewInstance(String name, String className, Class impl)
{
Class clazz;
try
{
clazz = LocalUtil.classForName(className);
}
catch (ClassNotFoundException ex)
{
// We expect this sometimes, hence debug
log.debug("Skipping '" + name + "' due to ClassNotFoundException on " + className + ". Cause: " + ex.getMessage());
return null;
}
catch (NoClassDefFoundError ex)
{
// We expect this sometimes, hence debug
log.debug("Skipping '" + name + "' due to NoClassDefFoundError on " + className + ". Cause: " + ex.getMessage());
return null;
}
catch (TransformerFactoryConfigurationError ex)
{
// We expect this sometimes, hence debug
log.debug("Skipping '" + name + "' due to TransformerFactoryConfigurationError on " + className + ". Cause: " + ex.getMessage());
return null;
}
// Check it is of the right type
if (!impl.isAssignableFrom(clazz))
{
log.error("Class '" + clazz.getName() + "' does not implement '" + impl.getName() + "'.");
return null;
}
// Check we can create it
try
{
return clazz.newInstance();
}
catch (InstantiationException ex)
{
log.error("InstantiationException for '" + name + "' failed:", ex);
return null;
}
catch (IllegalAccessException ex)
{
log.error("IllegalAccessException for '" + name + "' failed:", ex);
return null;
}
catch (TransformerFactoryConfigurationError ex)
{
log.error("TransformerFactoryConfigurationError for '" + name + "' failed:", ex);
return null;
}
catch (Exception ex)
{
log.error("Failed to load creator '" + name + "', classname=" + className + ": ", ex);
return null;
}
}
/**
@param in The resource to close
*/
public static void close(InputStream in)
{
if (in == null)
{
return;
}
try
{
in.close();
}
catch (IOException ex)
{
log.warn(ex.getMessage(), ex);
}
}
/**
@param in The resource to close
*/
public static void close(Reader in)
{
if (in == null)
{
return;
}
try
{
in.close();
}
catch (IOException ex)
{
log.warn(ex.getMessage(), ex);
}
}
/**
@param in The resource to close
*/
public static void close(RandomAccessFile in)
{
if (in == null)
{
return;
}
try
{
in.close();
}
catch (IOException ex)
{
log.warn(ex.getMessage(), ex);
}
}
/**
@return the List of superclasses in order going up from this one
*/
public static List getAllSuperclasses(Class clazz)
{
List classes = new ArrayList();
Class superclass = clazz.getSuperclass();
while (superclass != null)
{
classes.add(superclass);
superclass = superclass.getSuperclass();
}
return classes;
}
/**
This is like a union of {@link Class#getDeclaredFields()} which
/**
@return The complete list of fields
*/
private static Field[] getAllFields(List classes)
{
Set fields = new HashSet();
for (Iterator it = classes.iterator(); it.hasNext();)
{
Class clazz = (Class) it.next();
fields.addAll(Arrays.asList(clazz.getDeclaredFields()));
}
return (Field[]) fields.toArray(new Field[fields.size()]);
}
/**
/**
/**
/**
[/code]
自己写吧LZ
让人写程序,一个程序给1分。 :D
还是自己写的好。
太吝啬了吧!!! :o :o :o