0% found this document useful (0 votes)
9 views37 pages

Java Proctored Assessment Programming Solution 17th December

The document contains a series of Java programming questions and solutions, including tasks such as checking for perfect squares, filtering associates by technology and experience, counting characters and spaces in a string, and managing movie and inventory data. Each question is accompanied by a detailed solution in Java code, demonstrating various programming concepts such as classes, methods, and data manipulation. The document serves as a comprehensive guide for Java programming assessments with practical examples and solutions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views37 pages

Java Proctored Assessment Programming Solution 17th December

The document contains a series of Java programming questions and solutions, including tasks such as checking for perfect squares, filtering associates by technology and experience, counting characters and spaces in a string, and managing movie and inventory data. Each question is accompanied by a detailed solution in Java code, demonstrating various programming concepts such as classes, methods, and data manipulation. The document serves as a comprehensive guide for Java programming assessments with practical examples and solutions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 37

Java Proctored Assessment Programming

solution 17th December

Question 1

In main method read an integer (containing only numeric


digits without decimal and special characters) and check
whether the given number is perfect square or not. If it is print
TRUE (as string) else print FALSE(as string )

Solution:

import java.util.Scanner;
public class Solution4 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
double k = sc.nextInt();
k = Math.sqrt(k);
if((int)k == k)
System.out.println("TRUE");
else
System.out.println("FALSE");
}
}

Question 2

Create solution class Implement Static method "associates


forGivenTechnology" in solution class.
This method will take a string parameter as array of associate
objects.

The method will return array of Associates where string


parameter appears in technology
attribute(with case insensitive search) and experienceInyears
should be multiple of 5.

Create class Associate with below attributes:


associate id- string
associate name- string
Technology- string
experienceInYears-int

Write getters,setters and parameterized constructor.


Create class solution with the main method.
Implement Static method - associateForgiventechnology in
solution class.
This method will take a string parameter named technology
along with the other parameter as array of objects.
The method will return array of associate where the string
parameter appears in the technology attribute (with case
insensitive search) and the experienceInyears should be
multiples of 5.
This method should be called from main method and display
the id of returned objects in ascending order.

Before calling this method (associateForgiventechnology) in


the main method use scanner object to read values for five
associate objects referring the attributes in above sequence
then read value for search parameter.
Next call the method associateForgiventechnology,write the
logic to print the ID's (in main method) And display result.

Input

Alex
101
Java
15
Albert
102
Unix
20
Alferd
103
Testing
13
Alfa
104
Java
15
Almas
105
Java
29
Java // Given technology

Output

101
104

Solution:

