Skip to content

Commit

Permalink
Engine (IPA-35)
Browse files Browse the repository at this point in the history
  • Loading branch information
Arijit-SE committed Feb 26, 2023
1 parent f8754ef commit 412c74b
Show file tree
Hide file tree
Showing 2 changed files with 196 additions and 0 deletions.
127 changes: 127 additions & 0 deletions IPA20/IPA20.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
package IPA20;
import java.util.*;
public class IPA20 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Engine[] eng = new Engine[4];
for (int i = 0; i < eng.length; i++) {
int a = sc.nextInt();sc.nextLine();
String b = sc.nextLine();
String c = sc.nextLine();
double d = sc.nextDouble();sc.nextLine();

eng[i]=new Engine(a, b, c, d);
}
String t = sc.nextLine();
String n = sc.nextLine();
int ans1 = findAvgEnginePriceByType(eng,t);
if(ans1!=0)
{
System.out.println(ans1);
}
else
{
System.out.println("There are no engine with given type");
}

Engine[] ans2 = searchEngineByName(eng,n);
if(ans2!=null)
{
for (int i = 0; i < ans2.length; i++) {
System.out.println(ans2[i].getId());
}
}
}
public static int findAvgEnginePriceByType(Engine[]e, String t)
{
int sum=0, count=0;
for (int i = 0; i < e.length; i++) {
if(e[i].getType().equalsIgnoreCase(t))
{
sum+=e[i].getPrice();
count++;
}
}
if(count>0)
{
int avg = sum/count;
return avg;
}
else
{
return 0;
}
}

public static Engine[] searchEngineByName(Engine[]e, String n)
{
Engine[] arr = new Engine[0];
for (int i = 0; i < e.length; i++) {
if(e[i].getName().equalsIgnoreCase(n))
{
arr = Arrays.copyOf(arr,arr.length+1);
arr[arr.length-1]=e[i];
}
}
Engine data;
for (int i = 0; i < arr.length-1; i++)
{
for (int j = i+1; j < arr.length; j++)
{
if(arr[i].getId()>arr[j].getId())
{
data = arr[i];
arr[i] = arr[j];
arr[j] = data;
}
}
}
if(arr.length>0)
{
return arr;
}
else
{
return null;
}
}
}
class Engine
{
private int id;
private String name;
private String type;
private double price;
public Engine(int id, String name, String type, double price) {
this.id = id;
this.name = name;
this.type = type;
this.price = price;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}


}
69 changes: 69 additions & 0 deletions IPA20/IPA20.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
Create a class Engine with the below attributes:

engineld – int

engineName – String

engine Type – String

enginePrice – double

The above attributes should be private, write getters, setters and parameterized constructor as required.

Create class MyClass with main method.

Implement two static methods – findAvgEnginePriceByType and searchEngineByName in MyClass class.

findAvgEnginePriceByType method: This method will take two input parameters Engine objects and String parameter.
The method will return the average price of Engines from array of Engine objects for the given engine type
(String parameter passed). If no Engine with the given type is present in the array of Engine objects, then the method
should return 0.


searchEngineByName method: This method will take a String parameter and an array of Engine objects as the other parameter.
The method will return Engine object array in an ascending order of their engine ids, from the array of Engine objects whose
name attribute matches with the given engine name (String parameter passed). If no Engine with the given name is present in
the array of Engine objects, then the method should return null.

Note: No two Engine object would have the same engine id.

All searches should be case insensitive.

The above mentioned static methods should be called from the main method.

For findAvgEnginePriceByType method: The main method should print the average enginePrice of Engines as it is, if the returned
value is greater than 0, or it should print “There are no engine with given type”.

For searchEngineByName method: The main method should print the engineld from the returned Engine object array if the returned
value is not null. If the returned value is null, then it should print “There are no engine with the given name”.

Before calling these static methods in main, use Scanner object to read the values of four Engine objects referring attributes
in the above mentioned attribute sequence. next, read the value of two String parameters for capturing engine type and engine
name respectively.

Input :

1001
Maruti
Diesel
20000
1002
Kia
Pertro
17000
1003
Hyundai
Diesel
24000
1000
Maruti
Petrol
27500
Petrol
Maruti

Output :

27500
1000
1001

0 comments on commit 412c74b

Please sign in to comment.