Professional Documents
Culture Documents
int marketId
String spudType
String producingState
int price(per kg)
Write getters, setters and parameterized constructor in the above mentioned
attribute sequence as required.
1. findTypeByState in the solution class. This method will take array of Spud
objects, String state and returns spud type if found. If the state of any object
matches with String state, if found else return null if not found.
2. sortByPrice in the Solution class. This method will take array of Spud objects
and int pirce and returns the array of Spud object is ascending order of their
price which are less than Spud price input. If found else return null if not
found.
2. Take necessary input variable and call sortByPrice. For this method- The main
method should print the spudType and price from return object. As it is if the returned
value is not null, or it should print "No Spud found.”
Before calling these static methods in main, use Scanner object to read the
values of four spuds objects referring attributes in the above mentioned
Attribute sequence.
Next read the value for string parameter and int parameter.
Consider below sample input and output to test your code.
Input
121
Kufri Jyoti
Bengal
70
321
Bhura Aloo
Uttar Pradesh
55
621
Rosetta
Karnataka
40
921
Kufri Chipsona
Maharastra
90
Karnataka
60
Output
Rosetta
Rosetta
40
Bhura Aloo
55
Program-
import java.util.*;
class Spud{
int id;
String type;
String state;
int price;
class Solution{
public static void main(String []args)
{
Scanner sc=new Scanner(System.in);
Spud[] s=new Spud[4];
for(int i=0;i<s.length;i++)
{
int id=sc.nextInt();
sc.nextLine();
String type=sc.nextLine();
String state=sc.nextLine();
int price=sc.nextInt();
sc.nextLine();
s[i]=new Spud(id,type,state,price);
}
String state=sc.nextLine();
int price=sc.nextInt();
sc.close();
Spud m=findTypeByState(s,state);
if(m!=null)
{
System.out.println(m.getType());
}
else
{
System.out.println("State not found.");
}
Spud[] m2=sortByPrice(s,price);
if(m2!=null)
{
for(int i=0;i<m2.length;i++)
{
System.out.println(m2[i].getType());
System.out.println(m2[i].getPrice());
}
}
else
{
System.out.println("No Spud found.");
}
}
if(m==null)
{
return null;
}
else
{
return m;
}
}
s2=new Spud[size];
for(int i=0;i<s2.length;i++)
{
for(int j=0;j<s.length;j++)
{
if(pq.peek()==s[j].getPrice())
{
s2[i]=s[j];
pq.poll();
break;
}
}
if(pq.size()==0)
{
break;
}
}
if(s2==null)
{
return null;
}
else
{
return s2;
}
}
}