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

Java Haezron (1)

The document contains Java source code examples demonstrating various programming concepts including multiple constructors, inheritance, method overriding, arrays, interfaces, threading, exception handling, applets, and event handling. Each section includes code snippets that illustrate the implementation of these concepts in Java. The examples cover a range of topics suitable for understanding object-oriented programming and basic Java functionalities.

Uploaded by

sivamaheshpvi1
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 views33 pages

Java Haezron (1)

The document contains Java source code examples demonstrating various programming concepts including multiple constructors, inheritance, method overriding, arrays, interfaces, threading, exception handling, applets, and event handling. Each section includes code snippets that illustrate the implementation of these concepts in Java. The examples cover a range of topics suitable for understanding object-oriented programming and basic Java functionalities.

Uploaded by

sivamaheshpvi1
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/ 33

[Type text]

[Type text]

Multiple Constructors

Source Code:

import java.io.*;

class Student

String name;

int regno;

int marks1,marks2,marks3;

Student()

name="raju";

regno=12345;

marks1=56;

marks2=47;

marks3=78;

Student(String n,int r,int m1,int m2,int m3)

name=n;

regno=r;

marks1=m1;

marks2=m2;

marks3=m3;

Student(Student s)

{
[Type text]

name=s.name;

regno=s.regno;

marks1=s.marks1;

marks2=s.marks2;

marks3=s.marks3;

void display()

System.out.println(name+"\t"+regno+"\t"+marks1+"\t"+marks2+"\t"+marks3);

class Studentdemo

public static void main(String args[])

Student s1 = new Student();

Student s2 = new Student("john",34266,58,96,84);

Student s3 = new Student(s1);

s1.display();

s2.display();

s3.display();

}
[Type text]
[Type text]

Inheritance:

Source Code:

import java.io.*;

class Name

String name="Swathi";

int age=20;

class Mark extends Name

int m1=30,m2=30,m3=30;

class Student extends Mark

int total;

void calc()

total=m1+m2+m3;

void show()

System.out.println("\nNAME:"+name+"\nAGE:"+age+"\nMARK1="+m1+"\nMARK2="+m
2+"\nMARK3="+m3+"\nTOTAL="+total);

class MultilevelInheritance
[Type text]

public static void main(String args[])

Student ob =new Student();

ob.calc();

ob.show();

}
[Type text]
[Type text]

Overriding Methods:

Source Code:

import java.io.*;

class Super

int x;

Super(int x)

this.x=x;

void display()

System.out.println("super x="+x);

class Sub extends Super

int y;

Sub(int x,int y)

super(x);

this.y=y;

void display()

{
[Type text]

System.out.println("super x="+x);

System.out.println("sub y="+y);

class OverrideTest

public static void main(String args[])

Sub s1=new Sub(100,200);

s1.display();

}
[Type text]
[Type text]

One Dimensional Arrays:

Source Code:

import java.io.*;

class NumberSorting

public static void main(String args[])

int number[]={55,20,40,60,80,75,65,15,71,93};

int n = number.length;

System.out.println("Given list:");

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

System.out.println(" "+number[i]);

System.out.println("/n");

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

for(int j=i+1;j<n;j++)

if(number[i]<number[j])

int temp=number[i];

number[i]=number[j];

number[j]=temp;
[Type text]

System.out.println("Sorted List:");

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

System.out.println(" "+number[i]);

System.out.println(" ");

}
[Type text]
[Type text]

Two Dimensional Array

Source Code:

import java.util.Scanner;

class AddMatrix

public static void main(String args[])

int row,col,i,j;

Scanner in = new Scanner(System.in);

System.out.println("Enter the number of rows");

row = in.nextInt();

System.out.println("Enter the no of columns");

col=in.nextInt();

int mat1[][]=new int[row][col];

int mat2[][]=new int[row][col];

int res[][]=new int[row][col];

System.out.println("Enter the elements of matrix1");

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

for(j=0;j<col;j++)

mat1[i][j]=in.nextInt();

System.out.println();

System.out.println("Enter the elements of matrix2");


[Type text]

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

for(j=0;j<col;j++)

mat2[i][j]=in.nextInt();

System.out.println();

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

for(j=0;j<col;j++)

res[i][j]=mat1[i][j]+mat2[i][j];

