Question 1
Question 1
Question 1
Input:
12345
Output:
Reverse of the number is 54321
Solution
int n, reverse=0;
System.out.println(" ");
n=in.nextInt();
while(n!=0)
reverse=reverse*10;
reverse=reverse+n%10; n=n/10;
Question 3 :
Write main method in Solution class.
In the main method, read a String and print all consonants characters (in
lower case) in the sequence as they appear in the input value. Please note:
The consonants are characters that are not vowels.
Sample input:
DATABase
Output:
dtbs
Solution
import java.util.Scanner;
public class MyClass {
public static void main(String[] args) {
Scanner sc =new Scanner(System.in);
String str = sc.nextLine();
String str2 =str.toLowerCase();
StringBuilder strB =new StringBuilder();
int len =str2.length();
for(int i=0; i<len; i++)
{
char ch = str2.charAt(i);
if(ch =='a' || ch =='e' || ch =='o' || ch =='i' || ch =='u')
continue;
else
strB.append(ch);
}
System.out.println(strB);
}}
Question 4 :
Write main method in Solution class.
In the main method, write code to read a String value using Scanner and
print the smallest vowel. Assume all input values are in lower case.
E.g If the input value is "matrix" then output will be a (since there are two
vowels a and i where a is smaller as per ASCII sequence).
Solution
import java.util.*;
public class MyClass
{
public static void main(String[]args) {
Scanner sc=new Scanner(System.in);
String str =sc.nextLine();
int len =str.length();
int[] arr =new int[len-1];
int count= 0;
for(int i=0; i<len; i++)
{
char ch =str.charAt(i);
if(ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u')
{
arr[count] = (int)ch;
count++;
}
}
int newarr[] =new int[count];
newarr = Arrays.copyOfRange(arr,0,count);
Arrays.sort(arr);
System.out.println((char)newarr[0]);
}
}
Question 5 :
In the main method, write code to read a String value using Scanner and
print characters at odd position as single String (Not index but position.
Refer below example).
E.g If the input value is "Matrix". Then we have total 6 characters with 'M'at
first position and 'x' at 6th position.
Hence the characters at odd position will be 1st, 3rd and 5th character
which is "Mti".
If the input value has space or any other special character then the same
should be conuted as usual.
E.g if the input value is "Hi There" then the output will be "H hr"
Note that the output is printed in same line with all characters together.
("Mti" and "H hr").
Solution
import java.util.Scanner;
public class MyClass{
public static void main(String arg[]){
Scanner sc = new Scanner(System.in);
String str =sc.nextLine();
StringBuilder stringBuilder =new StringBuilder();
for(int i=0;i<str.length();i++) {
char ch =str.charAt(i);
if(i%2==0)
stringBuilder.append(ch);
}
System.out.println(stringBuilder);
}}
Question 6 :
Create a Class Medicine with the below attributes:
medicineName - String
batchNo - String
disease - String
price - int
This method will take a String parameter named disease along with the
other parameter as array of Medicine objects.
The method will return an array of Integer containing the price of the
medicines in ascending order, if the given input(disease) matches the
disease attribute of the medicine objects in the Array.
This method should be called from the main method and display the prices.
Main method mentioned above already has Scanner code to read values,
create objects and test above methods. Hence do not modify it.
Input:
Hyadry
FHW0602
EyeDryness
140
Dolo
FHW0603
Fever
10
OpsionHA
P5011
EyeDryness
435
Mucinex
C0011
Cold
15
EyeDryness
Output:
140
435
Solution
import java.util.Arrays;
import java.util.Scanner;
public class MyClass{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Medicine[] medicines = new Medicine[4];
for(int i =0;i<medicines.length;i++) {
medicines[i] = new
Medicine(sc.nextLine(),sc.nextLine(),sc.nextLine(),sc.nextInt());
sc.nextLine();
}
String disease = sc.nextLine();
sc.close();
Integer[] result = getPriceByDisease(medicines,disease);
for(int i=0;i<result.length;i++) {
System.out.println(result[i]);
}
}
public static Integer[] getPriceByDisease(Medicine[] medicines,String
disease) {
Integer[] result = new Integer[0];
for(int i=0;i<medicines.length;i++) {
if(medicines[i].getDisease().equalsIgnoreCase(disease)) {
result = Arrays.copyOf(result, result.length+1);
result[result.length-1] = medicines[i].getPrice();
}
Arrays.sort(result);
}
return result;
}
}
class Medicine{
String MedicineName;
String batch;
String disease;
int price;
Question 7:
playerId - int
playerName - String
iccRank - int
noOfMatchesPlayed - int
totalRunsScored - int
This method will take an int parameter named target along with the other parameter as
array of
Player objects.
The method will calculate the average runrate of the player based on totalRunsScored
and
This method should be called from the main method and display the Grade of the
players.
read values for four Player objects referring the attributes in the above sequence.
then, read the value for noOfMatchesPlayed parameter.
Next call the method findAverageScoreOfPlayers, write the logic to display the Grade(in
the main method) and display the result.
Input:
100
Sachin
1
102
13000
101
Shewag
2
110
10000
102
Dhoni
3
80
7500
104
Kholi
4
70
7000
100
Output:
Grade A Player
Grade A Player
Solution
import java.util.Arrays;
import java.util.Scanner;
public class MyClass{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
Player[] players = new Player[4];
for(int i = 0;i < players.length;i++){
players[i] = new
Player(sc.nextInt(),sc.next(),sc.nextInt(),sc.nextInt(),sc.nextInt());
}
int target = sc.nextInt();
sc.close();
double[] averageRun = findAverageOfRuns(players,target);
for(int i = 0;i < averageRun.length;i++){
if(averageRun[i] >= 80 && averageRun[i] <= 100)
System.out.println("Grade A Player");
else if(averageRun[i] >= 50 && averageRun[i] <= 79)
System.out.println("Grade B Player");
else
System.out.println("Grade A Player");
}
}
public static double[] findAverageOfRuns(Player[] players, int target){
double averageRun[]= new double[0];
for(int i = 0;i < players.length;i++){
if(players[i].getMatchesPlayed() >= target){
averageRun = Arrays.copyOf(averageRun,
averageRun.length+1);
averageRun[averageRun.length-1] = (double)
(players[i].getRunsScored()/players[i].getMatchesPlayed());
}
}
return averageRun;
}
}
class Player
{
int id;
String name;
int iccRank;
int matchesPlayed;
int runsScored;
Question 9 :
Create a class Sim with below attributes:
simId - int
customerName - String
balance - double
ratePerSecond - double
circle - String
This method will take first parameter as array of Sim class objects, second
parameter as circle to be transferred (which is String parameter circle1)
and third parameter as new circle (which is String parameter circle2).
Method will transfer the customer to new circle (circle2), where the circle
attribute would match second parameter (circle1). Method will return array
of Sim objects for which circle is transferred. Return array should be sorted
in descending order of ratePerSecond (assuming ratePerSecond is not
same for any of the Sim objects).
This method should be called from main method and display the
simId,customerName,circle and ratePerSecond of returned objects (as per
sample output).
Main method mentioned above already has Scanner code to read values,
create objects and test above methods. Hence do not modify it.
Input:
1
raj
100
1.5
KOL
2
chetan
200
1.6
AHD
3
asha
150
1.7
MUM
4
kiran
50
2.2
AHD
Solution
import java.util.*;
for(int i = 0;i<5;i++)
{
int simId = sc.nextInt();sc.nextLine();
String customerName = sc.nextLine();
double balance = sc.nextDouble();
double ratePerSecond = sc.nextDouble();sc.nextLine();
String circle = sc.nextLine();
for(Sim s: result)
System.out.println(s.getSimId()+" "+s.getCustomerName()+" "
+s.getCircle() + " " + s.getRatePerSecond());
}
public static Sim[] sortByRatePerSecond(Sim[] sims)
{
Sim temp= new Sim();
for(int i=0;i<sims.length-1;i++){
if (sims[i].getRatePerSecond() <sims[i+1].getRatePerSecond())
{
temp=sims[i];
sims[i]=sims[i+1];
sims[i+1]=temp;
}
}
return sims;
}
}
class Sim
{
//code to build Sim class
int simId;
String customerName;
double balance;
double ratePerSecond;
String circle;
Sim(){}
Sim(int simId, String customerName, double balance, double
ratePerSecond, String circle)
{
this.simId=simId;
this.customerName=customerName;
this. balance= balance;
this.ratePerSecond=ratePerSecond;
this.circle=circle;
}
public int getSimId(){
return this.simId;
}
public String getCustomerName(){
return this.customerName;
}
public String getCircle(){
return this.circle;
}
public double getRatePerSecond(){
return this.ratePerSecond;
}
public void setCircle(String circle){
this.circle=circle;
}
}
Question 10 :
Create a Class Inventory with below attributes:
inventoryId - String
maximumQuantity - int
currentQuantity - int
threshold - int
This method will take an int parameter named limit along with the other
parameter as array of Inventory objects.
The method will return array of Inventory where the threshold attribute is
less than or equal to the int parameter passed.
This method should be called from main method and display the id of
returned objects along with Filling status.
Input:
1
100
50
40
2
100
50
50
3
100
40
45
Solution
import java.util.Arrays;
import java.util.Scanner;
public class MyClass {
public static void main(String[] args) {
Inventory[] inventories = new Inventory[4];
Scanner sc = new Scanner(System.in);
for (int i = 0; i < inventories.length; i++) {
String inventoryId = sc.nextLine();
int maximumQuantity = sc.nextInt();
sc.nextLine();
int currentQuantity = sc.nextInt();
sc.nextLine();
int threshold = sc.nextInt();
sc.nextLine();
inventories[i] = new Inventory(inventoryId, maximumQuantity,
currentQuantity, threshold);
}
int limit = sc.nextInt();
sc.close();
Inventory[] result = replenish(inventories, limit);
for (int i = 0; i < result.length; i++){
if (result[i].getThreshold() >= 75)
System.out.println(result[i].getInventoryId() + " Critical Filling");
else if (result[i].getThreshold() >= 50 && result[i].getThreshold() <=
74)
System.out.println(result[i].getInventoryId() + " Moderate Filling");
else
System.out.println(result[i].getInventoryId() + " Non-Critical
Filling");
}
}
public static Inventory[] replenish(Inventory[] inventories, int limit){
Inventory[] refined = new Inventory[0];
for (int i = 0; i < inventories.length; i++){
if (inventories[i].getThreshold() <= limit){
refined=Arrays.copyOf(refined, refined.length+1);
refined[refined.length-1] = inventories[i];
}
}
return refined;
}
}
class Inventory{
String inventoryId;
int maximumQuantity;
int currentQuantity;
int threshold;
public Inventory(String inventoryId, int maximumQuantity, int
currentQuantity, int threshold){
this.inventoryId = inventoryId;
this.maximumQuantity = maximumQuantity;
this.currentQuantity = currentQuantity;
this.threshold = threshold;
}
public String getInventoryId(){
return inventoryId;
}
public int getThreshold(){
return threshold;
}
}