0% found this document useful (0 votes)
43 views6 pages

IRA Code Car

The document describes creating a Car class with attributes like id, speed, miles travelled, and brand. It then describes creating a Solution class with a main method. The main method will take user input of cars, call two static methods - countCarsByBrand to count cars by a given brand, and getSecondLowestMilesTravelledCar to return the car with the second lowest miles travelled above a given number. It then provides sample input/output to test the code.

Uploaded by

Rahul Deore
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views6 pages

IRA Code Car

The document describes creating a Car class with attributes like id, speed, miles travelled, and brand. It then describes creating a Solution class with a main method. The main method will take user input of cars, call two static methods - countCarsByBrand to count cars by a given brand, and getSecondLowestMilesTravelledCar to return the car with the second lowest miles travelled above a given number. It then provides sample input/output to test the code.

Uploaded by

Rahul Deore
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Create a class Car with below attributes

 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

Implement two static methods-


 countCarsByBrand
 getSecondLowestMilesTravelledCar

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.

This method should be called from main method.


Write code to perform following tasks:
1. Take necessary input variable and call countCarsByBrand. For this method- The
main method should print the total number of cars having the brand same. As if the
returned value is no 0, or it should print "No such car found ".

2. Take necessary input variable and call getSecondLowestMilesTravelledCar . For


this method- The main method should print the Car object details as it is if the
returned value us not null, or it should print "No such car found ".
Consider below sample input and output to test your code.

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;

Car(int carId, int topSpeed, double milesTravelled, String brand)


{
this.carId=carId;
this.topSpeed=topSpeed;
this.milesTravelled=milesTravelled;
this.brand=brand;
}

public int getCarId(){ return this.carId;}


public int getTopSpeed(){ return this.topSpeed;}
public double getMilesTravelled(){ return this.milesTravelled;}
public String getBrand(){ return this.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();

c[i]=new Car(carId, topSpeed, milesTravelled, brand);


}
String brand=sc.nextLine();
double miles=sc.nextDouble();
sc.nextLine();
sc.close();

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.");
}
}

public static int findVehicleByBrand(Car []c, String brand)


{
int count=0;

if(c!=null && brand.length()>0)


{
for(int i=0;i<c.length;i++)
{
if(c[i].getBrand().equalsIgnoreCase(brand))
{
count++;
}
}

return count;
}

public static Car getSecondLowestMilesTravelledCar(Car []c, double miles)


{
Car m=null;

PriorityQueue<Double> pq =new PriorityQueue<Double>();

if(c!=null && miles>0)


{
for(int i=0;i<c.length;i++)
{
if(c[i].getMilesTravelled()>=miles)
{
pq.add(c[i].getMilesTravelled());
}
}

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;
}

}
}

You might also like