BAI TAP JAVA
LE THI THU HA TIN 2006
BAI [Link]
import [Link].*; public class ManagerTest{ public static void main(String[] args){ Employee[] staff=new Employee[3]; staff[0]=new Employee("Harry Hacker",35000, new Date(1989,10,1)); staff[1]= new Manager("Carl Cracker",75000,new Date(1987,12,15)); staff[2]= new Employee("Tony Tester",38000, new Date(1990,3,15)); int i; for (i=0;i<3;i++) staff[i].raiseSalary(5); for (i=0;i<3;i++)staff[i].print(); } } class Employee{ public Employee(String n, double s, Date d){ name =n; salary=s; hireDate=d; } public void print(){ [Link](name+" "+salary+" "+hireYear()); } public void raiseSalary(double byPercent){ salary *=1+byPercent/100; } public int hireYear(){ return [Link](); } private String name; private double salary; private Date hireDate; } class Manager extends Employee{ public Manager(String n, double s, Date d){ super(n, s, d); } public void raiseSalary(double byPercent){ Date today = new Date(); double bonus=0.5*([Link]()-hireYear()); [Link](byPercent+bonus); } }
BAI [Link]
TRANG1
BAI TAP JAVA import [Link].*; class NotHelloWorld1 extends Frame {
LE THI THU HA TIN 2006
NotHelloWorld1(String title){ super(title); } public boolean handleEvent(Event e) { if ([Link] ==Event.WINDOW_DESTROY) [Link](0); return [Link](e); } public void paint(Graphics g) { [Link]("Not a hello, World program", 75, 100); } public static void main(String[] args){ Frame f = new NotHelloWorld1("Hello World 1"); [Link](300,200); [Link](); } }
BAI [Link]
import java.*; import [Link].*; class B { static int m; B(){ m=0; } static int getB(){ return m; } } class A{ B a = new B(); static B b = new B(); public static void main (String[] args) { [Link](B.m); B.m +=1; [Link](b.m); [Link](1,"3"); add(); } static void add(){ int i=0; [Link](3); TRANG2
BAI TAP JAVA [Link]("a"); while (![Link]()){ [Link]([Link]()); [Link]([Link]()); } } static Vector v= new Vector(); }
LE THI THU HA TIN 2006
BAI Card .java
public final class Card{ public static final int ACE=1; public static final int JACK=11; public static final int QUEEN=12; public static final int KING=13; public static final int CLUBS=1; public static final int DIAMONDS=2; public static final int HEARTS=3; public static final int SPADES=4; public Card(int v, int s){ value = v; suit = s; } public int getValue(){ return value; } public int getSuit(){ return suit; } public int rank(){ if (value==1) return 4*13+suit; else return 4*(value-1)+suit; } public String toString(){ String v; String s; if (value==ACE) v="Ace"; else if (value==JACK) v="Jack"; else if (value==QUEEN) v = "Queen"; else if (value==KING) v="King"; else v = [Link](value); if (suit==DIAMONDS)s="Diamonds"; else if (suit==HEARTS) s="Hearts"; TRANG3
BAI TAP JAVA else if (suit==SPADES)s="Spads"; else s="Clubs"; return v+" of "+s; } private int value; private int suit; }
LE THI THU HA TIN 2006
BAI [Link]
import [Link].*; import [Link].*; public class HashtableTest extends Frame { public HashtableTest() { Panel p =new Panel(); [Link](new FlowLayout()); name = new Choice(); add("Toaster",19.95); add("Blender",59.95); add("Microwave oven",179.95); add("Citrus press",19.95); add("Espresso maker",199.95); add("Rice cooker",29.95); add("Waffle iron",39.95); add("Bread machine",119.95); //quantity = new TextField(1,0,100,4); [Link](name); //[Link](quantity); [Link](new Button("Add")); add("South",p); add("Center",canvas = new PurchaseOrderCanvas()); [Link](a); } public void add(String n, double price) { [Link](n); [Link](n,new Double(price)); } public boolean handleEvent(Event evt) { if ([Link]==Event.WINDOW_DESTROY) [Link](0); return [Link](evt); } public boolean action(Event evt, Object arg) { if ([Link]("Add")) { TRANG4
BAI TAP JAVA
LE THI THU HA TIN 2006
if ([Link]()) { String n = [Link](); double price = ((Double)[Link](n)).doubleValue(); [Link](new Item(n,5,price)); } } else return [Link](evt,arg); [Link](a); return true; } public static void main(String args[]) { Frame f = new HashtableTest(); [Link](300,200); [Link](); } private Vector a = new Vector(); private Choice name; private TextField quantity; private PurchaseOrderCanvas canvas; private Hashtable prices= new Hashtable(); private int m = 1; } class Item{ public Item(String n, int q, double u) { name = n; quantity=q; unitPrice = u; } public String toString() { return "000"; //return new Format("%-20s").form(name) // + new Format("%6d").form(quantity) // + new Format("%8.2f").form(unitPrice); } private String name; private int quantity; private double unitPrice; } class PurchaseOrderCanvas extends Canvas { public void redraw(Vector new_a)
TRANG5
BAI TAP JAVA { a=new_a; repaint();
LE THI THU HA TIN 2006
} public void paint(Graphics g) { Font f = new Font("Courier",[Link],12); [Link](f); FontMetrics fm = [Link](f); int height=[Link](); int x=0; int y = 0; int i = 0; for(i=0;i<[Link]();i++) { y += height; [Link]([Link](i).toString(),x,y); } } private Vector a; }
BAI [Link]
import [Link].*; class HelloWorld extends Frame{ public boolean handleEvent(Event e){ if ([Link]==Event.WINDOW_DESTROY) [Link](0); return [Link](e); } public void paint(Graphics g){ [Link]([Link]); // [Link]([Link]); [Link]("Hello, this is a Java program",75,100); } public static void main(String[] args){ Frame f = new HelloWorld(); [Link](300,200); [Link](); } }
BAI [Link]
import [Link].*; class Sort { static int[] a;//Day n so nguyen nam trong [0, m] public Sort(int n,int m) TRANG6
BAI TAP JAVA { a = new int[n]; for (int i=0; i<[Link]; i++) a[i]= (int)(m*[Link]());
LE THI THU HA TIN 2006
} public void inDay(){ for (int i=0; i<[Link]; i++) [Link](a[i]+" "); } public void nhapDay(int n) { a = new int[n]; String str = new String(); DataInputStream stream = new DataInputStream([Link]); for (int i=0; i<[Link]; i++){ try { str = [Link](); } catch (Exception e){str="0";} try { a[i] = [Link](str).intValue(); } catch (NumberFormatException e){a[i]=0;} } } public void swap(int i, int j){ int temp; temp = a[i]; a[i]= a[j]; a[j] = temp; } public void selectionSort(){ int i, j, lowIndex, lowKey; for (i=0; i<[Link]-1; i++){ lowIndex = i; lowKey = a[i]; for (j=i+1; j<[Link]; j++){ if (a[j]<lowKey){ lowKey = a[j]; lowIndex = j; } } swap(i, lowIndex); } } //Quick Sort public int findPivot(int i, int j){ int firstKey, k;
TRANG7
BAI TAP JAVA k= i+1; firstKey = a[i]; while ((k<=j)&& (a[k] == firstKey)) k++; if (k>j) return -1; else if (a[k]>firstKey) return k; else return i; } public int partition(int i, int j, int pivot){ int l = i; int r = j; while (l<=r){ while (a[l]<pivot) l = l+1; while (a[r]>=pivot) r = r-1; if (l<r) swap(l,r); } return l; } public void quickSort(int i, int j){ int pivot; int pivotIndex, k; pivotIndex = findPivot(i,j); if (pivotIndex !=-1) { pivot = a[pivotIndex]; k = partition(i,j,pivot); quickSort(i,k-1); quickSort(k,j); } } public void pushDown(int first, int last){ int r = first; while (r<=(int)last/2){ if (last = 2*(r+1)-1){ if (a[r]>a[last]) swap(r, last); r = last; } else if (a[r]>a[2*(r+1)]) } } public static void main (String[] args) { Sort a = new Sort(20, 100); //[Link](5); //[Link](); [Link](0, [Link]-1); [Link](); } }
LE THI THU HA TIN 2006
TRANG8
BAI TAP JAVA
LE THI THU HA TIN 2006
BAI [Link]
import [Link].*; import [Link].*; import [Link].*; /** An applet that can also run as a standalone application */ public class StandaloneScribble extends Applet { /** * The main() method. If this program is invoked as an application, this * method will create the necessary window, add the applet to it, and * call init(), below. Note that Frame uses a PanelLayout by default. */ public static void main(String[] args) { Frame f = new Frame(); // Create a window Applet a = new StandaloneScribble(); // Create the applet panel [Link](a, "Center"); // Add applet to window [Link](); // Initialize the applet [Link](400, 400); // Set the size of the window [Link](); // Make the window visible [Link](new WindowAdapter() { // Handle window close requests public void windowClosing(WindowEvent e) { [Link](0); } }); } /** * The init() method. If the program is invoked as an applet, the browser * allocates screen space for it and calls this method to set things up */ public void init() { // Define, instantiate and register a MouseListener object [Link](new MouseAdapter() { public void mousePressed(MouseEvent e) { lastx = [Link](); lasty = [Link](); } }); // Define, instantiate and register a MouseMotionListener object [Link](new MouseMotionAdapter() { public void mouseDragged(MouseEvent e) { Graphics g = getGraphics(); int x = [Link](), y = [Link](); [Link]([Link]); [Link](lastx, lasty, x, y); lastx = x; lasty = y; } }); TRANG9
BAI TAP JAVA
LE THI THU HA TIN 2006
// Create a clear button Button b = new Button("Clear"); // Define, instantiate, and register a listener to handle button presses [Link](new ActionListener() { public void actionPerformed(ActionEvent e) { // clear the scribble Graphics g = getGraphics(); [Link](getBackground()); [Link](0, 0, getSize().width, getSize().height); } }); // And add the button to the applet [Link](b); } protected int lastx, lasty; // Coordinates of last mouse click }
BAI [Link]
import [Link].*; public class TileTest { public static void main(String[] args) { Tile[] a = new Tile[20]; int i; for (i=0; i<[Link]; i++) a[i]= new Tile(i,i,10,20,(int)(100*[Link]())); Sort.shell_sort(a); for (i=0;i<[Link]; i++) [Link](a[i]); } } interface Sortable { public int compare(Sortable b); } class Sort{ static void shell_sort(Sortable[] a){ int n = [Link]; int incr = n/2; while (incr>=1){ for (int i=incr; i<n; i++){ Sortable temp = a[i]; int j=i; while (j>=incr && [Link](a[j-incr])<0){ a[j]= a[j-incr]; j -= incr; } TRANG10
BAI TAP JAVA a[j]= temp; } incr /= 2; } } } class Tile extends Rectangle implements Sortable { public Tile(int x, int y, int w, int h, int zz){ super (x, y, w, h); z= zz; } public int compare(Sortable b){ Tile tb = (Tile) b; return z-tb.z; } public String toString(){ return [Link]() + "[z="+z+"]"; } private int z; }
LE THI THU HA TIN 2006
BAI [Link]
public class TinhTong{ public static void main (String args[]) { float s = 0.0F; String str = new String(); for (int i = 0; i < [Link]; i++ ){ s += [Link](args[i]).floatValue(); str = str + args[i]+" "; } [Link](" tong cua: " ); for (int i = 0; i < [Link]; i++ ) if (i==0)[Link](args[i]); else [Link](" + " + args[i]); [Link](" = " + s); } }
TRANG11