Programming Articles - Page 2053 of 3363

What are the components in a module-info file in Java 9?

raja
Updated on 20-Mar-2020 07:07:25

1K+ Views

A module is an independent unit of application that represents a single functionality. A module contains three important componentsName: To uniquely identify itDependencies: Other modules in which it depends onExported packages: Packages which are open for external applicationIn order to declare a module, we need to add the "module-info.java" file to the root source code. The components of the "module-info.java" file includes "name", "requires", "exports", and "exports to".Below is the template of "module-info.java" filemodule {    requires ;    requires ;    ...    exports ;    exports ;    ...    exports to ; }Name: It is ... Read More

How can we display all module names in Java 9?

raja
Updated on 19-Mar-2020 14:02:31

368 Views

In Java 9, the module concept has introduced. It is a named, self-describing collection of code and data. The code can be organized as a set of packages containing types like java classes and interfaces, and data includes resources and other kinds of static information. A module contains a name, dependencies, and exported packages.Syntaxmodule com.tutorialspoint.mymodule {    // some statements }In the below example, we can able to display all module names by using the ModuleLayer class.Examplepublic class AllModulesNamesTest {    public static void main(String args[]) {       ModuleLayer.boot().modules().forEach((module) -> {          System.out.println(module.getName());       ... Read More

How to print the pattern of stars in JShell in Java 9?

raja
Updated on 19-Mar-2020 12:10:10

307 Views

JShell is a REPL tool introduced in Java 9 that allows us to execute Java code and getting results immediately. We can evaluate expressions or simple algorithms without creating a new project, compile or build it by using JShell. We can also execute expressions, use imports, define classes, methods, and variables. It is a part of Java 9 JDK but not JRE.We can start JShell session in command-prompt by simply typing jshell. We can use different commands: /exit to quit the JShell session, reset/reload JShell anytime by typing /reset, and /reload,  /import to list the imports, etc.In the below example, we can print ... Read More

Minimum Flips to Make a OR b Equal to c in C++

Arnab Chakraborty
Updated on 30-Apr-2020 11:59:36

696 Views

Suppose we have 3 positives numbers a, b and c. We have to find the minimum flips required in some bits of a and b to make (a OR b == c ). Here we are considering bitwise OR operation.The flip operation consists of change any single bit 1 to 0 or change the bit 0 to 1 in their binary representation. So if a : 0010 and b := 0110, so c is 0101, After flips, a will be 0001, and b will be 0100To solve this, we will follow these steps −ans := 0for i in range 0 ... Read More

Sum of Nodes with Even-Valued Grandparent in C++

Arnab Chakraborty
Updated on 30-Apr-2020 11:51:56

211 Views

Suppose we have a binary tree, we have to find the sum of values of nodes with even-valued grandparent. (A grandparent of a node is the parent of its parent, if it exists.). If there are no such nodes with an even-valued grandparent, then return 0. So if the tree is like −The output will be 18. The red nodes are nodes with even-value grandparent, while the blue nodes are the even valued grandparents.To solve this, we will follow these steps −Define a map called parentDefine a method called solve(), this will take node and parif node is null, then ... Read More

Matrix Block Sum in C++

Arnab Chakraborty
Updated on 30-Apr-2020 11:33:32

491 Views

Suppose we have one m * n matrix called mat and an integer K, we have to find another matrix answer where each answer[i][j] is the sum of all elements mat[r][c] for i - K

XOR Queries of a Subarray in C++

Arnab Chakraborty
Updated on 30-Apr-2020 11:30:17

289 Views

Suppose we have the array arr of positive integers and the array queries where queries[i] = [Li, Ri], for each query the i compute the XOR of elements from Li to Ri (that is, arr[Li] XOR arr[Li+1] xor ... xor arr[Ri] ). We have to find the array containing the result for the given queries. So if the input is like − [1, 3, 4, 8], and queries are like [[0, 1], [1, 2], [0, 3], [3, 3]], then the result will be [2, 7, 14, 8]. This is because the binary representation of the elements in the array are ... Read More

Jump Game III in C++

Arnab Chakraborty
Updated on 30-Apr-2020 11:26:25

470 Views

Suppose we have an array of non-negative integers arr, we are initially positioned at start index of the array. When we are present at index i, we can jump to i + arr[i] or i - arr[i], check if we can reach to any index with value 0. We have to keep in mind that we cannot jump outside of the array at any time. So if the input is like: arr = [4, 2, 3, 0, 3, 1, 2] and start from 5, then output will be true, as move 5 → 4 → 1 → 3, or 5 ... Read More

All Elements in Two Binary Search Trees in C++

Arnab Chakraborty
Updated on 30-Apr-2020 11:22:51

292 Views

Suppose we have two binary search trees, we have to return a list of values, that has all elements present in these trees, and the list elements will be in ascending order. So if the trees are like −Then the output will be [0, 1, 1, 2, 3, 4].To solve this, we will follow these steps −Define an array called ans, define two stacks st1 and st2curr1 := root1 and curr2 := root2insert node root1 and all left nodes into st1, insert node root2 and all left nodes into st2while st1 is not empty or st2 is not emptyif st1 ... Read More

Deepest Leaves Sum in C++

Arnab Chakraborty
Updated on 30-Apr-2020 11:13:24

259 Views

Suppose we have a binary tree, we have to find the sum of values of its deepest leaves. So if the tree is like −Then the output will be 15.To solve this, we will follow these steps −Define a map m, and maxDepthDefine a recursive method solve(), this will take node and level, initially level is 0if node is not present, then returnmaxDepth := max of level and maxDepthincrease m[level] by value of nodesolve(left of node, level + 1)solve(right of node, level + 1)In the main method, setup maxDepth := 0, then solve(root, 0)return m[maxDepth]Example(C++)Let us see the following implementation ... Read More

Advertisements