Java Session-Vehicle

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

Question:

Create a class Vehicle with below attributes:


number - int
name - String
price - double

Write getters, setters and parameterized constructor in the above mentioned attribute
sequence as required.

Create class Solution with main method


Implement two static methods - findVehicleWithMinimumPrice and searchVehicleByName
in Solution class.

FindVehicleWithMinimumPrice

Create a static method findVehicleWithMinimumPrice in the Solution class. This method


will take array of Vehicle objects and returns the Vehicle object having the minimum Price
if found else return null if not found.

SearchVehicleByName

Create a static method searchVehicleByName in the Solution class. This method will take
array of Vehicle objects and Name as input and returns the Vehicle object having the
mentioned Name if found else return null if not found.

These methods should be called from the main method.

Write code to perform the following tasks:


1. Take necessary input variable and call findVehicleWithMinimumPrice. For this method -
The main method should print the Vehicle object with minimum of mentioned attribute as it
is if the returned value is not null, or it should print "No Vehicle found with mentioned
attribute".
2. Take necessary input variable and call searchVehicleByName. For this method - The
main method should print the Vehicle object details as it is, if the returned value is not null,
or it should print "No Vehicle found with mentioned attribute".
The above mentioned static methods should be called from the main method. Also write
the code for accepting the inputs and printing the outputs. Don't use any static test or
formatting for printing the result. Just invoke the method and print the result
Note :
All String comparison needs to be case in-sensitive
You can use/refer the below given sample input and output to verify your solution.
Sample Input (below) description:
The 1st input taken in the main section is the number of Vehicle objects to be added to the
list of Vehicle.
The next set of inputs are number,name,price for each Vehicle object taken one after other
and is repeated for number of Vehicle objects given in the first line of input.
The last line of inputs will be the arguments which needs to be passed as parameter to the
methods.

Consider below sample input and output to test your code:


Sample Input 1 - public
4
3111
bus
1000.0
4112
crane
1800.0
5114
Tractor
2400.0
6115
Scooter
2700.0
Scooter

Sample Output 1
number-3111
name-bus
price-1000.0
number-6115
name-Scooter
price-2700.0

Sample Input 2 - public


4
1109
Train
2000.0
5104
taxi
2800.0
4112
bike
5600.0
8107
Scooter
3300.0
bike

Sample Output 2
number-1109
name-Train
price-2000.0
number-4112
name-bike
price-5600.0
Solution:

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {


public static void main(String args[] ) throws Exception {
/* Enter your code here. Read input from STDIN. Print output to STDOUT */

Scanner sc=new Scanner(System.in);


int n=sc.nextInt();
Vehicle v[]=new Vehicle[n];
//always use extra sc.nextLine() between numeric and string input
for(int i=0;i<v.length;i++){
int num=sc.nextInt();sc.nextLine();
String name=sc.nextLine();
double price=sc.nextDouble();
v[i]=new Vehicle(num, name, price);
}
sc.nextLine();
String sn=sc.nextLine();
//System.out.println(sn);

// calling methods
Vehicle an1=findVehicleWithMinimumPrice(v);
Vehicle an2=searchVehicleByName(v, sn);

//printing methods
if(an1==null){
System.out.println("No Vehicle found with mentioned attribute");
}
else{
System.out.println("number-"+an1.getNum());
System.out.println("name-"+an1.getName());
System.out.println("price-"+an1.getPrice());

}
//2nd method printing
if(an2==null){
System.out.println("No Vehicle found with mentioned attribute");
}
else{
System.out.println("number-"+an2.getNum());
System.out.println("name-"+an2.getName());
System.out.println("price-"+an2.getPrice());

//main close
}
public static Vehicle findVehicleWithMinimumPrice(Vehicle[] v){

// Enter your code here


Vehicle ans1=null;
double min=v[0].getPrice(); //min=1000
for(int i=0;i<v.length;i++){
//i=0; 1000<=1000 true min=100 ans1=v[0]
//i=1; 1800<1000 false
//i=2; 2400<1000 false
//i=3 700<100 true min=700 ans1=v[3]
if(v[i].getPrice()<=min){
min=v[i].getPrice();
ans1=v[i];
}
}
return ans1;

public static Vehicle searchVehicleByName(Vehicle[] v,String sn){

// Enter your code here


Vehicle ans2=null;
for(int i=0;i<v.length;i++){
if(v[i].getName().equalsIgnoreCase(sn)){
// Scooter -- scooter true SCOOTER SCooter
ans2=v[i];
}
}
return ans2;
}
public static Vehicle findMaxNumber(Vehicle[] v)
{
vehicle ans3=null;
int max=0;
for(int i=0;i<v.length;i++){
if(v[i].getNum()>max){
ans3=v[i];
max=v[i].getNum();
}
}
return ans3;
}
public static double sumOfPrice(Vehicle[] v)
{
double sum=0;
for(int i=0;i<v.length;i++){
sum=sum+v[i].getPrice()
}
return sum;
}

public static double AvgOfPrice(Vehicle[] v)


{
double sum=0;
for(int i=0;i<v.length;i++){
sum=sum+v[i].getPrice()
}
return sum/v.length;

public static Vehicle searchVehicleByPrice(Vehicle[] v,double p){

// Enter your code here


Vehicle ans4=null;
for(int i=0;i<v.length;i++){
if(v[i].getPrice()==p){
ans4=v[i];
}
}
return ans4;
}

}//class closes

class Vehicle {

// Enter your code here


private int num;
private String name;
private double price;

public Vehicle(int num,String name,double price){


this.name=name;
this.num=num;
this.price=price;
}

public int getNum(){return num;}


public String getName(){return name;}
public double getPrice(){return price;}

public void setNum(int num){this.num=num;}


public void setName(String s){this.name=s;}
public void setPrice(double p){this.price=p;}

}
// extra brace- enum expected
//class }using incorrectly-- static and non static
// one less- reached end of file while parsing
// close main
//null pointer
//array out of bounds
//partial test cases

You might also like