0% found this document useful (0 votes)
14 views

Source ds

The document contains multiple Java code snippets demonstrating various programming concepts such as classes, inheritance, exception handling, threading, and database connectivity. It includes examples of defining classes, methods, constructors, and handling exceptions, along with the implementation of interfaces and abstract classes. The code also showcases the use of threads and synchronization in Java, as well as basic arithmetic operations and database interactions.

Uploaded by

bashaa0669
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)
14 views

Source ds

The document contains multiple Java code snippets demonstrating various programming concepts such as classes, inheritance, exception handling, threading, and database connectivity. It includes examples of defining classes, methods, constructors, and handling exceptions, along with the implementation of interfaces and abstract classes. The code also showcases the use of threads and synchronization in Java, as well as basic arithmetic operations and database interactions.

Uploaded by

bashaa0669
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/ 32

Source:-

import static java.long.system.*;

class student {

int rno=420;

string name=""raju”;

string_class="CSD";

Public void display(){

system.out.println(name);

system.out.println(rno);

system.out.println(-class);

Public class Details;

Public static void main(string[]args){

Student s=new student();

S.display();

}
Source:-

Public class Sum{

Public int sum(int x,int y){return(x+y;}

Public int sum(int x,int y,int z)

return (x+y+z);

Public double sum(double x,double y)

return(x+y);

Public static void main (string args[])

Sum s=new sum();

system.out.println(s.sum(10,20));

system.out.println(s.sum(10,20,30));

system.out.println(s.sum(10,5,20.5));

}
Source:-

Public class person{

//fields

Private string name;

Private int age;

//constructor

Public person(string name,int age){

this.name=name;

this,age=age;

//method to display information

Public void display Info(){

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

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

Public static void main(string[]args){

//Creating an instance of person using constructor

Person Person1=new person("John Doe",25);

Person1.displayInfo();

}
Source:-

class Box{

double width,height,depth;

//Constructor used when all dimensions specified

Box(double w,double h,double d)

width=w

height=h

depth=d

//constructor used when no dimensions are specified

Box(){width=height=deph=0;}

//constructor used when cube is created

Box(double len){width=height=depth=len;}

double volume(){return width*height*depth;}

Public class Test {

Public static void main(string args[])

Box mybox1=new Box(10,20,15);

Box mybox2=new Box();

Box mybox3=new Box(7);

double vol;

//get volume of first box

vol=mybox1.Volume();

system.out.println("Volume of mybox1 is"+vol);

vol=mybox2.Volume();
system.out.println("Volume of mybox2 is"*void);

vol=mycubes.volume c();

system.out.println("Volume of mycube is"+vol);

}
Source:-

Class shape{

//calculate area is member of shape class public int calculate Area(int length,int breadth){

return length * breadth;

//In heriting shape member using extends keyword class rectangle extends shape{

Public static void main(string[]args){

Rectangle rectangle=new rectangle();

//Creating object of child class

system.out.println("Area of rectangle:"+rectangle.Calculate Area(10,5));

//Calculate area method accessibleto rectangle class

}
Source:-

Class Sem1

int eng,math,phy,chem,avg1;

Sem1()

eng=95;

math=93;

phy=88;

chem=90;

avg1=(eng+math+phy+chem)/4;

Class Sem2 extends Sem1

int eng2,math2,phy2,chem2,avg2;

Sem2()

eng2=89;

math2=98;

phy2=97;

chem2=79;

avg2=(eng2+math2+phy2+chem2)/4;

Class Sem3 extends Sem2

{
int eng3,math3,phy3,chem3,avg3;

Sem3()

eng3=87;

math3=78;

phy3=88;

chem3=96;

avg3=(eng3+math3+phy3+chem3)/4;

Class Sem4 extends Sem3

int eng4,math4,phy4,chem4,avg4;

Sem4()

eng4=79;

math4=90;

phy4=77;

chem4=96;

avg4=(eng4+math4+phy4+chem4)/4;

int totalavg()

return(avg1+avg2+avg3+avg4)/4;

Class Multipleinheritanceexample
{

Public static void main(string args[])

Sem4 S4=new Sem4();

System.out.println("Semester 1avg:"+S4.avg1);

System.out.println("Semester 2avg:"+S4.avg2);

System.out.println("Semester 3avg:"+S4.avg3);

System.out.println("Semester 4avg:"+S4.avg4);

System.out.println("Total average:"+S4.totalavg(1));

}
Source:-

abstract class Shape{

abstract double calculateArea();

class Circle extends Shape{

private double radius;

public circle(double radius){

this.radius=radius;

@override

double calculateArea(){

return Math.PI*radius*radius;

class Rectangle extends shape{

private double length;

private double width;

Public Rectangle(double length,doublewidth){

this.length=length;

this.width=width;

@override

double calculateArea(){

return length*width;

public class Areacalculator{


public static void main(string args[])

Circle circle=new Circle(5.0);

system.out.println("Area of circle:"+circle.calculateArea());

Rectangle rectangle=new rectangle(4.0,6.0);

system.out.println("Area of rectangle:"+rectangle.calculateArea());

}
source code:-

class Person

int id;

string name;

person(int id,string name)

this.id=id;

this.name=name;

class Emp extends Person

float salary;

Emp(int id,string name,float salary)

super(id,name);

this.salary

void display()

system.out.println(id+""+name+""+salary);

class Testsuper

public static void main(string args[])


{

Emp E1=new Emp(1,"vyshu",20000);

E1.display();

}
Source code:

interface Animal{

void sound();

interface Pet{

void play();

class Dog implements Animal,Pet{

public void sound(){

system.out.println("The dog barks");

public void play(){

system.out.println("The dog plays with a ball");

public class main{

public static void main(string args[])

Dog dog=new Dog();

dog.sound();

dog.play();

}
Source code:

class Animal

Void sound()

system.out.println("Animal makes a sound");

class Dogextends Animal{

Void sound(){

system.out.println("The dog barks");

class cat extends Animal{

Void sound(){

system.out.println("The cat meows");

public class mains

public static void main(string args[])

Animal animal1=new Dog();

animal.sound();

Animal animal2=new cat();

animal.sound();

}
Source code:

Public class Exceptionhandling{

Public static void main(string args[])

int[]numbers={12345};

try{

system.out.println("Accepting element at index 5:"+number[5]);

catch(Array index out of Bound exception e){

system.out.println("exception caught:"+e);

finally{

system.out.println("The finally block is always executed");

system.out.println("Rest of the progam continues normally");

}
Source code:

package com.tutorialspoint;

public class except Test{

public static void main(string args[]){

try{

int a[]=new int [2];

int b=0;

int c=1/b;

system.out.println("Access element three:"+a[3]);

catch(ArrayIndexoutofBoundsException e){

system.out.println("ArrayIndexoutofBoundsexception thrown:"+e);

catch(Exception e)

system.out.println("Exception thrown:"+e);

system.out.println("Out of the block");

}
Sourc code:

package com.tutorialspoint;

public class EceptTest{

public static void main(string args[]){

try{

int b=0;

int c=1/b;

system.out.println("c:"+c);

catch(Arithmetic Exception e){

system.out.println("Exception thrown:"+e);

system.out.println("Out of the block");

}
Source code:

class MyException extends Exception{

public myException(string S)

Super(s);

public class main{

public static void main(string args[])

try{

throw new myException("Geeks for Geeks");

catch(myexception ex){

system.out.println("caught");

system.out.println(ex.getmessage());

}
Source code:

Import java.io.*;

Class one extends Thread

Public void run()

for(int i=0;i<100;i++)

try{

thread.sleep(1000);}

catch(interuptedExpextion e){

Systemout.printin(e);

Systemout.printin(“Good morning”);

}
}

Class Two extends Threads

Public void main()

for(int i=0;i<100;i++)

try{

Thread.sleep(200);}

Catch(interruptedException e)

Systemout.printin(e);

}
}

Systemprintin(“Hello”);

}
}
}

Class Three implements Runnable

Public void run()

for(i=0;i<100;i++)

try{

Thread.sleep(3000);}

Catch(interuptedException e){

Systemout.printin(“Welcome”);

}
}
}
class ThreadEx

Public static void main(string arys[])

One t1=new One();

Two t2=new Two();

Three t3=new Three();

Thread t3=new Thread(tt);.

t1.SetName(“One”);

t2.SetName(“Two”);
t3.SetName(“Three”);

Systemout.printin(t1);

Systemout.printin(t2);

Systemout.printin(t3);

Thread t=Thread.currentthread();

t1.start();

t2.start();

t3.start();

}
Source code:

Class Mytask extands thread

Private string name;

Private int sleeptime;

Public Mytask(string name,int sleepTime){

This.name=name;

This.sleeptime=sleeptime;

Public void run(){

Systemout.printin(“Thread”,+name+,”Starting”);

Try{

Thread.sleep(Sleeptime*1000);

Catch(interuptedException e)

Systemout.printin(“Thread”,+name+,”interrupted”);

Systemout.printin(“Thread”,+name+,”Finished”);

}
}

Public class thread Example{

Public static void main(string arys[]){

Mytask.thread1=new Mytask(“one”,2);

Mytask.thread2=new Mytask(“tab”,4);

Thread1.start();

Thread2.start();

Systemout.printin(“Is thread one alive?”,+thread1.isAlive());


Systemout.printin(“Is thread two alive?”,+thread2.isAlive());

try{

thread1.join();

thread2.join();

Catch(interruptedExpection e){

Systemout.printin(“Therad interrupted”);

Systemout.printin(“Is thread one alive?”,+thread1.isAlive());

Systemout.printin(“Is thread two alive?”,+thread2.isAlive());

}
}
Public class TestDaemon Thread1 extends thread

Public void run(){

if(Thread.currentthread() is daemon())

Systemout.printin(“daemon thread work”);

else{

Systemout.printin(“user thread work”);

}
}

Public statuc void main(string arys[])

TestDaemon Thread1 t1=newTestDaemon thread();

TestDaemon Thread2 t2=newTestDaemon thread();

TestDaemon Thread3 t3=newTestDaemon thread();

t1.setDaemon(true);

t1.start();

t2.start();

t3.start();

}
Source code:

Import java.util.linkedlidst;

Public class threadexample{

Public static void main(String arys[])

Throws interrupted exception

Finalpc pc=new pc();

Thread t1=new Thread(new runnable()){

@override

Public void run()

try{

pc.produce();

Catch(interuptedExeption e){

e.printstackTrace();

}
}
});

Thread t2=new Thread(new runnable()){

@override

Public void run()

try{

pc.consume();

Catch(interuptedExeption e){

e.printstackTrace();
}
}
});

t1.start():

t2.start();

t1.join()

t2.join();

Public static class pc{

Linkedlist<integer>list=new linkedlist<>();

Int capacity=2;

Public void produce() throws interruptedException

Int value=0;

While(true){

Synchronized(this)

While(list.size()==capacity)

Wait();

Systemoutprintin(“Producer produced-“,+value);

List.add(value++);

Notify();

Thread.sleep(100);

}
}
}

Public void consume() throws interruptedException

While(true){
Synchronized(this)

While(list.size()==0)

Wait();

Int val=list.removeFirst();

Systemoutprintin(“consumer consumed-“,+value);

notify();

Thread.sleep(1000);
Source Code:-

package com.example;

public class calculator{

public int add(int a,int b){

return a+b;

public int subtract (int a,int b)

return a-b;

public int multiply(int a,int b)

return a*b;

public int divide(int a,int b){

if(b!=0){

return a/b;

else {

throw new Arithmetic Exception("Cannot divided by zero");

import com.example.calculator;

public class PackageExample{

public static void main(string[]args){

Calculator calculator=new calculator();


int result=calculator.add(5,3);

system.out.println("Addition:"+result);

result=calculator.subtract(5,3);

system.out.println("Subtraction:"+result);

result=calculator.multiply(5,3);

system.out.println("Multiplication:"+result);

result=calculator.divide(10,2);

system.out.println("Division:"+result);

}
SOURCE CODE:

import java.io.*;

import java.sql.*;

class GFG{

public static void main(string[]args)throws exception

string url

="jdbc:my sql://local host:3306/table_name";

string username="root gfg";

string password="gfg123";

string query="select * from students";

class.forName(

"com.mysql.cj.jdbc.Driver");

connection.con = Drivermanager.getconnection(url,username,password);

system.out.println("Connection Established successfully");

statement st=con.createstatement();

Result rs=st.executeQuery(query);

rs.next();

string name=rs.getstring("name");

system.out.println(name);

st.close();

con.close();

system.out.println("connection closed");

You might also like