package chapter10;
//使用陣列儲存多的物件
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+"設定油量為"+gas);
}
public void show()
{
System.out.println("車號為"+num);
System.out.println("汽油量為"+gas);
}
}
public class Sample9 {
public static void main(String[] args) {
Car cars[];
cars = new Car[3];
//使用 for 迴圈產生 3個物件指定給 cars[] 陣列
for(int i=0; i < cars.length ; i++)
{
cars[i] = new Car();
}
for ( int i=0; i < cars.length; i++)
{
cars[i].show();
}
cars[0].setCar(1234, 20.5);
cars[1].setCar(2345, 30.5);
cars[2].setCar(3456, 40.5);
for ( int i=0; i < cars.length; i++)
{
cars[i].show();
}
}
}
package chapter10;
import java.io.*;
public class Homework10_2 {
public static void main(String[] args) throws IOException
{
System.out.println("請輸入字串");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String str = br.readLine();
//使用 StringBuffer 的類別來處理字串
StringBuffer sb= new StringBuffer(str);
//將字串的緩衝區反過來
sb.reverse();
System.out.println(str+"反過來寫就是"+sb);
}
}
package chapter10;
import java.io.*;
public class Homework10_3 {
public static void main(String[] args) throws IOException
{
System.out.println("請輸入字串");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String str = br.readLine();
System.out.println("你輸入的是"+str);
System.out.println("在第幾個字後面插入字母 a? 請輸入整數");
String str2 = br.readLine();
//這邊忘記要把 String 轉成 integer, 要記住, 因為後面是第幾個位置, 所以要轉
int num = Integer.parseInt(str2);
//StringBuffer 還沒有很熟, 要多練習
StringBuffer sb = new StringBuffer(str);
System.out.println("字串長度為="+str.length());
//使用StringBuffer 的類別來插入字元, 作法是, sb.insert(位置, 字元);
if(num <= str.length() )
{
sb.insert(num, 'a');
System.out.println("調整完後"+sb);
}
else
{
System.out.println("輸入的位置超出字串的長度 , 請輸入 <="+str.length()+"的整數");
}
}
}
package chapter10;
import java.io.*;
public class Homework10_4 {
public static void main(String[] args) throws IOException
{
System.out.println("請輸入兩個整數");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String str1 = br.readLine();
String str2 = br.readLine();
int num1 = Integer.parseInt(str1);
int num2 = Integer.parseInt(str2);
//這邊忘記 Math 類別如何使用, 要多練習
int ans = Math.min(num1, num2);
System.out.println(num1+"與"+num2+"比較小的是"+ans);
}
}
package chapter11.lab1;
//import chapter11.lab1.Car.RacingCar;
//類別的延伸 extends
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+"初始化油量為"+gas);
}
public void show()
{
System.out.println("車號為"+num);
System.out.println("油量為"+gas);
}
}
//宣告 賽車的子類別
class RacingCar extends Car
{
//宣告一個新欄位
private int course;
//子類別的建構子
public RacingCar()
{
course = 0;
System.out.println("初始化賽車");
}
//子類別的方法
public void setCourse(int c)
{
course = c;
System.out.println("編號為"+course);
}
}
public class Sample1 {
public static void main(String[] args)
{
RacingCar rccar1;
rccar1 = new RacingCar();
rccar1.setCar(1234, 20.5);
rccar1.setCourse(5);
rccar1.show();
}
}
package chapter11.lab2;
class Car
{
private int num;
private double gas;
public Car()
{
num = 0;
gas = 0;
System.out.println("初始化汽車");
}
public Car(int n, double g)
{
num = n;
gas = g;
System.out.println("初始化車號為"+num+"\t 設定油量為"+gas);
}
public void setCar(int n, double g)
{
num = n;
gas = g;
System.out.println("設定車號為"+num+"設定油量為"+gas);
}
public void show()
{
System.out.println("車號為"+num);
System.out.println("油量為"+gas);
}
}
//子類別 賽車
class RacingCar extends Car
{
private int course;
public RacingCar()
{
course = 0;
System.out.println("初始化賽車");
}
public RacingCar(int n, double g, int c)
{
//呼叫父類別內有2個引數的建構元
//super( ); 必須寫在第1行
super(n, g);
course = c;
System.out.println("初始化編號為"+course+"的賽車");
}
public void setCourse(int c)
{
course = c;
System.out.println("設定編號為"+course);
}
public void showCourse()
{
System.out.println("編號為"+course);
}
}
public class Sample2 {
public static void main(String[] args) {
RacingCar rccar1 = new RacingCar();
rccar1.show();
rccar1.setCar(1234, 20.5);
rccar1.show();
rccar1.showCourse();
System.out.println("建立 rccar2");
RacingCar rccar2 = new RacingCar(2345, 30.5, 6);
rccar2.show();
rccar2.showCourse();
}
}
package chapter11.lab3;
//存取保護 protected 成員的資料
//汽車的類別
class Car
{
// 這邊使用 protected 的方式, 讓子類別可以存取父類別的資料
protected int num;
protected double gas;
public Car()
{
num = 0;
gas = 0;
System.out.println("初始化汽車");
}
public void setCar(int n, double g)
{
num = n;
gas = g;
System.out.println("初始化車號為"+num+"初始化油量為"+num);
}
public void show()
{
System.out.println("車號為"+num);
System.out.println("油量為"+gas);
}
}
//賽車的子類別
class RacingCar extends Car
{
private int course;
public RacingCar()
{
course = 0;
System.out.println("初始化賽車");
}
public void setCourse(int c)
{
course = c;
System.out.println("設定編號為"+course);
}
public void newshow()
{
//因為父類別是使用 protected 所以可以存取
System.out.println("車號為"+num);
System.out.println("油量為"+gas);
System.out.println("編號為"+course);
}
}
public class Sample3 {
public static void main(String[] args) {
RacingCar rccar1 = new RacingCar();
rccar1.newshow();
rccar1.setCar(1234, 20.5);
rccar1.newshow();
rccar1.setCourse(7);
rccar1.newshow();
}
}
package chapter11.lab4;
//複寫方法
//父類別
class Car
{
protected int num;
protected double gas;
public Car()
{
num = 0;
gas = 0;
System.out.println("初始化汽車");
}
public void setCar(int n, double g)
{
num = n;
gas = g;
System.out.println("初始化車號為"+num+"初始化油量為"+gas);
}
public void show()
{
System.out.println("車號為"+num);
System.out.println("油量為"+gas);
}
}
//子類別
class RacingCar extends Car
{
private int course;
public RacingCar()
{
course = 0;
System.out.println("初始化賽車");
}
public void setCourse(int c)
{
course = c;
System.out.println("設定編號為"+course);
}
public void show()
{
System.out.println("賽車車號為"+num);
System.out.println("油量為"+gas);
System.out.println("編號為"+course);
}
}
public class Sample4 {
public static void main(String[] args) {
RacingCar rccar1 = new RacingCar();
//父類別與子類別都有 show() 的方法, 結果是呼叫子類別的方法
rccar1.show();
rccar1.setCar(1234, 20.5);
rccar1.setCourse(5);
rccar1.show();
}
}
package chapter11.lab5;
//使用父類別的變數來存取子類別的物件
//父類別
class Car
{
protected int num;
protected double gas;
public Car()
{
num = 0;
gas = 0;
System.out.println("初始化汽車");
}
public void setCar(int n, double g)
{
num = n;
gas = g;
System.out.println("初始化車號為"+num+"初始化油量為"+gas);
}
public void show()
{
System.out.println("車號為"+num);
System.out.println("油量為"+gas);
}
}
//子類別
class RacingCar extends Car
{
private int course;
public RacingCar()
{
course = 0;
System.out.println("初始化賽車");
}
public void setCourse(int c)
{
course = c;
System.out.println("設定編號為"+course);
}
public void show()
{
System.out.println("賽車車號為"+num);
System.out.println("油量為"+gas);
System.out.println("編號為"+course);
}
}
public class Sample5 {
public static void main(String[] args) {
//這邊是用父類別來產生變數
Car car1;
car1 = new RacingCar();
car1.setCar(1234, 20.5);
//這邊雖然是使用 父類別來產生變數 , 但是還是呼叫子類別的 show()
//由於 car1 是使用 RacingCar來產生物件, 所以以子類別為準
car1.show();
}
}
package chapter11.lab6;
//父類別
class Car
{
protected int num;
protected double gas;
public Car()
{
num = 0;
gas = 0;
System.out.println("初始化汽車");
}
public void setCar(int n, double g)
{
num = n;
gas = g;
System.out.println("初始化車號為"+num+"初始化油量為"+gas);
}
public void show()
{
System.out.println("車號為"+num);
System.out.println("油量為"+gas);
}
}
//子類別
class RacingCar extends Car
{
private int course;
public RacingCar()
{
course = 0;
System.out.println("初始化賽車");
}
public void setCourse(int c)
{
course = c;
System.out.println("設定編號為"+course);
}
public void show()
{
System.out.println("賽車車號為"+num);
System.out.println("油量為"+gas);
System.out.println("編號為"+course);
}
}
public class Sample6 {
public static void main(String[] args) {
//使用陣列的方式
Car cars[];
cars = new Car[2];
//從父類別產生物件
cars[0] = new Car();
cars[0].setCar(1234, 20.5);
//從子類別產生物件
cars[1] = new RacingCar();
cars[1].setCar(2345, 20.5);
//利用for 迴圈顯示
for (int i=0; i < cars.length ; i++)
{
//這邊會看到不同的物件所屬, 會使用不同的 show() 方法
cars[i].show();
}
}
}
package chapter11.lab7;
//在子類別覆寫 toString() 方法
class Car
{
protected int num;
protected double gas;
public Car()
{
num = 0;
gas = 0;
System.out.println("初始化汽車");
}
public void setCar(int n, double g)
{
num = n;
gas = g;
System.out.println("初始化車號為"+num+"初始化油量為"+gas);
}
//定義 toString 的方法
public String toString()
{
String str = "車號" + num + "汽油量" + gas;
return str;
}
}
public class Sample7 {
public static void main(String[] args) {
Car car1 = new Car();
car1.setCar(1234, 20.5);
System.out.println(car1);
}
}
package chapter11.lab8;
//使用 equals() 方法
class Car
{
protected int num;
protected double gas;
public Car()
{
num = 0;
gas = 0;
System.out.println("初始化汽車");
}
}
public class Sample8 {
public static void main(String[] args) {
Car car1 = new Car();
Car car2 = new Car();
Car car3;
//將 car1 指定給 car3
car3 = car1;
//判斷 car1 與 car2 是否相同
//car2 有使用 new 的關鍵字, 所以會配置一個新的記憶體, 當然不同
boolean br1 = car1.equals(car2);
//判斷 car1 與 car3 是否相同
//car3 是將car1 的變數指定給他, 指向同一個物件, 所以相同
boolean br2 = car1.equals(car3);
System.out.println("car1 與 car2 是否相同"+br1);
System.out.println("car1 與 car3 是否相同"+br2);
}
}
package chapter11.lab9;
//使用 getClass() 方法
//父類別
class Car
{
protected int num;
protected double gas;
public Car()
{
num = 0;
gas = 0;
System.out.println("初始化汽車");
}
}
//子類別
class RacingCar extends Car
{
private int course;
public RacingCar()
{
course = 0;
System.out.println("初始化賽車");
}
}
public class Sample9 {
public static void main(String[] args) {
Car cars[];
cars = new Car[2];
cars[0] = new Car();
cars[1] = new RacingCar();
for (int i=0; i < cars.length ; i++)
{
//使用 get.Class() 方法會傳回類別物件
Class c1 = cars[i].getClass();
System.out.println("第"+(i+1)+"個物件的類別是"+c1);
}
}
}
package chapter11.homework;
class Car
{
protected int num;
protected double gas;
public Car()
{
num = 0;
gas = 0;
System.out.println("初始化汽車");
}
public void setCar(int n, double g)
{
num = n;
gas = g;
System.out.println("初始化車號為"+num+"初始化油量為"+gas);
}
//定義 toString 的方法
public String toString()
{
String str = "車號為" + num + "汽油量為" + gas;
return str;
}
}
public class Homework11_4 {
public static void main(String[] args) {
Car car1 = new Car();
car1.setCar(1234, 20.5);
System.out.println(car1);
}
}
沒有留言:
張貼留言