JAVA PRACTICAL FILE — BASIC ELEMENTS OF JAVA (Class 9)
1. Class
A class is the basic building block of Java. It groups variables and methods together.
Use: Every Java program must have at least one class.
Code:
class ClassExample {
public static void main(String[] args) {
[Link]("This is a simple class example.");
}
}
Explanation: The class defines structure; main() executes the code.
2. main() Method
Definition: The entry point of every Java program.
Use: Java starts executing from this method.
Code:
class MainExample {
public static void main(String[] args) {
[Link]("Program started from main method!");
}
}
Explanation: JVM always looks for main() to start execution.
3. Variables
Definition: Containers that store data.
Use: To hold data temporarily during program execution.
Code:
class VariableExample {
public static void main(String[] args) {
int age = 15;
String name = "Ravi";
[Link](name + " is " + age + " years old.");
}
}
Explanation: Variables store different data types like numbers and text.
4. Data Types
Definition: Define the kind of value a variable can hold.
Use: Tell compiler what data is stored.
Code:
class DataTypeExample {
public static void main(String[] args) {
int rollNo = 12;
float marks = 92.5f;
char grade = 'A';
boolean passed = true;
String name = "Ananya";
[Link]("Roll No: " + rollNo);
[Link]("Marks: " + marks);
[Link]("Grade: " + grade);
[Link]("Passed: " + passed);
[Link]("Name: " + name);
}
}
Explanation: Different data types hold specific types of values.
5. Operators
Definition: Symbols used to perform operations.
Use: For calculations and logic.
Code:
class OperatorExample {
public static void main(String[] args) {
int a = 10, b = 3;
[Link]("Sum: " + (a + b));
[Link]("Difference: " + (a - b));
[Link]("Product: " + (a * b));
[Link]("Division: " + (a / b));
[Link]("Remainder: " + (a % b));
[Link]("Is a greater than b? " + (a > b));
}
}
Explanation: Arithmetic and comparison operations are demonstrated.
6. Input and Output
Definition: Input takes user data; output displays result.
Use: For user interaction.
Code:
import [Link];
class InputOutputExample {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter your name: ");
String name = [Link]();
[Link]("Enter your age: ");
int age = [Link]();
[Link]("Hello " + name + ", you are " + age + " years old.");
[Link]();
}
}
Explanation: Scanner reads input; println prints output.
7. Conditional Statements (if–else)
Definition: Used to make decisions.
Use: Executes different code based on condition.
Code:
class IfElseExample {
public static void main(String[] args) {
int marks = 75;
if (marks >= 40)
[Link]("Pass");
else
[Link]("Fail");
}
}
Explanation: If condition is true, it prints Pass; otherwise Fail.
8. Loops
Definition: Used to repeat a block of code.
Use: Reduces repetition.
Code:
class LoopExample {
public static void main(String[] args) {
for (int i = 1; i <= 5; i++) {
[Link]("Count: " + i);
}
}
}
Explanation: For loop runs code 5 times.
9. Methods (Functions)
Definition: Reusable block of code.
Use: Helps organize and reuse logic.
Code:
class MethodExample {
static void greet() {
[Link]("Hello! Welcome to Java.");
}
public static void main(String[] args) {
greet();
}
}
Explanation: Method greet() is defined once and can be reused.
10. Arrays
Definition: Store multiple values of same type.
Use: Manage lists of data.
Code:
class ArrayExample {
public static void main(String[] args) {
int marks[] = {85, 90, 75, 88, 92};
[Link]("Student Marks:");
for (int i = 0; i < [Link]; i++) {
[Link]("Subject " + (i+1) + ": " + marks[i]);
}
}
}
Explanation: Loop prints all values of the array.
11. Comments
Definition: Notes ignored by compiler.
Use: Explain code for humans.
Code:
class CommentExample {
public static void main(String[] args) {
// This is a single-line comment
/* This is a
multi-line comment */
[Link]("Comments are ignored by the compiler!");
}
}
Explanation: Comments are not executed by the compiler.
12. Import Statement
Definition: Used to include Java libraries.
Use: Allows using Scanner, Math, etc.
Code:
import [Link];
class ImportExample {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter a number: ");
int n = [Link]();
[Link]("You entered: " + n);
[Link]();
}
}
Explanation: import brings external Java classes into use.
Final Combined Program: Student Marks Analyzer
Topic: A program that takes a student’s name and marks, finds the average, and prints the
grade.
Code:
import [Link];
class StudentMarksAnalyzer {
static String findGrade(double avg) {
if (avg >= 90) return "A+";
else if (avg >= 75) return "A";
else if (avg >= 60) return "B";
else if (avg >= 40) return "C";
else return "Fail";
}
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter Student Name: ");
String name = [Link]();
[Link]("Enter number of subjects: ");
int n = [Link]();
int marks[] = new int[n];
int sum = 0;
for (int i = 0; i < n; i++) {
[Link]("Enter marks for subject " + (i+1) + ": ");
marks[i] = [Link]();
sum += marks[i];
}
double avg = (double)sum / n;
String grade = findGrade(avg);
[Link]("\n--- Student Report ---");
[Link]("Name: " + name);
[Link]("Average Marks: " + avg);
[Link]("Grade: " + grade);
[Link]();
}
}
Explanation: Combines all Java basics — class, methods, arrays, loops, conditions,
input/output, and variables.