import java.util.ArrayList;
import java.util.Scanner;
public class Solution {
public static void main(String[] args) {
// TODO Auto-generated method stub
int id ,exp;
String name,tech;
As[] ass = new As[5];
Scanner sc=new Scanner(System.in);
for(int i = 0;i<5;i++) {
System.out.println("id = ");
id = sc.nextInt();
System.out.println("name = ");
name = sc.next();
System.out.println("tech = ");
tech = sc.next();
System.out.println("exp = ");
exp = sc.nextInt();
ass[i] = new As(id, name, tech, exp);
}
String techa;
System.out.println("Enter tech = ");
techa = sc.next();
ArrayList<As> Os = getTech(ass,techa);
for(int i=0;i<Os.size();i++)
System.out.println(Os.get(i).getId()+" "+Os.get(i).getName()+"
"+Os.get(i).getTech()+" "+Os.get(i).getExp());
}
public static ArrayList<As> getTech(As[] d, String th) {
ArrayList<As> bs = new ArrayList<As>();
for(int i=0;i<d.length;i++) {
if(d[i].getTech().equals(th) && d[i].getExp()%5==0)
bs.add(d[i]);
}
return bs;
}
}
class As{
private int id;
private String name;
private String tech;
private int exp;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getTech() {
return tech;
}
public void setTech(String tech) {
this.tech = tech;
}
public int getExp() {
return exp;
}
public void setExp(int exp) {
this.exp = exp;
}
public As(int id, String name, String tech, int exp) {
this.id = id;
this.name = name;
this.tech = tech;
this.exp = exp;
}
}
Java Proctored Assessment Programming
solution 28th December

Question 1.

Write a Porgram to Print no.of Spaces and Characters in a


given input

input: Hello This is ABCD from XYZ city


output : No of Spaces : 6 and characters : 26

Solution:

import java.util.Scanner;
public class Solution {
public static void main(String [] args) {
Scanner in = new Scanner(System.in);
int space_count=0,char_count=0;
String str=in.nextLine();
for(int i=0;i<str.length();i++) {
if(str.charAt(i) == ' ') {
space_count++;
}
else {
char_count++;
}
}
System.out.println("No of Spaces : "+space_count+" and
characters :
"+char_count);
in.close();
}
}

Question 2.

Create a class Movie with Attributes :


movieName String
Company String
Genre String
budget int

Create setters and getters and parametrized Constructor as


required
Create another class Solution with a main method and scan
attributes as above sequence
ex :
aaa
Marvel
Action
250000000
and scan a search attribute

Create a Static method getMovieByGenre which will take


object array and search string as parameters and returns
movies which are matching with genre(Search) having budget
less than 80000000 if movie budget is above 8cr print "High
Budget Movie" ( Case Senstive Search) else " Low Budget
Movie"

input :
aaa
Arka
Action
250000000
bbbb
GeethaArts
Comedy
25000000
ccc
Marvel
Art
2000000
ddd
Mythri
Action
300000000
Action

Output:
High Budget Movie
High Budget Movie

Solution:

import java.util.Scanner;
class Movie {
private String movieName;
private String company;
private String genre;
private int budget;
public String getMovieName() {
return movieName;
}
public void setMovieName(String movieName) {
this.movieName = movieName;
}
public String getCompany() {
return company;
}
public void setCompany(String company) {
this.company = company;
}
public String getGenre() {
return genre;
}
public void setGenre(String genre) {
this.genre = genre;
}
public int getBudget() {
return budget;
}
public void setBudget(int budget) {
this.budget = budget;
}
Movie (String movieName, String company, String genre, int
budget) {
this.movieName = movieName;
this.company = company;
this.genre = genre;
this.budget = budget;
}
}
public class Solution {
public static void getMovieByGenre(Movie[] obj,String key) {
for(int i =0;i<4;i++) {
if(obj[i].getGenre().equals(key) && obj[i].getBudget() >
80000000){
System.out.println("High Budget Movie");
}
else if(obj[i].getGenre().equals(key) && obj[i].getBudget() <
80000000) {
System.out.println("Low Budget Movie");
}
}
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
Movie[] arr = new Movie[4];
for(int i =0;i<4;i++) {
String name=in.nextLine();
String company=in.nextLine();
String genre=in.nextLine();
int budget=in.nextInt();
in.nextLine();
arr[i]=new Movie(name,company,genre,budget);
}
String search =in.nextLine();
getMovieByGenre(arr,search);
in.close();
}
}
Java Proctored Assessment Programming
solution 15th January

Question 1.

To print characters at odd position of a string

input: Matrix
output : Mti

Solution:

import java.util.Scanner;
public class Solution {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
for (int i = 0; i < str.length(); i+=2) {
System.out.print(str.charAt(i));
}
sc.close();
}
}

Output:

Example 1:
Matrix
Mti
Example 2:
Hi how are you?
H o r o?
Question 2.

Create a Sim class with 5 attributes and also write getter


,setter
methods.
Implement a method which takes array of sim objects as first
parameter , string circle
as a second parameter and a double ratePerSecond as third
parameter.
This method should return an array of the
objects which containing second parameter and less than the
third
parameter and also the balance value of those objects must
be in descending order.
In the main method we have to display the id attribute values
of the array

input:

1
jio
430
1.32
mumbai
2
idea
320
2.26
mumbai
3
airtel
500
2.54
mumbai
4
vodafone
640
3.21
mumbai
mumbai
3.4

output:

4
3
1
2

Solution:

import java.util.Scanner;
public class SimCard {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Sim[] sim = new Sim[4];
for (int i = 0; i < sim.length; i++) {
int simId = sc.nextInt();sc.nextLine();
String name = sc.nextLine();
double balance = sc.nextDouble();sc.nextLine();
double ratePersecond = sc.nextDouble();sc.nextLine();
String circle = sc.nextLine();
sim[i] = new Sim(simId, name, balance, ratePersecond,
circle);
}
String circle1 = sc.nextLine();
double ratePerSecond1 = sc.nextDouble();
Sim[] result = find(sim, circle1, ratePerSecond1);
for (int i = 0; i < result.length; i++) {
System.out.println(result[i].getSimId());
}
sc.close();
}
public static Sim[] find(Sim[] sim, String circle1, double
ratePerSecond1) {
Sim[] temp;
int j = 0;
for (int i = 0; i < sim.length; i++) {
if (sim[i].getCircle().equals(circle1) &&
sim[i].getRatePersecond() < ratePerSecond1) {
j++;
}
}
temp = new Sim[j];
j=0;
for (int i = 0; i < sim.length; i++) {
if (sim[i].getCircle().equals(circle1) &&
sim[i].getRatePersecond() < ratePerSecond1) {
temp[j++] = sim[i];
}
}
for (int i = 0; i < j - 1; i++) {
for (int k = 0; k < j - 1; k++) {
if (temp[k].getBalance() < temp[k +
1].getBalance()) {
Sim a = temp[k];
temp[k] = temp[k + 1];
temp[k + 1] = a;
}
}
}
return temp;
}
}
class Sim {
private int simId;
private String name;
private double balance;
private double ratePersecond;
private String circle;
public Sim(int simId, String name, double balance, double
ratePersecond, String circle) {
this.simId = simId;
this.name = name;
this.balance = balance;
this.ratePersecond = ratePersecond;
this.circle = circle;
}
public int getSimId() {
return simId;
}
public void setSimId(int simId) {
this.simId = simId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
public double getRatePersecond() {
return ratePersecond;
}
public void setRatePersecond(double ratePersecond) {
this.ratePersecond = ratePersecond;
}
public String getCircle() {
return circle;
}
public void setCircle(String circle) {
this.circle = circle;
}
}

________________________________________________

Java Proctored Assessment Programming


solution 25th January

Question 1.

Write a program to print last characters in a string...

Example:
Input:
Hi tcs
Output:
is
Solution:

import java.util.Scanner;
public class LastCharOfWord {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
String[] words = str.split(" ");
for (int i = 0; i < words.length; i++) {
System.out.print((words[i].charAt(words[i].length()-1)));
}
sc.close();
}
}

