Unit - Hackerrank Activities - JAVA
Java - Numeric Computation:
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;
}
}
***********************************************************************************************
Java - Classes and Objects
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;
}
}
*************************************************************************************************
Conditional operators
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;
Point(double x, double y)
{
this.x=x;
this.y=y;
}
}
******************************************************************************************
Java Arrays - 1
Problem 1: Find docs with odd page
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 {
Document[] document=new Document[4];
Scanner sc=new Scanner(System.in);
for(int i=0;i<document.length;i++)
{
document[i]=new Document(sc.nextInt(),sc.next(),sc.next(),sc.nextInt());
}
Document[] output=docsWithOddPages(document);
for(Document i:output)
{
System.out.println(i.getId()+" "+i.getTitle()+" "+i.getFolderName()+" "+i.getPages());
}
}
public static Document[] docsWithOddPages(Document[] d)
{ Document[] d2=new Document[0];
for(Document i:d)
{
if(i.getPages()%2!=0)
{
d2=Arrays.copyOf(d2, d2.length+1);
d2[d2.length-1]=i;
}
}
for(int i=0;i<d2.length-1;i++)
{
for(int k=0;k<d2.length-i-1;k++)
{
if(d2[k].getId()>d2[k+1].getId())
{
Document temp=d2[k];
d2[k]=d2[k+1];
d2[k+1]=temp;
}
}
}
return d2;
}
}
class Document
{
private int id;
private String title;
private String folderName;
private int pages;
Document(int id,String title,String folderName,int pages)
{
this.id=id;
this.title=title;
this.folderName=folderName;
this.pages=pages;
}
public int getId()
{
return id;
}
public String getTitle()
{
return title;
}
public String getFolderName()
{
return folderName;
}
public int getPages()
{
return pages;}
}
Problem 2: Sort books by price
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
{
Scanner sc=new Scanner(System.in);
Book[] book=new Book[4];
for(int i=0;i<book.length;i++)
{
book[i]=new Book(sc.nextInt(),sc.next(),sc.next(),sc.nextInt());
}
Book[] output=sortBooksByPrice(book);
for(Book i: output)
{
System.out.println(i.getId()+" "+i.getTitle()+" "+i.getAuthor()+" "+i.getPrice());
}
}
public static Book[] sortBooksByPrice(Book[] book)
{
for(int i=0;i<book.length-1;i++)
{
for(int k=0;k<book.length-i-1;k++)
{
if(book[k].getPrice()>book[k+1].getPrice())
{
Book temp=book[k];
book[k]=book[k+1];
book[k+1]=temp;
}
}
}
return book;
}
}
class Book
{
private int id;
private String title;
private String author;
private double price;
Book(int id,String title,String author,double price)
{
this.id=id;
this.title=title;
this.author=author;
this.price=price;
}
public int getId()
{
return id;
}
public String getTitle()
{
return title;
}
public String getAuthor()
{
return author;
}
public double getPrice()
{
return price;
}
}
****************************************************************************************
Java Arrays - 2
Problem 1:
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.getTag()!=0)
System.out.println(s.getTag()+" "+s.getPrice()+ " " + s.getBrand());
}
}
/* implement your methods here*/
public static double getDiscountPrice(Shirt s)
{
double discount;
if(s.gender=='m')
discount=10;
else if(s.gender=='f')
discount=20;
else if(s.gender=='u')
discount=30;
else
discount=0;
return s.price-(discount*s.price)/100;
}
public static Shirt[] getShirtWithMoreThanSpecificPrice(Shirt [] shirts, double price)
{
Shirt[] res = new Shirt[5];
for(int i=0;i<res.length;i++)
res[i]=new Shirt(0,"",0,'c');// taking sample to avoid null pointer exception
int j=0;
for(int i=0;i<shirts.length;i++)
{
if(shirts[i].price>price)
{
res[j++]= new Shirt(shirts[i].tag,shirts[i].brand,shirts[i].price,shirts[i].gender);
}
}
return res;
}
}
class Shirt
{
//define the class as per details shared in the question
int tag;
String brand;
double price;
char gender;
Shirt(int tag, String brand, double price, char gender)
{
this.tag=tag;
this.brand=brand;
this.price=price;
this.gender=gender;
}
public int getTag()
{
return this.tag;
}
public void setTag(int tag)
{
this.tag=tag;
}
public String getBrand()
{
return this.brand;
}
public void setBrand(String brand)
{
this.brand=brand;
}
public double getPrice()
{
return this.price;
}
public void setPrice(double price)
{
this.price=price;
}
public char getGender()
{
return this.gender;
}
public void setGender(char gender)
{
this.gender=gender;
}
}
Problem 2:
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;
}
******************************************************************************
Java - Iteration 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 {
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
Scanner sc=new Scanner(System.in);
String s=sc.nextLine();
char min=s.charAt(0);
for(int i=0;i<=s.length()-1;i++)
{
char c=s.charAt(i);
if(min>c)
{
min=c;
}
}
System.out.println(min);
}
}
********************************************************************************************************
Java - Iteration 2
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 sc=new Scanner(System.in);
int fact;
int[] arr=new int[5];
for(int i=0;i<=arr.length-1;i++)
{
arr[i]=sc.nextInt();
}
for(int i=0;i<=arr.length-1;i++){
fact=1;
for(int j=1;j<=arr[i];j++)
{
fact=fact*j;
}
System.out.println(fact);
}
}
}