Thursday, 20 June 2024

class 2 - how to check no of RED car present in the CarShop

package com.suchil;

public class Car {

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

private String name;

public String getColor() {
return color;
}

public void setColor(String color) {
this.color = color;
}

public String getType() {
return type;
}

public void setType(String type) {
this.type = type;
}

@Override
public String toString() {
return "Car{" +
"name='" + name + '\'' +
", color='" + color + '\'' +
", type='" + type + '\'' +
'}';
}

private String color;
private String type;

} 




package com.suchil;


import java.util.ArrayList;
import java.util.List;

public class CarShop {


public static void main(String[] args) {


Car c1 = new Car();
c1.setColor("RED");
c1.setName("Honda");
c1.setType("Sedan");

System.out.println(c1);

Car c2 = new Car();
c2.setColor("WHITE");
c2.setName("Honda");
c2.setType("Sedan");


Car c3 = new Car();
c3.setColor("RED");
c3.setName("Honda");
c3.setType("Sedan");

Car c4 = new Car();
c4.setColor("RED");
c4.setName("Honda");
c4.setType("Sedan");

Car c5 = new Car();
c5.setColor("RED");
c5.setName("Honda");
c5.setType("Sedan");

List<Car> lst = new ArrayList<>();
lst.add(c1);
lst.add(c2);
lst.add(c3);
lst.add(c4);
lst.add(c5);

int redcount = 0;
for (int i = 0; i < lst.size(); i++) {
Car c = lst.get(i);

String s = c.getColor();
if (s.equals("RED")) {
redcount++;
}

}



}
}

No comments:

Post a Comment

06/20/024 ( TODO)

Q1 : Create array , add element ,remove element , and update the element  Q2: Show me how overloading and overriding work in inheritance wit...