Java HandsOn
Java HandsOn
Q1
Q1 .. Find
Find interest
interest value
value
Create
Create aa class
class Account
Account with
with below
below attributes:
attributes:
int
int id
id
double
double balance
balance
double
double interestRate
interestRate
Class
Class should
should have
have getters,
getters, setters
setters and
and constructor
constructor with
with
parameters
parameters in
in above
above sequence
sequence of
of attributes.
attributes.
Create
Create aa class
class Solution
Solution with
with main
main method.
method. Read
Read one
one integer
integer
and
and two
two double
double values
values using
using Scanner
Scanner object
object and
and create
create object
object
of
of Account
Account class.
class. These
These values
values should
should be
be mapped
mapped to
to id,
id,
balance
balance and
and interestRate
interestRate attributes.
attributes.
Convenient, Free 30-Day Return
BlueStone
Read
Read one
one more
more integer
integer value
value and
and store
store it
it in
in variable
variable
noOfYears.
noOfYears.
Write
Write aa method
method calculateInterest
calculateInterest which
which will
will take
take account
account
object
object and
and no
no of
of years
years as
as input
input parameters
parameters and
and return
return the
the
interest
interest amount
amount
Consider
Consider below
below logic
logic to
to calculate
calculate the
the interest
interest value:
value:
For
For specified
specified no
no of
of years,
years, first
first find
find out
out the
the percentage
percentage value
value
those
those no
no of
of years
years based
based on
on specified
specified interestRate.
interestRate. E.g.
E.g. if
if no
no of
of
years
years is
is 55 and
and specified
specified interestRate
interestRate is
is 4%,
4%, then
then 4%
4% of
of 55 is
is
0.2.
0.2.
This
This percentage
percentage should
should be
be added
added to
to original
original interstRate
interstRate for
for
calculating
calculating the
the final
final interest
interest value.
value. Hence
Hence for
for above
above example
example
it
it will
will be
be 4.2
4.2 (instead
(instead of
of 4).
4).
Display
Display the
the interest
interest amount
amount rounded
rounded upto
upto three
three decimal
decimal
places.
places. Even
Even if
if the
the result
result does
does not
not have
have decimal,
decimal, it
it should
should be
be
displayed
displayed with
with su!x
su!x ".000".
".000".
Input:
1
1000
10
Output:
105.000
Solutions:
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
int a;
double b,c;
b = sc.nextDouble();
c = sc.nextDouble();
Account account = new Account(a, b, c);
int noOfYear;
noOfYear = sc.nextInt();
System.out.format("%.3f",answer);
}
}
class Account {
this.balance = balance;
this.interestRate = interestRate;
}
public double getBalance() {
return this.balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
return this.interestRate;
}
Classes
Classes and
and Objects
Objects -
- Hands
Hands on
on 11
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
int x1,y1,x2,y2;
Scanner scn=new Scanner(System.in);
x1=scn.nextInt();
y1=scn.nextInt();
x2=scn.nextInt();
y2=scn.nextInt();
Point p1=new Point(x1, y1);
System.out.format("%.3f",distance);
}
public static double findDistance(Point p1, Point p2)
{
double distance=Math.sqrt((p2.x-p1.x)*(p2.x-p1.x)+(p2.y-p1.y)*(p2.y-
p1.y));
return distance;
}
}
class Point
int x,y;
Point(int x,int y)
{
this.x=x;
this.y=y;
}
Q2.
Q2. Compare
Compare 2D
2D points
points for
for distance
distance from
from origin
origin
Write
Write aa program
program to
to check
check distance
distance of
of 2D
2D points
points from
from origin
origin
and
and print
print the
the point
point with
with highest
highest distance.
distance.
Create
Create class
class Point
Point with
with attributes
attributes as
as below:
below:
double
double xx
double
double yy
In
In Solution
Solution class,
class, define
define main
main method
method to
to read
read values
values for
for three
three
point
point objects.
objects.
Next,
Next, create
create below
below method
method in
in Solution
Solution class
class which
which will
will take
take
three
three point
point objects
objects as
as input
input parameters
parameters and
and return
return the
the point
point
with
with highest
highest distance
distance from
from origin
origin
public
public static
static Point
Point pointWithHighestOriginDistance(Point
pointWithHighestOriginDistance(Point p1,
p1,
Point
Point p2,
p2, Point
Point p3)
p3)
Consider sample input as below to read values for six point objects
2
2
3
-4
-4
Output:
-4.0
-4.0
Solution:
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
*/
double x1,y1,x2,y2,x3,y3;
x1=scn.nextDouble();
y1=scn.nextDouble();
x2=scn.nextDouble();
y2=scn.nextDouble();
x3=scn.nextDouble();
y3=scn.nextDouble();
System.out.format("%.1f \n",highest.x);
System.out.format("%.1f",highest.y);
}
public static Point pointWithHighestOriginDistance(Point p1, Point p2,
Point p3)
{
double d1=Math.sqrt(p1.x*p1.x+p1.y*p1.y);
double d2=Math.sqrt(p2.x*p2.x+p2.y*p2.y);
double d3=Math.sqrt(p3.x*p3.x+p3.y*p3.y);
return d1>d2?(d1>d3?p1:p3):(d2>d3?p2:p3);
}
}
class Point
{
double x,y;
5
Point(double x, double y)
{
this.x=x;
this.y=y;
}
}
Q3.
Q3. Write
Write main
main method
method in
in Solution
Solution class.
class.
The
The method
method will
will read
read aa String
String value
value and
and print
print the
the minimum
minimum
valued
valued character
character (as
(as per
per alphabet
alphabet and
and ASCII
ASCII sequence).
sequence).
Consider
Consider below
below sample
sample input
input and
and output:
output:
Input:
Input:
HellO
HellO
Output:
Output:
H
H
Important:
Important: Answer
Answer is
is not
not 'e'
'e' since
since 'H'
'H' has
has lower
lower ASCII
ASCII value
value
then
then 'e'
'e'
Solutions:
import
Python java.io.*;
Tutorials
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
/* Enter your code here. Read input from STDIN. Print output to STDOUT
*/
String str;
for(int i=0;i<str.length();i++)
{
values[i]=(int)(str.charAt(i));
}
int min=values[0];
for(int i=0;i<values.length;i++)
{
if(values[i]<=min)
min=values[i];
}
char c=(char)min;
System.out.print(c);
Q4.
Q4. Write
Write aa program
program to
to read
read 55 numbers
numbers and
and print
print factorials
factorials of
of
each.
each.
(Final
(Final answers
answers should
should be
be non
non decimal
decimal numbers).
numbers).
Example:
Input:
2
3
4
6
5
Output:
2
24
720
120
Solutions:
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
*/
for(int i=0;i<5;i++)
{
num[i]=scn.nextInt();
String res=factorial(num[i]);
System.out.println(res);
}
}
for(int i=1;i<=n;i++){
fact=fact.multiply(new BigInteger(i+""));
return fact.toString();
}
Q5.
Q5. Find
Find Docs
Docs with
with Odd
Odd Pages
Pages
Create
Create class
class Document
Document with
with below
below attributes
attributes
id
id -
- int
int
title
title -
- String
String
folderName
folderName -
- String
String
pages
pages -
- int
int
Write
Write getters,
getters, setters
setters and
and parameterized
parameterized constructor
constructor as
as
required.
required.
Create
Create class
class Solution
Solution with
with main
main method.
method.
Implement
Implement static
static method
method -
- docsWithOddPages
docsWithOddPages in
in Solution
Solution
class.
class.
This
This method
method will
will take
take array
array of
of Document
Document objects
objects and
and return
return
another
another array
array with
with Document
Document objects
objects which
which has
has odd
odd number
number
of
of pages.
pages.
This
This method
method should
should be
be called
called from
from main
main method
method and
and display
display
values
values of
of returned
returned objects
objects as
as shared
shared in
in the
the sample
sample (in
(in
ascending
ascending order
order of
of id
id attribute).
attribute).
Before
Before calling
calling this
this method,
method, use
use Scanner
Scanner object
object to
to read
read values
values
for
for four
four Document
Document objects
objects referring
referring attributes
attributes in
in the
the above
above
sequence.
sequence.
Next
Next call
call the
the method
method and
and display
display the
the result.
result.
Consider
Consider below
below sample
sample input
input and
and output:
output:
Input:
1
resume
personal
50
2
question1
exams
55
question2
exams
45
4
India
misc
40
2 question1 exams 55
3 question2 exams 45
Solutions:
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
*/
docsArray[i]=new Document();
res[i]=new Document();
}
for(int i=0;i<docsArray.length;i++)
docsArray[i].id=scn.nextInt();
docsArray[i].title=scn.next();
docsArray[i].folderName=scn.next();
docsArray[i].pages=scn.nextInt();
res= docsWithOddPages(docsArray);
for(int i=0;i<res.length;i++)
{
if(res[i].getTitle()!=null)
System.out.println(res[i].getId()+" "+res[i].getTitle()+"
"+res[i].getFolderName()+" "+res[i].getPages());
}
public static Document[] docsWithOddPages(Document[]docsArray){
for(int i=0;i<docsArray.length;i++)
{
oddDocs[i]=new Document();
}
int k=0;
for(int i=0;i<docsArray.length;i++)
{
if(docsArray[i].pages%2!=0)
{
oddDocs[k++]=docsArray[i];
}
}
int p=oddDocs.length;
for(int i=0;i<p-1;i++){
for(int j=0;j<p-i-1;j++){
if(oddDocs[j].id>oddDocs[j+1].id){
Document t=oddDocs[j];
oddDocs[j]=oddDocs[j+1];
oddDocs[j+1]=t;
}
}
return oddDocs;
}
}
class Document
int id,pages;
String title,folderName;
this.id=id;
}
}
public void setPages(int pages){
this.pages=pages;
}
public int getId(){
return this.id;
}
return this.title;
}
}
}
Q6.
Q6. Sort
Sort Books
Books by
by Price.
Price. Create
Create class
class Book
Book with
with below
below
attributes:
attributes:
id
id -
- int
int
title
title -
- String
String
author
author -
- String
String
price
price -
- double
double
Write
Write getters,
getters, setters
setters and
and parameterized
parameterized constructor.
constructor.
Create
Create class
class Solution
Solution with
with main
main method.
method.
Implement
Implement static
static method
method -
- sortBooksByPrice
sortBooksByPrice in
in Solution
Solution
class.
class.
This
This method
method will
will take
take aa parameter
parameter as
as array
array of
of Book
Book objects.
objects.
It
It will
will return
return array
array of
of books
books which
which is
is sorted
sorted in
in ascending
ascending
order
order of
of book
book price.
price. Assume
Assume that
that no
no two
two books
books will
will have
have same
same
price.
price.
This
This method
method should
should be
be called
called from
from main
main method
method and
and display
display
values
values of
of returned
returned objects
objects as
as shared
shared in
in the
the sample.
sample.
Before
Before calling
calling this
this method,
method, use
use Scanner
Scanner object
object to
to read
read values
values
for
for four
four Book
Book objects
objects referring
referring attributes
attributes in
in the
the above
above
sequence.
sequence.
Next
Next call
call the
the method
method and
and display
display the
the result.
result.
Consider
Consider below
below sample
sample input
input and
and output:
output:
Input:
1
hello
writer1
50
cup
writer2
55
3
Planet
writer3
45
India
writer4
40
Solutions:
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
/* Enter your code here. Read input from STDIN. Print output to STDOUT
*/
Scanner scn=new Scanner(System.in);
for(int i=0;i<booksArray.length;i++)
{
booksArray[i]=new Book();
sorted[i]=new Book();
}
for(int i=0;i<booksArray.length;i++)
{
booksArray[i].id=scn.nextInt();
booksArray[i].title=scn.next();
booksArray[i].author=scn.next();
booksArray[i].price=scn.nextDouble();
}
sorted=sortBooksByPrice(booksArray);
for(int i=0;i<sorted.length;i++)
{
"+sorted[i].price);
}
}
public static Book[] sortBooksByPrice(Book[]booksArray){
int n=booksArray.length;
for(int i=0;i<n-1;i++)
{
for(int j=0;j<n-i-1;j++)
{
if(booksArray[j].price>booksArray[j+1].price)
{
Book temp=booksArray[j];
booksArray[j]=booksArray[j+1];
booksArray[j+1]=temp;
}
}
return booksArray;
}
class Book
{
int id;
String title,author;
double price;
Q7.
Q7. Shirt
Shirt Discount,
Discount, Price
Price
Class
Class Solution
Solution has
has predefined
predefined main
main method
method to
to read
read values
values and
and
display
display the
the results.
results.
main
main method
method reads
reads the
the value
value for
for five
five objects
objects of
of class
class Shirt.
Shirt.
It
It also
also calls
calls methods
methods -
- getDiscountPrice
getDiscountPrice and
and
getShirtWithMoreThanSpecificPrice
getShirtWithMoreThanSpecificPrice defined
defined in
in same
same Solution
Solution
class.
class.
Code
Code inside
inside main
main method
method should
should not
not be
be altered
altered else
else your
your
solution
solution might
might be
be scored
scored as
as zero.
zero.
Guidelines
Guidelines to
to implement
implement the
the code:
code:
Create
Create class
class Shirt
Shirt with
with attributes:
attributes:
int
int tag
tag
String
String brand
brand
double
double price
price
char
char gender
gender
Create
Create constructor
constructor which
which takes
takes parameters
parameters in
in above
above sequence.
sequence.
Create
Create getters
getters and
and setters
setters for
for these
these attributes.
attributes.
Next,
Next, in
in Solution
Solution class,
class, define
define two
two static
static methods
methods as
as below:
below:
public
public static
static double
double getDiscountPrice(Shirt
getDiscountPrice(Shirt s):
s):
This
This method
method will
will return
return the
the discounted
discounted price
price based
based on
on gender
gender
for
for the
the Shirt
Shirt object
object which
which is
is passed
passed as
as input
input parameter.
parameter.
If
If gender
gender is
is 'm'
'm' then
then discount
discount is
is 10%.
10%. For
For 'f'
'f' it
it is
is 20%
20% and
and for
for
'u'
'u' it
it is
is 30%.
30%.
public
public static
static Shirt[]
Shirt[]
getShirtWithMoreThanSpecificPrice(Shirt[]
getShirtWithMoreThanSpecificPrice(Shirt[] shirts,double
shirts,double
price):
price):
This
This method
method will
will take
take array
array of
of Shirt
Shirt objects
objects and
and price
price value.
value.
The
The method
method will
will return
return array
array of
of Shirt
Shirt objects
objects with
with more
more than
than
specified
specified price
price in
in ascending
ascending order
order of
of price.
price.
Main
Main method
method already
already has
has Scanner
Scanner code
code to
to read
read values,
values, create
create
objects
objects and
and test
test above
above methods.
methods.
Main
Main method
method reads
reads values
values for
for five
five Shirt
Shirt objects,
objects, followed
followed by
by
price
price
Price
Price will
will be
be input
input for
for method
method
getShirtWithMoreThanSpecificPrice.
getShirtWithMoreThanSpecificPrice.
Input:
1
arrow
500
m
2
bare
600
f
arrow
400
m
4
bare
300
m
5
arrow
1000
u
500
Output:
450.0
480.0
360.0
270.0
700.0
2 600.0 bare
5 1000.0 arrow
Solutions:
import java.util.Scanner;
public class Solution {
for(int i = 0;i<5;i++)
{
char g = sc.nextLine().charAt(0);
shirts[i] = new Shirt(tag,brand,price,g);
for(Shirt s: shirts)
System.out.println(getDiscountPrice(s));
}
Shirt[] result = getShirtWithMoreThanSpecificPrice(shirts,price);
for(Shirt s: result)
{ if(s.tag!=0)
char ge=s.g;
int f=0;
if(ge=='m')
f=10;
if(ge=='f')
f=20;
if(ge=='u')
f=30;
double p=s.price;
return p-((p*f)/100);
}
public static Shirt[] getShirtWithMoreThanSpecificPrice(Shirt[]
shirts,double price){
r[i]=new Shirt(0,"",0.0,'f');
}
int k=0;
for(int i=0;i<r.length;i++){
if(shirts[i].price>price)
r[k++]=shirts[i];
}
int n=r.length;
for(int i=0;i<n-1;i++){
for(int j=0;j<n-i-1;j++){
if(r[j].price>r[j+1].price){
Shirt t=r[j];
r[j]=r[j+1];
r[j+1]=t;
}
}
}
return r;
}
}
class Shirt
{ int tag;
String brand;
double price;
char g;
this.brand=brand;
this.price=price;
this.g=g;
}
}
Q8
Q8 .. Create
Create class
class Book
Book with
with below
below attributes:
attributes:
id
id -
- int
int
title
title -
- String
String
author
author -
- String
String
price
price -
- double
double
Write
Write getters,
getters, setters
setters and
and parameterized
parameterized constructor
constructor as
as
required.
required.
Create
Create class
class Solution
Solution with
with main
main method.
method.
Implement
Implement static
static method
method -
- searchTitle
searchTitle in
in Solution
Solution class
class
This
This method
method will
will take
take aa parameter
parameter of
of String
String value
value along
along with
with
other
other parameter
parameter as
as array
array of
of Book
Book objects.
objects.
It
It will
will return
return array
array of
of books
books where
where String
String value
value parameter
parameter
appears
appears in
in the
the title
title attribute
attribute (with
(with case
case insensitive
insensitive search).
search).
This
This method
method should
should be
be called
called from
from main
main method
method and
and display
display
id
id of
of returned
returned objects
objects in
in ascending
ascending order.
order.
Before
Before calling
calling this
this method,
method, use
use Scanner
Scanner object
object to
to read
read values
values
for
for four
four Book
Book objects
objects referring
referring attributes
attributes in
in the
the above
above
sequence.
sequence.
Next,
Next, read
read the
the value
value search
search parameter.
parameter.
Next
Next call
call the
the method
method and
and display
display the
the result.
result.
Input:
hello world
aaa writer
50
2
World cup
bbb writer
55
3
Planet earth
ccc writer
45
4
India's history
ddd writer
40
WORLD
Output:
1
Solutions:
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
import java.util.*;
/* Enter your code here. Read input from STDIN. Print output to STDOUT
*/
for(int i=0;i<booksArray.length;i++)
{
booksArray[i]=new Book();
res[i]=new Book();
}
for(int i = 0;i<4;i++)
{
booksArray[i].id = scn.nextInt();scn.nextLine();
booksArray[i].title = scn.nextLine();
booksArray[i].author = scn.nextLine();
booksArray[i].price = scn.nextDouble();
}
String value=scn.next();
res=searchTitle(value, booksArray);
int [] matchedId=new int[4];
int j=0;
for(int i=0;i<res.length;i++)
if(res[i].id!=0)
{
matchedId[j++]=res[i].id;
}
Arrays.sort(matchedId);
for(int i=0;i<matchedId.length;i++)
{
if(matchedId[i]!=0)
System.out.println(matchedId[i]);
}
int k=0;
matching[i]=new Book();
for(int i=0;i<books.length;i++)
String val=value.toLowerCase();
String bookTitle=books[i].title.toLowerCase();
if(bookTitle.contains(val))
{
matching[k++]=books[i];
}
}
return matching;
}
class Book
{
int id;
String title;
String author;
double price;
public int getId()
{
return this.id;
}
public void setId(int id)
this.id=id;
}
{
return this.title;
}
public void setTitle(String title)
this.title=title;
}
return this.author;
}
public void setAuthor(String author)
{
this.author=author;
return this.price;
}
{
this.price=price;
}
}
TCS Xplore PA/OPA MCQs Question and Answers TCS all previous
Proctored Assessment(iPA), Online Proctored Assessment (OPA) mcq from
KYT, BizzSkills, RiO, Unix, UI, MYSQL SQL/PLSQL, JAVA Python. Answer is
in bold . if any answer is wrong please let me know through comment …
Powered by Blogger