IRA Code Car
IRA Code Car
int carId
int topSpeed
double milesTravelled
String brand
Write getters, setters and parameterized constructor in the above mentioned
attribute sequence as required.
Create class Solution with main method
1. countCarsByBrand
countCarsByBrand in the solution class. This method will take array of Cars objects,
String brand and returns the total number of car with brand name if found else return
0 if not found.
2. getSecondLowestMilesTravelledCar
getSecondLowestMilesTravelledCar in the Solution class. This method will take
array of Cars objects and miles and returns the Car object having the second lowest
miles travelled whose travelled miles is more than miles input, if found else return
null if not found.
Input
4
1001
200
2000
Audi
1002
134
3000
Maserati
1003
180
5500
Audi
1004
205
6000
Ferrari
Audi
5000
Ouput
2
carId-1003
topSpeed-180
milesTravelled-5500.0
brand-Audi
Program
import java.util.*;
class Car{
int carId;
int topSpeed;
double milesTravelled;
String brand;
}
public class Main
{
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
Car []c=new Car[n];
for(int i=0;i<c.length;i++)
{
int carId=sc.nextInt();
sc.nextLine();
int topSpeed=sc.nextInt();
sc.nextLine();
double milesTravelled=sc.nextDouble();
sc.nextLine();
String brand=sc.nextLine();
int count=findVehicleByBrand(c,brand);
if(count!=0)
{
System.out.println(count);
}
else
{
System.out.println("No such car found.");
}
Car result=getSecondLowestMilesTravelledCar(c,miles);
if(result!=null)
{
System.out.println("carId-"+result.getCarId());
System.out.println("topSpeed-"+result.getTopSpeed());
System.out.println("milesTravelled-"+result.getMilesTravelled());
System.out.println("brand-"+result.getBrand());
}
else
{
System.out.println("No such car found.");
}
}
return count;
}
if(pq.size()>0)
{
while(pq.size()!=2)
{
pq.poll();
double z=pq.peek();
for(int i=0;i<c.length;i++)
{
if(c[i].getMilesTravelled()==z)
{
m=c[i];
}
}
}
if(m==null)
{
return null;
}
else
{
return m;
}
}
}