Tuesday, February 2, 2021

SKP's Java Problem Solving Series : Refresh Java Lambdas (FP)

[Question/Problem Statement is the Property of Techgig]

Java Advanced - Lambda Expressions [www.techgig.com] 
Write the Following Methods that Return a Lambda Expression Performing a Specified Action: PerformOperation isOdd(): The Lambda Expression must return  if a Number is Odd or  If it is Even. PerformOperation isPrime(): The lambda expression must return  if a number is prime or  if it is composite. PerformOperation isPalindrome(): The Lambda Expression must return  if a number is a Palindrome or if it is not.

Input Format
Input is as Show in the Format Below (Deduce Unknowns!)

Input
3
1 3
2 7
3 7777

Constraints
NA

Output Format
Output is as Show in the Format Below (Deduce Unknowns!)

Output
ODD
PRIME
PALINDROME
______________ 
 
 
[Explanation of the Solution]
This is a Good Question to Refresh Java 8 Lambdas. In my Solution, I Implemented the Functional Interfaces within my main() Method and assigned it to Local Reference Variables.
 
________________  
 

[Source Code, Sumith Puri (c) 2021 - Free to Use & Distribute]
 import java.util.Scanner;

/*    
 * Techgig Core Java Basics Problem - Knock Off Java Lambdas!   
 * Author: Sumith Puri [I Bleed Java!] // GitHub: @sumithpuri   
 */
interface LambdaYogi {
	public boolean opYog(int x);
}

public class CandidateCode {
	public static void main(String args[]) throws Exception {

                // you may choose to refactor, as this method
                // becomes really long and unmanageable #TODO
		LambdaYogi isOdd = a -> {
			boolean retFlag = false;
			if (a % 2 != 0)
				retFlag = true;
			return retFlag;
		};
		LambdaYogi isPrime = a -> {
			boolean retFlag = true;
			for (int i = 2; i < a - 1; i++) {
				if (a % i == 0) {
					retFlag = false;
					break;
				}
			}
			return retFlag;
		};
		LambdaYogi isPalindrome = a -> {
			boolean retFlag = false;
			String actStr = String.valueOf(a).trim();
			String revStr = new StringBuffer(actStr).reverse().toString();

			// using string basis, not mathematical
			if (actStr.equals(revStr))
				retFlag = true;
			return retFlag;
		};

		Scanner scanner = new Scanner(System.in);
		int val = scanner.nextInt();

		for (int i = 0; i < val; i++) {
			int op = scanner.nextInt();
			int no = scanner.nextInt();
			switch (op) {
			case 1: {
				if (isOdd.opYog(no))
					System.out.println("ODD");
				else
					System.out.println("EVEN");
				break;
			}
			case 2: {
				if (isPrime.opYog(no))
					System.out.println("PRIME");
				else
					System.out.println("COMPOSITE");
				break;
			}
			case 3: {
				if (isPalindrome.opYog(no))
					System.out.println("PALINDROME");
				else
					System.out.println("NONPALIN");
			}
			}
		}
	}
}

Happy Problem Solving using Java!

Saturday, January 30, 2021

SKP's Java Problem Solving Series : Simple Java Inheritance (OOPs)

[Question/Problem Statement is the Property of Techgig]
 
Java Inheritance / Simple OOPs [www.techgig.com] 
Create Two Classes:

BaseClass
The Rectangle class should have two data fields-width and height of int types. The class should have display()method, to print the width and height of the rectangle separated by space.

DerivedClass 
The RectangleArea class is Derived from Rectangle class, i.e., it is the Sub-Class of Rectangle class. The class should have read_input() method, to Read the Values of width and height of the Rectangle. The RectangleArea class should also Overload  the display() Method to Print the Area (width*height) of the Rectangle.

Input Format
The First and Only Line of Input contains two space separated Integers denoting the width and height of the Rectangle.

Constraints
1 <= width,height <= 10^3

Output Format
The Output Should Consist of Exactly Two Lines.
In the First Line, Print the Width and Height of the Rectangle Separated by Space. 
In the Second Line, Print the Area of the Rectangle.

______________ 
 
 
[Explanation of the Solution]
This is the Simplest of all OOPs Questions! Demonstration of Inheritance and Overriding (Very Loosely, Liskov Substitution of SOLID). 
 
________________  
 

[Source Code, Sumith Puri (c) 2021 - Free to Use & Distribute]
 /*    
  * Techgig Core Java Basics Problem - Get Simple OOPs Right!  
  * Author: Sumith Puri [I Bleed Java!]; GitHub: @sumithpuri;  
  */   
     
  import java.io.*;   
  import java.util.*;   
   
   
  class Rectangle {  
   
    private int width;  
    private int height;  
   
    public void display() {  
   
      System.out.println(width + " " + height);  
    }  
   
    public int getWidth() {  
   
      return width;  
    }  
   
    public void setWidth(int width) {  
   
      this.width=width;  
    }  
   
    public int getHeight() {  
   
      return height;  
    }  
   
    public void setHeight(int height) {  
   
      this.height=height;  
    }  
  }  
   
  class RectangleArea extends Rectangle {  
   
    public void read_input() {  
   
     Scanner scanner = new Scanner (System.in);   
       
     setWidth(scanner.nextInt());   
     setHeight(scanner.nextInt());  
    }  
   
    public void display() {  
   
      super.display();  
      System.out.println(getWidth()*getHeight());  
    }  
  }  
     
  public class CandidateCode {   
     
   public static void main(String args[] ) throws Exception {   
     
     RectangleArea rectangleArea = new RectangleArea();  
     rectangleArea.read_input();  
   
     rectangleArea.display();  
   }   
 } 