Example 1:
Input: welcome home buddY
Output: eeY

Example 2:
Input: Hi how are you ?
Output: iweu?

Question 2.

Take a class name inventory and its 4 object into array and
implement a method replenish where it will take array objects
and a limit as a parameter and it will display the id along with
fillings where filling if greater than 75 it will be critical filling
and if it is between 74 and 50 then moderate filling else non-
critical filling
Input:
1
100
50
50
2
200
60
40
3
150
35
45
4
80
45
40
45
Output:
2 Non-Critical Filling
3 Non-Critical Filling
4 Non-Critical Filling

Solution:

import java.util.Scanner;
public class InventorySolution {
public static void main(String[] args) {
// TODO Auto-generated method stub
Inventory[] in = new Inventory[4];
Scanner sc = new Scanner(System.in);
for (int i = 0; i < in.length; i++) {
String a = sc.nextLine();
int b = sc.nextInt();
sc.nextLine();
int c = sc.nextInt();
sc.nextLine();
int d = sc.nextInt();
sc.nextLine();
in[i] = new Inventory(a, b, c, d);
}
int limit = sc.nextInt();
Inventory[] result = replenish(limit, in);
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");
}
}
sc.close();
}
public static Inventory[] replenish(int limit, Inventory[] in) {
// TODO Auto-generated method stub
Inventory[] temp;
int j = 0;
for (int i = 0; i < in.length; i++) {
if (in[i].getThreshold() <= limit) {
j++;
}
}
temp = new Inventory[j];
j = 0;
for (int i = 0; i < in.length; i++) {
if (in[i].getThreshold() <= limit) {
temp[j++] = in[i];
}
}
return temp;
}
}
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 void setInventoryId(String inventoryId) {
this.inventoryId = inventoryId;
}
public int getMaximumQuantity() {
return maximumQuantity;
}
public void setMaximumQuantity(int maximumQuantity) {
this.maximumQuantity = maximumQuantity;
}
public int getCurrentQuantity() {
return currentQuantity;
}
public void setCurrentQuantity(int currentQuantity) {
this.currentQuantity = currentQuantity;
}
public int getThreshold() {
return threshold;
}
public void setThreshold(int threshold) {
this.threshold = threshold;
}
}

Java Proctored Assessment Programming


solution 10th February

Question 1.

Write a Java program to read a string str and to count both


vowels and consonants in that string.

Input:
Hello World!

Output:
Vowels count - 3
Consonants count - 7
Solution:

import java.util.*;
import java.lang.*;
public class Main
{
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
String str = s.nextLine();
str = str.toLowerCase();
int vowelCount = 0, consonantCount = 0;
for(int i=0;i<str.length();i++){
if(str.charAt(i)>='a' && str.charAt(i)<='z'){
if(str.charAt(i)=='a' || str.charAt(i)=='e' ||
str.charAt(i)=='i' || str.charAt(i)=='o' || str.charAt(i)=='u')
vowelCount += 1;
else
consonantCount += 1;
}
}
System.out.println("Vowels count - "+vowelCount);
System.out.println("Consonant Count
- "+consonantCount);
}
}

