Java Practical File
Java Practical File
SUBJECT:
JAVA
201503
2
Index
S.NO. TOPIC PAGE NO.
1 Print message 5
2 Import package lang in java 7
3 Arithmetic operators 9
4 Relational operators 11
5 Increment operators 13
6 Conditional operators 15
7 Casting Operators 17
8 If Statement 19
9 If else 21
10 Else if 23
11 Nested if 25
12 Switch statement 27
13 While statement 29
14 Do while statement 31
15 For loop 33
16 Break statement and continue 35
17 Method Declaration 37
18 Constructor 39
19 Method overloading 41
20 String member 43
21 Nesting of methods 45
22 Method Overriding in inheritance 47
23 Sorting a list of number 49
24 Two dimensional array 51
3
Output:
4
Input:
class Harsh
{
5
System.out.println("Hello");
Output:
6
Input:
import java.lang.Math;
class bca
7
double x=10,y;
y=Math.sqrt(x);
System.out.println("y= "+y);
</body>
</html>
Output:
8
Input:
class test
{
9
int a=10,b=20;
System.out.println("a= "+a);
System.out.println("b= "+b);
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/b= "+(a/b));
Output:
Input:
classRelationaloperators
11
}
}
Output:
Input:
classIncrementoperator
13
Output:
14
Input:
15
class Arth
Output:
Input:
class Casting
17
float sum;
int i;
sum = 0.0f;
Program 8: If Statement
18
Output:
Input:
{
19
int a=10;
if(a==10)
System.out.println("true");
Output:
20
Input:
class ifelse
{
21
if ((number[i] % 2) == 0){
even += 1; }
else
odd += 1;
Output:
22
Input:
class Elseifladder
{
23
else
Output:
Input:
class Nesting{
int m, n;
25
m = x;
n = y;
int largest( )
if (m >= n)
return(m);
else
return(n);
void display( ){
class Nesting1{
next.display();
Output:
Input:
class swi
{
27
int ch=2;
switch (ch )
break;
break;
break;
Output:
28
Input:
class whiletest
{
29
char c;
System.out.println("enter a string");
try
string.append(c);
catch (Exception e)
System.out.println("Error in input");
System.out.println(string);
Output:
Input:
{
31
row = 1;
do
column = 1;
do
y = row * column;
column = column + 1;
System.out.println("\n");
row = row + 1;
Output:
32
Input:
class forloop
{
33
long p;
int n;
double q;
p = 1;
if(n==0)
p = 1;
else
p = p * 2;
q = 1.0 / (double)p;
Output:
34
Input:
class ContinueBreak
{
35
System.out.println(" ");
if (i>=10) break;
if(j == i)
continue Loop1;
System.out.println("Termination by BREAK");
Output:
Input:
class access{
37
int x;
void m1(){
int y;
x=10;
y=x;
System.out.println("x= "+x);
System.out.println("y= "+y);
class met
a.m1();
Output:
Input:
class rect
39
rect()
rect(int x,int y)
int z=x*y;
System.out.println("Area is "+z);
class ar
Output:
Input:
class Adder{
41
class over1{
System.out.println(Adder.add(11,11));
System.out.println(Adder.add(11,11,11));
}}
Output:
Input:
class Mathoperation
43
return (x*y);
return c/d;
class Math
Output:
Input:
45
class Nesting
{
int m, n;
Nesting (int x, int y)
{
m = x;
n = y;
}
int largest( )
{
if (m >= n)
return(m);
else
return(n);
}
void display( )
{
int large = largest ( );
System.out.println("Largest value = " + large);
}
}
class Nestingtest
{
public static void main(String[] args)
{
Nesting next = new Nesting(60, 80);
next.display();
}
Output:
Input:
47
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);
}
}
class Over
{
public static void main(String[] args) {
Sub s1 = new Sub(100,200);
s1.display();
}
}
Program 23: Sorting a List of Numbers
48
Output:
Input
49
class sort
{
public static void main (String args [])
{
int i,j;
int number[] = {57, 80, 78};
int n = number.length;
System.out.print("given list : ");
for (i=0; i<n; i++){
System.out.print(" " + number[i]);
}
System.out.println("\n");
//Sorting begins
for (i = 0; i < n; i++)
{
for ( j = i+1; j < n; j++){
if (number [i] < number[j]){
// Interchange values
int temp = number[i];
number[i] = number[j];
number[j] = temp;
}
}
}
System.out.print ("Sorted list");
for ( i = 0; i < n; i++)
{
System.out.print (" " + number[i]);
}
System.out.println(" ");
}
}
50
Output:
51
Input:
class TwodimensionalStandard
System.out.println(a[0][0]);
System.out.println(a[0][1]);
System.out.println(a[1][0]);
System.out.println(a[1][1]);
}
52
Output:
53
Input:
class string{
for(int i=0;i<size;i++){
for(int j=i+1;j<size;j++)
if(name[j].compareTo(name[i]) <0)
temp = name[i];
name[i] = name[j];
name[j] = temp;
System.out.println(name[i]);
}
54
Output:
55
Input:
import java.util.*;
Collections.addAll(v, arr);
System.out.println(v);
}
56
Output:
57
Input:
class Main {
int a = 5;
double b = 5.65;
}
58
Output:
59
Input:
package mypack;
class Balance
{
String name;
double bal;
Balance(String n,double b)
{
name=n;
bal=b;
}
void show()
{
if(bal<0)
System.out.print("-->");
System.out.println(name + ": $" + bal);
}
}
class accb
{
public static void main(String args[])
{
Balance current[]=new Balance[3];
current[0]=new Balance("K. J. Fielding", 123.23);
current[1]=new Balance("Will Tell", 157.02);
current[2]=new Balance("Tom Jackson", -12.33);
for(int i=0;i<3;i++)
current[i].show();
}
}
}
60
Output:
61
Input:
class box{ {
private double width; double cost;
private double height; ship(ship s)
private double depth; {
box(box b1){ super(s);
width=b1.width; cost=s.cost;
height=b1.height; }
depth=b1.depth; ship(double w,double h,double d,double
} m,double c)
box(double w,double h,double d){ {
width=w; super(w,h,d,m);
height=h; cost=c;
depth=d; }
} ship()
box(){ {
width=-1; super();
height=-1; cost=-1;
depth=-1; }
} ship(double len,double m,double c)
box(double len){ {
width=height=depth=len; super(len,m);
} cost=c;
double volume(){ }
return width*height*depth; }
} class ships
} {
class boxw extends box{ public static void main(String args[])
double weight; {
boxw(boxw b){ ship s1=new ship(10,1,1);
super(b); ship s2=new ship(20,2,2);
weight=b.weight; double vol;
} vol=s1.volume();
boxw(double w,double h,double d,double System.out.println("Volume of s1 is
m) "+vol);
{ System.out.println("Weight of s1 is
super(w,h,d); "+s1.weight);
weight=m; System.out.println("Shipping cost
} "+s1.cost);
boxw() System.out.println();
{ vol=s2.volume();
super(); System.out.println("Volume of s2 is
weight=-1; "+vol);
} System.out.println("Weight of s2 is
boxw(double len,double m){ "+s2.weight);
super(len); System.out.println("Shipping cost
weight=m; "+s2.cost);
} System.out.println();
} }
class ship extends boxw }
62
Output:
63
Input:
interface in
in i=new in1();
in.disp();
i.disp2();
}
64
Output:
65
Input:
Output:
67
Input:
Output:
69
Input:
Output:
71
Input:
class X implements Runnable{
System.out.println("End of ThreadX");
class RunnableTest
X runnable = new X( );
threadX. start();
}
72
Output:
73
Input:
class exe
int c;
try
c=10/0;
System.out.println(c);
catch(ArithmeticException e)
finally
System.out.println("Do it again");
}
74
Output:
75
Input:
class exe2
{
public static void main(String args[])
{
int a[]={10,10,10};
int b=2;
try
{
int x=a[3]/b;
x=a[3];
b=Integer.parseInt("aa");
}
catch(ArithmeticException e)
{
System.out.println("Division by zero");
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("Array index exception");
}
catch(NumberFormatException e)
{
System.out.println("Number Format Exception");
}
finally
{
System.out.println("Finally is executed");
}
int y=a[1]/10;
System.out.println("Y="+y);
}
}
76
Output:
77
Input:
class exe3
int a[]={10,10};
int b=10;
try
try{
int x=a[0]/(b-a[0]);
catch(ArithmeticException e){
System.out.println("Division by zero");
int x=a[2]/b-a[1];
catch(ArrayIndexOutOfBoundsException e)
System.out.println("Finally is executed");
int y=a[1]/a[0];
System.out.println("Y="+y);
}
78
Output:
79
Input:
import java.io.*;
class throw1
displayMethod();
BufferedReader br=
String name=br.readLine();
}
80
Output:
81
Input:
import java.awt.*;
import java.applet.Applet;
g.setColor(Color.red);
g.setFont(new Font("CASTELLAR",Font.BOLD,24));
g.setColor(new Color(192,182,200));
g.setFont(font);
}
82
Output:
83
Input:
import java.awt.*;
import javax.swing.*;
private int x;
g.setColor(Color.white);
g.setColor(Color.black);
g.setColor(Color.blue);
}
84
Output:
85
Input:
import java.applet.*;
import java.applet.*;
{
int y1 [] = {20, 129, 20, 20};
}
g.drawOval (160, 92, 15, 30);
}
}
}
86
Output:
87
Input:
import java.applet.Applet;
import java.awt.Graphics;
import java.awt.Color;
/*
* </applet>
String s1,s2;
s1=getParameter("Name");
g.setColor(Color.red);
s2=getParameter("Text");
g.drawString(s1,10,20);
g.drawString(s2,10,35);
}
88
Output:
89
Input:
import java.awt.*;
import java.applet.*;
int n = x.length;
g.drawPolygon(x, y, n);
}
90
Output:
91
Input:
import java.awt.*;
import java.applet.*;
/*
* <applet Code=UserValid.class width=400 height=150>
* </applet>
*/
public class app6 extends Applet
{
Label lblName,lblPasswd;
TextField txtName,txtPasswd,txt3;
Button btnLogin,btnCancel;
public void init()
{
String msg="";
Checkbox c1,c2,c3,c4,c5;
CheckboxGroup cg;
lblName=new Label("User Name");
lblPasswd=new Label("Password");
txtName=new TextField(10);
txtPasswd=new TextField(10);
txtPasswd.setEchoChar('*');
btnLogin=new Button("Login");
btnCancel=new Button("Cancel");
cg=new CheckboxGroup();
c1=new Checkbox("10th",cg,true);
c2=new Checkbox("12th",cg,false);
c3=new Checkbox("Graduate",cg,false);
c4=new Checkbox("Post Graduate",cg,false);
c5=new Checkbox("Ph.D",cg,false);
add(lblName);
add(txtName);
add(lblPasswd);
add(txtPasswd);
add(btnLogin);
add(btnCancel);
add(c1);
add(c2);
add(c3);
add(c4);
add(c5);
}
}
92
Output:
93
Input:
if(e1.getSource()==b1)
setBackground(Color.red);
if(e1.getSource()==b2)
setBackground(Color.yellow);
if(e1.getSource()==b3)
setBackground(Color.blue);
if(e1.getSource()==b4)
setBackground(Color.green);
}
94
Output:
95
Input:
import java.io.*;
class MyUserInput
{String str1,str2,str3;
str1=br1.readLine();
System.out.println("Enter a Character");
char ch1=(char)br1.read();
str2=br1.readLine();
str2=br1.readLine();
int ino=Integer.parseInt(str2);
str3= br1.readLine();
float fno=Float.parseFloat(str3);
}
96
Output:
97
Input:
import java.util.Scanner;
class ScannerInput
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Write a number: ");
int number=sc.nextInt();
String newline1=sc.nextLine(); // to consume the in character
System.out.println("It is to consume newline character after number "+number);
System.out.print( "Text 1: ");
String txt1=sc.nextLine();
System.out.print("Text2: ");
String txt2=sc.nextLine();
System.out.println("Write an integer: ");
int number1=sc.nextInt();
System.out.println("Write a float value");
float num=sc.nextFloat();
System.out.println("Write a double value: ");
double numl=sc.nextDouble();
System.out.println("The int value is number1+ float value is "+num+
"double value is "+num);
Scanner sc1= new Scanner("23,34,56");
sc1.useDelimiter(",");
System.out.println("number is "+sc1.nextInt());
System.out.println("number is "+sc1.nextInt());
System.out.println("number is "+sc1.nextInt());
}
}
98
Output:
Input:
99
import java.io.*;
int x=Integer.parseInt(str2);
String str3=dis.readLine();
double y=Double.parseDouble(str3);
System.out.println("Marks of Roll No "+str2+" having name "+ str1+ " is " + str3);
dis.close();