INDEX
[Link]. TOPICS
1. Inheritance
2. Abstract class and Method
3. Interface
4. Method Overiding
5. Multiple Inhertance
6. Method Overloding
7. Scientific Calculator
8. Exception Handling
9. Multithreading
10. I/O Stream
11. Applet
12. File Handling
13. Text Editor
Program 1: -Write a program of Inheritance .
Simple Inheritance
class NUM
public int x=24,y=45;
class B extends NUM{
void add()
int x=super.x;
int y=super.y;
[Link]("Sum is :"+(x+y));
public class simple_inheritance {
public static void main(String[] args) {
B ob=new B();
[Link]();
OUTPUT
Multilevel inheritance
class NUM
public int x=24,y=45;
class B extends NUM{
void add()
int x=super.x;
int y=super.y;
[Link]("Sum is :"+(x+y));
class C extends B {
void sub()
int a=super.x;
int b=super.y;
[Link]("Sub is :"+(b-a));
public class multilevel_inheritance {
public static void main(String[] args) {
B ob=new B();
[Link]();
C ob1=new C();
[Link]();
[Link]();
OUTPUT
Hybrid Inheritance
package [Link];
class NUM
public int x=24,y=45;
class B extends NUM{
void add()
int x=super.x;
int y=super.y;
[Link]("Sum is :"+(x+y));
class C extends NUM{
void sub()
int a=super.x;
int b=super.y;
[Link]("Sub is :"+(b-a));
public class hybrid_inheritance {
public static void main(String[] args) {
B ob=new B();
[Link]();
C ob1=new C();
[Link]();
OUTPUT
Program 2:-Write a program of Abstract class and
abstract method
package [Link];
abstract class Class1
abstract int area();
void display()
[Link]("Area of Shapes is ");
class Class2 extends Class1{
int x=10;
int area()
return x*x;
class Class3 extends Class1
int x=10;
int y=5;
int area()
return x*y;
public class abstract1 {
public static void main(String[] args) {
Class1 ob=new Class2();
Class1 ob1=new Class3();
int a=[Link]();
int b=[Link]();
[Link]();
[Link]("Area of Square is : "+a);
[Link]("Area of rectangle is : "+b);
OUTPUT
Program 3: Write a program of Interface.
package [Link];
interface xyz
void second();
interface lmn
void first();
class myclass implements lmn,xyz
public void first()
[Link]("first method");
public void second()
[Link]("second method");
public class interface1 {
public static void main(String[] args) {
myclass ob=new myclass();
[Link]();
[Link]();
OUTPUT
Program 4:-Write a program of Method Overidding .
package [Link];
class Bank
int interest()
return 0;
class SBI extends Bank
int interest()
return 7;
class PNB extends Bank{
int interest()
return 8;
public class overriding {
public static void main(String[] args) {
SBI s=new SBI();
PNB p=new PNB();
[Link]("SBI interest is: "+[Link]());
[Link]("PNB interest is "+[Link]());
OUTPUT
Program 5:-Write a program of multiple inheritance.
interface inf1
void first();
interface inf2
void two();
class abc implements inf1,inf2
public void first()
[Link]("my first method");
public void two()
[Link]("this is my second method");
public class multilevel
public static void main(String[] args)
inf1 ob1=new abc();
inf2 ob2=new abc();
[Link]();
[Link]();
}
Output:
this is my first method.
This is my second method
Program 6: write a program on method overloding.
public class metodoverloding
void add()
int a=10,b=20,c;
c=a+b;
[Link](c);
void add(int x,int y)
int c;
c=x+y;
[Link](c);
void add(int x,double y)
double c;
c=x+y;
[Link](c);
public static void main(String[] args) {
metodoverloding r=new metodoverloding();
[Link]();
[Link](100,200);
[Link](50,45.32);
}
}
Output:
[Link] a program on scientific calculator.
import [Link].*;
public class ScientificCalculator {
public static void main(String args[])
double x,y,c;
int ch, choice;
[Link]("\n Enter two numbers");
Scanner sc=new Scanner([Link]);
x=[Link]();
y=[Link]();
[Link]("x="+ " " + x+"y="+ " " + y);
[Link]("1. Simple Calculator" + "\n" + "2. Scientific Calculator" + "\n");
ch=[Link]();
if(ch==1)
[Link]("1. Addition" + "\n" + "2. Subtraction" + "\n" + "3. Multiplication" + "4.
Division" + "\n");
choice=[Link]();
//if(ch==1)
//{
switch(choice)
case 1:
c=x+y;
[Link]("Addition of two numbers is: " + c); break;
case 2: c=x-y;
[Link]("Subtraction of two numbers is:" + c); break;
case 3: c=x*y;
[Link]("Multiplication of two numbers is:" + c); break;
case 4: c=x/y;
[Link]("Division of two numbers is:" + c); break;
default:
[Link]("\n Enter values between 1 to 4"); break;
else if(ch==2)
[Link]("1. Sine of a number" + "\n" + "2. Cosine of a number" + "\n" +
"3. tangent of a number" + "\n" + "4. Cot of a number" + "\n"+ "5. Secant of a number" +
"\n" +
"6. Cosecant of a number" + "\n" + "7. Square root of a number" + "\n" + "8. Exponential of
a number" +
"\n" + "9. Logarithm of a number" + "\n" + "10. Modulus of two numbers" +"\n" +
"11. Hyperbolic sine function of a number" + "\n" + "12. Hyperbolic cosine function of a
number"+ "\n"+
"13. Cube root of a number" + "\n");
choice=[Link]();
switch(choice)
case 1: c=[Link](x);
[Link]("Sine of a number is:" + c);
break;
case 2: c=[Link](x);
[Link]("Cosine of a number is" + c);
break;
case 3: c=[Link](x);
[Link]("The tangent of a number is:"+c);
break;
case 4: c=[Link](x)/[Link](x);c=1/[Link](x);
[Link]("The Cot of a number is: "+ c);
break;
case 5. c=1/[Link](x);
[Link]("The Secant of a number is: "+c);
break;
case 6:
case 7: c=[Link](x);
[Link]("Square root of a number is" + c);
break;
case 8: c=[Link](x, y);
[Link]("The Exponentiation of number is: "+ c);
break;
default : [Link]("enter a valid value");
break;
Output:
[Link] a program on exception handling.
import [Link];
class throwDemo
public static void main(String[] args)
Scanner sc=new Scanner([Link]);
int num1,num2,r;
[Link]("Enter any two numbers");
num1=[Link]();
num2=[Link]();
try
if(num2==0)
throw new ArithmeticException("/ by zero");
r=num1/num2;
[Link](num1+"/" + num2 + "="+ r);
catch(ArithmeticException e)
[Link]("Problem is "+[Link]());
}
OUTPUT
[Link] a program on multithreading.
class P extends Thread
public void run()
for(int i=1;i<=5;i++)
[Link]("Abhishek");
} }
class multithreading {
public static void main(String[] args) {
P t=new P();
[Link]();
for(int i=1;i<=5;i++)
[Link]("Nancy");
} }
Output:
[Link] a program of i/o stream
import [Link].*;
class main{
public static void main(String[] args) throws IOException {
try{
// loading a file into f variable
FileInputStream f = new FileInputStream("[Link]");
// initializing x to 0
int x = 0;
// while loop untill the end of the file.
while ((x = [Link]())!=-1){
// printing the character
[Link]((char)x);
// closing a file
[Link]();
catch(Exception e){
// printing exception
[Link](e);
Output:
Hello I am Soham Medewar%
[Link] a program on applet.
import [Link].*;
import [Link];
public class Myapplet extends Applet
public void paint(Graphics g)
[Link]("Java Programming",200,200);
<html>
<body>
<applet code="[Link]",width="500",height="500"></applet>
</body>
</html>
Output.
[Link] a program on file handling.
public class file
public static void main(String[] args )
File myfile=new File("[Link]");
try
[Link]();
catch(Exception e)
[Link]("unable to create file");
[Link]();
try
FileWriter fileWrite=new FileWriter("[Link]");
[Link]("This is first file from this java course");
[Link]();
catch(Exception e)
[Link]();
try{
FileReader fileRead =new FileReader("[Link]");
char[] a=new char[50];
[Link](a);
for(char c:a)
[Link](c);
[Link]();
catch(IOException e)
[Link](e);
Output:
13. Write a program to make a Text Editor.
// Java Program to create a text editor using java
import [Link].*;
import [Link].*;
import [Link].*;
import [Link].*;
import [Link].*;
import [Link].*;
class editor extends JFrame implements ActionListener {
// Text component
JTextArea t;
// Frame
JFrame f;
// Constructor
editor()
// Create a frame
f = new JFrame("editor");
try {
// Set metal look and feel
[Link]("[Link]");
// Set theme to ocean
[Link](new OceanTheme());
}
catch (Exception e) {
// Text component
t = new JTextArea();
// Create a menubar
JMenuBar mb = new JMenuBar();
// Create amenu for menu
JMenu m1 = new JMenu("File");
// Create menu items
JMenuItem mi1 = new JMenuItem("New");
JMenuItem mi2 = new JMenuItem("Open");
JMenuItem mi3 = new JMenuItem("Save");
JMenuItem mi9 = new JMenuItem("Print");
// Add action listener
[Link](this);
[Link](this);
[Link](this);
[Link](this);
[Link](mi1);
[Link](mi2);
[Link](mi3);
[Link](mi9);
// Create amenu for menu
JMenu m2 = new JMenu("Edit");
// Create menu items
JMenuItem mi4 = new JMenuItem("cut");
JMenuItem mi5 = new JMenuItem("copy");
JMenuItem mi6 = new JMenuItem("paste");
// Add action listener
[Link](this);
[Link](this);
[Link](this);
[Link](mi4);
[Link](mi5);
[Link](mi6);
JMenuItem mc = new JMenuItem("close");
[Link](this);
[Link](m1);
[Link](m2);
[Link](mc);
[Link](mb);
[Link](t);
[Link](500, 500);
[Link]();
}
// If a button is pressed
public void actionPerformed(ActionEvent e)
String s = [Link]();
if ([Link]("cut")) {
[Link]();
else if ([Link]("copy")) {
[Link]();
else if ([Link]("paste")) {
[Link]();
else if ([Link]("Save")) {
// Create an object of JFileChooser class
JFileChooser j = new JFileChooser("f:");
// Invoke the showsSaveDialog function to show the save dialog
int r = [Link](null);
if (r == JFileChooser.APPROVE_OPTION) {
// Set the label to the path of the selected directory
File fi = new File([Link]().getAbsolutePath());
try {
// Create a file writer
FileWriter wr = new FileWriter(fi, false);
// Create buffered writer to write
BufferedWriter w = new BufferedWriter(wr);
// Write
[Link]([Link]());
[Link]();
[Link]();
catch (Exception evt) {
[Link](f, [Link]());
// If the user cancelled the operation
else
[Link](f, "the user cancelled the operation");
else if ([Link]("Print")) {
try {
// print the file
[Link]();
catch (Exception evt) {
[Link](f, [Link]());
}
else if ([Link]("Open")) {
// Create an object of JFileChooser class
JFileChooser j = new JFileChooser("f:");
// Invoke the showsOpenDialog function to show the save dialog
int r = [Link](null);
// If the user selects a file
if (r == JFileChooser.APPROVE_OPTION) {
// Set the label to the path of the selected directory
File fi = new File([Link]().getAbsolutePath());
try {
// String
String s1 = "", sl = "";
// File reader
FileReader fr = new FileReader(fi);
// Buffered reader
BufferedReader br = new BufferedReader(fr);
// Initialize sl
sl = [Link]();
// Take the input from the file
while ((s1 = [Link]()) != null) {
sl = sl + "\n" + s1;
}
// Set the text
[Link](sl);
catch (Exception evt) {
[Link](f, [Link]());
// If the user cancelled the operation
else
[Link](f, "the user cancelled the operation");
else if ([Link]("New")) {
[Link]("");
else if ([Link]("close")) {
[Link](false);
// Main class
public static void main(String args[])
editor e = new editor();