Modular Programming: Python vs.
Java
In Python: The "Module" Approach
In Python, a module is simply a file containing Python code (functions, classes, or variables) that you want
to include in another program. Think of it as a way to modularize your project—breaking a large, complex
script into smaller, manageable, and reusable pieces. It is lightweight and flexible; you can import a file
and immediately use its contents.
In Java: The "Class & Package" Approach
In Java, the concept of a module is handled through Classes and Packages. Because Java is an Object-
Oriented language, you don’t just import a file; you interact with a Class. While the goal is the same—
reusability and organization—Java requires you to wrap your code inside a class structure (public\ class\
Name) to make it accessible to other parts of your program.
Example Output
Filename: [Link]
import math
def get_area(radius): Enter the radius: 5
return [Link] * (radius ** 2)
Area: 78.54
def get_circumference(radius):
return 2 * [Link] * radius Circumference: 31.42
Filename: [Link]
from geometry import get_area, get_circumference
# 1. Get the input
r = float(input("Enter the radius: "))
# 2. Calculate using the imported functions
area = get_area(r)
circ = get_circumference(r)
# 3. Print the results
print(f"Area: {area:.2f}")
print(f"Circumference: {circ:.2f}")
How about in Java?
In Java, every file must be a Class, and the file name must match the class name exactly.
1. The Logic File ([Link])
Instead of just defining functions, you define methods inside a class.
public class Geometry {
// We use 'static' so we don't have to create an object to use them
public static double getArea(double radius) {
return [Link] * [Link](radius, 2);
}
public static double getCircumference(double radius) {
return 2 * [Link] * radius;
}
}
2. The Main File ([Link])
In Java, you don't always need an "import" statement if the files are in the same folder (the same "default
package"), but you must refer to the Class name.
import [Link];
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner([Link]);
[Link]("Enter radius: ");
double r = [Link]();
// Accessing the other file: [Link]
double area = [Link](r);
double circ = [Link](r);
[Link]("Area: %.2f\n", area);
[Link]("Circumference: %.2f\n", circ);
}
}
Graded Activity 13 Modular Cylinder Engineering (Python – Data Structures and Algorithms)
Build a calculation engine for 3D cylinders. You will separate the mathematical logic from the user input
using two distinct Python files.
Instructions
Step 1: Create [Link]
In this file, you will write the functions. Use the math module for the value of pi. Ensure your parameter
names match the formulas below.
Function 1: get_total_area(r, h)
Function 2: get_volume(r, h)
Function 3: get_lateral_area(r, h)
Step 2: Create [Link]
Import all three functions from [Link].
Ask the user for the radius and height using float(input()).
Call the functions and store the results.
Print the results using f-strings rounded to 3 decimal places.
Python Programming Formulas
Use these exact expressions inside your functions to ensure accuracy. In Python, **2 is used for squaring
a number.
Property Python Code Formula
Total Surface Area (2 * [Link] * r * h) + (2 * [Link] * r**2)
Volume [Link] * r**2 * h
Lateral Area 2 * [Link] * r * h
Sample Output
ENTER RADIUS: 4
ENTER HEIGHT: 10
--- CYLINDER DATA ---
Total Surface Area: 351.858
Volume: 502.655
Lateral Area: 251.327
Activity 13: Modular Cylinder Engineering (Java – Programming II)
Build a calculation engine for 3D cylinders. You will separate the mathematical logic from the user input
using two distinct Java class files.
Instructions
Step 1: Create [Link]
In this file, you will define the math logic. Use [Link] for the value of $\pi$ and [Link](r, 2) for
squaring the radius.
Method 1: public static double getTotalArea(double r, double h)
Method 2: public static double getVolume(double r, double h)
Method 3: public static double getLateralArea(double r, double h)
Step 2: Create [Link]
Create a Scanner object to accept user input.
Ask the user for the Radius and Height.
Call the methods from the Engine class (Syntax: [Link](r, h)).
Print the results using [Link] rounded to 3 decimal places.
Java Programming Formulas
Property Java Code Formula
Total Surface Area (2 * [Link] * r * h) + (2 * [Link] * [Link](r, 2))
Volume [Link] * [Link](r, 2) * h
Lateral Area 2 * [Link] * r * h
Sample Output
ENTER RADIUS: 4
ENTER HEIGHT: 10
--- CYLINDER DATA ---
Total Surface Area: 351.858
Volume: 502.655
Lateral Area: 251.327
Java Specific Rules for this Activity
1. File Names: Your files must be named [Link] and [Link] exactly (capitalized).
2. The Static Keyword: We use static in the Engine methods so that the Main file can use them
without needing to "create" a new Engine object (e.g., new Engine()).
3. Printing: Use %.3f inside a printf statement to handle the 3-decimal place requirement.
4. Scanner: Don't forget to import [Link]; at the very top of your [Link] file!