1.
Data types available in Java
Aim:
To understand and demonstrate the use of different data types available in Java
Algorithm:
1. Start the program
2. Declare variables for each primitive data type
3. Assign values to each variable
4. Declare a variable for a reference data type (e.g., String)
5. Print the value of each variable
6. End the program
Program:
package oopsfile;
public class Program1
{
public static void main(String args[])
{
int a;
char
c;
float
b;
double d;
boolean
f; a=10;
c='A';
b=25.63f;
d=12.698
f;
f=true;
[Link]("a="+a);
[Link]("b="+b);
[Link]("c="+c);
[Link]("d="+d);
[Link]("f="+f);
}
}
OOPs Lab Manual 1
Output
2. Armstrong number or not.
Aim: To test whether the given number is Armstrong number or not in java.
Algorithm:
Step 1: Class: Armstrong Step 2:
variable s=0,k,n Step 3: while n>0 {
Step 4: s=s+(n%10)*(n%10)*(n%10); step 5:
n/=10; }
step 6: if(k==s)
1) k is an armstrong number (or)
2) k is not an armstrong number Step 7:
Stop.
OOPs Lab Manual 2
Program:
import [Link].*;
class Ams
{
static public void main(String [] args)
{
int s=0;
Scanner s=new Scanner([Link]);
[Link]("Enter the number");
int n=[Link]();
int k=n;
while(n>0)
{
s=s+(n%10)*(n%10)*(n%10);
n/=10;
}
if(k==s)
[Link]("The given number "+k+" is an armstrong number");
else
[Link]("The given number "+k+" is not an armstrong number");
}
}
Output:
Enter the number
153
The given number 153 is an armstrong number
OOPs Lab Manual 3
3. Classes and Objects
Aim: Write a Program in Java to implement the Classes and Objects.
Algorithm:
STEP 1: Start the Program
STEP 2: Create Class
STEP 3: Declare the Input and Output Variables
STEP 3: Create object and access the method
STEP 4: Implement it with return type and without parameter list
STEP 5: Implement it with return type and with parameter list
STEP 6: Implement the constructor by creating classes and objects
OOPs Lab Manual 4
Classes and Objects
Program:
class Student
{
int id;
String name;
}
class TestStudent3
{
public static void main(String args[])
{
Student s1=new Student();
Student s2=new Student();
[Link]=101;
[Link]="Sonoo";
[Link]=102; [Link]="Amit";
[Link]([Link]+" "+[Link]);
[Link]([Link]+" "+[Link]);
}
}
Output:
101 Sonoo
102 it
4. Multidimensional arrays and looping constructs
Aim
:
To write a java program to demonstrate the use of multidimensional arrays and looping
constructs.
Algorithm:
Step 1 : Open Notepad
Step 2 : import all packages
Step 3 : Get the row size and column size of first matrix
Step 4 : Get the values of first matrix
Step 5 : Get the row size and column size of second matrix
Step 6 : Check the number of columns in the 1st matrix is equal to the
number of rows in the 2nd matrix.
Step 7 : if both are equal then multiply the elements of each row of the first
matrix by the elements of each column in the second matrix. Add the
products.
OOPs Lab Manual 5
Step 8 : Otherwise multiplication not possible.
Step 9 : Display the output
Step 10: Terminate the program
OOPs Lab Manual 6
Multidimensional arrays and looping constructs
Program :
import [Link];
class Matrixmul
{
public static void main(String args[])
{
int m, n, p, q, sum = 0, i, j, k;
Scanner in = new Scanner([Link]);
[Link]("Enter the number of rows and columns of first matrix");
m = [Link]();
n = [Link]();
int first[][] = new int[m][n];
[Link]("Enter elements of first matrix");
for (i = 0; i < m; i++)
for (j = 0; j < n; j++)
first[i][j] = [Link]();
[Link]("Enter the number of rows and columns of second matrix");
p = [Link]();
q = [Link]();
if (n != p)
[Link]("The matrices can't be multiplied with each other.");
else
{
int second[][] = new int[p][q];
int multiply[][] = new int[m][q];
[Link]("Enter elements of second matrix");
for (i = 0; i < p; i++)
for (j = 0; j < q; j++)
second[i][j] = [Link]();
for (i = 0; i < m; i++)
{
for (j = 0; j < q; j++)
{
for (k = 0; k < p; k++)
sum = sum + first[i][k]*second[k][j]; multiply[i]
[j] = sum;
sum = 0;
}
}
OOPs Lab Manual 7
[Link]("Product of the matrices:");
for (i = 0; i < m; i++)
{
for (j = 0; j < q; j++)
[Link](multiply[i][j]+"\t");
[Link]("\n");
}
}
}
}
OOPs Lab Manual 8
Output :
Result :
Thus the java program using multidimensional array and looping construct were written and output
verified successfully.
OOPs Lab Manual 9
5. Simple calculator using switch case statement
Aim: To implement calculator program using switch case statement
Algorithm:
Step 1:Start the program
Step 2: Read the numbers
Step 3:Switch-case on the operator:
Case '+':
Calculate result = num1 + num2
Case '-':
Calculate result = num1 - num2
Case '*':
Calculate result = num1 * num2
Case '/':
Check if num2 != 0
o If yes, calculate result = num1 / num2
o Else, print error "Division by zero is not allowed"
Default:
Print error "Invalid operator"
Step 4: If valid operation, print the result
Step 5: End
Program:
package oopsfile;
import [Link];
public class Program2 {
public static void main(String[] args) {
// TODO Auto-generated method stub
[Link]("Press 1: Addition");
[Link]("Press 2: Subtraction");
[Link]("Press 3: Multiply");
[Link]("Press 4: Division");
int a,b,c;
int choice;
OOPs Lab Manual 10
[Link]("Enter your choice");
Scanner sc=new Scanner([Link]);
choice=[Link]();
[Link]("Enter First Number");
a=[Link]();
[Link]("Enter Second
Number"); b=[Link]();
switch (choice)
{
case 1:
c=a+b;
[Link]("Addition is "+c);
break;
case 2:
c=a-b;
[Link]("Subtraction is "+c);
break;
case3:
c=a*b;
[Link]("Multiplication is "+c);
break;
case 4:
c=a/b;
[Link]("Division is "+c);
break;
default:
[Link]("Bad Choice");
break;
}
}
}
Output
OOPs Lab Manual 11
OOPs Lab Manual 12
6. METHOD OVERLOADING AND CONSTRUCTOR OVERLOADING
Aim:
To demonstrate method overloading in Java by creating multiple methods with the same
Algorithm:
1. Start the program
2. Define a class with a method name, e.g., add.
3. Create multiple versions of the method add with different parameter lists, for example:
o add(int a, int b)
o add(double a, double b)
o add(int a, int b, int c)
4. Inside each method, perform addition and return or print the result.
5. In the main method:
o Call add with different arguments to invoke different overloaded methods.
6. Observe that the appropriate method is called based on the arguments' types and count.
7. End the program
Program:
class DisplayOverloading
{
//adding two integer numbers int add(int a, int b)
{
int sum
= a+b;
return
sum;
}
//adding three integer
numbers int add(int a,
int b, int c)
{
int sum
= a+b+c;
return
sum;
}
}
class JavaExample
{
OOPs Lab Manual 13
public static void main(String args[])
{
DisplayOverloading obj = new
DisplayOverloading();
[Link]([Link](10, 20));
[Link]([Link](10, 20, 30));
}
}
Output:
30
60
7. Constructor Overloading
Aim: To demonstrate constructor overloading in Java by defining multiple constructors with different
parameter lists in a class
Algorithm:
1. Start
2. Define a class, for example, Student.
3. Declare instance variables inside the class (e.g., id, name).
4. Create multiple constructors inside the class
5. Inside each constructor, assign parameter values to instance variables.
6. Create a method (optional) to display the values of the instance variables.
7. In the main method:
Create objects of the class using different constructors.
Call the display method or print the instance variables to verify initialization.
8. End
Program:
public class Student {
//instance variables of the class
int id;
String name;
Student(){
[Link]("this a default constructor");
}
OOPs Lab Manual 14
Student(int i,
String n){ id =
i;
name = n;
}
public static void main(String[] args) {
//object creation
Student s = new Student();
[Link]("\nDefault Constructor
values: \n");
[Link]("Student Id : "+[Link] + "\nStudent Name : "+[Link]);
[Link]("\nParameterized Constructor values: \n");
Student student = new Student(10, "David");
[Link]("Student Id : "+[Link] + "\nStudent Name : "+[Link]);
}
}
Output:
this a default constructor
Default Constructor values:
Student Id : 0
Student Name : null
Parameterized Constructor values:
Student Id : 10
Student Name : David
OOPs Lab Manual 15