Practical Assignment-2
1. Write a JAVA program to Implement of class and object.
Program:
class Student{
int id;
String name;
}
class TestStudent3{
public static void main(String args[]){
//Creating objects
Student s1=new Student();
Student s2=new Student();
//Initializing objects
[Link]=101;
[Link]="Sonoo";
[Link]=102;
[Link]="Amit";
//Printing data
[Link]([Link]+" "+[Link]);
[Link]([Link]+" "+[Link]);
}
}
Output:
101 Sonoo
102 Amit
2. Write a JAVA program to Implement of data types, variables, local
variables, and static variables.
Program:
public class VariablesDemo {
// Static variable
static int staticVariable = 50;
// Instance variable
int instanceVariable;
// Constructor to initialize the instance variable
public VariablesDemo(int instanceVariable) {
[Link] = instanceVariable;
}
// Method to demonstrate local variables
public void demonstrateLocalVariable() {
// Local variable
int localVariable = 10;
[Link]("Local Variable: " + localVariable);
}
// Method to display instance and static variables
public void displayVariables() {
[Link]("Instance Variable: " + instanceVariable);
[Link]("Static Variable: " + staticVariable);
}
// Main method
public static void main(String[] args) {
// Creating an object of VariablesDemo
VariablesDemo demo1 = new VariablesDemo(20);
VariablesDemo demo2 = new VariablesDemo(30);
// Displaying variables for demo1
[Link]();
// Displaying variables for demo2
[Link]();
// Demonstrating local variable
[Link]();
// Modifying static variable
[Link] = 100;
// Displaying variables again to see the effect of static variable modification
[Link]();
[Link]();
}
}
Output:
Instance Variable: 20
Static Variable: 50
Instance Variable: 30
Static Variable: 50
Local Variable: 10
Instance Variable: 20
Static Variable: 100
Instance Variable: 30
Static Variable: 100
3. Write a JAVA program using Looping statements, conditional
statements.
Program:
import [Link];
public class NumberOperations {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
// Take input from the user
[Link]("Enter a positive integer: ");
int number = [Link]();
// Check if the number is even or odd using if-else
if (number % 2 == 0) {
[Link](number + " is even.");
} else {
[Link](number + " is odd.");
}
// Print the multiplication table using a for loop
[Link]("Multiplication table of " + number + ":");
for (int i = 1; i <= 10; i++) {
[Link](number + " x " + i + " = " + (number * i));
}
// Find and print the factorial using a while loop
int factorial = 1;
int tempNumber = number;
while (tempNumber > 0) {
factorial *= tempNumber;
tempNumber--;
}
[Link]("Factorial of " + number + " is " + factorial);
// Print a message based on the value of the number using switch
switch (number) {
case 1:
[Link]("You entered one.");
break;
case 2:
[Link]("You entered two.");
break;
case 3:
[Link]("You entered three.");
break;
default:
[Link]("You entered a number greater than three.");
break;
}
// Close the scanner
[Link]();
}
}
Output:
Enter a positive integer: 5
5 is odd.
Multiplication table of 5:
5x1=5
5 x 2 = 10
5 x 3 = 15
5 x 4 = 20
5 x 5 = 25
5 x 6 = 30
5 x 7 = 35
5 x 8 = 40
5 x 9 = 45
5 x 10 = 50
Factorial of 5 is 120
You entered a number greater than three.
4. Write a JAVA program to Implement user-defined methods, instance
method, static method.
Program:
public class MathOperations {
// Instance method to add two numbers
public int add(int a, int b) {
return a + b;
}
// Static method to multiply two numbers
public static int multiply(int a, int b) {
return a * b;
}
// User-defined method to demonstrate instance and static methods
public void demonstrateMethods() {
int num1 = 5;
int num2 = 3;
// Calling the instance method
int sum = add(num1, num2);
[Link]("Sum (using instance method): " + sum);
// Calling the static method
int product = multiply(num1, num2);
[Link]("Product (using static method): " + product);
}
// Main method
public static void main(String[] args) {
// Create an object of MathOperations
MathOperations mathOps = new MathOperations();
// Call the user-defined method to demonstrate instance and static methods
[Link]();
}
}
Output:
Sum (using instance method): 8
Product (using static method): 15
5. Write a JAVA program to Implement single dimensional array,
multidimensional array.
Program:
public class ArraysDemo {
public static void main(String[] args) {
// Single-dimensional array
int[] singleDimArray = {1, 2, 3, 4, 5};
// Print elements of single-dimensional array
[Link]("Single-dimensional array elements:");
for (int i = 0; i < [Link]; i++) {
[Link]("Element at index " + i + ": " + singleDimArray[i]);
}
// Multi-dimensional array (2D array)
int[][] multiDimArray = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
// Print elements of multi-dimensional array
[Link]("\nMulti-dimensional array elements:");
for (int i = 0; i < [Link]; i++) {
for (int j = 0; j < multiDimArray[i].length; j++) {
[Link]("Element at [" + i + "][" + j + "]: " + multiDimArray[i][j]);
}
}
}
}
Output:
Single-dimensional array elements:
Element at index 0: 1
Element at index 1: 2
Element at index 2: 3
Element at index 3: 4
Element at index 4: 5
Multi-dimensional array elements:
Element at [0][0]: 1
Element at [0][1]: 2
Element at [0][2]: 3
Element at [1][0]: 4
Element at [1][1]: 5
Element at [1][2]: 6
Element at [2][0]: 7
Element at [2][1]: 8
Element at [2][2]: 9