Professional Documents
Culture Documents
Program should
display following output:
-Class description for methods
-Retrieve a list of Method objects (include public, protected, package, and
private methods)
-Display the information on parameter types.
-Display the information on exception types.
-Display the information on return types.
package Reflection;
/**
*
* @author Quang Dung
*/
public class Student {
private int id;
private String name;
private float mark;
public Student() {
}
package Reflection;
import java.lang.reflect.Method;
/**
*
* @author Quang Dung
*/
public class UsingReflection {
Student st = new Student();
public void getClassName(){
Class cl = st.getClass();
String name = cl.getName();
System.out.println("class Name is "+ name);
}
public void getMethod(){
Class cl = st.getClass();
Method m[]=cl.getMethods();
for(int i=0;i<m.length;i++)
{
String method = m[i].getName();
System.out.println("Method is "+method);
String returntyper = m[i].getReturnType().getName();
System.out.println("returntTyper:"+returntyper);
Class para[] = m[i].getParameterTypes();
for (int j=0;j<para.length;j++)
{
String parametter = para[j].getName();
System.out.println("parameter types:"+parametter);
}
Class excep[] = m[i].getExceptionTypes();
for(int k=0;k<excep.length;k++){
String exception = excep[k].getName();
System.out.println("exception types:"+exception);
}
}
package RegularEpression;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
*
* @author Quang Dung
*/
public class Regular {
public static void main(String agrs[]){
String regular = "(\\w+)(\\d[0-9])(\\w+)";
Pattern pat = Pattern.compile(regular);
String input = "AAA99SuperMario";
Matcher m = pat.matcher(input);
while(m.find())
{
System.out.println(m.group(1));
System.out.println(m.group(2));
System.out.println(m.group(3));
}
}
}
Đề Bài tập:
Question 3: Dictionary
fish: con ca
home: nha
Write a class Dictionary with method lookup to search the meaning of a new word:
This method return the meaning of the word(given in the dictionary.txt). In case the word
is not in dictionary, return null.
The main method should receive the word from user and show the meaning until an
empty string is input. For example:
Đáp án:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package Session_5;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Scanner;
import java.util.StringTokenizer;
/**
*
* @author Quang Huy
*/
public class Dictionary {
HashMap <String,String>h = new HashMap <String,String> ();
public String lookup(String word)
{
for(String w: h.keySet() )
{
if(w.equalsIgnoreCase(word))
{
return h.get(w);
}
}
return null;
}
public void docfile()
{
String dictionary="TuDien.txt";
FileReader f;
BufferedReader b;
try
{
f= new FileReader(dictionary);
b= new BufferedReader(f);
String s;
while((s = b.readLine())!=null)
{
StringTokenizer st = new StringTokenizer (s,":");
while(st.hasMoreTokens())
{
String tu = st.nextToken();
String nghia = st.nextToken();
h.put(tu, nghia);
}
}
}
catch(FileNotFoundException fe)
{
System.out.println("Khong tim thay tu dien");
}
catch(IOException io)
{
System.out.println("Loi doc file");
}
}
public static void main(String args[])
{
Dictionary a = new Dictionary();
a.docfile();
boolean k =true;
Scanner nh = new Scanner(System.in);
continues:
while(k)
{
System.out.print("Enter the Word: ");
String word = nh.nextLine();
if(word.equals(""))
{
System.out.print("....\n Press anything to continue...");
nh.nextLine();
k=true;
continue continues;
}
String kt = a.lookup(word);
if(kt!=null)
{
System.out.println("Meaning : "+kt);
}
else
System.out.println("Meaning : Not Found");
}
}
}