System.out.println("Sum of matrics:-");

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

for(j=0;j<col;j++)

System.out.print(res[i][j]+"\t");

System.out.println();

}
[Type text]
[Type text]

Implementing Interface:

Source Code:

import java.lang.*;

import java.io.*;

interface Exam

void percent_cal();

class Student

String name;

int roll_no,mark1,mark2;

Student(String n,int r,int m1,int m2)

name=n;

roll_no=r;

mark1=m1;

mark2=m2;

void display()

System.out.println("\n Name of Student:"+name);

System.out.println("RollNo of Student:"+roll_no);

System.out.println("Mark of Subject1:"+mark1);
[Type text]

System.out.println("Mmark of Subject2:"+mark2);

class Result extends Student implements Exam

Result (String n,int r,int m1,int m2)

super(n,r,m1,m2);

public void percent_cal()

int total=(mark1+mark2);

float percent=total*100/200;

System.out.println("Percentage:"+percent+"%");

void display()

super.display();

class MultipleinheritaneDemo

public static void main(String args[])

Result R = new Result("Anoop Reddy",12,93,84);

R.display();
[Type text]

R.percent_cal();

}
[Type text]
[Type text]

Create and deal multiple threads

Source Code:

import java.io.*;

class first extends Thread

public void run()

for(int i=0; i<=20; i+=2)

System.out.print("\n Even Number:"+i);

class second extends Thread

public void run()

for(int i=1; i<=20; i+=2)

System.out.print("\n Even Number:"+i);

class MultiThread

public static void main(String args[])

System.out.print("Thread is started.....");
[Type text]

first obj=new first();

second obj1=new second();

obj.start();

obj.start();

System.out.println("Thread is executed....");

}
[Type text]
[Type text]

Create and Import Package:

Source Code: Add.java

package mypack;

public class Add

public void addition()

int a = 100;

int b = 200;

System.out.println("The sum is :"+(a+b));

Myclass.java

import mypack.Add;

public class myclass

public static void main(String args[])

Add a = new Add();

a.addition();

}
[Type text]
[Type text]

Throwing your own Exception:

Source Code:

import java.io.*;

class ExceptionDemo

public static void main(String args[])

int a=15;

for(int i=3;i>=0;i--)

try

System.out.println(a/i);

catch(ArithmeticException e)

System.out.println(e);

}
[Type text]
[Type text]

Applet to Design a Webpage:

Source Code:

HelloworldApplet.java

import java.applet.*;

import java.awt.*;

public class HelloworldApplet extends Applet

public void paint(Graphics g)

g.drawString("Hellow World",25,50);

HelloworldApplet.html

<html>

<title> The Helloe World Applet</title>

<applet code=”HelloworldApplet1.class” width=”320”height=”120”>

</applet>

</hr>

</html>
[Type text]
[Type text]

Mouse Events handling:

Source Code:

import java.awt.*;

import java.awt.event.*;

public class MouseListenerExample2 extends Frame implements MouseListener

MouseListenerExample2()

addMouseListener(this);

setSize(300,300);

setLayout(null);

setVisible(true);

public void mouseClicked(MouseEvent e)

Graphics g=getGraphics();

g.setColor(Color.BLUE);

g.fillOval(e.getX(),e.getY(),30,30);

public void mouseEntered(MouseEvent e)

public void mouseExited(MouseEvent e)

public void mousePressed(MouseEvent e)


[Type text]

public void mouseReleased(MouseEvent e)

public static void main(String[]args)

new MouseListenerExample2();

Keyboard Events Handling:

Source Code:

import java.awt.*;

import java.awt.event.*;

import java.applet.Applet;

/*<applet code="Demokey" width=300 height=300></applet>*/

public class Demokey extends Applet implements KeyListener

String msg;

int x=50;

int y=100;

public void init()

{
[Type text]

addKeyListener(this);

requestFocus();

public void keyPressed(KeyEvent e)

showStatus("Key Pressed");

repaint();

public void keyReleased(KeyEvent e)

showStatus("Key Released");

repaint();

public void keyTyped(KeyEvent e)

msg+=e.getKeyChar();

repaint();

public void paint(Graphics g)

g.drawString(msg,x,y);

}
[Type text]

You might also like