0% found this document useful (0 votes)
7 views18 pages

Java Lab Manual

The document is a lab manual for a Java course at Takshshila Institute of Engineering & Technology, detailing various programming experiments. It includes ten programs demonstrating key Java concepts such as variable scope, inheritance, encapsulation, constructors, access specifiers, polymorphism, and exception handling. Each program is accompanied by code snippets and expected outputs.

Uploaded by

aatul6419
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views18 pages

Java Lab Manual

The document is a lab manual for a Java course at Takshshila Institute of Engineering & Technology, detailing various programming experiments. It includes ten programs demonstrating key Java concepts such as variable scope, inheritance, encapsulation, constructors, access specifiers, polymorphism, and exception handling. Each program is accompanied by code snippets and expected outputs.

Uploaded by

aatul6419
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 18

TAKSHSHILA INSTITUTE OF ENGINEERING

& TECHNOLOGY, JABALPUR(M.P)

LAB MANUAL

JAVA

CS-406

Submitted By: Submitted To:

Name: Prof. Harsha Kohli


Enrollment No: Asst. Professor
CSE DEPARTMENT

RAJIV GANDHI PRODYOGIKI


VISHWAVIDYALAY, BHOPAL,(M.P)
INDEX:

S.NO List Of Experiments


.
1. Write a program to show Scope of Variables

2. Write a program to show the concept of super keyword.

3. Write a program to show Type Casting in Java.

4. Write a Program to show Concept of Class in Java.

5. Write a program to show the concept of Encapsulation.

6. Write a program to show the concept of Constructor.

7. Write a program to show the concept of copy constructor.

8. Write a program to show the Access Specifiers (Public, Private,


Protected) in Java.

9. Write a program to show the concept of Polymorphism with the help


of method overloading.

10. Write a program to show How Exception Handling in Java.


Program 1. Write a program to show Scope of Variables.

// Using Static variables


import java.io.*;

class Test{
// static variable in Test class
static int var = 10;
}

class Test1
{
public static void main (String[] args) {
// accessing the static variable
System.out.println("Static Variable : "+Test.var);
}
}

Output
Static Variable : 10

Program 2 Write a program to show the concept of super keyword.

class Vehicle
{
int a =20;
void show()
{
System.out.println("DATA STRUCTURE");

}
}

class Car extends Vehicle


{
int a =50;
void show()
{
super.show(); // It will refer super class member
System.out.println("SOFTWARE ENGINEERING");
}
}
class Main
{
public static void main(String[] args)
{
Car r = new Car();
r.show();
}
}

OUTPUT : DATA STRUCTURE


SOFTWARE ENGINEERING

Program 3 Write a program to show Type Casting in Java.

public class Main {


public static void main(String[] args) {
int myInt = 9;
double myDouble = myInt; // Automatic casting: int to double

System.out.println(myInt); // Outputs 9
System.out.println(myDouble); // Outputs 9.0
}
}

Output:
9
9.0

Program 4 Write a Program to show Concept of Class in Java.


class Vehicle {
protected String brand = "Audi";
public void honk() {
System.out.println("Car is black!");
}
}

class Car extends Vehicle {


private String modelName = "A6";
public static void main(String[] args) {
Car myFastCar = new Car();
myFastCar.honk();
System.out.println(myFastCar.brand + " " + myFastCar.modelName);
}
}
Output:
Car is black!
Audi A6

Program 5 Write a program to show the concept of Encapsulation.

class Main
{
private int age =23;

public int getage()


{
return age;
}
private String name ="SOFTWARE";
public String getname()
{
return name;
}
}

class Second
{
public static void main(String[] args)
{
Main obj1 = new Main();
System.out.println(obj1.getage() +"::::" + obj1.getname());
}
}
OUTPUT:
23::::SOFTWARE
Program 6 Write a program to show the concept of Constructor.
public class Main {
int num1;
int num2;

// Creating parameterized constructor

Main(int a, int b) {
num1 = a;
num2 = b;
}

public static void main(String[] args) {


// Creating two objects by passing the values
// to initialize the attributes.
// parameterized constructor will invoke
Main obj_x = new Main(10, 20);
Main obj_y = new Main(100, 200);

// Printing the objects values


System.out.println("obj_x");
System.out.println("num1 : " + obj_x.num1);
System.out.println("num2 : " + obj_x.num2);

System.out.println("obj_y");
System.out.println("num1 : " + obj_y.num1);
System.out.println("num2 : " + obj_y.num2);
}
}
OUTPUT:
obj_x
num1 : 10
num2 : 20
obj_y
num1 : 100
num2 : 200

Program 7 Write a program to show the concept of copy constructor.

class Main
{
int a;
String b;
Main() // DEFAULT CONSTRUCTOR
{
a = 10;
b ="ROBOTIC";
System.out.println(a+" "+b);
}
Main(Main ref) // PARAMETRIZED CONSTRUCTOR
{
a = ref.a;
b = ref.b;
System.out.println(a+" "+b);
}
}
class Second
{
public static void main(String[] args)
{
Main myobj = new Main();
Main myobj1 = new Main(myobj);
}
}

OUTPUT:
10 ROBOTIC
10 ROBOTIC

Program 8 Write a program to show the Access Specifiers (Public,


Private, Protected) in Java
public class Main {
// Static method
static void myStaticMethod() {
System.out.println("Static methods can be called without creating
objects");
}

// Public method
public void myPublicMethod() {
System.out.println("Public methods must be called by creating
objects");
}

// Main method
public static void main(String[] args) {
myStaticMethod(); // Call the static method

Main myObj = new Main(); // Create an object of MyClass


myObj.myPublicMethod(); // Call the public method
}
}
OUTPUT:
Static methods can be called without creating objects
Public methods must be called by creating objects

Program 9 Write a program to show the concept of Polymorphism


with the help of method overloading.

class Main
{
void add()
{

int a = 10, b = 20, c;


c = a+b;
System.out.println(c);
}
void add (int x, int y) // method
{
int c;
c= x+y;
System.out.println(c);
}

void add(int x, double y)


{
double c;
c = x+y;
System.out.println(c);
}
public static void main(String[] args)
{
Main obj1 = new Main();
obj1.add();
obj1.add(50,80);
obj1.add(70, 45.32);
}
}

OUTPUT:
30
130
115.32

Program 10 Write a program to show How Exception Handling in Java.


// Java program to demonstrates handling
// the exception using try-catch block
import java.io.*;
class Geeks {
public static void main(String[] args)
{
int n = 10;
int m = 0;
try {
// Code that may throw an exception
int ans = n / m;
System.out.println("Answer: " + ans);
}
catch (ArithmeticException e) {

// Handling the exception


System.out.println(
"Error: Division by zero is not allowed!");
}
finally {
System.out.println(
"Program continues after handling the exception.");
}
}
}

Output
Error: Division by zero is not allowed!
Program continues after handling the exception.

You might also like