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

Advance Java File

This document contains 10 Java programs submitted by a student named Dhriti Saluja for their 7th semester Computer Science course. The programs cover topics like inheritance, interfaces, exception handling, applets, polymorphism, threads, 1D and 2D arrays, packages, and file input/output. Each program is accompanied by the output and aims to demonstrate a different Java concept through code examples.

Uploaded by

DHRITI SALUJA
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
64 views

Advance Java File

This document contains 10 Java programs submitted by a student named Dhriti Saluja for their 7th semester Computer Science course. The programs cover topics like inheritance, interfaces, exception handling, applets, polymorphism, threads, 1D and 2D arrays, packages, and file input/output. Each program is accompanied by the output and aims to demonstrate a different Java concept through code examples.

Uploaded by

DHRITI SALUJA
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

MERI- College of Engineering And Technology

Department of Computer Science & Engineering

Session: 2017-2021

ADVANCE JAVA PRACTICAL FILE

Submitted by
Name : Dhriti Saluja
Roll No. : 17/CSE/09
Branch : CSE
Semester : 7th

Submitted to
Mr. Neeraj Kumar
(Assistant Professor CSE Department)
INDEX
S.No. Program Name Faculty
Signature

1 Write a Program to Show Inheritence.

2 Write a Program to Implementing


interface.

3 Write a Program to Show Exception


Handling.

4 Write a Programing to Show the Working


of applets.

5 Write a Program to Show Polymorphism.

6 Write a Program to Implements Threads.

7 Write a Program to Implement One-d


Array.

8 Write a Program to Implement Two-d-


Array.

9 Write a Program to implement package.

10 To Write data On File.


PROGRAM: 1

AIM: Write a program to show inheritance.


class base
{
int a=4;
void square()
{
System.out.println("square:"+(a*a));
}
}
class child extends base
{
int b=8;
void square()
{
System.out.println("square:"+(b*b));
super.square();
}
public static void main(String arg[])
{
child cc=new child();
cc.square();
}
}

Output is:
PROGRAM: 2

AIM: Write a Program to Implementing interface.


interface A
{
void calculate(int a,int b);
}
interface B
{
void calculate(int a,int b);
}
class child3 implements A,B
{
public void calculate(int a,int b)
{
int x,y,z,p;
x=a+b;
y=a*b;
System.out.println("sum:"+x);
System.out.println("mul:"+y);
}
public static void main(String arg[])
{
child3 ch=new child3();
ch.calculate(9,3);
}}

Output is:
PROGRAM: 3

AIM: Write a Program to show Exception Handling.

class exception
{
public static void main(String arg[])
{
java.util.Scanner sc=new java.util.Scanner(System.in);
System.out.println("enter numerator");
int x=sc.nextInt();
System.out.println("enter denominator");
int y=sc.nextInt();
int res;
try
{
res=x/y;
}
catch(ArithmeticException ex)
{
System.out.println("enter the value of denominator greater than 0");
y=sc.nextInt();
res=x/y;
}
System.out.println("result is:"+res);
}
}

Output is:
PROGRAM: 4

AIM: Write a Program to show the working of Applets.

/*
<applet code="grp11.class" height="200" width="200">
</applet>
*/
import java.applet.Applet;
import java.awt.Graphics;
public class grp11 extends Applet
{
String str="Aptech Computer Education";
public void paint(Graphics grp)
{
grp.drawString(str,100,100);
}
}

Output is:
PROGRAM: 5

AIM: Write a Program to show polymorphism.

class base
{
void sum(int a,int b)
{
System.out.println("sum is:"+(a+b));
}
void sum(int a,int b,int c)
{
System.out.println("sum is:"+(a+b+c));
}
void sum(int a,float b,int c,int d)
{
System.out.println("sum is:"+(a+b+c+d));
}
public static void main(String arg[])
{
base bb=new base(); bb.sum(2,3);
bb.sum(10,20,30);
bb.sum(10,22.1f,13,20);
}
}

Output is:
PROGRAM: 6

AIM: Write a Program to implement threads.

class thread2 extends Thread


{
public void run()
{
for(int i=0;i<10;i++)
{
System.out.println(i); try
{
Thread.sleep(2000);
}
catch(Exception ex)
{}
}
}
}
class mainthr
{
public static void main(String arg[])
{
thread2 tt=new thread2(); Thread t=new Thread(tt); t.start();
}

Output is:
PROGRAM: 7

AIM: Write a Program to implement 1-D Array.

import java.util.Scanner; class Onedarray


{
static int total(int x[])
{
int total=0;
for(int i=0;i<x.length;i++) total+=x[i];
return total;
}
public static void main(String s[])
{
Scanner sc=new Scanner(System.in); System.out.println("enter num of array"); int
num=sc.nextInt();
int x[]=new int[num];
System.out.println("enter "+(num)+" elements of array"); for(int i=0;i<x.length;i++)
x[i]=sc.nextInt(); int sum=total(x);
System.out.println("sum is "+sum);
}}

Output is:
PROGRAM: 8

AIM: Write a Program to implement 2-D Array.

import java.util.Scanner;
class Twodarra
{
static int total(int x[][])
{
int total=0;

for(int i=0;i<x.length;i++)
for(int j=0;j<x[i].length;j++)
total+=x[i][j];
return total;
}
public static void main(String s[])
{
Scanner sc=new Scanner(System.in);
System.out.println("enter num of array");
int num=sc.nextInt();
System.out.println("enter size of each array");
int size=sc.nextInt();
int x[][]=new int[num][size];
System.out.println("enter "+(num*size)+" elements of array");
for(int i=0;i<x.length;i++)
for(int j=0;j<x[i].length;j++)
x[i][j]=sc.nextInt();
int sum=total(x); System.out.println("sum is "+sum);
}}

Output is:
PROGRAM: 9

AIM: Write a Program to implement package.

demo.java:
package my; public class demo
{
public void show()
{
System.out.println("show");

}}

test.java :
import my.*; class Test
{
public static void main(String s[])
{
demo d=new demo(); d.show();
}}

Output is:
PROGRAM: 10

AIM: Write a Program to write data on a file.

import java.io.*; import java.util.*;


class fileoutputstreamtest
{
public static void main(String s[])
{
try
{
Scanner sc=new Scanner(System.in);
System.out.println("enter data");
String data=sc.nextLine();
File f=new File("output.txt");

FileOutputStream fos=new FileOutputStream(f);


byte b[]=data.getBytes();
fos.write(b);
fos.close();
System.out.println("data written on file successfully");
}
catch(Exception e)
{
e.printStackTrace();
}}}

Output is:

You might also like