Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
JAVAPGMS.docx
1. JAVA LAB B.C.A IV SEM (CBCS)
A.S.PATIL COLLEGE OF COMMERCE [AUTONOMOUS], VIJAYPUR
B.C.A PROGRAMME
ASSIGNMENT-1
/*PROGRAM TO DEMONSTRATE TYPECASTING IN JAVA*/
class Typecast
{
public static void main(String args[])
{
System.out.println("Variables created");
byte b=50;
short s =1996;
int i=123456789;
long l=1234567654321L;
float f1=3.142F;
double d2=9.8700000;
System.out.println("b="+b);
System.out.println("s="+s);
System.out.println("i="+i);
System.out.println("l="+l);
System.out.println("f1="+f1);
System.out.println("d2="+d2);
System.out.println("Types converted");
int a=b;
double x=i;
byte t=(byte)d2;
short s1=(short)f1;
System.out.println("Conversion from byte to int[int a=b]="+a);
System.out.println("Conversion from int to double[double x=i] ="+x);
System.out.println("Conversion from doubleto byte[byte
t=(byte)d2]="+t);
System.out.println("Conversion from floatto short[short
s1=(short)f1]="+s1);
}
}
2. JAVA LAB B.C.A IV SEM (CBCS)
A.S.PATIL COLLEGE OF COMMERCE [AUTONOMOUS], VIJAYPUR
B.C.A PROGRAMME
OUTPUT
C:jdk1.6.0_02bin>javac Typecast.java
C:jdk1.6.0_02bin>java Typecast
Variables created
b=50
s=1996
i=123456789
l=1234567654321
f1=3.142
d2=9.87
Types converted
Conversion from byte to int[int a=b]=50
Conversion from int to double[double x=i] =1.23456789E8
Conversion from double to byte[byte t=(byte)d2] =9
Conversion from float to short[shorts1=(short)f1]=3
3. JAVA LAB B.C.A IV SEM (CBCS)
A.S.PATIL COLLEGE OF COMMERCE [AUTONOMOUS], VIJAYPUR
B.C.A PROGRAMME
ASSIGNMENT-2
/*PROGRAM TO IMPLEMENT ALL BITWISE OPERATIONS BY READING
THE INPUT BY USER */
import java.util.Scanner;
public class operators
{
public static void main (String args[])
{
System.out.println ("Enter the value of a");
Scanner scanner = new Scanner(System.in);
int a = scanner.nextInt ();
System.out.println ("Enter the value of b");
int b = scanner.nextInt ();
System.out.println ("a&b = " + (a & b));
System.out.println ("a|b = " + (a | b));
System.out.println ("a^b = " + (a ^ b));
System.out.println ("~a = " + ~a);
System.out.println ("a<<1 = " + (a << 1));
System.out.println ("a>>1 = " + (a >> 1));
}
}
4. JAVA LAB B.C.A IV SEM (CBCS)
A.S.PATIL COLLEGE OF COMMERCE [AUTONOMOUS], VIJAYPUR
B.C.A PROGRAMME
OUTPUT
C:jdk1.6.0_02bin>javac operators.java
C:jdk1.6.0_02bin>java operators
Enter the value of a
5
Enter the value of b
7
a&b = 5
a|b = 7
a^b = 2
~a = -6
a<<1 = 10
a>>1 = 2
5. JAVA LAB B.C.A IV SEM (CBCS)
A.S.PATIL COLLEGE OF COMMERCE [AUTONOMOUS], VIJAYPUR
B.C.A PROGRAMME
ASSIGNMENT-3
/*PROGRAM TO IMPLEMENT AT LEAST 10 STRING OPERATIONS ON
STRINGS. */
class StringOperation
{
public static void main(String args[])
{
String s1="Hello";
String s2="World";
System.out.println("The strings are "+s1+"
and "+s2+"");
int len1=s1.length();
int len2=s2.length();
System.out.println("The length of "+s1+" is
:"+len1);
System.out.println("The length of "+s2+" is
:"+len2);
System.out.println("The concatenation of two
strings = "+s1.concat(s2));
6. JAVA LAB B.C.A IV SEM (CBCS)
A.S.PATIL COLLEGE OF COMMERCE [AUTONOMOUS], VIJAYPUR
B.C.A PROGRAMME
System.out.println("First character of
"+s1+"is="+s1.charAt(0));
System.out.println("The uppercase of
"+s1+"is="+s1.toUpperCase());
System.out.println("The lower case of
"+s2+"is="+s2.toLowerCase());
System.out.println("The letter e occurs at
position"+s1.indexOf("e")+"in"+s1);
System.out.println("Substring of
"+s1+"starting from index 2 and ending at 4 is
= "+s1.substring(2,4));
System.out.println("Replacing 'e' with 'o' in
"+s1+"is ="+s1.replace('e','o'));
boolean check = s1.equals(s2);
if(check==false)
System.out.println(""+s1+" and "+s2+" are
not same");
else
System.out.println("" + s1+" and " + s2+"are
same");
}
7. JAVA LAB B.C.A IV SEM (CBCS)
A.S.PATIL COLLEGE OF COMMERCE [AUTONOMOUS], VIJAYPUR
B.C.A PROGRAMME
}
OUTPUT
C:jdk1.6.0_02bin>javac StringOperation.java
C:jdk1.6.0_02bin>java StringOperation
The strings are Hello and World
The length of Hello is: 5
The length of World is: 5
The concatenation of two strings = HelloWorld
First character of Hellois=H
The uppercase of Hellois=HELLO
The lower case of Worldis=world
The letter e occurs at position1inHello
Substring of Hellostarting from index 2 and ending at 4 is = ll
Replacing 'e' with 'o' in Hellois =Hollo
Hello and World are not same
8. JAVA LAB B.C.A IV SEM (CBCS)
A.S.PATIL COLLEGE OF COMMERCE [AUTONOMOUS], VIJAYPUR
B.C.A PROGRAMME
ASSIGNMENT-4
/*PROGRAM TO DEMONSTRATE MULTILEVEL INHERITANCE. SHOW
THE USAGE OF SUPER ()*/
class Vehicle
{
int maxSpeed = 120;
}
/* sub class Car extending vehicle */
class Car extends Vehicle
{
int maxSpeed = 180;
void display()
{
System.out.println("Maximum Speed: " + super.maxSpeed);
}
}
class driver extends Car
{
int maxSpeed = 200;
void display()
{
System.out.println("Maximum Speed:" + super.maxSpeed);
}
class Test
{
public static void main(String[] args)
{
9. JAVA LAB B.C.A IV SEM (CBCS)
A.S.PATIL COLLEGE OF COMMERCE [AUTONOMOUS], VIJAYPUR
B.C.A PROGRAMME
driver small = new driver();
small.display();
}
}
OUTPUT
C:jdk1.6.0_02bin>javac Test.java
C:jdk1.6.0_02bin>java Test
Maximum Speed:180
10. JAVA LAB B.C.A IV SEM (CBCS)
A.S.PATIL COLLEGE OF COMMERCE [AUTONOMOUS], VIJAYPUR
B.C.A PROGRAMME
ASSIGNMENT-5
/*PROGRAM TO DEMONSTRATE METHOD OVERRIDING*/
class Super
{
int x;
Super (int x)
{
this.x = x;
}
void display ()
{
System.out.println (“super x=”+x);
}
}
class Sub extends Super
{
int y;
Sub (int x, int y)
{
super(x);
this.y = y;
}
void display ()
{
System.out.println (“Super x =”+x);
System.out.println (“Sub y =”+y);
}
11. JAVA LAB B.C.A IV SEM (CBCS)
A.S.PATIL COLLEGE OF COMMERCE [AUTONOMOUS], VIJAYPUR
B.C.A PROGRAMME
}
class Override
{
public static void main (String args [])
{
Sub s1=new Sub (100,200);
s1.display ();
}
}
12. JAVA LAB B.C.A IV SEM (CBCS)
A.S.PATIL COLLEGE OF COMMERCE [AUTONOMOUS], VIJAYPUR
B.C.A PROGRAMME
OUTPUT
C:jdk1.6.0_02bin>javac Override.java
C:jdk1.6.0_02bin>java Override
Super x=100
Sub y=200
13. JAVA LAB B.C.A IV SEM (CBCS)
A.S.PATIL COLLEGE OF COMMERCE [AUTONOMOUS], VIJAYPUR
B.C.A PROGRAMME
ASSIGNMENT-6
/* PROGRAM TO DEMONSTRATE CONSTRUCTOR OVERLOADING BY
PASSINGDIFFERENTNUMBER OF PARAMETERS OFDIFFERENTTYPES*/
class Box
{
double width, height, depth;
Box(double w, double h, double d)
{
width = w;
height = h;
depth = d;
}
Box()
{
width = height = depth = 0;
}
Box(double len)
{
width = height = depth = len;
}
double volume()
{
return width * height * depth;
}
}
class cons
{
public static void main(String args[])
{
Box mybox1 = new Box(10, 20, 15);
14. JAVA LAB B.C.A IV SEM (CBCS)
A.S.PATIL COLLEGE OF COMMERCE [AUTONOMOUS], VIJAYPUR
B.C.A PROGRAMME
Box mybox2 = new Box();
Box mycube = new Box(7);
double vol;
vol = mybox1.volume();
System.out.println(" Volume of mybox1 is " + vol);
vol = mybox2.volume();
System.out.println(" Volume of mybox2 is " + vol);
vol = mycube.volume();
System.out.println(" Volume of mycube is " + vol);
}
}
15. JAVA LAB B.C.A IV SEM (CBCS)
A.S.PATIL COLLEGE OF COMMERCE [AUTONOMOUS], VIJAYPUR
B.C.A PROGRAMME
OUTPUT
C:jdk1.6.0_02bin>javac cons.java
C:jdk1.6.0_02bin>java cons
Volume of mybox1 is 3000.0
Volume of mybox2 is 0.0
16. JAVA LAB B.C.A IV SEM (CBCS)
A.S.PATIL COLLEGE OF COMMERCE [AUTONOMOUS], VIJAYPUR
B.C.A PROGRAMME
ASSIGNMENT-7
/*PROGRAM TO DEMONSTRATE a) PACKAGES b)INTERFACES */
a)PACKAGES
//class A.java
/* creating package*/
package package1;
public class class A
{
public void display A()
{
System.out.println(“hello class A”);
}
}
/*In cmd prompt type
javac classA.java {only compile}
In bin folder, create folder called package1
CopyclassA.java and class A. class into package 1
{ both pgm & class file }*/
/*packageTest.java*/
import package1.class A;{Accessing package
class packageTest
{
public static void main(string args{})
{
Class A objA=new class A();
17. JAVA LAB B.C.A IV SEM (CBCS)
A.S.PATIL COLLEGE OF COMMERCE [AUTONOMOUS], VIJAYPUR
B.C.A PROGRAMME
objA.displayA();
}
}
OUTPUT
C:jdk1.6.0_02bin>javac packageTest.java
C:jdk1.6.0_02bin>java packageTest
Hello class A
18. JAVA LAB B.C.A IV SEM (CBCS)
A.S.PATIL COLLEGE OF COMMERCE [AUTONOMOUS], VIJAYPUR
B.C.A PROGRAMME
b)INTERFACES
interface emp
{
public void salary();
int increment=10;
}
class interfacetest implements emp
{
public void salary()
{
System.out.println(“salary of the employee is: ”+(increment+5000));
}
public static void main(String args[])
{
interfacetest obj1=new interfacetest();
obj1.salary();
}
}
19. JAVA LAB B.C.A IV SEM (CBCS)
A.S.PATIL COLLEGE OF COMMERCE [AUTONOMOUS], VIJAYPUR
B.C.A PROGRAMME
OUTPUT
C:jdk1.6.0_02bin>javac interfacetest.java
C:jdk1.6.0_02bin>java interfacetest
Salary of the employee is: 5010
20. JAVA LAB B.C.A IV SEM (CBCS)
A.S.PATIL COLLEGE OF COMMERCE [AUTONOMOUS], VIJAYPUR
B.C.A PROGRAMME
ASSIGNMENT-8
/*PROGRAM TO DEMONSTRATE THREAD PRIORITIES. CREATE THE
THREAD BY EXTENDING THREAD CLASS*/
class A extends Thread
{
public void run ( )
{
for (int i=1;i<=5;i++)
{
System.out.println ("A:"+i );
}
System.out.println ("exit from A");
}
}
class B extends Thread
{
public void run ( )
{
for (int i=1;i<=5;i++)
{
System.out.println ("B:"+i );
}
System.out.println ("exit from B");
}
}
class prioritytest
{
public static void main ( String args[] )
{ A a = new A( );
B b = new B( );
21. JAVA LAB B.C.A IV SEM (CBCS)
A.S.PATIL COLLEGE OF COMMERCE [AUTONOMOUS], VIJAYPUR
B.C.A PROGRAMME
a.setPriority (10);
b.setPriority (1);
a. start( );
b. start( );
}
}
OUTPUT
C:jdk1.6.0_02bin>javac prioritytest.java
C:jdk1.6.0_02bin>java prioritytest
A:1
A:2
A:3
A:4
A:5
exit from A
B:1
B:2
B:3
B:4
B:5
exit from B
22. JAVA LAB B.C.A IV SEM (CBCS)
A.S.PATIL COLLEGE OF COMMERCE [AUTONOMOUS], VIJAYPUR
B.C.A PROGRAMME
ASSIGNMENT-9
/*PROGRAM TO DEMONSTRATE THE FOLLOWING*/
A) ABSTRACT CLASS
abstract class parent class
{
abstract void meth1 ();
void meth2 ();
{
System.out.println (“method of abstract class”);
}
}
class child class extends parent class
{
void meth1 ()
{
System.out.println (“method of child class”);
}
}
class abstracttest
{
public static void main (String args [])
{
child_class c1=new child_class ();
C1.meth1 ();
C1.meth2 ();
}
}
23. JAVA LAB B.C.A IV SEM (CBCS)
A.S.PATIL COLLEGE OF COMMERCE [AUTONOMOUS], VIJAYPUR
B.C.A PROGRAMME
OUTPUT:
C:jdk1.6.0_02bin>javac abstracttest.java
C:jdk1.6.0_02bin> java abstracttest
Method of child class
Method of abstract class
24. JAVA LAB B.C.A IV SEM (CBCS)
A.S.PATIL COLLEGE OF COMMERCE [AUTONOMOUS], VIJAYPUR
B.C.A PROGRAMME
B) INNER CLASS
class Nesting
{
int m,n;
nesting (int x,int y) //constructor method
{
m=x;
n=y;
}
int largest()
{
if (m>=n)
return (m);
else
return(n);
}
void display()
{
int large = larest()
System.out.println(“largest value = “ +large);
}
}
class NestingTest
{
public static void main(String args[])
{
Nesting nest=NewNesting(50,40);
Nest.display();
}
25. JAVA LAB B.C.A IV SEM (CBCS)
A.S.PATIL COLLEGE OF COMMERCE [AUTONOMOUS], VIJAYPUR
B.C.A PROGRAMME
OUTPUT:
C:jdk1.6.0_02bin>javac NestingTest.java
C:jdk1.6.0_02bin> java NestingTest
Largest value=50
26. JAVA LAB B.C.A IV SEM (CBCS)
A.S.PATIL COLLEGE OF COMMERCE [AUTONOMOUS], VIJAYPUR
B.C.A PROGRAMME
ASSIGNMENT-10
/* WRITE A JAVA PROGRAM TODRAW OVAL, RECTANGLE, LINE AND FILL
THE COLOR IN IT AND DISPLAY IT ON APPLET */
import java.applet.*;
import java.awt.*;
public class GApplet extends Applet
{
public void paint(Graphics g)
{
g.drawLine(20, 20, 500, 20);
g.drawRect(20, 40, 200, 40);
g.fillRect(300, 40, 200, 40);
g.drawRoundRect(20, 100, 200, 40, 10, 10);
g.fillRoundRect(300, 100, 200, 40, 10, 10);
g.setColor(Color.RED);
g.drawOval(20, 160, 200, 100);
g.fillOval(300, 160, 200, 100);
}
}
/*
<applet code="GApplet" height="500" width="700" border="1">
</applet>
*/
27. JAVA LAB B.C.A IV SEM (CBCS)
A.S.PATIL COLLEGE OF COMMERCE [AUTONOMOUS], VIJAYPUR
B.C.A PROGRAMME
OUTPUT: