Q:

implement class Car using java OOP

0

Implement the following in java:

  • Class "Car" where it has two instance attributes:

           1. String Owner

           2. String Color.

           3. one method to print their values called "display".

  • Class "Example", where we have the "main" method to run the program.

you need to use the keyword "this" with the constructor of class Car.

you need to insure information hiding by making the Car attributes "private". you also need to write appropriate get method in class Car to receive the values of attributes. please create two objects:

"AbdullahCar", Red" and "AhmedCar", white" and print their owner and color information from main method. 

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

class Car{
    private String owner;
    private String color;
    public Car(String owner,String color)
    {
        this.owner=owner;
        this.color=color;
    }
    public void setOwner(String owner)
    {
        this.owner=owner;
    }
    public void setColor(String color)
    {
        this.color=color;
    }
    public String getOwner()
    {
        return owner;
    }
    public String getColor()
    {
        return color;
    }
    public void display()
    {
        System.out.println("the car owner: "+owner+", the car color: "+color);
    }
}

public class Example {

    public static void main(String[] args) {
	    Car c1=new Car("Abdullah","Red");
	    c1.display();
		Car c2=new Car("Ahmed","white");
	    c2.display();
    }
}

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Write a java class called Products that reads prod... >>
<< What is the output of the following program? Class...