Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Engine Java
//'main' method must be in a class 'Rextester'. //openjdk version '11.0.5' import java.util.*; import java.lang.*; class Engine{ private int engineId; private String engineName; private String engineType; private double enginePrice; public Engine(int id,String name,String type,double price) { engineId=id; engineName=name; engineType=type; enginePrice=price; } public int getEngineId() { return engineId; } public double getEnginePrice() { return enginePrice; } public String getEngineName() { return engineName; } public String getEngineType() { return engineType; } } class Rextester { 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 engineid = sc.nextInt(); sc.nextLine(); String enginename=sc.nextLine(); String enginetype=sc.nextLine(); double engineprice=sc.nextDouble(); eng[i] = new Engine(engineid,enginename,enginetype,engineprice); } sc.nextLine(); String type = sc.nextLine(); String name = sc.nextLine(); double ans = findAvgEnginePriceByType(type,eng); if(ans>0) System.out.println(ans); else System.out.println("There are no engine with given type"); Engine[] ans1 = searchEngineByName(name,eng); if(ans1[0]==null) System.out.println("there are no engine with given name"); else { int j=0; while(ans1[j]!=null) { System.out.println(ans1[j].getEngineId()); j++; } } } public static double findAvgEnginePriceByType (String type,Engine en[]) { int sz=0; double price=0; for(int i=0;i<4;i++) { if((en[i].getEngineType()).equalsIgnoreCase(type)) { sz++; price+=en[i].getEnginePrice(); } } price=price/sz; return price; } public static Engine [] searchEngineByName(String name,Engine en[]) { int sz=0; for(int i=0;i<4;i++) { if((en[i].getEngineName()).equalsIgnoreCase(name)) { sz++; } } if(sz==0) return null; Engine ans[]=new Engine[sz]; int k=0; for(int i=0;i<4;i++) { if((en[i].getEngineName()).equalsIgnoreCase(name)) { ans[k++]=en[i]; } } Engine temp; for (int i = 0; i < sz;i++) { for(int j=i;j<sz;j++) { if(ans[i].getEngineId()>ans[j].getEngineId()) { temp=ans[i]; ans[i]=ans[j]; ans[j]=temp; } } } return ans; } } /* Link=>https://codebashers10.blogspot.com/2021/04/TCS-Xplore-CPA-27-march-2021-Shift-2-Java-Coding-Solution.html */
run
|
edit
|
history
|
help
0
PE #7
first activity
Counter.java
Java
Coding Challenge - 02 (Odd numbers)
Hello
a+b
move zeros to end of array
Sort arraylist bubble sort
jb7.0 extends.override