Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
3 views

Practical Assignment 2

The document contains multiple Java programming assignments demonstrating various concepts such as class and object implementation, data types, variables, looping and conditional statements, user-defined methods, and array usage. Each section includes a program example along with its output, showcasing practical applications of Java programming. The assignments cover creating classes, using static and instance variables, performing arithmetic operations, and handling arrays.

Uploaded by

anshpatel1093
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Practical Assignment 2

The document contains multiple Java programming assignments demonstrating various concepts such as class and object implementation, data types, variables, looping and conditional statements, user-defined methods, and array usage. Each section includes a program example along with its output, showcasing practical applications of Java programming. The assignments cover creating classes, using static and instance variables, performing arithmetic operations, and handling arrays.

Uploaded by

anshpatel1093
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

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
s1.id=101;
s1.name="Sonoo";
s2.id=102;
s2.name="Amit";
//Printing data
System.out.println(s1.id+" "+s1.name);
System.out.println(s2.id+" "+s2.name);
}
}
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) {
this.instanceVariable = instanceVariable;
}

// Method to demonstrate local variables


public void demonstrateLocalVariable() {
// Local variable
int localVariable = 10;
System.out.println("Local Variable: " + localVariable);
}

// Method to display instance and static variables


public void displayVariables() {
System.out.println("Instance Variable: " + instanceVariable);
System.out.println("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


demo1.displayVariables();

// Displaying variables for demo2


demo2.displayVariables();

// Demonstrating local variable


demo1.demonstrateLocalVariable();

// Modifying static variable


VariablesDemo.staticVariable = 100;
// Displaying variables again to see the effect of static variable modification
demo1.displayVariables();
demo2.displayVariables();
}
}
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 java.util.Scanner;

public class NumberOperations {

public static void main(String[] args) {


Scanner scanner = new Scanner(System.in);

// Take input from the user


System.out.print("Enter a positive integer: ");
int number = scanner.nextInt();

// Check if the number is even or odd using if-else


if (number % 2 == 0) {
System.out.println(number + " is even.");
} else {
System.out.println(number + " is odd.");
}

// Print the multiplication table using a for loop


System.out.println("Multiplication table of " + number + ":");
for (int i = 1; i <= 10; i++) {
System.out.println(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--;
}
System.out.println("Factorial of " + number + " is " + factorial);

// Print a message based on the value of the number using switch


switch (number) {
case 1:
System.out.println("You entered one.");
break;
case 2:
System.out.println("You entered two.");
break;
case 3:
System.out.println("You entered three.");
break;
default:
System.out.println("You entered a number greater than three.");
break;
}

// Close the scanner


scanner.close();
}
}
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);
System.out.println("Sum (using instance method): " + sum);

// Calling the static method


int product = multiply(num1, num2);
System.out.println("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


mathOps.demonstrateMethods();
}
}

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


System.out.println("Single-dimensional array elements:");
for (int i = 0; i < singleDimArray.length; i++) {
System.out.println("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


System.out.println("\nMulti-dimensional array elements:");
for (int i = 0; i < multiDimArray.length; i++) {
for (int j = 0; j < multiDimArray[i].length; j++) {
System.out.println("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

You might also like