Question 2.

Write a class named as Main. Read the following parameters


- id (int)
- name (String)
- exp (int)
- matchesPlayed (int)
- runsScored (int)
Calculate the average of the players in that innings depends
upon the target(int)
- if avgRuns >=80 and <=100, print Grade A
- if avgRuns >=50 and <=79, print Grade B
- else, print Grade C

Input:

100
Sachin
5
150
13000
101
Sehwag
4
120
10000
103
Dhoni
7
110
7000
104
Kohli
15
57
4400

100

Output:

Grade A
Grade A
Grade B
Solution:

import java.util.Scanner;
public class feb10tcsxplore {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
Players []players=new Players[4];
for(int i=0;i<players.length;i++)
{
int playerid=sc.nextInt();sc.nextLine();
String playername=sc.nextLine();
int iccrank =sc.nextInt();sc.nextLine();
int matchplayed=sc.nextInt();sc.nextLine();
int runscored=sc.nextInt();sc.nextLine();
players[i]=new
Players(playerid,playername,iccrank,matchplayed,runscored)
;
}
int target=sc.nextInt();
double avgrunrate[]=findavgofruns(players,target);
for(int i=0;i<avgrunrate.length;i++)
{
if(avgrunrate[i]>=80&& avgrunrate[i]<=100)
System.out.println("Grade A");
else if(avgrunrate[i]>=50&& avgrunrate[i]<=79)
System.out.println("Grade B");
else
System.out.println("Grade C");
}
}
public static double[] findavgofruns(Players[] ob, int target) {
// TODO Auto-generated method stub
double temp[];
int j=0;
for(int i=0;i<ob.length;i++)
{
if(ob[i].getmatchplayed()>=target)
j++;
}
temp=new double[j];
j=0;
for(int i=0;i<ob.length;i++)
{
if(ob[i].getmatchplayed()>=target)
{
temp[i]=(double)
(ob[i].getrunscored()/ob[i].getmatchplayed());
}
}
return temp;
}
}
class Players
{
int id;
String name;
int iccrank;
int matchplayed;
int runscored;
public Players(int id,String name,int iccrank,int
matchplayed,int runscored)
{
this.id=id;
this.name=name;
this.iccrank=iccrank;
this.matchplayed=matchplayed;
this.runscored=runscored;
}
public int getid()
{
return id;
}
public String getname()
{
return name;
}
public int geticcrank()
{
return iccrank;
}
public int getmatchplayed()
{
return matchplayed;
}
public int getrunscored()
{
return runscored;
}
}
Java Proctored Assessment Programming
solution 13th February

Question 1.

Write a Program to print smallest vowel in the given


line…(Ascii Values)

input:
matrix

output:
a

Solution:

import java.lang.*;
import java.util.*;
import java.io.*;
import java.math.*;
public class Main
{
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String s=sc.nextLine();
s=s.toLowerCase();
int n=s.length();
int c=0;
char[] ch=s.toCharArray();
for(int i=0;i<n;i++)
{
if(s.charAt(i)=='a' || s.charAt(i)=='e' || s.charAt(i)=='i' ||
s.charAt(i)=='o' || s.charAt(i)=='u')
{
c++;
}
}
char[] c1=new char[c];
int k=0;
for(int i=0;i<n;i++)
{
if(s.charAt(i)=='a' || s.charAt(i)=='e' || s.charAt(i)=='i' ||
s.charAt(i)=='o' || s.charAt(i)=='u')
{
c1[k++]=ch[i];
}
}
Arrays.sort(c1);
System.out.println(c1[0]);

}
}

Question 2.

Create a Sim Class with following Attributes


int simId;
String customerName;
double balance;
double ratePerSecond;
String circle;

create a public Class Solution in which take input of 5 object


and then take 2 input (circle1, circle2) for circle of matches as
circle1 and choose only those objects which are match with
circle1.
Create a method named as transferCircle() and passed sim
object and circle1, circle2 as parameter and arrange sim
object in descending order as per ratePerSecond

Print the output as simId, customerName, circle,


ratePerSecond.

Input:

1
raju
130
1.32
mum
2
sachin
120
2.26
ahd
3
ram
200
2.54
kol
4
shubham
640
3.21
ahd
5
kalpesh
150
1.8
ahd
ahd
kol
Output:

4 shubham kol 3.21


2 sachin kol 2.26
5 kalpesh kol 1.8

Solution:

