You are on page 1of 7

Write a program to demonstrate reflection API in java.

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() {
}

public Student(int id, String name, float mark) {


this.id = id;
this.name = name;
this.mark = mark;
}

public int getId() {


return id;
}

public void setId(int id) {


this.id = id;
}

public float getMark() {


return mark;
}
public void setMark(float mark) {
this.mark = mark;
}

public String getName() {


return name;
}

public void setName(String name) {


this.name = name;
}

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);

}

Write a program to demostrate Regular Expression in java. Give a String


str= “AAA99SuperMario”, display as three parts “AAA”, “99”,
“SuperMario”.

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));
}
}

Session 5 _Question 3 :viết chương trình Dictionary HashMap

Đề Bài tập:

Question 3: Dictionary

A simple English – Vietnamese dictionary is stored in a text file named dictionary.txt:

apple: qua tao

ball: qua bong

cat : con meo

dog : con cho

elephant: con voi

fish: con ca

gift: mon qua

home: nha

Write a class Dictionary with method lookup to search the meaning of a new word:

String lookup(String 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:

Enter the word: cat

Meaning: con meo

Enter the word: bear

Meaning: not found

Enter the word:

Presss any key to continued…

Hint: use HashMap or HashTable

Đá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");
            
        }
        
    }

}
 

You might also like