0

Currently I am teaching myself Java but I came across a simple problem but have no one to ask from. From one of the exercises, I wrote a class and write a driver class that instantiates and updates several objects. I am confused by "instantiates and updates several objects." Here is what I mean: So here is my class:

public class PP43Car {

    private String make = "";
    private String model = "";
    private int year;

    public PP43Car(String ma, String m, int y)
    {
        make = ma;
        model = m;
        year = y;
    }

    public void setMake(String ma)
    {
        make = ma;
    }

    public String getMake()
    {
        return make;
    }

    public void setModel(String m)
    {
        model = m;
    }

    public String getModel()
    {
        return model;
    }

    public void setYear(int y)
    {
        year = y;
    }

    public int getYear()
    {
        return year;
    }

    public String toString()
    {
        String result = "Make of the vehicle: " + make +
                        " Model of the vehicle " + model + 
                        " Year of the vehicle: " + year;
        return result;
    }
}

Which instantiates make, model and year. Then once I was writing the driver class, the way I began was:

import java.util.Scanner;

public class PP43CarTest {


    public static void main(String[] args) {

        PP43Car car1;

        Scanner scan = new Scanner(System.in);

        System.out.println("Enter the model of the vehicle:");
        car1.getModel();
    }

}

But this class produces error and here is where I am stuck. Do I keep on going with this or is this what is meant by "instantiating and updating several objects?"

import java.util.Scanner;

public class PP43CarTest {

    static PP43Car car1;

    public static void main(String[] args) {
        //Scanner scan = new Scanner(System.in);
        car1 = new PP43Car("Millenia", "Mazda", 2011);      
    }

}

If the above code is correct, then can anyone show me how I can use the Scanner class to actually get the user input and update it that way because I would like to learn that as well?

4

1 に答える 1

0

さて、コードの最後のフラグメントでは、次のことを行っているため、実際にオブジェクトをインスタンス化しています。

car1 = new PP43Car("Millenia", "Mazda", 2011);

オブジェクトを作成するとき、クラスnewの新しいインスタンスを作成しているので、はい、オブジェクトをインスタンス化しています。

ここでの更新はオブジェクトの変更を意味すると思いますが、オブジェクトを作成するだけで、変更することはありません...

このようなものは更新になります:

car1.setYear(2013);

オブジェクトの属性に異なる値を設定しているため、更新しています...


編集: このコードを試してみてください。例外をスローすることはできません。これは Java の基本です! それがあなたの疑問を明確にすることを願っています...

public class PP43CarTest {

    public static void main(String[] args) {

        //Declaring objects
        PP43Car car1;
        PP43Car car2;
        PP43Car car3;

        //Instantiating objects
        car1 = new PP43Car("Millenia", "Mazda", 2011);
        car2 = new PP43Car("Aaaa", "Bbb", 2012);
        car3 = new PP43Car("Ccc", "Ddd", 2012);

        //Updating objects
        car1.setMake("Xxx");
        car1.setMake("Yyy");
        car1.setYear(2013);

        //Printing objects
        System.out.println("CAR 1: " + car1.toString());
        System.out.println("CAR 2: " + car2.toString());
        System.out.println("CAR 3: " + car3.toString());
    }
}
于 2013-06-10T00:17:42.390 に答える