Java

It is similar to Python. Navigate to the new directory to save the project files and in all of the sub program write:

package app_name;

On the starting line, and create a class as usual.
Import the module into new java program by again writing: package app_name and simply reference that particular module.function() as they belong to same package (are stored in same directory) and java implicitly adds the following lines but if you need to import new module(s) from different package(s) then do so by:

import package.*; 
import package.classname; 
import static package.*;
Fully Qualified Name      
//  eg: package.classname ob = new classname ();

The 1st and 2nd ways of look similar to python’s from…import syntax but you have to explicitly state the class. In order to achieve such a not recommended but pythonic way of from…import syntax style, you have to use the 3rd method i.e., import static to achieve similar results but you have to resort to using fully qualified name to prevent collisions and clear up human misunderstandings anyway.

Example of a single cluttered File:

Check.java




// Check.java
  
import java.util.*;
  
class Math {
    static int check(int a, int b)
    {
        return a > b ? 'a' : 'b';
    }
}
  
class Main {
    public static void main(String args[])
    {
  
        Scanner s = new Scanner(System.in);
  
        System.out.print("Enter value of a: ");
        int a = s.nextInt();
  
        System.out.print("Enter value of b: ");
        int b = s.nextInt();
  
        if (a == b)
            System.out.println("Both Values are Equal");
        else
            System.out.printf("%c's value is Greater\n",
                              Math.check(a, b));
    }
}


Once again there is scope of division and abstraction. We can create Multiple standalone files that deal with the numerics and here for the example, We can divide

Dividing the code into smaller parts:
The Program can be divided such that:
1) Main File -> Driver program, Write the Manipulative code here.
2) Math File -> All the methods regarding Mathematics (here Partially implemented Check Function).

The Sample Program contains:

Math.java -> Which belongs to foo package and a Math class that consists of method check which can only compare 2 nos. excluding inequality.

Main.java -> Main Program takes 2 numbers as input and prints the greater of 2.

Math.java




// Math.java
  
package foo;
  
public class Math {
    public static int check(int a, int b)
    {
        return a > b ? 'A' : 'B';
    }
}


Main.java




// Main.java
// Driver Program
  
package foo;
  
import java.util.*;
  
class Main {
    public static void main(String args[])
    {
  
        Scanner s = new Scanner(System.in);
  
        System.out.print("Enter value of a: ");
        int a = s.nextInt();
  
        System.out.print("Enter value of b: ");
        int b = s.nextInt();
  
        if (a == b)
            System.out.println("Both Values are Equal");
        else
            System.out.printf("%c's value is Greater\n",
                              Math.check(a, b));
    }
}


Compilation:

javac -d /path file1.java file2.java 

Sometimes you might want to set your classpath to point to somewhere, use the:

set classpath= path/to/location

// (or) pass the switch for both java and javac as
javac -cp /path/to/location file.java

// (or)
java -classpath /path/to/location file

By default it points to current directory i.e., “.

Executing the code:

java packagename.Main // Here in the example it is: “java foo.Main”

Output:



Dividing a Large file into Separate Modules in C/C++, Java and Python

If you ever wanted to write a large program or software, the most common rookie mistake is to jump in directly and try to write all the necessary code into a single program and later try to debug or extend later.

This kind of approach is doomed to fail and would usually require re-writing from scratch.

So in order to tackle this scenario, we can try to divide the problem into multiple subproblems and then try to tackle it one by one.

Doing so, not only makes our task easier but also allows us to achieve Abstraction from the high-level programmer and also promotes Re-usability of code.

If you check any Open-Source project from either GitHub or GitLab or any other site of the likes, we can see how the large program is “decentralized” into many numbers of sub-modules where each individual module contributes to a specific critical function of the program and also various members of the Open Source Community come together for contributing or maintaining such file(s) or repository.

Now, the big question lies in how to “break-down” not theoretically but PROGRAMMATICALLY.

We will see some various types of such divisions in popular languages such as C/C++, Python & Java.

  • Jump to C/C++

  • Jump to Python

  • Jump to Java

Similar Reads

C/C++

For Illustrative Purposes,...

Python

...

Java

...