Question: Write the static getCounter method public class Car { private String color; private int numDoors; private double topSpeed; static int counter = 0; // Default

Write the static getCounter method

public class Car

{

private String color;

private int numDoors;

private double topSpeed;

static int counter = 0;

// Default constructor

Car()

{

this("Black", 4, 150.0);

}

// Parameterized constructor

Car(String color, int numDoors, double topSpeed)

{

super();

counter++;

this.color = color;

this.numDoors = numDoors;

this.topSpeed = topSpeed;

}

// Copy constructor

public String getColor()

{

return color;

}

public void setColor(String color)

{

this.color = color;

}

public int getnumDoors()

{

return numDoors;

}

public void setnumDoors(int newNumber)

{

if (newNumber < 0)

{

System.out.println("Invalid!");

return;

}

numDoors = newNumber;

}

public double gettopSpeed()

{

return topSpeed;

}

public void settopSpeed(double newtopSpeed)

{

if (newtopSpeed < 0)

{

System.out.println("Invalid!");

return;

}

topSpeed = newtopSpeed;

}

public void display()

{

System.out.println("Color: " + color);

System.out.println("Number of Doors: " + numDoors);

System.out.println("Top Speed: " + topSpeed);

}

@Override

public String toString()

{

return ("Color: " + color + " numDoors: " + numDoors + " topSpeed: " + topSpeed);

}

public boolean equals(Car other)

{

// if (other == this)

// return true;

// if (!(other instanceof Car))

// return false;

// else

// return true;

return (this.color == other.color) && (this.numDoors == other.numDoors) && (this.topSpeed == other.topSpeed) ;

}

}

---------------------------------------------------------------------------

public class Demo {

public static void main(String[] args)

{

Car car1 = new Car();

car1.setColor("White");

car1.setnumDoors(4);

car1.settopSpeed(140.0);

car1.display();

Car car2 = new Car("Red", 4, 100);

car2.display();

System.out.println(car2.toString());

boolean result = car2.equals(car1);

System.out.println("Compairing car2 and car1 " + result);

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!