662305 10

6
662305 Information Technology Laboratory II- Chapter 10 หน ้า 1 จาก 6 บททีบทที10 การ การ ออกแบบ ออกแบบ Class แบบ แบบ Inheritance และใช้งาน และใช้งาน Garbage Collection การออกแบบและสร้าง Class แบบ Inheritance โดยมีการออกแบบ Class ตาม Class Diagram ดังนี Point - x : int - y : int - static count : int = 0 + Point() + Point(int xValue, int yValue) + setX(int xValue) : void + setY(int yValue) : void + getX() : int + getY() : int + static getCount() : int + toString() : String Rectangle - width : int = 10 - height : int = 10 - static count : int = 0 + Rectangle() + Rectangle(int x, int y, int w, int h) + setWidth(int w) : void + setHeight(int h) : void + getWidth() : int + getHeight() : int + static getCount() : int + toString() : String การทดลองที10-1 // File Name : Point.java public class Point { private int x = 10; // x part of coordinate pair private int y = 10; // y part of coordinate pair private static int count = 0; // no-argument constructor public Point() { setX(0); setY(0); count++; }

Upload: nitigan-nakjuatong

Post on 15-May-2015

399 views

Category:

Documents


2 download

DESCRIPTION

ใบงานที่10 วิชา Lab2 (Java)

TRANSCRIPT

Page 1: 662305 10

662305 Information Technology Laboratory II- Chapter 10 หนา้ 1 จาก 6

บทที ่บทที ่1100 การการออกแบบออกแบบ CCllaassss แบบ แบบ IInnhheerriittaannccee และใช้งาน และใช้งาน GGaarrbbaaggee CCoolllleeccttiioonn

การออกแบบและสร้าง Class แบบ Inheritance โดยมีการออกแบบ Class ตาม Class Diagram ดงัน้ี

Point - x : int - y : int - static count : int = 0 + Point() + Point(int xValue, int yValue) + setX(int xValue) : void + setY(int yValue) : void + getX() : int + getY() : int + static getCount() : int + toString() : String

Rectangle - width : int = 10 - height : int = 10 - static count : int = 0 + Rectangle() + Rectangle(int x, int y, int w, int h) + setWidth(int w) : void + setHeight(int h) : void + getWidth() : int + getHeight() : int + static getCount() : int + toString() : String

การทดลองที ่10-1 // File Name : Point.java public class Point { private int x = 10; // x part of coordinate pair private int y = 10; // y part of coordinate pair private static int count = 0; // no-argument constructor public Point() { setX(0); setY(0); count++; }

Page 2: 662305 10

662305 Information Technology Laboratory II- Chapter 10 หนา้ 2 จาก 6

// constructor public Point( int xValue, int yValue ) { setX(xValue); setY(yValue); count++; } // finalizer protected void finalize() { count--; } // set x in coordinate pair public void setX( int xValue ) { x = xValue; // no need for validation } // return x from coordinate pair public int getX() { return x; } // set y in coordinate pair public void setY( int yValue ) { y = yValue; // no need for validation } // return y from coordinate pair public int getY() { return y; } public static int getCount() { return count; } // return String representation of Point object public String toString() { return "[" + getX() + ", " + getY() + "]"; } } // end class Point // File Name : PointApplet.java import javax.swing.*; import java.awt.*; public class PointApplet extends javax.swing.JApplet { int size; Point p[] ;

Page 3: 662305 10

662305 Information Technology Laboratory II- Chapter 10 หนา้ 3 จาก 6

public void init() { String input; // user's input // obtain user's choice input = JOptionPane.showInputDialog( "Enter number of point : " ); size = Integer.parseInt( input ); // convert input to int p = new Point[size]; for(int n = 0 ; n < p.length ; n++) { int x = 5 + (int) (Math.random() * 300); int y = 5 + (int) (Math.random() * 200); p[n] = new Point(x, y); } } // end method init // draw shapes on applet's background public void paint( Graphics g ) { super.paint( g ); //call paint method inherited from JApplet for ( int n = 0; n < p.length; n++ ) { // set color g.setColor( new Color(255,0,0) ); // plot point g.drawLine( p[n].getX(), p[n].getY(), p[n].getX(), p[n].getY() ); } // end for showStatus("จาํนวน Object : "+ Point.getCount()); } // end method paint } ผลลพัธ์

Page 4: 662305 10

662305 Information Technology Laboratory II- Chapter 10 หนา้ 4 จาก 6

การทดลองที ่10-2 // File Name : Rectangle.java public class Rectangle extends Point { private int width = 10; private int height = 10; private static int count = 0; /** Creates a new instance of Rectangle */ public Rectangle() { } public Rectangle(int x, int y, int w, int h ) { super(x,y); setWidth(w); setHeight(h); } public void setWidth(int w) { width = w; } public void setHeight(int h) { height = h; } public int getWidth() { return width; } public int getHeight() { return height; } public int getArea() { return width*height; }

Page 5: 662305 10

662305 Information Technology Laboratory II- Chapter 10 หนา้ 5 จาก 6

public static int getCount() { return count; } protected void finalize() { count--; } public String toString() { return "Conner Left = " + super.toString() + "; Width = " + getWidth() + "; Height = " + getHeight(); } } // File Name : RectangleApplet.java import javax.swing.*; import java.awt.*; public class RectangleApplet extends javax.swing.JApplet { int x, y; Rectangle r ; public void init() { String input; // user's input // obtain user's choice input = JOptionPane.showInputDialog( "Enter value x of left point : " ); x = Integer.parseInt( input ); // convert input to int input = JOptionPane.showInputDialog( "Enter value y of left point : " ); y = Integer.parseInt( input ); // convert input to int int w = 10 + (int) (Math.random() * 280); int h = 10 + (int) (Math.random() * 180); r = new Rectangle(x, y, w, h); } // end method init // draw shapes on applet's background public void paint( Graphics g ) { super.paint( g ); //call paint method inherited from JApplet // set color g.setColor( Color.ORANGE ); g.drawRect(r.getX(), r.getY(), r.getWidth(),r.getHeight() ); g.setColor( Color.BLUE ); g.drawString( "Point Left : " + r.getX() + ", " + r.getY(), r.getX(), r.getY());

Page 6: 662305 10

662305 Information Technology Laboratory II- Chapter 10 หนา้ 6 จาก 6

g.drawString( "Width : " + r.getWidth() , r.getX(), r.getY() + 15); g.drawString( "Height : " + r.getHeight() , r.getX(), r.getY() + 30); g.drawString( "Area : " + r.getArea() , r.getX(), r.getY() + 45); } // end method paint } ผลลพัธ์