0% found this document useful (0 votes)
31 views12 pages

BCA-2 Java Programming - Record

Uploaded by

nagesh_salla
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)
31 views12 pages

BCA-2 Java Programming - Record

Uploaded by

nagesh_salla
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/ 12

BCA II YEAR III SEM

PRACTICAL QUESTION BANK WITH SOLUTIONS


SUBJECT: JAVA PROGRAMMING
Lab Experiments
Page
S. No Name of the Program
No.
1 Write a Java Program to find largest of three numbers
2 Write a Java Program to find GCD of given two numbers

Write a Java Program to calculate area of a rectangular room using


3
Class

4 Write a program to implement method overloading

5 Write a Program to demonstrate constructors

6 Write a program to find sum of elements of array

7 Program to demonstrate StringTokenizer


8 Write a program to demonstrate single Inheritance

9 Write a program to implement method overriding

10 Write a program to create a package and using a package

11 Write a program creating customized exception


12 Write a program for creating new thread by extending Thread class

Write a program for creating new thread by implementing Runnable


13
interface

14 Write a program to add AWT components to Frame.

15 Write a java Program to create simple Applet

16 Write a program to read data from a file and write to a file


1. Write a Java Program to find largest of three numbers
import java.util.Scanner;
class largest
{
public static void main(String ar[])
{
Scanner s=new Scanner(System.in);
int a,b,c;
System.out.println(“Enter three numbers”);
a=s.nextInt();
b=s.nextInt();
c=s.nextInt();
if(a>b)
{
if(a>c)
{
System.out.println("largest" +a);
}
else
{
System.out.println("largest" +c);
}
}
else
{
if(b>c)
{
System.out.println("largest" +b);
}
else
{
System.out.println("largest" +c);
}
}
}
}
2. Write a Java Program to find GCD of given two numbers
import java.util.Scanner;
class Gcd
{
public static void main(String ar[])
{
Scanner s=new Scanner(System.in);
int a,b;
System.out.println(“Enter two numbers”);
a=s.nextInt();
b=s.nextInt();
int g=0,r;
do
{
r=a%b;
if(r==0)
g=b;
else
{
a=b;
b=r;
}
}while(r!=0)
System.out.println("Gcd="+g);
}
}

3. Write a Java Program to calculate area of a rectangular room using class


class Rectangle
{
int length, breadth;
void getdata(int x,int y)
{
length=x;
breadth=y;
}
void area()
{
int a=length*breadth;
System.out.println(“Area=”+a);
}
}
class Room
{
public static void main(String ar[])
{
Rectangle r1=new Rectangle();
Rectangle r2=new Rectangle();
r1.getdata(30,15);
r2.getdata(22,10);
System.out.println(“Area of Room1”);
r1.area();
System.out.println(“Area of Room2”);
r2.area();
}
}

4. Write a program to implement method overloading


class overloading
{
void sum(int x,int y)
{
int c=x+y;
System.out.println("sum="+c);
}
void sum(int x,double y)
{
double c=x+y;
System.out.println("sum="+c);
}
void sum(double x,double y)
{
double c=x+y;
System.out.println("sum="+c);
}
public static void main(String ar[])
{
overloading a=new overloading();
a.sum(10.5,20.5);
a.sum(10,20);
a.sum(10,20.5);
}
}
5. Write a Program to demonstrate constructors
class Rectangle
{
int l,b;
Rectangle()
{
l=25;
b=10;
}
Rectangle(int x,int y)
{
l=x;
b=y;
}
void area()
{
int a=l*b;
System.out.println("area="+a);
}
void perimeter()
{
int p=2*(l+b);
System.out.println("perimeter="+p);
}
}
class Rect
{
public static void main(String ar[])
{
Rectangle r1=new Rectangle();
Rectangle r2=new Rectangle(45,15);
r1.area();
r1.perimeter();
r2.area();
r2.perimeter();
}
}
6. Write a program to find sum of elements of array.
import java.util.Scanner;
class Sum
{
public static void main(String ng[])
{
Scanner scan=new Scanner(System.in);
int a[]=new int[10];
int n,s=0;
System.out.println("Enter size of array");
n=scan.nextInt();
System.out.println("Enter array elements");
for(int i=0;i<n;i++)
{
a[i]=scan.nextInt();
}
for(int i=0;i<n;i++)
{
s=s+a[i];
}
System.out.println("Sum of elements of array="+s);
}
}
7. Program to demonstrate StringTokenizer
import java.util.StringTokenizer;
class Splitting
{
public static void main(String ng[])
{
String str="Welcome to Department of Computer Science";
StringTokenizer tokens=new StringTokenizer(str);
while(tokens.hasMoreTokens())
{
System.out.println(tokens.nextToken());
}
}
}
8. Write a program to demonstrate single Inheritance
class Rectangle
{
int l,b;
void getdata(int x,int y)
{
l=x;
b=y;
}
void area()
{
int a=l*b;
System.out.println("Area="+a);
}
void perimeter()
{
int p=2*(l+b);
System.out.println("perimeter="+p);
}
}
class Rect extends Rectangle
{
int h;
void readdata(int x)
{
h=x;
}
void volume()
{
int v=l*b*h;
System.out.println("volume="+v);
}
}
class Single
{
public static void main(String ar[])
{
Rect r=new Rect();
r.getdata(40,15);
r.readdata(12);
r.area();
r.perimeter();
r.volume();
}
}
9. Write a program to implement method overriding
class A
{
void show()
{ System.out.println("super class");
}
}
class B extends A
{
void show ()
{ System.out.println("subclass");
}
}
class Override
{
public static void main(String ar[])
{
B ob=new B();
ob.show();
}
}