Happy Problem Solving using Java!

Friday, January 29, 2021

SKP's Java Problem Solving Series : Monkeys in the Garden

[Question/Problem Statement is the Property of Techgig]

Monkeys in the Garden [www.techgig.com] 

In a garden, trees are arranged in a circular fashion with an equal distance between two adjacent trees. The height of trees may vary. Two monkeys live in that garden and they were very close to each other. One day they quarreled due to some misunderstanding. None of them were ready to leave the garden. But each one of them wants that if the other wants to meet him, it should take maximum possible time to reach him, given that they both live in the same garden.

The conditions are that a monkey cannot directly jump from one tree to another. There are 30 trees in the garden. If the height of a tree is H, a monkey can live at any height from 0 to H. Lets say he lives at the height of K then it would take him K unit of time to climb down to the ground level. Similarly, if a monkey wants to climb up to K height it would again take K unit of time. The time to travel between two adjacent trees is 1 unit. A monkey can only travel in a circular fashion in the garden because there is a pond at the center of the garden.

So the question is where should two monkeys live such that the traveling time between them is maximum while choosing the shortest path between them in any direction clockwise or anti-clockwise. You have to answer only the maximum traveling time.

Input Format
The First Line consists of Total Number of Trees (N). Each of the Following N Lines contains the Height of Trees in a Clockwise Fashion.

Constraints
1 <= Total Trees <= 30

1 <= Height Of Trees(H) <= 10000

Output Format
You must Print an Integer which will be the Maximum Possible Travel Time.

________________ 
 
 
[Explanation of the Solution]
Surprisingly, this Problem is under the Object Oriented Programming (OOP) Section of the Problems! Initially, I was on the Lookout for a 'Mathematically Superior' Solution. But Couldn't Reach Anywhere - In the End, I have a Simple Solution at O(n²). Iterate through Each 'Combination of Trees' and then Find the Clockwise and Anti-Clockwise Distance - At Each Iteration, the Minimum of the Two is Added to the Length of Each Tree. If this Value is Greater than the Maximum Path Length (Previous Iterations) - Replace the Maximum Path Length.
 
________________ 
 
[Source Code, Sumith Puri (c) 2021 - Free to Use & Distribute]
 /*   
  * Techgig Core Java Basics Problem - Monkeys in the Garden 
  * Author: Sumith Puri [I Bleed Java!]; GitHub: @sumithpuri
  */  
   
 import java.io.*;  
 import java.util.*;  
 import java.lang.Math;  
   
 public class CandidateCode {  
   
   public static void main(String args[] ) throws Exception {  
   
     Scanner scanner = new Scanner (System.in);  
       
     int n = scanner.nextInt();  
     int h[] = new int[n], max=0;  
     int cwLen=0,acLen=0,hiLen=0,toLen=0;  
   
     for(int i=0;i<n;i++) {  
       h[i] = scanner.nextInt();        
     }  
     max=h[0];  
   
     for(int i=0;i<n;i++) {  
   
       for(int j=i+1;j<n;j++) {  
         cwLen=Math.abs(((n-j)+i)); // clockwise  
         acLen=Math.abs((j-i));     // anti-clockwise  
         hiLen=(cwLen<=acLen)?(cwLen):(acLen);  
         toLen=hiLen+h[i]+h[j];     // path length    
         if(toLen>max) max=toLen;   // maximum path length  
       }  
     }  
          
     System.out.println (max);   
   }  
 }  
 
Happy Problem Solving using Java!

Thursday, February 6, 2020

Elevated to the Elite Developers/Contributors List! ;-)

All Blog Followers, Random Visitors, Developers, Contributors => Take Note.

My Blog / I Got [Promoted / Elevated] to DZone Core! The Elite Developers/Contributors List on Planet Earth!  Spent Decent Enough Time - About ~17+ Years Now => Championing, Evangelizing, Learning, Teaching, Applying, Prototyping, Building, [Doing], Glorifying Core Java and Jakarta EE ;-)  Primarily I am a Backend (Presentation, Business/Service, Web Services/Integration, Persistence, Database) Developer or Architect. [Feb 2020] Right Now, I am Employed as a Senior Product Development Manager at Ultria Software Private Limited, Bengaluru, Karnataka, India.

By the Way, I hold 7 Certifications* in Core Java/Java EE, 3 in C/CPP/Data Structures => Also, I am a Java Code Geek; Senior Member, ACM; Senior Member, IEEE;

Thanks, DZone [Blake Ethridge].

 
 

Friday, November 22, 2019

[Stop, Pause, Re-Start] Future Topics on this Blog!