import java.util.Scanner;
public class SimSolution {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Sim1[] sim = new Sim1[5];
for (int i = 0; i < sim.length; i++) {
int simId = sc.nextInt();
sc.nextLine();
String name = sc.nextLine();
double balance = sc.nextDouble();
double ratePersecond = sc.nextDouble();
sc.nextLine();
String circle = sc.nextLine();
sim[i] = new Sim1(simId, name, balance,
ratePersecond, circle);
}
String circle1 = sc.nextLine();
String circle2 = sc.nextLine();
Sim1[] result = transferCircle(sim, circle1, circle2);
for (int i = 0; i < result.length; i++) {
System.out.println(result[i].getSimId() + " " +
result[i].getName() + " " + result[i].getCircle() + " "
+ result[i].getRatePersecond());
}
sc.close();
}
public static Sim1[] transferCircle(Sim1[] sim, String
circle1, String circle2) {
Sim1[] temp;
int j = 0;
for (int i = 0; i < sim.length; i++) {
if (sim[i].getCircle().equals(circle1)) {
j++;
}
}
temp = new Sim1[j];
j = 0;
for (int i = 0; i < sim.length; i++) {
if (sim[i].getCircle().equals(circle1)) {
temp[j] = sim[i];
temp[j++].setCircle(circle2);
}
}
for (int i = 0; i < j - 1; i++) {
for (int k = 0; k < j - 1; k++) {
if (temp[k].getRatePersecond() < temp[k +
1].getRatePersecond()) {
Sim1 a = temp[k];
temp[k] = temp[k + 1];
temp[k + 1] = a;
}
}
}
return temp;
}
}
class Sim1 {
private int simId;
private String name;
private double balance;
private double ratePersecond;
private String circle;
public Sim1(int simId, String name, double balance, double
ratePersecond, String circle) {
this.simId = simId;
this.name = name;
this.balance = balance;
this.ratePersecond = ratePersecond;
this.circle = circle;
}
public int getSimId() {
return simId;
}
public void setSimId(int simId) {
this.simId = simId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
public double getRatePersecond() {
return ratePersecond;
}
public void setRatePersecond(double ratePersecond) {
this.ratePersecond = ratePersecond;
}
public String getCircle() {
return circle;
}
public void setCircle(String circle) {
this.circle = circle;
}
}

________________________________________________

Java Proctored Assessment Programming


solution 2nd march

Question 1.

Reverse the given number

INPUT

12345

OUTPUT

Reverse of the number is 54321

Solution:

import java.util.Scanner;
public class Solution1000 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
StringBuffer sb = new
StringBuffer(sc.nextLine());
System.out.println("Reverse of the number is
"+sb.reverse());
}
}

Question 2.

Sort the medicines according to Medicine prices with


Respective to the disease

INPUT :

dolo650
batch1
fever
100
dolo990
batch2
headache
101
paracetemol
batch3
bodypains
102
almox500
batch4
fever
103
fever

OUTPUT :

100
103
Solution

import java.util.Arrays;
import java.util.Scanner;
public class Solution{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Medicine[] m = new Medicine[4];
for(int i =0;i<m.length;i++) {
String a = sc.nextLine();
String b = sc.nextLine();
String c = sc.nextLine();
int d = sc.nextInt();sc.nextLine();
m[i] = new Medicine(a,b,c,d);
}
String e = sc.nextLine();
Integer[] f = sortAccordingtoPrices(m,e);
for(Integer g : f) {
System.out.println(g);
}
}
public static Integer[] sortAccordingtoPrices(Medicine[] m,
String e) {
int count =0 ;
for(int i=0;i<m.length;i++) {
if(m[i].getDisease().equals(e)) {
count++;
}
}
Integer[] j = new Integer[count];
int k = 0;
for(int i=0;i<m.length;i++) {
if(m[i].getDisease().equalsIgnoreCase(e)) {
j[k++] = m[i].getPrice();
}
}
Arrays.sort(j);
return j;
}
}
class Medicine{
private String MedicineName;
private String batch;
private String disease;
private int price;
public String getMedicineName() {
return MedicineName;
}
public void setMedicineName(String medicineName) {
MedicineName = medicineName;
}
public String getBatch() {
return batch;
}
public void setBatch(String batch) {
this.batch = batch;
}
public String getDisease() {
return disease;
}
public void setDisease(String disease) {
this.disease = disease;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public Medicine(String medicineName, String batch, String
disease, int price) {
this.MedicineName = medicineName;
this.batch = batch;
this.disease = disease;
this.price = price;
}
}

You might also like