662305 10
Upcoming SlideShare
Loading in...5
×
 

662305 10

on

  • 546 views

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

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

Statistics

Views

Total Views
546
Views on SlideShare
546
Embed Views
0

Actions

Likes
0
Downloads
24
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

662305 10 662305 10 Document Transcript

  • บทที่ 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.javapublic 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++; }662305 Information Technology Laboratory II- Chapter 10 หน ้า 1 จาก 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.javaimport javax.swing.*;import java.awt.*;public class PointApplet extends javax.swing.JApplet { int size; Point p[] ;662305 Information Technology Laboratory II- Chapter 10 หน ้า 2 จาก 6
  • public void init() { String input; // users input // obtain users 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 applets 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}ผลลัพธ์662305 Information Technology Laboratory II- Chapter 10 หน ้า 3 จาก 6
  • การทดลองที่ 10-2// File Name : Rectangle.javapublic 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; }662305 Information Technology Laboratory II- Chapter 10 หน ้า 4 จาก 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.javaimport javax.swing.*;import java.awt.*;public class RectangleApplet extends javax.swing.JApplet { int x, y; Rectangle r ; public void init() { String input; // users input // obtain users 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 applets 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());662305 Information Technology Laboratory II- Chapter 10 หน ้า 5 จาก 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}ผลลัพธ์662305 Information Technology Laboratory II- Chapter 10 หน ้า 6 จาก 6