in JAVA by
Q:
TCS xplore java coding questions

1 Answer

0 votes
by

Question :

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

Code : 

import java.util.*;
import java.lang.*;
import java.io.*;


class Myclass
{
	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++)
		{
		    Engine temp = new Engine();
		    temp.engineid = sc.nextInt();
		    sc.nextLine();
		    temp.enginename=sc.nextLine();
		    temp.enginetype=sc.nextLine();
		    temp.engineprice=sc.nextDouble();
		    eng[i] = temp;
		}
		
		sc.nextLine();
		String type = sc.nextLine();
		String name = sc.nextLine();
		
		double ans = findavgpriceofenginebytype(eng,type);
		
		if(ans>0) System.out.println(ans);
		
		else System.out.println("There are no engine with given type");
		
		Engine[] ans1 = searchenginebyname(eng,name);
		
		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].engineid);
		        j++;
		    }
		}
		
	}
	
	public static double findavgpriceofenginebytype(Engine[] eng , String type)
	{
	    double totalprice=0.0;
	    double count=0;
	    
	    for(int i=0;i<eng.length;i++)
	    {
	        if((eng[i].enginetype).equalsIgnoreCase(type))
	        {
	            totalprice+=eng[i].engineprice;
	            count++;
	        }
	    }
	    
	    if(count==0) return 0.0;
	    
	    return totalprice/count;
	    
	}
	
	public static Engine[] searchenginebyname(Engine[] eng , String name)
	{
	    
	    for(int i=0;i<eng.length;i++)
	    {
	        for(int j=1;j<eng.length;j++)
	        {
	            if(eng[j].engineid<eng[j-1].engineid)
	            {
	                Engine temp = eng[j];
	                eng[j] = eng[j-1];
	                eng[j-1] = temp;
	            }
	        }
	    }
	    
	    Engine[] ans = new Engine[4];
	    int j=0;
	    
	    for(int i=0;i<eng.length;i++)
	    {
	        if((eng[i].enginename).equalsIgnoreCase(name))
	        {
	            ans[j] = eng[i];
	            j++;
	        }
	    }
	    
	    return ans;
	}
}

class Engine
{
    public int engineid;
    public String enginename,enginetype;
    public double engineprice;
    
    Engine()
    {
        this.engineid=0;
        this.enginename="";
        this.enginetype="";
        this.engineprice=0.0;
    }
}

Related questions and answers

0 votes
asked Dec 4, 2019 in JAVA by rajeshsharma
0 votes
asked Aug 21 in JAVA by sharadyadav1986
+2 votes
asked May 13, 2021 in JAVA by rajeshsharma
...