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

Abhishek Java Practical

Uploaded by

girikiran1977
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)
28 views

Abhishek Java Practical

Uploaded by

girikiran1977
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/ 36

INDEX

S.NO. 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;

System.out.println("Sum is :"+(x+y));

public class simple_inheritance {

public static void main(String[] args) {

B ob=new B();

ob.add();

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;

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));

public class multilevel_inheritance {

public static void main(String[] args) {

B ob=new B();

ob.add();

C ob1=new C();

ob1.sub();
ob1.add();

OUTPUT
Hybrid Inheritance
package com.company;

class NUM

public int x=24,y=45;

class B extends NUM{

void add()

int x=super.x;

int y=super.y;

System.out.println("Sum is :"+(x+y));

class C extends NUM{

void sub()

int a=super.x;

int b=super.y;

System.out.println("Sub is :"+(b-a));

public class hybrid_inheritance {

public static void main(String[] args) {

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;

abstract class Class1

abstract int area();

void display()

System.out.println("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=ob.area();

int b=ob1.area();

ob.display();

System.out.println("Area of Square is : "+a);

System.out.println("Area of rectangle is : "+b);

OUTPUT
Program 3: Write a program of Interface.
package com.company;

interface xyz

void second();

interface lmn

void first();

class myclass implements lmn,xyz

public void first()

System.out.println("first method");

public void second()

System.out.println("second method");

public class interface1 {

public static void main(String[] args) {

myclass ob=new myclass();

ob.first();
ob.second();

OUTPUT
Program 4:-Write a program of Method Overidding .
package com.company;

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();


System.out.println("SBI interest is: "+s.interest());

System.out.println("PNB interest is "+p.interest());

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()

System.out.println("my first method");

public void two()

System.out.println("this is my second method");

public class multilevel

public static void main(String[] args)

inf1 ob1=new abc();

inf2 ob2=new abc();

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);

void add(int x,int y)

int c;

c=x+y;

System.out.println(c);

void add(int x,double y)

double c;

c=x+y;

System.out.println(c);

public static void main(String[] args) {

metodoverloding r=new metodoverloding();

r.add();

r.add(100,200);

r.add(50,45.32);

}
}

Output:
7.write a program on scientific calculator.
import java.util.*;

public class ScientificCalculator {

public static void main(String args[])

double x,y,c;

int ch, choice;

System.out.println("\n Enter two numbers");

Scanner sc=new Scanner(System.in);

x=sc.nextDouble();

y=sc.nextDouble();

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

System.out.println("1. Simple Calculator" + "\n" + "2. Scientific Calculator" + "\n");

ch=sc.nextInt();

if(ch==1)

System.out.println("1. Addition" + "\n" + "2. Subtraction" + "\n" + "3. Multiplication" + "4.


Division" + "\n");

choice=sc.nextInt();

//if(ch==1)

//{

switch(choice)

case 1:

c=x+y;

System.out.println("Addition of two numbers is: " + c); break;

case 2: c=x-y;

System.out.println("Subtraction of two numbers is:" + c); break;

case 3: c=x*y;
System.out.println("Multiplication of two numbers is:" + c); break;

case 4: c=x/y;

System.out.println("Division of two numbers is:" + c); break;

default:

System.out.println("\n Enter values between 1 to 4"); break;

else if(ch==2)

System.out.println("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=sc.nextInt();

switch(choice)

case 1: c=Math.sin(x);

System.out.println("Sine of a number is:" + c);

break;

case 2: c=Math.cos(x);

System.out.println("Cosine of a number is" + c);

break;

case 3: c=Math.tan(x);

System.out.println("The tangent of a number is:"+c);

break;
case 4: c=Math.cos(x)/Math.sin(x);c=1/Math.tan(x);

System.out.println("The Cot of a number is: "+ c);

break;

case 5. c=1/Math.cos(x);

System.out.println("The Secant of a number is: "+c);

break;

case 6:

case 7: c=Math.sqrt(x);

System.out.println("Square root of a number is" + c);

break;

case 8: c=Math.pow(x, y);

System.out.println("The Exponentiation of number is: "+ c);

break;

default : System.out.println("enter a valid value");

break;

Output:
8.write a program on exception handling.
import java.util.Scanner;

class throwDemo

public static void main(String[] args)

Scanner sc=new Scanner(System.in);

int num1,num2,r;

System.out.println("Enter any two numbers");

num1=sc.nextInt();

num2=sc.nextInt();

try

if(num2==0)

throw new ArithmeticException("/ by zero");

r=num1/num2;

System.out.println(num1+"/" + num2 + "="+ r);

catch(ArithmeticException e)

System.out.println("Problem is "+e.getMessage());

}
OUTPUT
9.write a program on multithreading.
class P extends Thread

public void run()

for(int i=1;i<=5;i++)

System.out.println("Abhishek");

} }

class multithreading {

public static void main(String[] args) {

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{

public static void main(String[] args) throws IOException {

try{

// loading a file into f variable

FileInputStream f = new FileInputStream("input.txt");

// initializing x to 0

int x = 0;

// while loop untill the end of the file.

while ((x = f.read())!=-1){

// printing the character

System.out.print((char)x);

// closing a file

f.close();

catch(Exception e){

// printing exception

System.out.println(e);

Output:

Hello I am Soham Medewar%


11.write a program on applet.
import java.awt.*;

import java.applet.Applet;

public class Myapplet extends Applet

public void paint(Graphics g)

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.

public class file

public static void main(String[] args )

File myfile=new File("firstfile.txt");

try

myfile.createNewFile();

catch(Exception e)

System.out.println("unable to create file");

e.printStackTrace();

try

FileWriter fileWrite=new FileWriter("firstfile.txt");

fileWrite.write("This is first file from this java course");

fileWrite.close();

catch(Exception e)

e.printStackTrace();

try{
FileReader fileRead =new FileReader("firstfile.txt");

char[] a=new char[50];

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.*;

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

UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");

// Set theme to ocean

MetalLookAndFeel.setCurrentTheme(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

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

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

mi4.addActionListener(this);

mi5.addActionListener(this);

mi6.addActionListener(this);

m2.add(mi4);

m2.add(mi5);

m2.add(mi6);

JMenuItem mc = new JMenuItem("close");

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

public void actionPerformed(ActionEvent e)

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")) {

// Create an object of JFileChooser class

JFileChooser j = new JFileChooser("f:");

// Invoke the showsSaveDialog function to show the save dialog

int r = j.showSaveDialog(null);

if (r == JFileChooser.APPROVE_OPTION) {

// Set the label to the path of the selected directory

File fi = new File(j.getSelectedFile().getAbsolutePath());

try {
// Create a file writer

FileWriter wr = new FileWriter(fi, false);

// Create buffered writer to write

BufferedWriter w = new BufferedWriter(wr);

// Write

w.write(t.getText());

w.flush();

w.close();

catch (Exception evt) {

JOptionPane.showMessageDialog(f, evt.getMessage());

// If the user cancelled the operation

else

JOptionPane.showMessageDialog(f, "the user cancelled the operation");

else if (s.equals("Print")) {

try {

// print the file

t.print();

catch (Exception evt) {

JOptionPane.showMessageDialog(f, evt.getMessage());

}
else if (s.equals("Open")) {

// Create an object of JFileChooser class

JFileChooser j = new JFileChooser("f:");

// Invoke the showsOpenDialog function to show the save dialog

int r = j.showOpenDialog(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(j.getSelectedFile().getAbsolutePath());

try {

// String

String s1 = "", sl = "";

// File reader

FileReader fr = new FileReader(fi);

// Buffered reader

BufferedReader br = new BufferedReader(fr);

// Initialize sl

sl = br.readLine();

// Take the input from the file

while ((s1 = br.readLine()) != null) {

sl = sl + "\n" + s1;

}
// Set the text

t.setText(sl);

catch (Exception evt) {

JOptionPane.showMessageDialog(f, evt.getMessage());

// If the user cancelled the operation

else

JOptionPane.showMessageDialog(f, "the user cancelled the operation");

else if (s.equals("New")) {

t.setText("");

else if (s.equals("close")) {

f.setVisible(false);

// Main class

public static void main(String args[])

editor e = new editor();

You might also like