may 31 java coding tcs cpa question.
create the class Allium with below attributes.
marketId=int
alliumType=String
producingState=String
price=int
the above attributes should be private.write getter
and setter and parameterized constructor.
create class AlliumProgram with main method.
implement two static methods- findTypeByState
and sortByprice in AlliumProgram class.
findTypeByState method:
This method will take an array of Allium objects.
and a String value as input parameters. The method
will find out type of Allium for given producing state
(String parameter passed).This method will return
AlliumType if found.if there is no Allium that matches
then the method should return null.
sortByPrice method:
This method will take an array of Allium objects and
an int value as input parameters.
This method should return an array of Allium objects
in an ascending order of their price which are less
than the given Allium price(int parameter passed).
if there is no such object found, then the method
should return null.
the above mentioned methods should be called
from main method.
for findTypeByState method- The main method should
print the AlliumType if the returned value is not null.
if the returned value is null,then it should print
"State not found."
for sortByPrice method-The main method should print
the AlliumType and price from the returned Allium
object array if the returned value is not null.
if the returned value is null then the
main method should print "No Allium found."
input1:
121
yellow Allium
bengal
70
321
sweet Allium
uttar pradesh
55
621
red Allium
karnataka
40
921
white Allium
maharashtra
90
karnataka
60
output1:
red Allium
red Allium
40
sweet Allium
55
input 2:
221
scallions
bihar
120
721
pearl Allium
uttar pradesh
100
521
shallots
sikkim
150
421
nigrum
tamil nadu
200
Andhra pradesh
90
output2:
State not found.
No Allium found.
code:
package practice;
import java.util.Arrays;
import java.util.Scanner;
public class alliumProgram {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
Allium[] alliums=new Allium[4];
for (int i = 0; i <alliums.length ; i++) {
int a=sc.nextInt();
sc.nextLine();
String b=sc.nextLine();
String c=sc.nextLine();
int d=sc.nextInt();
sc.nextLine();
alliums[i]=new Allium(a,b,c,d);
}
String input1=sc.nextLine();
int input2=sc.nextInt();
sc.nextLine();
String ans1=findTypeByState(alliums,input1);
if(ans1==null)
{
System.out.println("State not found.");
}
else
{
System.out.println(ans1);
}
Allium[] ans2=sortByPrice(alliums,input2);
if(ans2==null)
{
System.out.println("No Allium found.");
}
else
{
for (int i = 0; i <ans2.length ; i++) {
System.out.println(ans2[i].alliumType);
System.out.println(ans2[i].price);
}
}
}
public static String findTypeByState(Allium[] alliums,String input1)
{
for (int i = 0; i <alliums.length ; i++) {
if(alliums[i].producingState.equalsIgnoreCase(input1))
{
return alliums[i].alliumType;
}
}
return null;
}
public static Allium[] sortByPrice(Allium[] alliums,int input2)
{
Allium[] help=new Allium[0];
for (int i = 0; i <alliums.length ; i++)
{
if(alliums[i].price<input2)
{
help= Arrays.copyOf(help,help.length+1);
help[help.length-1]=alliums[i];
}
}
//bubble sort
for (int i = 0; i <help.length-1 ; i++) {
for (int j = 0; j <help.length-i-1 ; j++) {
if(help[j].price>help[j+1].price)
{
Allium temp=help[j];
help[j]=help[j+1];
help[j+1]=temp;
}
}
}
if(help.length>0)
{
return help;
}
return null;
}
}
class Allium{
int marketId;
String alliumType;
String producingState;
int price;
//parametrized constructor
public Allium(int marketId, String alliumType, String producingState, int price) {
this.marketId = marketId;
this.alliumType = alliumType;
this.producingState = producingState;
this.price = price;
}
}
Comments
Post a Comment