Abhishek Java Practical
Abhishek Java Practical
S.NO. TOPICS
1. Inheritance
3. Interface
4. Method Overiding
5. Multiple Inhertance
6. Method Overloding
7. Scientific Calculator
8. Exception Handling
9. Multithreading
11. Applet
class NUM
void add()
int x=super.x;
int y=super.y;
System.out.println("Sum is :"+(x+y));
B ob=new B();
ob.add();
OUTPUT
Multilevel inheritance
class NUM
void add()
int x=super.x;
int y=super.y;
System.out.println("Sum is :"+(x+y));
class C extends B {
void sub()
int a=super.x;
int b=super.y;
System.out.println("Sub is :"+(b-a));
B ob=new B();
ob.add();
C ob1=new C();
ob1.sub();
ob1.add();
OUTPUT
Hybrid Inheritance
package com.company;
class NUM
void add()
int x=super.x;
int y=super.y;
System.out.println("Sum is :"+(x+y));
void sub()
int a=super.x;
int b=super.y;
System.out.println("Sub is :"+(b-a));
B ob=new B();
ob.add();
C ob1=new C();
ob1.sub();
OUTPUT
Program 2:-Write a program of Abstract class and
abstract method
package com.company;
void display()
int x=10;
int area()
return x*x;
int x=10;
int y=5;
int area()
return x*y;
int a=ob.area();
int b=ob1.area();
ob.display();
OUTPUT
Program 3: Write a program of Interface.
package com.company;
interface xyz
void second();
interface lmn
void first();
System.out.println("first method");
System.out.println("second method");
ob.first();
ob.second();
OUTPUT
Program 4:-Write a program of Method Overidding .
package com.company;
class Bank
int interest()
return 0;
int interest()
return 7;
int interest()
return 8;
OUTPUT
Program 5:-Write a program of multiple inheritance.
interface inf1
void first();
interface inf2
void two();
ob1.first();
ob2.two();
}
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;
System.out.println(c);
int c;
c=x+y;
System.out.println(c);
double c;
c=x+y;
System.out.println(c);
r.add();
r.add(100,200);
r.add(50,45.32);
}
}
Output:
7.write a program on scientific calculator.
import java.util.*;
double x,y,c;
x=sc.nextDouble();
y=sc.nextDouble();
ch=sc.nextInt();
if(ch==1)
choice=sc.nextInt();
//if(ch==1)
//{
switch(choice)
case 1:
c=x+y;
case 2: c=x-y;
case 3: c=x*y;
System.out.println("Multiplication of two numbers is:" + c); break;
case 4: c=x/y;
default:
else if(ch==2)
"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"+
choice=sc.nextInt();
switch(choice)
case 1: c=Math.sin(x);
break;
case 2: c=Math.cos(x);
break;
case 3: c=Math.tan(x);
break;
case 4: c=Math.cos(x)/Math.sin(x);c=1/Math.tan(x);
break;
case 5. c=1/Math.cos(x);
break;
case 6:
case 7: c=Math.sqrt(x);
break;
break;
break;
Output:
8.write a program on exception handling.
import java.util.Scanner;
class throwDemo
int num1,num2,r;
num1=sc.nextInt();
num2=sc.nextInt();
try
if(num2==0)
r=num1/num2;
catch(ArithmeticException e)
System.out.println("Problem is "+e.getMessage());
}
OUTPUT
9.write a program on multithreading.
class P extends Thread
for(int i=1;i<=5;i++)
System.out.println("Abhishek");
} }
class multithreading {
P t=new P();
t.start();
for(int i=1;i<=5;i++)
System.out.println("Nancy");
} }
Output:
10.write a program of i/o stream
import java.io.*;
class main{
try{
// initializing x to 0
int x = 0;
System.out.print((char)x);
// closing a file
f.close();
catch(Exception e){
// printing exception
System.out.println(e);
Output:
import java.applet.Applet;
g.drawString("Java Programming",200,200);
<html>
<body>
<applet code="Myapplet.class",width="500",height="500"></applet>
</body>
</html>
Output.
12.write a program on file handling.
try
myfile.createNewFile();
catch(Exception e)
e.printStackTrace();
try
fileWrite.close();
catch(Exception e)
e.printStackTrace();
try{
FileReader fileRead =new FileReader("firstfile.txt");
fileRead.read(a);
for(char c:a)
System.out.print(c);
fileRead.close();
catch(IOException e)
System.out.println(e);
Output:
13. Write a program to make a Text Editor.
// Java Program to create a text editor using java
import java.awt.*;
import javax.swing.*;
import java.io.*;
import java.awt.event.*;
import javax.swing.plaf.metal.*;
import javax.swing.text.*;
// Text component
JTextArea t;
// Frame
JFrame f;
// Constructor
editor()
// Create a frame
f = new JFrame("editor");
try {
UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
MetalLookAndFeel.setCurrentTheme(new OceanTheme());
}
catch (Exception e) {
// Text component
t = new JTextArea();
// Create a menubar
mi1.addActionListener(this);
mi2.addActionListener(this);
mi3.addActionListener(this);
mi9.addActionListener(this);
m1.add(mi1);
m1.add(mi2);
m1.add(mi3);
m1.add(mi9);
// Create amenu for menu
mi4.addActionListener(this);
mi5.addActionListener(this);
mi6.addActionListener(this);
m2.add(mi4);
m2.add(mi5);
m2.add(mi6);
mc.addActionListener(this);
mb.add(m1);
mb.add(m2);
mb.add(mc);
f.setJMenuBar(mb);
f.add(t);
f.setSize(500, 500);
f.show();
}
// If a button is pressed
String s = e.getActionCommand();
if (s.equals("cut")) {
t.cut();
else if (s.equals("copy")) {
t.copy();
else if (s.equals("paste")) {
t.paste();
else if (s.equals("Save")) {
int r = j.showSaveDialog(null);
if (r == JFileChooser.APPROVE_OPTION) {
try {
// Create a file writer
// Write
w.write(t.getText());
w.flush();
w.close();
JOptionPane.showMessageDialog(f, evt.getMessage());
else
else if (s.equals("Print")) {
try {
t.print();
JOptionPane.showMessageDialog(f, evt.getMessage());
}
else if (s.equals("Open")) {
int r = j.showOpenDialog(null);
if (r == JFileChooser.APPROVE_OPTION) {
try {
// String
// File reader
// Buffered reader
// Initialize sl
sl = br.readLine();
sl = sl + "\n" + s1;
}
// Set the text
t.setText(sl);
JOptionPane.showMessageDialog(f, evt.getMessage());
else
else if (s.equals("New")) {
t.setText("");
else if (s.equals("close")) {
f.setVisible(false);
// Main class