Practical Record Book: T.N. Chehul Chinnappa
Practical Record Book: T.N. Chehul Chinnappa
Signature of Student
Test 2 31.07.2021
AVG Marks (out of 15 marks)
THEORY: Over loading allows different methods to have same name and different signature where
signature can differ by number or type of input parameters.
Mathclass class provides several methods to work on math calculations like min(),max(),avg(),sin()etc.
In java array is an object of a dynamically generated class. Java array inherits object class, and implements
the serializable as well as cloneable interface.
CODE:
PROGRAM 1(A)
public class MethodOverloading_arg_datatype {
display("Hello");
}
}
OUTPUT:
Page No: 1
Course Code: 19CSL46
PROGRAM(1B)
public class Mathclass1 {
public static void main(String[] args)
{
double x = 28;
double y = 4;
OUTPUT:
Page No: 2
Course Code: 19CSL46
PROGRAM 1(C)
import java.util.Scanner;
class MatrixMultiplication {
public static void main(String args[]) {
int m, n, p, q, sum = 0, c, d, k;
if (n != p)
System.out.println("The matrices can't be multiplied with each other.");
else {
int second[][] = new int[p][q];
int multiply[][] = new int[m][q];
multiply[c][d] = sum;
sum = 0;
}
}
Page No: 3
Course Code: 19CSL46
System.out.print("\n");
}
}
}
}
OUTPUT: 1
OUTPUT: 2
Page No: 4
Course Code: 19CSL46
PROGRAM NO.: 2.
Program 2
Write a Java Program to define a class, describe its constructor, overload the Constructors and
instantiate its object, and use static members.
AIM: to define a class, describe its constructor, overload the Constructors and instantiate its object, and
use static members.
THEORY: A java class file contains java bytecode that can be executed on the JVM. A java class file used
to produce by compiler.
A constructor is a special method that is called when an object is instantiated. A java class constructor
initializes instances of the class.
CODE:
Program 2 (A):
Write a Java Program to define a class, describe its constructor, overload the Constructors and
instantiate its object.
class Student{
String name,dept;
int id;
double marks;
Student()
{
name="abc";
id=1;
dept="CSE";
marks=70;
}
Student(Student st)
{
name=st.name;
id=st.id;
dept=st.dept;
marks=st.marks;
}
void display(){
System.out.println("Name: "+name+" Id: "+id+" Dept: "+dept+" Marks: "+marks);
}
OUTPUT
Page No: 6
Course Code: 19CSL46
Program 2(B) :
Write a Java Program to define a class, define instance methods for setting and Retrieving values of
instance variables and instantiate its object
class Student
{
String name,dept;
int id;
double marks;
void display(){
System.out.println("Name: "+name+" Id: "+id+" Dept: "+dept+" Marks: "+marks);
}
OUTPUT
Page No: 7
Course Code: 19CSL46
Program 2(C):
Write a Java Program to define a class, describe its constructor, overload the Constructors and
instantiate its object, and use static members.
class Student{
int rollno;
String name;
static String college = "ENGG";
OUTPUT:
Page No: 8
Course Code: 19CSL46
PROGRAM NO.: 3
Write a Java program to demonstrate String class, String Buffer class
and its methods
AIM: To demonstrate String class, String Buffer class and its methods
THEORY: The string class represent strings. All string literals in java programs, such as "abc" are
implemented as instances of the class. Strings are constant, their values cannot be change.
String buffer is a peer class String that provides much of the functionality of strings. String represents fixed-
length, immutable character sequences. StringBuffer may have characters and substrings inserted in the
middle or appended at the end.
CODE:
Program 3(A)
Write a Java program to practice using String class and its methods
str1.concat(str2);
System.out.println(str1);
System.out.println(str1.concat(str2));
}
}
Output
Page No: 9
Course Code: 19CSL46
Program 3(B)
Write a Java program to practice using String Buffer class and its methods
public class Student
{
System.out.println(sb.insert(1, 'i'));
System.out.println(sb.delete(2,5));
System.out.println(sb.deleteCharAt(2));
}
}
Output:
Page No: 10
Course Code: 19CSL46
PROGRAM NO.: 4
: Write a Java program to demonstrate nested classes and array of
Objects
AIM: To demonstrate nested classes and array of Objects
THEORY In java it allows to define a class within another class. Which is known as nested class, is a
member of its enclosing class.
The array of objects as defined its name, stores an array of objects. An object represents a single record in
memory.
void test()
{
Inner in=new Inner();
in.display();
}
class Inner
{
int y=20;
void display()
{
System.out.println("Outer x : " + x);
System.out.println("Inner y : " + y);
}
}}
OUTPUT:
Page No: 11
Course Code: 19CSL46
Program 4(B)
Write a Java Program to implement array of objects
class Student
{
String name,dept;
int id;
double marks;
void display()
{
System.out.println("Name: "+name+" Id: "+id+" Dept: "+dept+" Marks: "+marks);
}
OUTPUT:
Page No: 12
Course Code: 19CSL46
PROGRAM NO.: 5.
Write a Java Program to implement inheritance and demonstrate use of method overriding
AIM: To implement inheritance and demonstrate use of method overriding
THEORY: Inheritance is basically parent and child relation between the classes. There are three types of
inheritance single, multilevel, hierarchical.
If the subclass same method as declared in the parent class, it known as method overriding in java. method
that has been declared by one parent class is known as method overriding
CODE:
Program 5 (a) Single Inheritance
class Employee {
String companyName;
int salary;
Employee(String n,int s) {
companyName=n;
salary=s;
}
void showDetails() {
System.out.println("Employee's company name is: "+companyName+" salary
is: " +salary);
}
void status() {
System.out.println("I am the Parent class:Employee");
}
}
void showDetails() {
super.status();
super.showDetails();
this.status();
totsal=salary+bonus;
System.out.println("Programmer's Company name is: "+companyName+" total salary
is: "+totsal);
}
void status() {
System.out.println("I am the Child class:"+this.getClass().getSimpleName());
}
Output:
Page No: 14
Course Code: 19CSL46
PROGRAM NO.: 6.
Write a Java Program to implement multilevel inheritance by applying various access controls to its
data members and methods
AIM: To implement multilevel inheritance by applying various access controls to its data members and
methods
THEORY: When class extends a class, which extends another class then its is called multilevel inheritance.
So in this case class C is implicitly inheriting the properties and methods of class along with class B what is
called multilevel inheritance.
CODE:
class Person {
private int pwd = 63106; // can be accessed only within class Person
public String name; // can be accessed anywhere
String addr; // default-can be accessed only within the same package
protected String status; // can be accessed within same package or subclass in another pkg
Page No: 15
Course Code: 19CSL46
Student_1(String n, String a, String s, String se, String d, String u) {
super(n, a, s);
sem = se;
dept = d;
usn = u;
}
Marks(String n, String a, String s, String se, String d, String u, String[] su, int[] m) {
super(n, a, s, se, d, u);
sub = su;
marks = m;
}
void total_marks() {
for (int i = 0; i < marks.length; i++)
total = total + marks[i];
}
}
public class MultiLevelAccessCntrldemo_6 {
public static void main(String[] args) {
String[] sub1 = { "Java", "Arm", "Maths" };
int[] marks1 = { 91, 92, 93 };
Marks m1 = new Marks("Deepak", "SubhasNagar", "Student", "4thsem", "CSE", "1NH19CS716", sub
1, marks1);
m1.display();
}
}
OUTPUT:
Page No: 17
Course Code: 19CSL46
PROGRAM NO.: 7.
Write a program to demonstrate use of implementing interfaces
AIM: To demonstrate use of implementing interfaces
THEORY: Interface is an abstract type, which is used to specify behaviour that classes must implement.
They are similar to protocols. Interfaces are declared using the interface key word, and may only contain
method signature
CODE:
package pk1;
interface Printable
{
void print();
}
interface Showable
{
intYear=2020;
void show();
}
}
public void print()
{
System.out.println("Hello World");
}
public void show()
{
System.out.println("Welcome "+Year);
//Year=Year+1;
}
}
OUTPUT:
Page No: 18
Course Code: 19CSL46
PROGRAM NO.: 8.
Write a program to demonstrate use of extending interfaces
AIM: To demonstrate use of extending interfaces
THEORY: And interface can extend another interface in the same way that a class can extend another class.
The extends keyword is used to extend an interface. One interface can extends many interface but a class
implements many classes.
CODE:
interface Printable
{
void print();
}
interface Showable extends Printable
{
void show();
}
OUTPUT:
Page No: 19
Course Code: 19CSL46
PROGRAM NO.: 9.
9(A): Write a Java program to implement the concept of importing classes from user defined package
and creating packages.
AIM: To demonstrate implement the concept of importing classes from user defined package and creating
packages.
THEORY: A java package organizes java classes into namespaces, providing a unique namespace for each
type it contains. Classes in the same package can access each other's package and protected members
CODE:
Package mypack;
package mypack1;
import mypack.*;
Page No: 20
Course Code: 19CSL46
}
package mypack2;
class SimplePack3
{
SimplePack3()
{
SimplePack1 obj3=new SimplePack1();
System.out.println("Default Variable value = "+obj3.var3);
}
OUTPUT:
Page No: 21
Course Code: 19CSL46
PROGRAM NO.: 9.
9(B): A Java Program to demonstrate dynamic binding.
AIM: To demonstrate dynamic binding.
THEORY: In dynamic binding the method call is boned to method body runtime.WHich is also known as
late binding. This is done by using instance methods
CODE:
publicclassDynamicDispatch
{
class Bank
{
intgetRateOfInterest()
{
return 0;
}
}
//Creating child classes.
class SBI extends Bank
{
intgetRateOfInterest()
{
return 8;
}
}
Page No: 22
Course Code: 19CSL46
OUTPUT:
Page No: 23
Course Code: 19CSL46
PROGRAM NO.: 10.
Write a program to implement the concept of threading by extending Thread Class
AIM: To implement the concept of threading by extending Thread Class
THEORY: Extend thread class creates a tread by a new class that extends thread class and create an instance
of the class.
CODE:
import java.lang.Thread;
class NewThread1 extends Thread {
Thread t;
NewThread1() {
t=new Thread(this,"DemoThread");//Constructor Thread(Runnable r,String name)
System.out.println("Child Thread" +t);
t.start();
}
public void run() {
try {
for(int i=5;i>0;i--) {
System.out.println("Child Thread"+i);
Thread.sleep(3500);
}}
catch(InterruptedException e) {
System.out.println("Child Interrupted");
}
System.out.println("Child Thread exiting");
}}
public class NewThread{
public static void main(String[] args) {
new NewThread1();
try {
for(int n=5;n>0;n--){
System.out.println("Main Thread"+n);
Thread.sleep(500);
}}
catch(InterruptedException e) {
System.out.println("Main Thread Interrupted");
}
System.out.println("Main Thread exiting");
}}
OUTPUT:
Page No: 24
Course Code: 19CSL46
PROGRAM NO.: 11.
Write a program to implement the concept of threading by implementing Runnable Interface
AIM: To implement the concept of threading by implementing Runnable Interface
THEORY: The runnable should be implemented by any class whose instance are intended to be executed
by a thread. The class must define a method of no arguments called run.
CODE:
import java.lang.Thread;
OUTPUT:
Page No: 25
Course Code: 19CSL46
PROGRAM NO.: 12.
Write a java program that implements a multi-thread application that has three threads. First thread
generates random integer every 1 second and if the value is even, second thread computes the square
of the number and prints. If the value is odd, the third thread will print the value of cube of the number.
while(true) {
if(num % 2 == 0)
new Square(num).start();
else
new Cube(num).start();
try {
sleep(1000);
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
OUTPUT:
Page No: 29
Course Code: 19CSL46
PROGRAM NO.: 13.
Write a program to implement the concept of Exception Handling
using predefined exception
OUTPUT:
Page No: 30
Course Code: 19CSL46
VoteException(String message) {
super(message);
}
}
class UserDefExcep {
public static void main(String a[]) {
int age;
Scanner in = new Scanner(System.in);
try {
System.out.println("Enter your age:");
age = in.nextInt();
if (age< 18) {
throw new VoteException("You are not eligible to vote! ");
}
else {
System.out.println("You are eligible to vote!");
}
}
catch (VoteException e) {
System.out.println("Caught VoteException");
System.out.println(e.getMessage());
}
}
}
OUTPUTS:
Page No: 31
Course Code: 19CSL46
PROGRAM NO.: 15
CODE:
import java.io.*;
fw.close();
//Writing to input file is over. Hence close FileOutputStream
do {
i = fin.read();
if(i != -1) fout.write(i);
}
while(i != -1);
fout.close();
fin.close();
Page No: 32
Course Code: 19CSL46
//Now read contents of input.txt and output.txt
FileInputStream fin1;
FileInputStream fin2;
fin1.close();
fin2.close();
}
}
OUTPUT:
Page No: 33
Course Code: 19CSL46
PROGRAM NO.: 16
LinkedList is a part of the Collection framework present in java.util package. This class is an
implementation of the LinkedList data structure which is a linear data structure where the elements are not
stored in contiguous locations and every element is a separate object with a data part and address part. The
elements are linked using pointers and addresses.
TreeSet class in java implements the Set interface that uses a tree for storage. It inherits AbstractSet class
and implements the NavigableSet interface. The objects of the TreeSet class are stored in ascending order.
CODE:
import java.util.*;
OUTPUT:
Page No: 35