This Blog has been Silent for a Long Time (Actually, I Wrote a Blog on 03-Jul-2018 Titled "Future Topics on this Blog", But I Could Never Write any Entries as per the Plan Mentioned There). The Most Active Time Period on the Blog were the Year(s) 2014, 2015, 2016, 2017. 

Beyond this, I ran my Own Startup with the Name Vajrin Software, Bangalore. The Startup Could Not Succeed and I Moved to a Senior Architect (Java/Java EE) Position in a IT Products Company in Philippines via a Staffing Firm, in the Travel Domain. After a Very Short Stay, I was Back as a Head of Product Engineering in a Bangalore Software Company in the Employee Transportation Domain. From February 2017 to July 2019, I also traveled to Malaysia, Nepal, Bhutan, Thailand (2 Times), Myanmar, Singapore and Indonesia/Bali. The Above is the Career Story from February 2017 to July 2019.

Taking a Job Break and Having Lost 20 Kgs of Body Weight from June 2019 to November 2019, I have joined as a Senior Product Development Manager at Ultria Software Private Limited, Bangalore. With Last 7 Designations/Roles the String Arc  It is a Small Sized Firm with Big Customer Names, Just Like my Previous Employer. Hope to be Stable Now in this Position! Still on my Way to Lose Another 10 Kgs to be Truly Agile! :-) ;-)
 
This Blog will now really have Regular Updates and Entries from abut December 2019. I wanted to give a heads-up to all, the Topics on which I plan to Blog in the next one year. These Blog Entries will arise out of my Daily Office Work, Professional Certification Preparation (Big Data and Hadoop Architect Master's), Udemy Courses and Self-Learning of Various Core Java and Jakarta EE Topics. Also, Few of my Articles may Get Re-Published in Popular Developer Sites like DZone, Ulitzer and Java Code Geek. The Crux of the Entries will be in Microservices, Spring Boot, Software Architecture, Java 9 Concurrency, Java 9 Modularity, Java 10/11/12/13. With Around 15.5+ Years of Professional Software Experience, I will start delving into User Interface with Angular 8, Node.js. Even though late into the world of Apache Kafka, NoSQL, MongoDB world, I hope that my Articles Count! I also will write articles in Machine Learning, Innovative Product Design and Development, Intelligent Agents and Data Mining. I am heavily invested into Udemy, with a Target to Complete 14 Course Titles in about 3 Months!

So, The Most Probable List (Subset of Below) of Topics in the Coming Future:
  • Microservices Architecture (Architect, Developer)
  • Java 8 : Lambdas (Developer)
  • Master Lambdas in Java 8 (Advanced Developer) 
  • Java 9 : Modular Programming (Developer)
  • Java 9 : Concurrency (Developer)
  • Java 9 : Advanced Concurrency (Advanced Developer)
  • MongoDB : NoSQL from Scratch (Developer)
  • Apache Kafka for Beginners (Developer)
  • Java 8, 9, 10, 11, 12 (Architect, Developer, Manager)
  • Angular 8 - Complete Guide (UI Initiation)
  • Model Driven Architecture (Architect)
  • AWS Fargate, ECS, Spring Boot (Developer)
  • Kubernetes for Beginner (DevOps)
  • Microservices with Spring Cloud (Developer)

Also, The Future Blog Entries (Subset of Below) will be From Below Topics: 
  • Introduction to Big Data and Hadoop Ecosystem (Architect, Developer)
  • Introduction to Docker (Developer)
  • Introduction to Apache Spark and Scala (Developer)
  • Big Data and Hadoop Administration (Architect's Point of View)
  • Introduction to Apache Storm (Developer)
  • Introduction to Apache Kafka (Developer)
  • Introduction to Apache Cassandra (Developer)
  • Are Professional Certifications worth the Effort? (Professional)
  • Introduction to Machine Learning with Java (Developer)
  • Appreciation of the Java EE 6.0 Release (Architect, Developer)
  • Appreciation of the Java EE 7.0 Release (Architect, Developer)
  • Appreciation of the Java EE 8.0 Release  (Architect, Developer)
  • Java SE 9... What's New? (Code Samples) (Architect, Developer)
  • Java SE 10... What's New? (Architect, Developer)
  • Java SE and Java EE Roadmap and Versioning Strategy (Architect)
  • Functional Programming with Java (Code Samples) (Developer)
  • NoSQL, NewSQL and OldSQL - Needs and Motivations (Developer)
  • Appreciation of Service Oriented Architecture [SOA] (Architect)
  • Reusable Software Components (Java/Jakarta EE)
  • Understanding/Detecting the OWASP Top 10 (Developer)
  • SQL and Database/RDBMS Optimization (Developer)
  • SKP's Hardware/Server Sizing Calculator for Enterprise Java (Architect)
  • Innovative Software Product Design & Development - 02/10 (Product Development)

I hope to add few more to the above list as time goes on. I hope that many of the articles are selected and re-published on DZone, Java Code Geek and Ulitzer, so that many more in the Java Development Community benefit from my knowledge. 

The first blog entry from the above list should be here by December 2019. Until then, Happy Times with Core Java and Jakarta EE!