Java Record Final
Java Record Final
- F/ TL/ 021
Rev.00 Date 20.03.2020
RECORD NOTEBOOK
DEPARTMENT
Of
COMPUTER SCIENCE AND ENGINEERING
BONAFIDE CERTIFICATE
REGISTER NO : 211061101314
NAME OF LAB : BCS18ET1 – JAVA PROGRAMMING
DEPARTMENT : COMPUTER SCIENCE AND ENGINEERING
Certified that, this Record note book is a bonafide record of work done by
NITISH KUMAR of II Year B.Tech / CSE, Sec- ‘F’ in the JAVA PROGRAMMING
LAB during the year 2022-2023.
7 Swap Numbers
ADD TO NUMBERS
AIM:-
ALGORITHM:-
1. Start
2. Declare variables num1, num2, and sum.
3. Initialize num1 to 5 and num2 to 15.
4. Add num1 and num2, storing the result in sum.
5. Print the sum of num1 and num2.
6. End
PROGRAM:-
Class AddTwoNumbers
{
Public static void main(String[]args)
{
int num1=5,num2=15,sum;
sum=num1+num2;
System.out.println("Sum of these numbers:"+sum);
}
}
OUTPUT:-
RESULT:-
AIM:-
ALGORITHM:-
PROGRAM:-
class Simple
{
public static void main(String[]args)
{
System.out.println("HelloWorld!");
}
}
OUTPUT:-
RESULT:-
AIM:-
To write a java program to get the Fibonacci series using for loop.
ALGORITHM:-
1. Start
2. Declare variables count, num1, and num2.
3. Initialize count to 7, num1 to 0, and num2 to 1.
4. Print "Fibonacci Series of 7 numbers:" to the console.
5. Start a loop from i=1 to count (inclusive).
6. Inside the loop:
- Print the current value of num1.
- Calculate the sum of the previous two numbers and store it in sumOfPrevTwo.
- Update num1 to the value of num2.
- Update num2 to the value of sumOfPrevTwo.
7. End the loop.
8. End.
PROGRAM:-
RESULT:-
AIM:-
To write a java program to get the Fibonacci series using while loop.
ALGORITHM:-
PROGRAM:-
RESULT:-
AIM:-
To write a java program to get the factorial number using for loop.
ALGORITHM:-
PROGRAM:-
RESULT:-
AIM:-
To write a java program to get the factorial number using while loop.
ALGORITHM:-
PROGRAM:-
RESULT:-
AIM:-
ALGORITHM:-
PROGRAM:-
RESULT:-
SWAP ELEMENTS
AIM:-
ALGORITHM:-
1. Start
2. Define a public class named "SwapElementsExample".
3. Define the main method inside the class.
Inside the main method:
- Declare and initialize two integer variables num1 and num2 with values 10 and 20 respectively.
- -Print "Before Swapping" to the console.
-Print the value of num1 to the console.
-Print the value of num2 to the console.
-Call the swap method with arguments num1 and num2.
4. Define a private static method named "swap" with two integer parameters num1 and num2.
5. Inside the swap method:
-Declare an integer variable temp and assign it the value of num1.
-Assign the value of num2 to num1.
–Assign the value of temp to num2.
–Print "After Swapping" to the console.
-Print the value of num1 to the console.
-Print the value of num2 to the console.
6. End.
PROGRAM:-
num1=num 2;
num2=temp;
System.out.println("After Swapping");
System.out.println("Value of num1is:"+num1);
System.out.println("Value of num2is: "+num2);
}
}
OUTPUT:-
RESULT:-
PRIME NUMBERS
AIM:-
ALGORITHM:-
1. Start
2. Define a public class named "PrimeNumbers".
3. Define the main method inside the class. Inside the main method:
-Declare and initialize two integer variables i and num to 0.
-Declare an empty string variable named primeNumbers to store prime numbers.
– Start a for loop with i initialized to 1 and loop until it reaches 100.
4. Declare an integer variable counter and initialize it to 0.
5. Start a nested for loop with num initialized to i and loop until it's greater than or equal to 1.
6. Check if i modulo num equals 0.
7. If true, increment counter by 1. Check if counter equals 2.
8. If true, concatenate i with a space to the primeNumbers string.
9. Print "Prime numbers from 1 to 100 are:" to the console.
10. Print the primeNumbers string to the console.
11. End.
PROGRAM:-
Class PrimeNumbers
{
Public static void main(String[]args)
{
int i=0,num=0;
String primeNumbers = "";
for(i=1;i<=100;i++)
{
int counter=0;
for(num =i; num>=1; num--)
{
if(i%num==0)
counter= counter+1;
}
if (counter==2)
{
primeNumbers=primeNumbers+i+" ";
}
}
System.out.println("Primenumbersfrom1to100are:");
System.out.println(primeNumbers);
}
}
OUTPUT:-
RESULT:-
AIM:-
ALGORITHM:-
1. Start
2. Define a public class named "SumOfArray".
3. Define the main method inside the class.
4. Inside the main method:
- Declare an integer array named "array" containing elements 10, 20, 30, 40, 50, and 10.
- Declare an integer variable "sum" and initialize it to 0.
- Start a for-each loop to iterate over each element "num" in the array.
- Add the value of "num" to the "sum" variable.
- Print "Sum of array elements is: " followed by the value of "sum" to the console.
5. End.
PROGRAM:-
Class SumOfArray
{
Public static void main(String args[])
{
int[]array={10,20,30,40,50,10};
int sum=0;
for (int num:array)
{
sum=sum+num;
}
System.out.println("Sumofarrayelementsis:"+sum);
}
}
OUTPUT:-
RESULT:-
AIM:-
ALGORITHM:-
1. Start
2. Define a class named "Student" with two instance variables:
- "id" of type integer to store student ID.
- "name" of type String to store student name.
3. Define another class named "TestStudent3".
4. Define the main method inside the "TestStudent3" class.
5. Inside the main method:
- Create two instances of the "Student" class named "s1" and "s2".
- Assign values to the "id" and "name" attributes of each student:
- For "s1", set id as 101 and name as "Sonoo".
- For "s2", set id as 102 and name as "Amit".
6. Print the ID and name of each student to the console using println method:
- Print "s1.id" followed by "s1.name" with no space in between.
- Print "s2.id" followed by "s2.name" with no space in between.
7. End.
PROGRAM:-
Class Student
{
int id;
String name;
}
classTestStudent3
{
Public static void main(String args[])
{
Students1=newStudent();
Students2=new Student();
s1.id=101;
s1.name="Sonoo";
s2.id=102;
s2.name="Amit";
System.out.println(s1.id+""+s1. name);
System.out.println(s2.id+""+s2.name);
}
}
OUTPUT:-
RESULT:-
AIM:-
ALGORITHM:-
1. Start
2. Define a class named "Student" with two instance variables:
- "rollno" of type integer to store the roll number.
- "name" of type String to store the name.
3. Define two methods inside the "Student" class:
- "insertRecord" method with two parameters "r" (roll number) and "n" (name):
- Assign the value of "r" to the "rollno" attribute.
- Assign the value of "n" to the "name" attribute.
- "displayInformation" method:
- Print the value of "rollno" followed by a space and then the value of "name" to the console.
4. Define another class named "TestStudent4".
5. Define the main method inside the "TestStudent4" class.
6. Inside the main method:
- Create two instances of the "Student" class named "s1" and "s2".
- Call the "insertRecord" method for "s1" with arguments 111 and "Karan".
- Call the "insertRecord" method for "s2" with arguments 222 and "Aryan".
- Call the "displayInformation" method for both "s1" and "s2".
7. End.
PROGRAM:-
class Student
{
int rollno;
String name;
void insert Record(int r, String n)
{
rollno=r; name=n;
}
void displayInformation()
{
System.out.println(rollno+" "+name);
}
}
class TestStudent4
{
public static void main(String args[])
{
Student s1=new Student();
Student s2=new Student();
s1.insertRecord(111,"Karan");
s2.insertRecord(222,"Aryan");
s1.displayInformation();
s2.displayInformation();
}
}
OUTPUT:-
RESULT:-
SINGLE INHERITANCE
AIM:-
ALGORITHM:-
1. Start
2. Define a class named "Animal".
3. Inside the "Animal" class:
-Define a method named "eat" with no parameters:
- Print "eating..." to the console.
4. Define a class named "Dog" which extends the "Animal" class.
5. Inside the "Dog" class:
- Define a method named "bark" with no parameters:
- Print "barking..." to the console.
6. Define another class named "TestInheritance".
7. Inside the "TestInheritance" class:
- Create an instance of the "Dog" class named "d".
- Call the "bark" method using the instance "d".
- Call the "eat" method using the instance "d".
8. End.
PROGRAM:-
Class Animal
{
Void eat()
{
System.out.println("eating...");
}
}
Class Dog extends Animal
{
Void bark()
{
System.out.println("barking...");
}
}
Class TestInheritance
{
Pubic static void main(String args[])
{
Dog d=new Dog();
d.bark();
d.eat();
}
}
OUTPUT:-
RESULT:-
MULTILEVEL INHERITANCE
AIM:-
ALGORITHM:-
1. Start
2. Define a class named "Animal".
3. Inside the "Animal" class:
- Define a method named "eat" with no parameters:
- Print "eating..." to the console.
4. Define a class named "Dog" which extends the "Animal" class.
5. Inside the "Dog" class:
- Define a method named "bark" with no parameters:
- Print "barking..." to the console.
6. Define a class named "BabyDog" which extends the "Dog" class.
7. Inside the "BabyDog" class:
- Define a method named "weep" with no parameters:
- Print "weeping..." to the console.
8. Define another class named "TestInheritance2".
9. Inside the "TestInheritance2" class:
- Create an instance of the "BabyDog" class named "d".
- Call the "weep" method using the instance "d".
- Call the "bark" method using the instance "d".
- Call the "eat" method using the instance "d".
10. End.
PROGRAM:-
Class Animal
{
Void eat()
{
System.out.println("eating...");
}
}
Class Dog extends Animal
{
void bark()
{
System.out.println("barking...");
}
}
Class BabyDog extends Dog
{
Void weep()
{
System.out.println("weeping...");
}
}
classTestInheritance2
{
Public static void main(String args[])
{
BabyDog d=new BabyDog();
d.weep();
d.bark();
d.eat();
}
}
OUTPUT:-
RESULT:-
THREADS
AIM:-
ALGORITHM:-
1. Start
2. Define a class named "Multi3" which implements the "Runnable" interface.
3. Inside the "Multi3" class:
- Implement the "run" method required by the "Runnable" interface:
- Print "thread is running..." to the console.
4. Define the main method inside the "Multi3" class.
5. Inside the main method:
- Create an instance of the "Multi3" class named "m1".
- Create a new thread object named "t1" passing "m1" as a parameter to its constructor.
- Start the thread "t1" by invoking the "start" method.
6. End.
PROGRAM:-
{
Public void run()
{
System.out.println("thread is running...");
}
Public static void main(String args[])
{
Multi3m1=newMulti3();
Thread t1=new Thread(m1);
t1.start();
}
}
OUTPUT:-
RESULT:-
MULTIPLE THREADING
AIM:-
ALGORITHM:-
1. Start
2. Define a class named "MultithreadingDemo" which extends the "Thread" class.
3. Inside the "MultithreadingDemo" class Override the "run" method:
- Inside a try block:
- Print "Thread" followed by the ID of the current thread and "is running" to the console.
- Inside the catch block:
- Print "Exception is caught" to the console if any exception occurs.
4. Define another class named "Multithread".
5. Inside the "Multithread" class, define the main method.
6. Inside the main method:
- Declare an integer variable "n" and initialize it with the value 8.
- Start a loop from 0 to 7 (inclusive) using a for loop:
- Create an instance of the "MultithreadingDemo" class named "object".
- Start the thread by calling the "start" method on the "object".
7. End.
PROGRAM:-
RESULT:-
AIM:-
ALGORITHM:-
PROGRAM:-
class TestDeadlockExample1
{
public static void main(String[] args)
{
final String resource1 = "ratanjaiswal";
final String resource2 = "vimaljaiswal";
Thread t1 = new Thread()
{
public void run()
{
synchronized (resource1)
{
System.out.println("Thread 1: locked resource 1"); try
{
Thread.sleep(100);
}
catch (Exception e){} synchronized (resource2)
{
System.out.println("Thread 1: locked resource 2");
}
}
}
};
Thread t2 = new Thread()
{
public void run()
{
synchronized (resource2)
{
System.out.println("Thread 2: locked resource 2"); try
{
Thread.sleep(100);
}
catch (Exception e) {} synchronized (resource1)
{
System.out.println("Thread 2: locked resource 1");
}
}
}
};
t1.start();
t2.start();
}
}
OUTPUT:-
RESULT:-
AIM:-
ALGORITHM:-
PROGRAM:-
import java.io.FileInputStream;
public class DataStreamExample
{
public static void main(String args[])
{
int i=0; try
{
FileInputStream fin=new FileInputStream("D:\\testout.txt"); while((i=fin.read())!=-1)
{
System.out.print((char)i);
}
fin.close();
}
catch(Exception e)
{
System.out.println(e);
}
}
}
OUTPUT:-
RESULT:-
AIM:-
ALGORITHM:-
PROGRAM:-
import java.io.FileOutputStream;
public class FileOutputStreamExample
{
public static void main(String args[])
{
fout.write(65);
fout.close();
try
{
FileOutputStream fout=new FileOutputStream("D:\\testout.txt");
System.out.println("success...");
}
catch(Exception e)
{
System.out.println(e);
}
}
}
OUTPUT:-
RESULT:-
CHARACTER STREAM
AIM:-
ALGORITHM:-
1. Start
2. Import the necessary package `java.io.FileInputStream`.
3. Define a public class named "DataStreamExample".
4. Inside the "DataStreamExample" class, define the main method.
5. Inside the main method:
6.Open a try block. Inside the try block
- Create a new instance of `FileInputStream` named "fin" and pass the file path "D:\\
testout.txt" to its constructor.
- Call the `read` method on "fin" to read a byte from the file and store it in integer variable "i".
- Print the character representation of the byte read using `(char)i` to the console.
- Close the FileInputStream object "fin" by calling the `close` method.
7. Catch any exceptions that might occur and print the exception message to the console.
8. End.
PROGRAM:-
import java.io.FileInputStream;
public class DataStreamExample
{
public static void main(String args[])
{
try
{
FileInputStream fin=new FileInputStream("D:\\testout.txt");
int i=fin.read();
System.out.print((char)i);
fin.close();
}
catch(Exception e)
{
System.out.println(e);
}
}
}
OUTPUT:-
RESULT:-