星期一, 6月 06, 2011

20110606 Java 練習小記


package chapter8;




//建立一個 Car 的類別
class Car
{
int num;
double gas;
}


public class Sample1 {


public static void main(String[] args) {
//建立一個新物件
Car car1;
car1 = new Car();

//定義物件內的內容
car1.num = 1234;
car1.gas = 20.5;

System.out.println("車號是"+car1.num);
System.out.println("汽油量為"+car1.gas);


}


}


package lab2;

class Car
{
int num;
double gas;
//定義方法
void show()
{
System.out.println("車號是"+num);
System.out.println("汽油量為"+gas);
}
}

class Sample2 {

public static void main(String[] args) {
Car car1;
car1 = new Car();
//輸入變數內容
car1.num = 1234;
car1.gas = 20.5;
car1.show();
car1.show();

}

}


package lab3;
//chapter 8

class Car
{
int num;
double gas;
//建立一個方法顯示車號及油量
void show()
{
System.out.println("車號是"+num);
System.out.println("汽油量為"+gas);
}
void showCar()
{
System.out.println("顯示汽車的資料");
//呼叫物件本身 的 show()
show();
}
}

public class Sample3 {

public static void main(String[] args) {
Car car1;
car1 = new Car();
car1.num = 1234;
car1.gas = 20.5;
car1.showCar();

}

}


package lab4;
//chapter 8

class Car
{
int num;
double gas;
//使用引數的方式來傳遞資料
//
void setNum(int n)
{
num = n;
System.out.println("使車號改變為"+num);
}
void SetGas(double g)
{
gas = g;
System.out.println("使加油量改變為"+gas);
}
void show()
{
System.out.println("車號是"+num);
System.out.println("汽油量是"+gas);
}
}

public class Sample4 {

public static void main(String[] args) {
Car car1;
car1 = new Car();
//觀察相關變數內容
car1.show();
//透過上面的方法來改變內容
car1.setNum(1234);
car1.SetGas(20.5);
//觀察相關變數內容
car1.show();

}

}


package lab5;
//Chapter 8
class Car
{
int num;
double gas;
//使用兩個參數, 就可以放進兩個引數
void setNumGas(int n, double g)
{
num = n;
gas = g;
System.out.println("使車號改變為"+num+"以及將汽油量改變為"+gas);
}
void show()
{
System.out.println("車號為"+num);
System.out.println("汽油量為"+gas);
}
}

public class Sample5 {

public static void main(String[] args) {
Car car1 = new Car();
int number = 1234;
double gasoline = 20.5;
//利用變數的方式傳遞引數進去
car1.show();
car1.setNumGas(number, gasoline);
car1.show();

}

}


package lab6;
//chapter 8

class Car
{
int num;
double gas;
//這個方法會回傳 num 然後型態是 int
int getNum()
{
System.out.println("調查車號");
return num;
}
double getGas()
{
System.out.println("調查汽油量");
return gas;
}
void setNumGas(int n, double g)
{
num = n;
gas = g;
System.out.println("改變車號為"+num+"改變汽油量為"+gas);
}
void show()
{
System.out.println("車號為"+num);
System.out.println("汽油量為"+gas);
}
}

public class Sample6 {

public static void main(String[] args) {
Car car1 = new Car();
car1.show();
car1.setNumGas(1234, 20.5);
car1.show();
//呼叫具有傳回值的方法
//number 就會等於 num 回傳的值
int number = car1.getNum();
double gasoline = car1.getGas();
System.out.println("車號是"+number+"汽油量是"+gasoline);

}

}


package chapter8;

class MyPoint
{
int x;
int y;
void setX(int px)
{
x = px;
System.out.println("設定x座標");
}
void setY(int py)
{
y = py;
System.out.println("設定y座標");
}
int getX()
{
System.out.println("讀取X座標");
return x;
}
int getY()
{
System.out.println("讀取Y座標");
return y;
}
void show()
{
System.out.println("***X座標為"+x);
System.out.println("***Y座標為"+y);
}
}


package chapter8;

public class TestMyPoint {
public static void main(String[] args) {
MyPoint m1 = new MyPoint();
int x1 = m1.getX();
int y1 = m1.getY();
System.out.println("X座標為"+x1+"Y座標為"+y1);
m1.setX(100);
m1.setY(200);
int x2 = m1.getX();
int y2 = m1.getY();
System.out.println("X座標為"+x2+"Y座標為"+y2);
m1.show();

}

}

沒有留言: