TCS Java Hackathon - Java Hands-on
Q1 . Find interest value
Create a class Account with below attributes:
int id
double balance
double interestRate
Class should have getters, setters and constructor with parameters in above sequence of attributes.
Create a class Solution with main method. Read one integer and two double values using Scanner object and create object of Account class. These values should be mapped to id, balance and interestRate attributes.
Read one more integer value and store it in variable noOfYears.
Write a method calculateInterest which will take account object and no of years as input parameters and return the interest amount
Consider below logic to calculate the interest value:
For specified no of years, first find out the percentage value those no of years based on specified interestRate. E.g. if no of years is 5 and specified interestRate is 4%, then 4% of 5 is 0.2.
This percentage should be added to original interstRate for calculating the final interest value. Hence for above example it will be 4.2 (instead of 4).
Display the interest amount rounded upto three decimal places. Even if the result does not have decimal, it should be displayed with suffix ".000".
Consider below sample input and output:
Input:
1
1000
10
5
Output:
105.000
Output is 55 since the interest rate is 10 and no of years is 5. Hence, the final interest will be 10 + 10 percentage of 5, which is 10.5. Hence final answer is 105.000.
Solutions:
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
public static void main(String args[] ) throws Exception {
int a;
double b,c;
Scanner sc = new Scanner(System.in);
a = sc.nextInt();
b = sc.nextDouble();
c = sc.nextDouble();
Account account = new Account(a, b, c);
int noOfYear;
noOfYear = sc.nextInt();
double answer = calculateInterest(account, noOfYear);
System.out.format("%.3f",answer);
}
public static double calculateInterest(Account account, int noOfYear)
{
double temp = noOfYear * account.getInterestRate() / 100;
return (account.getBalance() * (account.getInterestRate()+temp) / 100);
}
}
class Account {
private int id;
private double balance;
private double interestRate;
Account(int id, double balance, double interestRate) {
this.id = id;
this.balance = balance;
this.interestRate = interestRate;
}
public int getId() {
return this.id;
}
public void setId(int id) {
this.id = id;
}
public double getBalance() {
return this.balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
public double getInterestRate() {
return this.interestRate;
}
public void setInterestRate(double interestRate) {
this.interestRate = interestRate;
}
}
Classes and Objects - Hands on 1
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
public static void main(String args[] ) throws Exception {
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);
Point p2=new Point(x2, y2);
double distance=findDistance(p1, p2);
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. Compare 2D points for distance from origin
Write a program to check distance of 2D points from origin and print the point with highest distance.
Create class Point with attributes as below:
double x
double y
In Solution class, define main method to read values for three point objects.
Next, create below method in Solution class which will take three point objects as input parameters and return the point with highest distance from origin
public static Point pointWithHighestOriginDistance(Point p1, Point p2, Point p3)
Consider sample input as below to read values for six point objects
2
2
3
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.*;
public class Solution {
public static void main(String args[] ) throws Exception {
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
double x1,y1,x2,y2,x3,y3;
Scanner scn=new Scanner(System.in);
x1=scn.nextDouble();
y1=scn.nextDouble();
x2=scn.nextDouble();
y2=scn.nextDouble();
x3=scn.nextDouble();
y3=scn.nextDouble();
Point p1=new Point(x1, y1);
Point p2=new Point(x2, y2);
Point p3=new Point(x3, y3);
Point highest=pointWithHighestOriginDistance(p1, p2, p3);
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;
}
}
The method will read a String value and print the minimum valued character (as per alphabet and ASCII sequence).
Consider below sample input and output:
Input:
HellO
Output:
H
Important: Answer is not 'e' since 'H' has lower ASCII value then 'e'
Solutions:
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
public static void main(String args[] ) throws Exception {
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
String str;
Scanner scn=new Scanner(System.in);
str=scn.next();
int[] values=new int[str.length()];
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. Write a program to read 5 numbers and print factorials of each.
(Final answers should be non decimal numbers).
Example:
Input:
2
3
4
6
5
Output:
2
6
24
720
120
Solutions:
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
public static void main(String args[] ) throws Exception {
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
Scanner scn=new Scanner(System.in);
int []num=new int[5];
for(int i=0;i<5;i++)
{
num[i]=scn.nextInt();
String res=factorial(num[i]);
System.out.println(res);
}
}
public static String factorial(int n)
{
BigInteger fact=new BigInteger("1");
for(int i=1;i<=n;i++){
fact=fact.multiply(new BigInteger(i+""));
}
return fact.toString();
}
}
Q5. Find Docs with Odd Pages
Create class Document with below attributes
id - int
title - String
folderName - String
pages - int
Write getters, setters and parameterized constructor as required.
Create class Solution with main method.
Implement static method - docsWithOddPages in Solution class.
This method will take array of Document objects and return another array with Document objects which has odd number of pages.
This method should be called from main method and display values of returned objects as shared in the sample (in ascending order of id attribute).
Before calling this method, use Scanner object to read values for four Document objects referring attributes in the above sequence.
Next call the method and display the result.
Consider below sample input and output:
Input:
1
resume
personal
50
2
question1
exams
55
3
question2
exams
45
4
India
misc
40
Output (each line has values separated by single space):
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 {
public static void main(String args[] ) throws Exception {
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
Scanner scn=new Scanner(System.in);
Document[] docsArray=new Document[4];
Document[] res=new Document[4];
for(int i=0;i<docsArray.length;i++)
{
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){
Document[] oddDocs=new Document[4];
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;
public void setId(int id){
this.id=id;
}
public void setTitle(String title){
this.title=title;
}
public void setFolderName(String folderName){
this.folderName=folderName;
}
public void setPages(int pages){
this.pages=pages;
}
public int getId(){
return this.id;
}
public String getTitle(){
return this.title;
}
public String getFolderName(){
return this.folderName;
}
public int getPages(){
return this.pages;
}
}
Q6. Sort Books by Price. Create class Book with below attributes:
id - int
title - String
author - String
price - double
Write getters, setters and parameterized constructor.
Create class Solution with main method.
Implement static method - sortBooksByPrice in Solution class.
This method will take a parameter as array of Book objects.
It will return array of books which is sorted in ascending order of book price. Assume that no two books will have same price.
This method should be called from main method and display values of returned objects as shared in the sample.
Before calling this method, use Scanner object to read values for four Book objects referring attributes in the above sequence.
Next call the method and display the result.
Consider below sample input and output:
Input:
1
hello
writer1
50
2
cup
writer2
55
3
Planet
writer3
45
4
India
writer4
40
Solutions:
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
public static void main(String args[] ) throws Exception {
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
Scanner scn=new Scanner(System.in);
Book[] booksArray=new Book[4];
Book[] sorted=new Book[4];
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++)
{
System.out.println(sorted[i].id+" "+sorted[i].title+" "+sorted[i].author+"
"+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. Shirt Discount, Price
Class Solution has predefined main method to read values and display the results.
main method reads the value for five objects of class Shirt.
It also calls methods - getDiscountPrice and getShirtWithMoreThanSpecificPrice defined in same Solution class.
Code inside main method should not be altered else your solution might be scored as zero.
Guidelines to implement the code:
Create class Shirt with attributes:
int tag
String brand
double price
char gender
Create constructor which takes parameters in above sequence. Create getters and setters for these attributes.
Next, in Solution class, define two static methods as below:
public static double getDiscountPrice(Shirt s):
This method will return the discounted price based on gender for the Shirt object which is passed as input parameter.
If gender is 'm' then discount is 10%. For 'f' it is 20% and for 'u' it is 30%.
public static Shirt[] getShirtWithMoreThanSpecificPrice(Shirt[] shirts,double price):
This method will take array of Shirt objects and price value.
The method will return array of Shirt objects with more than specified price in ascending order of price.
Main method already has Scanner code to read values, create objects and test above methods.
Main method reads values for five Shirt objects, followed by price
Price will be input for method getShirtWithMoreThanSpecificPrice.
Input:
1
arrow
500
m
2
bare
600
f
3
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 {
public static void main(String args[] ) throws Exception {
/* Do not alter code in main method */
Shirt[] shirts = new Shirt[5];
Scanner sc = new Scanner(System.in);
for(int i = 0;i<5;i++)
{
int tag = sc.nextInt();sc.nextLine();
String brand = sc.nextLine();
double price = sc.nextDouble();sc.nextLine();
char g = sc.nextLine().charAt(0);
shirts[i] = new Shirt(tag,brand,price,g);
}
double price = sc.nextDouble();
for(Shirt s: shirts)
{
System.out.println(getDiscountPrice(s));
}
Shirt[] result = getShirtWithMoreThanSpecificPrice(shirts,price);
for(Shirt s: result)
{ if(s.tag!=0)
System.out.println(s.tag+" "+s.price+ " " + s.brand);
}
}
public static Double getDiscountPrice(Shirt s){
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){
Shirt[] r=new Shirt[5];
for(int i=0;i<r.length;i++){
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;
Shirt(int tag,String brand,double price, char g){
this.tag=tag;
this.brand=brand;
this.price=price;
this.g=g;
}
}
Q8 . Create class Book with below attributes:
id - int
title - String
author - String
price - double
Write getters, setters and parameterized constructor as required.
Create class Solution with main method.
Implement static method - searchTitle in Solution class
This method will take a parameter of String value along with other parameter as array of Book objects.
It will return array of books where String value parameter appears in the title attribute (with case insensitive search).
This method should be called from main method and display id of returned objects in ascending order.
Before calling this method, use Scanner object to read values for four Book objects referring attributes in the above sequence.
Next, read the value search parameter.
Next call the method and display the result.
Consider below sample input and output:
Input:
1
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
2
Solutions:
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
import java.util.*;
public class Solution {
public static void main(String args[] ) throws Exception {
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
Scanner scn=new Scanner(System.in);
Book[] booksArray=new Book[4];
Book[] res=new Book[4];
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]);
}
}
public static Book[] searchTitle(String value, Book[] books)
{
int k=0;
Book[] matching=new Book[4];
for(int i=0;i<matching.length;i++)
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;
}
public String getTitle()
{
return this.title;
}
public void setTitle(String title)
{
this.title=title;
}
public String getAuthor()
{
return this.author;
}
public void setAuthor(String author)
{
this.author=author;
}
public double getPrice()
{
return this.price;
}
public void setPrice(double price)
{
this.price=price;
}
}
Comments
Post a Comment