package chapter9;
//汽車 class
class Car
{
int num;
double gas;
void show()
{
System.out.println("車號是"+num);
System.out.println("汽油量為"+gas);
}
}
public class Sample1 {
public static void main(String[] args) {
Car car1 = new Car();
car1.num = 1234;
car1.gas = 20.5;
car1.show();
}
}
package lab2;//chapter 9
class Car
{
//設定欄位為 private 型態, 不可以任意存取 private int num;
private double gas;
//設定一個 public 型態來存取 public void setNumGas(int n, double g)
{
//在這邊設定存取的邏輯 if ( g > 0 && g < 1000)
{
num = n;
gas = g;
System.out.println("改變車號為"+num+"改變油量為"+gas);
}
else
{
System.out.println(g+"不是合理的汽油量數字");
}
}
public void show()
{
System.out.println("車號是"+num);
System.out.println("汽油量為"+gas);
}
}
public class Sample2 {
public static void main(String[] args) {
Car car1 = new Car();
//呼叫剛剛設定的 public method 來存取 car1.setNumGas(1234, 20.5);
car1.show();
System.out.println("測試不合理的汽油量");
car1.setNumGas(1234, -20);
car1.show();
}
}
package chapter9.lab3;
class Car
{
private int num;
private double gas;
//練習 多載 overloading public void setCar(int n)
{
num = n;
System.out.println("改變車號為"+num);
}
//可以有同一個方法, 但是引數不一樣, 稱為overloading
//這邊的引數使用的是 double 用來改變汽油量 public void setCar(double g)
{
gas = g;
System.out.println("改變油量為"+gas);
}
//當然也可以同時設定兩個 public void setCar(int n, double g)
{
num = n;
gas = g;
System.out.println("改變車號為"+num+"\t改變汽油量為"+gas);
}
public void show()
{
System.out.println("車號為"+num);
System.out.println("汽油量為"+gas);
}
}
public class Sample3 {
public static void main(String[] args) {
Car car1 = new Car();
car1.setCar(1234, 20.5);
car1.show();
System.out.println("只變更車號");
car1.setCar(2345);
car1.show();
System.out.println("只變更油量");
car1.setCar(30.7);
car1.show();
}
}
package chapter9.lab4;
class Car
{
private int num;
private double gas;
//使用建構子, 一般用來初始化值
//產生一個新物件時候, 就會去執行建構子 public Car()
{
num =0;
gas = 0;
System.out.println("Car 初始化");
}
public void show()
{
System.out.println("車號是"+num);
System.out.println("汽油量為"+gas);
}
}
public class Sample4 {
public static void main(String[] args) {
Car car1 = new Car();
car1.show();
}
}
package chapter9.lab5;
class Car
{
private int num;
private double gas;
//這個是沒有引數的建構子 public Car()
{
num =0;
gas =0;
System.out.println("Car 初始化 num and gas");
}
//同樣都是建構子,但是引數不同 public Car(int n, double g)
{
num = n;
gas = g;
System.out.println("初始化車號"+num+"汽油量"+gas+"的Car");
}
public void show()
{
System.out.println("車號為"+num);
System.out.println("汽油量為"+gas);
}
}
public class Sample5 {
public static void main(String[] args) {
//建立物件的時候, 沒有輸入任何引數 Car car1 = new Car();
car1.show();
Car car2 = new Car(1234, 20.5);
car2.show();
}
}
package chapter9.lab6;
class Car
{
private int num ;
private double gas;
public Car()
{
num =0;
gas = 0;
System.out.println("Car 初始化");
}
public Car(int n, double g)
{
//呼叫沒有引數的建構元, this(); 一定要寫在建構子第一行 this();
num = n;
gas = g;
System.out.println("改變車號為"+num+"\t改變油量為"+gas);
}
public 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();
Car car2 = new Car(1234, 20.5);
car2.show();
}
}
package chapter9.lab7;//實體變數 instance variable與實體方法 instance method的練習
class Car
{
private int num;
private double gas;
public Car()
{
num = 0;
gas = 0.0;
System.out.println("生產初始化汽車");
}
public void setCar(int n, double g)
{
num = n;
gas = g;
System.out.println("初始化車號為"+num+"\t初始化油量為"+gas);
}
public void show()
{
System.out.println("車號為"+num);
System.out.println("汽油量為"+gas);
}
}
public class Sample7 {
public static void main(String[] args) {
Car car1 = new Car();
car1.setCar(1234, 20.5);
car1.show();
Car car2 = new Car();
car2.setCar(4567, 30.5);
car2.show();
}
}
package chapter9.lab8;
class Car
{
//使用 static 的修飾子標明是類別變數
//使用 static 的話那就是整個類別用同一個變數 public static int sum = 0;
private int num;
private double gas;
public Car()
{
num = 0;
gas = 0.0;
//每呼叫一次 Car(), 就累加一次 sum sum++;
System.out.println("生產初始化汽車");
}
public void setCar(int n, double g)
{
num = n;
gas = g;
System.out.println("初始化車號為"+num+"\t初始化油量為"+gas);
}
public static void showSum()
{
System.out.println("目前總共生產"+sum+"汽車");
}
public void show()
{
System.out.println("車號為"+num);
System.out.println("油量為"+gas);
}
}
public class Sample8 {
public static void main(String[] args) {
//先顯示目前 sum 的值, 呼叫類別方法 Car.showSum();
Car car1 = new Car();
car1.setCar(1234, 20.5);
car1.show();
Car.showSum();
//因為有呼叫 Car(), 所以 sum會被累加 Car car2 = new Car();
car2.setCar(2345, 30.5);
car2.show();
//顯示目前 sum 的值, 呼叫類別方法 Car.showSum();
}
}
package chapter9;
class MyPoint
{
private int x;
private int y;
public MyPoint()
{
x = 0;
y = 0;
}
public MyPoint(int px, int py)
{
if( px >=0 && px <= 100)
{
x = px;
}
else
{
x = 0;
}
if ( py >=0 && py <=100)
{
y = py;
}
else
{
y = 0;
}
System.out.println("設定初始座標值");
}
public void setX(int px)
{
if( px >= 0 && px <= 100)
{
x = px;
}
System.out.println("設定X座標為"+x);
}
public void setY(int py)
{
if ( py >= 0 && py <= 100)
{
y = py;
}
System.out.println("設定Y座標為"+y);
}
public int getX()
{
System.out.println("讀取X座標");
return x;
}
public int getY()
{
System.out.println("讀取Y座標");
return y;
}
}
public class Homework9_5 {
public static void main(String[] args) {
MyPoint m1 = new MyPoint();
m1.setX(10);
m1.setY(5);
int px1 = m1.getX();
int py1 = m1.getY();
System.out.println("X座標為"+px1+"\tY座標為"+py1);
MyPoint m2 = new MyPoint(20, 10);
int px2 = m2.getX();
int py2 = m2.getY();
System.out.println("X座標為"+px2+"\tY座標為"+py2);
}
}
//汽車 class
class Car
{
int num;
double gas;
void show()
{
System.out.println("車號是"+num);
System.out.println("汽油量為"+gas);
}
}
public class Sample1 {
public static void main(String[] args) {
Car car1 = new Car();
car1.num = 1234;
car1.gas = 20.5;
car1.show();
}
}
package lab2;//chapter 9
class Car
{
//設定欄位為 private 型態, 不可以任意存取 private int num;
private double gas;
//設定一個 public 型態來存取 public void setNumGas(int n, double g)
{
//在這邊設定存取的邏輯 if ( g > 0 && g < 1000)
{
num = n;
gas = g;
System.out.println("改變車號為"+num+"改變油量為"+gas);
}
else
{
System.out.println(g+"不是合理的汽油量數字");
}
}
public void show()
{
System.out.println("車號是"+num);
System.out.println("汽油量為"+gas);
}
}
public class Sample2 {
public static void main(String[] args) {
Car car1 = new Car();
//呼叫剛剛設定的 public method 來存取 car1.setNumGas(1234, 20.5);
car1.show();
System.out.println("測試不合理的汽油量");
car1.setNumGas(1234, -20);
car1.show();
}
}
package chapter9.lab3;
class Car
{
private int num;
private double gas;
//練習 多載 overloading public void setCar(int n)
{
num = n;
System.out.println("改變車號為"+num);
}
//可以有同一個方法, 但是引數不一樣, 稱為overloading
//這邊的引數使用的是 double 用來改變汽油量 public void setCar(double g)
{
gas = g;
System.out.println("改變油量為"+gas);
}
//當然也可以同時設定兩個 public void setCar(int n, double g)
{
num = n;
gas = g;
System.out.println("改變車號為"+num+"\t改變汽油量為"+gas);
}
public void show()
{
System.out.println("車號為"+num);
System.out.println("汽油量為"+gas);
}
}
public class Sample3 {
public static void main(String[] args) {
Car car1 = new Car();
car1.setCar(1234, 20.5);
car1.show();
System.out.println("只變更車號");
car1.setCar(2345);
car1.show();
System.out.println("只變更油量");
car1.setCar(30.7);
car1.show();
}
}
package chapter9.lab4;
class Car
{
private int num;
private double gas;
//使用建構子, 一般用來初始化值
//產生一個新物件時候, 就會去執行建構子 public Car()
{
num =0;
gas = 0;
System.out.println("Car 初始化");
}
public void show()
{
System.out.println("車號是"+num);
System.out.println("汽油量為"+gas);
}
}
public class Sample4 {
public static void main(String[] args) {
Car car1 = new Car();
car1.show();
}
}
package chapter9.lab5;
class Car
{
private int num;
private double gas;
//這個是沒有引數的建構子 public Car()
{
num =0;
gas =0;
System.out.println("Car 初始化 num and gas");
}
//同樣都是建構子,但是引數不同 public Car(int n, double g)
{
num = n;
gas = g;
System.out.println("初始化車號"+num+"汽油量"+gas+"的Car");
}
public void show()
{
System.out.println("車號為"+num);
System.out.println("汽油量為"+gas);
}
}
public class Sample5 {
public static void main(String[] args) {
//建立物件的時候, 沒有輸入任何引數 Car car1 = new Car();
car1.show();
Car car2 = new Car(1234, 20.5);
car2.show();
}
}
package chapter9.lab6;
class Car
{
private int num ;
private double gas;
public Car()
{
num =0;
gas = 0;
System.out.println("Car 初始化");
}
public Car(int n, double g)
{
//呼叫沒有引數的建構元, this(); 一定要寫在建構子第一行 this();
num = n;
gas = g;
System.out.println("改變車號為"+num+"\t改變油量為"+gas);
}
public 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();
Car car2 = new Car(1234, 20.5);
car2.show();
}
}
package chapter9.lab7;//實體變數 instance variable與實體方法 instance method的練習
class Car
{
private int num;
private double gas;
public Car()
{
num = 0;
gas = 0.0;
System.out.println("生產初始化汽車");
}
public void setCar(int n, double g)
{
num = n;
gas = g;
System.out.println("初始化車號為"+num+"\t初始化油量為"+gas);
}
public void show()
{
System.out.println("車號為"+num);
System.out.println("汽油量為"+gas);
}
}
public class Sample7 {
public static void main(String[] args) {
Car car1 = new Car();
car1.setCar(1234, 20.5);
car1.show();
Car car2 = new Car();
car2.setCar(4567, 30.5);
car2.show();
}
}
package chapter9.lab8;
class Car
{
//使用 static 的修飾子標明是類別變數
//使用 static 的話那就是整個類別用同一個變數 public static int sum = 0;
private int num;
private double gas;
public Car()
{
num = 0;
gas = 0.0;
//每呼叫一次 Car(), 就累加一次 sum sum++;
System.out.println("生產初始化汽車");
}
public void setCar(int n, double g)
{
num = n;
gas = g;
System.out.println("初始化車號為"+num+"\t初始化油量為"+gas);
}
public static void showSum()
{
System.out.println("目前總共生產"+sum+"汽車");
}
public void show()
{
System.out.println("車號為"+num);
System.out.println("油量為"+gas);
}
}
public class Sample8 {
public static void main(String[] args) {
//先顯示目前 sum 的值, 呼叫類別方法 Car.showSum();
Car car1 = new Car();
car1.setCar(1234, 20.5);
car1.show();
Car.showSum();
//因為有呼叫 Car(), 所以 sum會被累加 Car car2 = new Car();
car2.setCar(2345, 30.5);
car2.show();
//顯示目前 sum 的值, 呼叫類別方法 Car.showSum();
}
}
package chapter9;
class MyPoint
{
private int x;
private int y;
public MyPoint()
{
x = 0;
y = 0;
}
public MyPoint(int px, int py)
{
if( px >=0 && px <= 100)
{
x = px;
}
else
{
x = 0;
}
if ( py >=0 && py <=100)
{
y = py;
}
else
{
y = 0;
}
System.out.println("設定初始座標值");
}
public void setX(int px)
{
if( px >= 0 && px <= 100)
{
x = px;
}
System.out.println("設定X座標為"+x);
}
public void setY(int py)
{
if ( py >= 0 && py <= 100)
{
y = py;
}
System.out.println("設定Y座標為"+y);
}
public int getX()
{
System.out.println("讀取X座標");
return x;
}
public int getY()
{
System.out.println("讀取Y座標");
return y;
}
}
public class Homework9_5 {
public static void main(String[] args) {
MyPoint m1 = new MyPoint();
m1.setX(10);
m1.setY(5);
int px1 = m1.getX();
int py1 = m1.getY();
System.out.println("X座標為"+px1+"\tY座標為"+py1);
MyPoint m2 = new MyPoint(20, 10);
int px2 = m2.getX();
int py2 = m2.getY();
System.out.println("X座標為"+px2+"\tY座標為"+py2);
}
}
沒有留言:
張貼留言