You are on page 1of 11

Calculate Interest :

import java.util.Scanner;

class Account {
private int id;

private double balance;


private double interesrtRate;
public Account(int id, double balance, double interesrtRate) {
this.id = id;
this.balance = balance;
this.interesrtRate = interesrtRate;
}

public int getId() {


return id;
}
public void setId(int id) {
this.id = id;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
public double getInterestRate() {
return interesrtRate;
}
public void setInterestRate(double interesrtRate) {
this.interesrtRate = interesrtRate;
}
}
class Solution {
public static void main(String [] args) {
Scanner in = new Scanner(System.in);
int id=in.nextInt();
double balance =in.nextDouble();
double interestRate=in.nextDouble();
Account account =new Account(id,balance,interestRate);
int noOfYears = in.nextInt();
double result = calculateInterest(account,noOfYears);
System.out.printf("%.3f",result);

}
public static double calculateInterest(Account a , int noOfYears) {
double result =noOfYears*a.getInterestRate()/100;
return (a.getBalance()*(a.getInterestRate()+result)/100);
}

}
Distance From Origin :

import java.util.Scanner;

class Solution {
public static void main (String [] args) {
Scanner in =new Scanner(System.in);
int x1=in.nextInt();
int y1=in.nextInt();
int x2=in.nextInt();
int y2=in.nextInt();
int x3=in.nextInt();
int y3=in.nextInt();
Point p1 = new Point(x1,y1);
Point p2=new Point(x2,y2);
Point p3=new Point(x3,y3);
Point high = highestPoint(p1,p2,p3);

}
public static Point highestPoint(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;
double y;
Point(double x , double y){
this.x=x;
this.y=y;

}
}

Distance b/w 2 Points :

import java.util.Scanner;
class Solution {
public static void main(String [] args) {
Scanner in =new Scanner(System.in) ;
int x1=in.nextInt();
int x2=in.nextInt();
int y1=in.nextInt();
int y2=in.nextInt();
Point p1=new Point(x1,y1);
Point p2=new Point(x2,y2);
double distance = findDistance(p1,p2);
System.out.printf("%.3f",distance);
}
public static double findDistance(Point p1, Point p2) {
double distance =Math.sqrt(Math.pow((p2.x-p1.x),2)+Math.pow((p2.y-
p1.y),2));
return distance;
}
}
class Point {
int x;
int y;
Point(int x,int y){
this.x=x;
this.y=y;
}

Min Alphabet :

import java.util.Scanner;

class Solution{
public static void main(String [] args) {
Scanner in = new Scanner(System.in);
String str= in.nextLine();
char min = str.charAt(0);
for(int i =0;i<str.length();i++) {
if((int)str.charAt(i)< (int)min) {
min=str.charAt(i);
}
}
System.out.println(min);
}
}

Factorial :

import java.util.Scanner;

class Solution{
public static void main(String []args ) {
Scanner in =new Scanner(System.in);
int [] result = new int[5];
for(int i=0 ; i<5;i++) {
int num=in.nextInt();
result[i]=factorial(num);
}
for(int i : result) {
System.out.println(i);
}
}

public static int factorial(int n ) {


return (n==0 || n == 1)?1 : n*factorial(n-1);
}

Document With Odd Pages :

import java.util.*;

class Document {
private int id;
private String title;
private String folderName;
private int pages;

public 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 void setId(int id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getFolderName() {
return folderName;
}
public void setFolderName(String folderName) {
this.folderName = folderName;
}
public int getPages() {
return pages;
}
public void setPages(int pages) {
this.pages = pages;
}
}
class Solution{
public static Document[] docsWithOddPages(Document [] obj) {

int j=0;
int count=0;
for(int i=0; i<4;i++) {
if(obj[i].getPages()%2 != 0) {
count++;
}
}
Document[] doc =new Document[count];

for(int i=0; i<4;i++) {


if(obj[i].getPages()%2 != 0) {
doc[j++]=obj[i];
}
}
int l =doc.length;
for(int i=0;i<l;i++){
for(int k=i+1;k<l;k++){
if(doc[i].getId() > doc[k].getId()){
Document temp=doc[i];
doc[i]=doc[k];
doc[k]=temp;
}
}
}
return doc;

}
public static void main(String [] args) {
Scanner in =new Scanner(System.in);
Document [] obj = new Document[4];
for(int i =0;i<4;i++) {
int id =in.nextInt();
in.nextLine();
String title =in.nextLine();
String folderName=in.nextLine();
int pages=in.nextInt();
obj[i]=new Document(id,title,folderName,pages);
}
Document[] obj1 =docsWithOddPages(obj);
for(Document d : obj1) {
if(d != null){
System.out.println(d.getId()+" "+d.getTitle()+" "+d.getFolderName()+"
"+d.getPages());
}
}

}
}

Sort Books By Price :

import java.util.Scanner;

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 void setId(int id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}

}
class Solution {
public static Book[] sortBooksByPrice(Book[] obj) {
for(int i=0;i<4;i++) {
for(int j=i+1;j<4;j++) {
if(obj[i].getPrice() > obj[j].getPrice()) {
Book temp = obj[i];
obj[i]=obj[j];
obj[j]=temp;

}
}
}

return obj;
}
public static void main(String [] args) {
Scanner in = new Scanner(System.in);
Book [] obj = new Book[4];
for(int i=0 ;i<4;i++) {
int id =in.nextInt();
in.nextLine();
String title =in.nextLine();
String author=in.nextLine();
double price =in.nextDouble();

obj[i]=new Book(id,title,author,price);

Book [] obj1 = sortBooksByPrice(obj);


for(Book b : obj1) {
System.out.println(b.getId()+" "+b.getTitle() +" "+b.getAuthor()+"
"+b.getPrice() );
}

}
}

Search Title (BOOK) :

import java.util.Scanner;

class Book {
private int id;
private String title;
private String author;
private double price;

public 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 void setId(int id) {


this.id = id;
}

public String getTitle() {


return title;
}

public void setTitle(String title) {


this.title = title;
}

public String getAuthor() {


return author;
}

public void setAuthor(String author) {


this.author = author;
}

public double getPrice() {


return price;
}

public void setPrice(double price) {


this.price = price;
}

}
class Solution{

public static Book [] searchTitle(Book[] obj , String title) {


String key =title.toLowerCase();

int j=0;
int count=0;
for(int i=0;i<4;i++) {
if(obj[i].getTitle().toLowerCase().contains(key)) {
count++;
}
}
Book[] obj1 = new Book[count];
for(int i =0;i<4;i++) {
if(obj[i].getTitle().toLowerCase().contains(key)) {
obj1[j++]=obj[i];
}
}
for(int i =0;i<obj1.length;i++) {
for(int k=i+1;k<obj1.length;k++) {
if(obj1[i].getId()>obj[k].getId()) {
Book temp=obj1[i];
obj1[i]=obj1[k];
obj1[k]=temp;
}
}
}

return obj1;
}
public static void main(String [] args) {
Scanner in =new Scanner(System.in);
Book [] obj=new Book[4];
for(int i=0;i<4;i++) {
int id=in.nextInt();
in.nextLine();
String title =in.nextLine();
String author =in.nextLine();
double price =in.nextDouble();

obj[i]= new Book(id,title,author,price);

String key =in.next();


Book [] obj1 = searchTitle(obj,key);
for(Book b : obj1) {
if(b != null)
System.out.println(b.getId());
}
}
}

Shirt with More than specific price :

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)
{
System.out.println(s.getTag()+" "+s.getPrice()+ " " + s.getBrand());
}
}

public static double getDiscountPrice(Shirt s){

double discount =(s.getGender() == 'm')?10:((s.getGender()=='f')?20:((s.getGender()


=='u')?30:0));

return s.getPrice()-(discount*s.getPrice())/100;

}
public static Shirt[] getShirtWithMoreThanSpecificPrice(Shirt[] shirts , double
price){
int size=0;

for(int i =0 ;i<shirts.length;i++){
if(shirts[i].getPrice() > price){
size++;
}
}
Shirt [] obj = new Shirt[size];
int j=0;
for(int i =0 ;i<shirts.length;i++){
if(shirts[i].getPrice() > price){
obj[j++]=shirts[i];
}
}
for(int i=0;i<size;i++){
for(int k=i+1;k<size;k++){
if(obj[i].getPrice()>obj[k].getPrice()){
Shirt temp = obj[i];
obj[i]=obj[k];
obj[k]=temp;
}
}
}
return obj;
}
}

class Shirt
{
private int tag;
private String brand;
private double price;
private char gender;
Shirt(int tag, String brand, double price, char gender){
this.tag=tag;
this.brand=brand;
this.price=price;
this.gender=gender;
}
//Getters
public int getTag(){
return tag;
}
public double getPrice(){
return price;

}
public String getBrand(){
return brand;

}
public char getGender(){
return gender;

}
//Setters
public void setTage(int tag){
this.tag=tag;
}
public void setBrand(String brand){
this.brand=brand;
}
public void setPrice(double price){
this.price=price;
}
public void setGender(char gender){
this.gender=gender;
}
}

You might also like