SKP’s Algorithms and Data Structures #9: Java Problem: 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.

SKP’s Algorithms and Data Structures #8: Java Problem: Simple 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 and Distribute]
Java
 




x
67


1
 /*    
2
  * Techgig Core Java Basics Problem - Get Simple OOPs Right!  
3
  * Author: Sumith Puri [I Bleed Java!]; GitHub: @sumithpuri;  
4
  */   
5
     
6
  import java.io.*;   
7
  import java.util.*;   
8
   
9
   
10
  class Rectangle {  
11
   
12
    private int width;  
13
    private int height;  
14
   
15
    public void display() {  
16
   
17
      System.out.println(width + " " + height);  
18
    }  
19
   
20
    public int getWidth() {  
21
   
22
      return width;  
23
    }  
24
   
25
    public void setWidth(int width) {  
26
   
27
      this.width=width;  
28
    }  
29
   
30
    public int getHeight() {  
31
   
32
      return height;  
33
    }  
34
   
35
    public void setHeight(int height) {  
36
   
37
      this.height=height;  
38
    }  
39
  }  
40
   
41
  class RectangleArea extends Rectangle {  
42
   
43
    public void read_input() {  
44
   
45
     Scanner scanner = new Scanner (System.in);   
46
       
47
     setWidth(scanner.nextInt());   
48
     setHeight(scanner.nextInt());  
49
    }  
50
   
51
    public void display() {  
52
   
53
      super.display();  
54
      System.out.println(getWidth()*getHeight());  
55
    }  
56
  }  
57
     
58
  public class CandidateCode {   
59
     
60
   public static void main(String args[] ) throws Exception {   
61
     
62
     RectangleArea rectangleArea = new RectangleArea();  
63
     rectangleArea.read_input();  
64
   
65
     rectangleArea.display();  
66
   }   
67
 } 



SKP’s Algorithms and Data Structures #7: Functional Programming and Java Lambdas

[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: Perform Operation isOdd(): The Lambda Expression must return if a Number is Odd or  If it is Even. Perform Operation 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
Input
3
1 3
2 7
3 7777

Constraints
NA

Output Format
Output is as Show in the Format Below
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.