วันอาทิตย์ที่ 11 มีนาคม พ.ศ. 2555

mypg


package mypg;

import java.util.*;

public class Geometric {
private String color = "white";
private Date dateCreated;
public Geometric()
{
dateCreated = new Date();

}
public Geometric (String color)
{
this.color = color;
}
public void setColor(String color)
{
this.color = color;
}
public String getColor()
{
return this.color;
}
public Date getDate()
{
return dateCreated;
}
}
+++++++++++++++++++++++++++++++
package mypg;

public class Circle extends Geometric{
private double radius;

public Circle (double radius)
{
this.radius = radius;

}
public Circle(double radius,String color)
{
this.radius = radius;
setColor(color);
}
public double getArea()
{
return radius * radius * Math.PI;
}
}
+++++++++++++++++++++++++++++++++
package mypg;

public class Test {

public static void main(String[] args) {
Geometric geo1 = new Geometric();
System.out.println(geo1.getColor()+" ; "+geo1.getDate());
Circle cir1 = new Circle(4);
cir1.setColor("red");
System.out.println(cir1.getColor()+ " ; " +cir1.getDate() + " ; " +cir1.getArea());

}

}


Polymorphism


nแปลว่า หลายรูปทรง แปลงสภาพได้
Example
class CustomerData{
   protected void CustomerPhone(){
        System.out.println(“02 123 456”);
   {
   protect void ClearAll(){
        System.out.println(“Delete All Data”);
   }
}
+++++++++++++++++++++++++++++++++++++++++ๅๅๅๅ
เวลาเรียกใช้
class CustomerData2 extends CustomerData {
   protected void CustomerPhone(){
         super.CustomerPhone();
         System.out.println(“02 123 456 7”);
   {
   protect void ClearAll(){
   }
}


Inheritance


nแปลว่า การสืบสันดาน หรือ การสืบมรดก
nต่อยอดงานใหม่ จากงานเก่าไม่ต้องเริ่มจากศูนย์
nภาษา OOP สนับสนุน Inheritance เสมอ
Example
class Foo{
   public void WriteFoo(){
      System.out.println(“Foo”);
   }
}

class Bar extends Foo{
   public void WriteBar(){
      System.out.println(“Bar”);
   }
}

+++++++++++++++++++++++++++++++++++++++++ๅ
เวลาเรียกใช้
class Program{
   public static void main (String[] args){
      Bar myBar = new Bar();
      myBar.WriteFoo();
      myBar.WriteBar();
   }



Overloaded Constructor


nเป็นคอนสตรัคเตอร์กำหนดเอง
nคอนสตรัคเตอร์ทุกตัวชื่อเหมือนกับคลาส
nทำการรับพารามิเตอร์ไม่เหมือนกัน

class Calculator{
  Calculator(int x){
  return x + x;}
   Calculator(int x, int y){
  return x + y;}  
   Calculator(int x, int y, int z){
  return x + y + z;}
}

เวลาเรียกใช้คอนสตรัคเตอร์
class TestCalculator{
  public static void main(String[] args) {
  Calculator a = new Calculator();
  System.out.println(a.Calculator(1));
 
  Calculator b = new Calculator();  
  System.out.println(b.Calculator(1, 2));
  Calculator c = new Calculator();
  System.out.println(c.Calculator(1, 2, 3));
  }
}


โอเวอร์โหลดเมธอด


public class TestOver1 {
  public static int add() {
  return 0;       }
  public static int add(int x) {
  return x;      }
  public static int add(int x, int y)   {
  return x+y;               }
  public static int add(int x, int y, int z) {
  return x+y+z;                      }


เวลาเรียกใช้เมธอด
  public static void main(String[] args)                   
{
  System.out.println("Sum = "+add());
  System.out.println("Sum = "+add(3));
  System.out.println("Sum = "+add(1,2));
  System.out.println("Sum = "+add(1,2,3)); }
}
Test_java2

วันจันทร์ที่ 6 กุมภาพันธ์ พ.ศ. 2555

TV

package
java_1;


public
class TV {

private int chanel = 1;//1-10

private int volume = 1;//0-5

private boolean status = false;// By default TV is off

private String brand = "";

public static int count = 0;


TV(){

count++;

brand = "Sony";

}

TV(String newBrand){

count++;

brand = newBrand;

}

TV(String newBrand,
int newchanel){

count++;

brand = newBrand;

chanel = newchanel;

}

public String getBrand(){

return brand;

}

public void turnOn(){

//Mutator of status variable

status = true;

}

public void turnOff(){

//Mutator of status variable

status = false;

}

public boolean getStatus(){

//Accessor of status variable

return status;

}

public void chanelUp(){

chanel++;

if(chanel > 10){

chanel = 1;


}

}

public void chanelDown(){

chanel--;

if(chanel < 1){

chanel = 10;

}

}

public int getChanel(){

if(status == false)

{

System.
out.println("Please TurnOn TV");

return 0;

}

else

return chanel;

}

public void volumeUp(){

if(volume < 5)

volume++;

else

System.
out.println("Maximum");

}

public void volumeDown(){

if(volume > 0)

volume--;

else

System.
out.println("Minimum");


}

public int getVolume(){

return volume;

}

public boolean equalsTV(TV o){

if(o.getChanel() != this.getChanel())

return false;

if(o.getStatus() != this.getStatus())

return false;

if(o.getVolume() != this.getVolume())

return false;

return true;

}


}

Test TV

package
java_1;
import
java.util.ArrayList;
public
class Test {
public static void main(String[] args) {
TV tv1 =
new TV();
System.
out.println("******TV 1*******");
tv1.turnOn();
//turnOn TV
System.
out.println(tv1.getBrand());
System.
out.println(tv1.getChanel());
tv1.chanelDown();
System.
out.println(tv1.getChanel());
tv1.volumeDown();
tv1.volumeDown();
System.
out.println(tv1.getVolume());
tv1.volumeUp();
tv1.volumeUp();
tv1.volumeUp();
tv1.volumeUp();
tv1.volumeUp();
tv1.volumeUp();
System.
out.println(tv1.getVolume());
TV tv2 =
new TV("Sumsung",10);
System.
out.println("******TV 2*******");
tv2.turnOn();
//turnOn TV
System.
out.println(tv2.getBrand());
System.
out.println(tv2.getChanel());
tv2.chanelUp();
System.
out.println(tv2.getChanel());
////// ArrayList ////
ArrayList<TV> listTV =
new ArrayList<TV>();
listTV.add(
new TV());
listTV.add(
new TV("PHILIPS"));
listTV.add(
new TV("SAMSUNG"));
listTV.add(
new TV("PANASONIC"));
System.
out.println("ArraySize:"+listTV.size());
System.
out.println(listTV.get(0).getBrand());
System.
out.println(listTV.get(1).getBrand());
System.
out.println(listTV.get(2).getBrand());
System.
out.println(listTV.get(3).getBrand());
////// Array /////
/*
TV[] arrayTV = new TV[5];
for(int i = 0; i < 5; i++)
{
arrayTV[i] = new TV();
}
System.out.println("Count TV:"+TV.count);
System.out.println(arrayTV[0].getBrand());
System.out.println("Brand:"+arrayTV[0].getBrand());
*/
///// check equals /////
/*tv1.turnOn();
tv1.turnOn();
if(tv1.equalsTV(tv2)==true){
System.out.println("Equal");
}else{
System.out.println("Not equal");
}
tv2.chanelDown();
if(tv1.equalsTV(tv2)==true){
System.out.println("Equal");
}else{
System.out.println("Not equal");
}*/
}
}

วันจันทร์ที่ 30 มกราคม พ.ศ. 2555

class Circle

package
Circle;


public
class Circle

{

/*The radius of this circle*/

private double radius = 1.0;


Circle()

{


}

/*Construct a circle object*/

Circle(
double newRadius)

{

radius = newRadius;

}

/*Return the area of this circle*/

double getArea()

{

return Math.pow(radius,2) * Math.PI;

}


double getCircumrefence()

{

return 2 * Math.PI * radius;

}

double getRadius()

{

return radius;

}



void setRadius(double newRadius)

{

radius = newRadius;

}

}