0% found this document useful (0 votes)
5 views13 pages

Java Lab Ex

The document contains multiple Java programming examples demonstrating key concepts such as classes and objects, inheritance and polymorphism, interfaces, exception handling, multithreading, and file handling. Each example includes code snippets and their respective outputs, showcasing the functionality of each concept. The examples serve as practical implementations for understanding Java programming principles.

Uploaded by

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

Java Lab Ex

The document contains multiple Java programming examples demonstrating key concepts such as classes and objects, inheritance and polymorphism, interfaces, exception handling, multithreading, and file handling. Each example includes code snippets and their respective outputs, showcasing the functionality of each concept. The examples serve as practical implementations for understanding Java programming principles.

Uploaded by

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

Ex .

no:1 /* CLASSES AND OBJECTS*/


--------------------------------------------
public class Student

String name;

int rollno;

int age;

void info()

System.out.println("Name: "+name);

System.out.println("Roll Number: "+rollno);

System.out.println("Age: "+age);

public static void main(String[] args)

Student student = new Student();

// Accessing and property value student.name = "Ramesh";

student.rollno = 253;

student.age = 25;

// Calling method

student.info();

}
OUTPUT

Name: Ramesh

Roll Number: 253

Age: 25
Ex .no:2 /* INTERITANCE AND POLYMORPHISM*/
---------------------------------------------------------------
class Animal

public void animalSound()

System.out.println("The animal makes a sound");

}}

class Pig extends Animal {

public void animalSound() {

System.out.println("The pig says: wee wee");

}}

class Dog extends Animal {

public void animalSound() {

System.out.println("The dog says: bow wow");

}}

class Main {

public static void main(String[] args) {

Animal myAnimal = new Animal(); // Create a Animal object

Animal myPig = new Pig(); // Create a Pig object

Animal myDog = new Dog(); // Create a Dog object

myAnimal.animalSound();

myPig.animalSound();

myDog.animalSound();

}}
OUTPUT:

The animal makes a sound

The pig says: wee wee

The dog says: bow wow


Ex .no:3 /* INTERFACE AND PACKAGE*/
-----------------------------------------------------

interface Results
{
final static float pi = 3.14f;
float areaOf(float l, float b);
}
class Rectangle implements Results
{
public float areaOf(float l, float b)
{
return (l * b);
}
}
class Square implements Results
{
public float areaOf(float l, float b)
{
return (l * l);
}
}
class Circle implements Results
{
public float areaOf(float r, float b)
{
return (pi * r * r);
}
}
public class InterfaceDemo
{
public static void main(String args[])
{
Rectangle rect = new Rectangle();
Square square = new Square();
Circle circle = new Circle();
System.out.println("Area of Rectangle: "+rect.areaOf(20.3f, 28.7f));
System.out.println("Are of square: "+square.areaOf(10.0f, 10.0f));
System.out.println("Area of Circle: "+circle.areaOf(5.2f, 0));
}
}

Output:
Area of Rectangle: 582.61
Are of square: 100.0
Area of Circle: 84.905594
Ex.no:07 : SWING CONCEPT
------------------------------

import javax.swing.*;

public class FirstSwingExample {

public static void main(String[] args) {

JFrame f=new JFrame();//creating instance of JFrame

JButton b=new JButton("click");//creating instance of JButton

b.setBounds(130,100,100, 40);//x axis, y axis, width, height

f.add(b);//adding button in JFrame

f.setSize(400,500);//400 width and 500 height

f.setLayout(null);//using no layout managers

f.setVisible(true);//making the frame visible

}
OUTPUT:

---------------
Ex.no : 8 /*Implementing Exception Handling*/

class MyException extends Exception


{
String s1;
MyException(String s2)
{
s1=s2;
}

public String toString()


{
return("output string=" +s1);
}
}
public class newclass
{
public static void main(String[] args)
{
try
{
throw new MyException("custom message");
}
catch(MyException exp)
{
System.out.println(exp);
}
}
}

Output:

output string=custom message


Ex.no : 9 /*Implementing Multithreading*/

class MultithreadingDemo extends Thread


{
public void run()
{
try
{

System.out.println("thread"+Thread.currentThread().getId()+"isrunning");

}
catch(Exception e)
{
System.out.println("Exception is caught");

}
}
}
public class multithreading
{
public static void main(String[] args)
{
int n=8;
for(int i=0;i<8;i++)
{
MultithreadingDemo obj=new MultithreadingDemo();
obj.start();

}
}

}
Output:

thread10isrunning
thread9isrunning
thread13isrunning
thread14isrunning
thread16isrunning
thread12isrunning
thread15isrunning
thread11isrunning
Ex.no:10 : I/O - STREAM FILE HANDLING
----------------------------------------------
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
class copy05
{
public static void main(String args[])throws IOException
{
FileInputStream in=null;
FileOutputStream out=null;
try
{
in=new FileInputStream("G://sawc//abi.txt");
out=new FileOutputStream("G://sawc//outagain.txt");
int c;
while((c=in.read())!=-1)
{
out.write(c);
}
}
finally
{
if(in!=null)
{
in.close();
}
if(out!=null)
{
out.close();
}
System.out.print("\n The string has been copied into new string...");
System.out.print("\n The IO handling is performed...");
}
}
}

OUTPUT:

---------------

C:\jdk1.2.1\bin>javac copy05.java

C:\jdk1.2.1\bin>java copy05

The string has been copied into new string...

The IO handling is performed...

You might also like