初階Java array 問題

功課問題...做到呢度比佢玩謝左
後面D 問題都答唔到落去..
請問如果我想寫個car[] array 去裝車既details, e.g. brand 同price
個寫array constructor 應該點寫先有getter 同setter 效果???
例如寫2個class, 一個係Car, 一個係TestCar

public class Car{
    public Car(Car[] car){
        for (int i=0; i < car.length; i++) {
            ????點寫setter?
        }
    }
}

public class TestCar {   
    public static void main (String[] args){
        Car[] carShop = {new Car("audi",500000), new Car("toyota",150000), new Car("honda",48000)};
    }
}

本帖最後由 bbp082674 於 2015-1-5 12:19 編輯

回覆 1# murooney

個for loop係你自己寫?定題目比?好似無咩用咁...

public class Car{

  String name;
  int price;
    public Car(String name, int price){
        
            this.name = name;
            this.price = price;
      
    }
  //getter setting可以另外寫or好似上面constructor咁直接入
  //...
}

public class TestCar {   
    public static void main (String[] args){
        Car[] carShop = {new Car("audi",500000), new Car("toyota",150000), new Car("honda",48000)};
    }
}

TOP

回覆  murooney

個for loop係你自己寫?定題目比?好似無咩用咁...

public class Car{

  String name;
...
bbp082674 發表於 2015-1-5 12:13


for loop係我自己寫

因為題目要用Car(Car[] car)
所以唔知點樣放野入去....

TestCar 都係我自己寫
題目只係要我入哂果3樣野入去

題目淨係話要寫個Car(Car[] car) constractor

btw, thanks!!!!!!

TOP

本帖最後由 bbp082674 於 2015-1-5 14:15 編輯
for loop係我自己寫

因為題目要用Car(Car[] car)
所以唔知點樣放野入去....

TestCar 都係我自己寫
題目 ...
murooney 發表於 2015-1-5 13:16


題目話要array constructor定比埋個 Car(Car[] car) 你?
正常來講 一架車入面唔會有幾個brand同price

一個carShop有幾個car係合理

Car[] carShop = new Car[]{new Car("audi",500000), new Car("toyota",150000), new Car("honda",48000)};
定係你想咁?

TOP

定係你想寫
for (int i=0; i < car.length; i++) {
       this.car[i] = car[i];
}

TOP

你肯定Car(Car[] car)?

TOP

本帖最後由 murooney 於 2015-1-5 15:23 編輯
題目話要array constructor定比埋個 Car(Car[] car) 你?
正常來講 一架車入面 唔會有幾個brand同price

...
bbp082674 發表於 2015-1-5 14:06



係! 應該CarShop(Car[] car) 先岩!
我係根據記憶大約打出黎
題目是咁的:

a)
Create the class CarShop which contains an attribute car which is a Car array. In the constructor CarShop(Car[] car), initialize the attribute carusing the parameter. Also write the getter/setter method of the attribute car. Copy the content of the file as the answers to this part

b)
Create another class TestCarShop in a separate file with a method main() to perform the following:
1. create a CarShop object myCarShop and initialize it with data for 3 cars: ("white", 50), ("black", 45), ("red", 48);


上一題寫左個, 唔知岩唔岩用....
public class Car {
    private String colour;
    private int size;
   
    public Car(String aColour, int aSize){
        colour = aColour;
        size = aSize;
    }
    public String getColour(){
        return colour;
    }
    public int getSize(){
        return size;
    }
}

TOP

你肯定Car(Car[] car)?
Jackass_TMxCK 發表於 2015-1-5 14:54



睇返題目係CarShop(Car[] car)
岩岩憑記憶大約打出黎
諗住同CLASS 名一樣就得.....

TOP

定係你想寫
for (int i=0; i < car.length; i++) {
       this.car = car;
}
KinChungE 發表於 2015-1-5 14:46



我都唔知 Car[] car點先裝到2樣野入去...係唔係要CALL 多個CLASS?

TOP

我都唔知 Car[] car點先裝到2樣野入去...係唔係要CALL 多個CLASS?
murooney 發表於 2015-1-5 15:14


call多個class?  唔明
Class係用黎call架咩

TOP