10. Write a program to create a package and using a package


// Program to create a arithmetic package

package arithmetic;
public class Airth
{
public int sum(int x,int y)
{
return(x+y);
}
public int sub(int x,int y)
{
return(x-y);
}
public int product(int x,int y)
{
return(x*y);
}
public int quotient(int x,int y)
{
return(x/y);
}
public int mod(int x,int y)
{
return(x%y);
}
}
// Program to use the arithmetic package
import arithmetic.Airth;
class pack
{
public static void main(String args[])
{
int x,y,a,b;
Airth o=new Airth();
x=20;y=10;
a=o.sum(x,y);
b=o.sub(x,y);
System.out.println("sum="+a);
System.out.println("subrt="+b);
}
}
11. Write a program creating customized exception
class MarksOutOfbound extends Exception
{
MarksOutOfbound(String msg)
{
super(msg);
}
}
class Testexception
{
public static void main(String ar[])
{
int m;
m=98;
try
{
if(m>100)
{
String msg="invalid marks";
throw new MarksOutOfbound(msg);
}
System.out.println("marks="+m);
}
catch(MarksOutOfbound e)
{
System.out.println(e.getMessage());
}
System.out.println("program completed");
}
}

12. Write a program for creating new thread by extending Thread class
class even extends Thread
{
public void run()
{
for(int i=2;i<=10;i=i+2)
{
System.out.println("even"+i);
}
}
}
class odd extends Thread
{
public void run ()
{
for(int i=1;i<=10;i=i+2)
{
System.out.println("odd"+i);
}
}
}
class TestThread
{
public static void main(String ar[])
{
even e=new even();
odd o=new odd();
o.start();
e.start();
}
}

13. Write a program for creating new thread by implementing Runnable interface
class even implements Runnable
{
public void run()
{
for(int i=2;i<=10;i=i+2)
{
System.out.println("even"+i);
}
}
}
class odd implements Runnable
{
public void run()
{
for(int i=1;i<=10;i=i+2)
{
System.out.println("odd"+i);
}
}
}
class Demothread
{
public static void main(String ar[])
{
even e=new even();
odd o=new odd();
Thread t1=new Thread(e);
Thread t2=new Thread(o);
t1.start();
t2.start();
}
}
14. Program to add AWT Components to Frame
import java.awt.*;
import java.awt.event.*;
public class Demoawt extends Frame
{
public Demoawt()
{
setTitle("AWT Component Addition");
Label lb=new Label("Enter Your Name");
lb.setBounds(50,50,100,50);
add(lb);
TextField tf = new TextField();
tf.setBounds(200, 50, 100, 25);
add(tf);
Button cb = new Button("Click Me");
cb.setBounds(80, 150, 180, 30);
add(cb);
setLayout(null);
setSize(300, 200);
setVisible(true);
}

15. Write a java Program to create simple Applet


import java.applet.*;
import java.awt.*;
public class demoapplet extends Applet
{
public void paint(Graphics g)
{
setBackground(Color.green);
setForeground(Color.red);
g.drawString("welcome to",50,60);
g.drawString("neelagiri",50,70);
}
}
/*<Applet code="demoapplet.class" height=500 width=500>
</Applet>
*/
16. Write a program to read data from a file and write to a file

import java.io.*;
class demofile
{
public static void main(String ar[])throws IOException
{
File f=new File("sample.txt");
byte b[]={};
if(f.exists())
{
FileInputStream fs=new FileInputStream(f);
int n=fs.available();
b=new byte[n];
fs.read(b);
String s=new String(b);
System.out.println("data in file:"+s);
}
else
{
System.out.println("File not found read");
System.exit(0);
}
f=new File("XYZ.txt");
if(f.exists())
{
FileOutputStream fo=new FileOutputStream(f);
fo.write(b);
System.out.println("Data written into file");
}
else
{
System.out.println("File not found to write");
}
}
}

You might also like