Professional Documents
Culture Documents
$ java ReadArgs 1 2
1+2=3
Practical 1 : Write a Java programs for add two number using command line
arguments.
class ReadArgs
{
public static final void main(String args[])
{
int a,b;
a=Integer.parseInt(args[0]);
b=Integer.parseInt(args[1]);
{
System.out.println("Add Two Numbers :");
System.out.println(+a+ " + " +b+" = "+ (a+b) );
}
}
}
OUTPUT :
$ java TestBalance
J. J. Singh : $ 99.88
Practical 2 : Usage of import statement and package declaration in java programs.
Package Mypack;
public class Balance {
string name;
double bal;
public Balance (String n, double b) {
name =n;
bal = b ;
}
public void show () {
if (bal< 0)
System.out.print ( “ - - > “);
System.out.println(name + “: $ “ + bal );
}
}
import MyPack.* ;
Class TestBalance {
public static final void main(String args[])
{
Balance test = new Balance(“J. J. Singh “, 99.88);
test.show(); // calling function
}
}
Output :
Value of x is : 10
Value of x is : 20
Value of z is : 10
Value of z is : 30
Value of x is : 10
Value of x is : 20
Practical 3 : Declaring variables of various data types and their effect by changing
the access
class BaseClass {
subClassObj.setY(20);
System.out.println("Value of y is : "+subClassObj.y);*/
//Access Modifiers - Protected
System.out.println("Value of z is : " + subClassObj.z);
subClassObj.setZ(30);
System.out.println("Value of z is : " + subClassObj.z);
//Access Modifiers - Default
System.out.println("Value of x is : " + subClassObj.a);
subClassObj.setA(20);
System.out.println("Value of x is : " + subClassObj.a);
}
}
OUTPUT :
$ java operators
z is : 10
y is : 5
+ operator resulted in 15
- operator resulted in -5
* operator resulted in 50
/ operator resulted in 0
% operator resulted in 5
f : 12
x > y : false
x < y : true
x >= y : false
x <= y : true
x == y : false
x != y : true
a & b : false
a && b : false
a | b : true
a || b: true
a ^ b : true
!a : false
~m : -193
m << n : 768
m >> n : 48
m >>> n : 48
//Conditional operators
int d = 10, e = 12, f = 0;
f = d > e ? d : e;
System.out.println("f : " + f);
//Relational operators
//int x=10, y=5;
System.out.println("x > y : "+(x > y));
System.out.println("x < y : "+(x < y));
System.out.println("x >= y : "+(x >= y));
System.out.println("x <= y : "+(x <= y));
System.out.println("x == y : "+(x == y));
System.out.println("x != y : "+(x != y));
//Logical operators
boolean a = true;
boolean b = false;
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));
System.out.println("a ^ b : " + (a ^ b));
System.out.println("!a : " + (!a));
// Bitwise Operators
int m = 192; //1 1 1 1 1 0 1 0 1 1 1 0 1 1 1 1
int n = 2; //1 1 1 1 1 0 0 0 1 1 1 0 1 0 0 1
int p;
System.out.println("~m : " + (~m));
System.out.println("m << n : " + (m << n));
System.out.println("m >> n : " + (m >> n));
System.out.println("m >>> n : " + (m >>> n));
//There is no unsigned left shift operator
}
public static void main(String args[]){
new operators();
}
}
OUTPUT
$java operator_and_or
a && b : false
a || b: true
value1 is 1 OR value2 is 1
class operator_and_or
{
public static void main(String[] args){
int value1 = 1;
int value2 = 2;
boolean a = true;
boolean b = false;
System.out.println("a && b : " + (a && b));
System.out.println("a || b: " + (a || b));
if((value1 == 1) && (value2 == 2))
System.out.println("value1 is 1 AND value2 is 2");
if((value1 == 1) || (value2 == 1))
System.out.println("value1 is 1 OR value2 is 1");
}
}
OUTPUT :
$ java WhileDemo
Count is: 1
Count is: 2
Count is: 3
Count is: 4
Count is: 5
Count is: 6
Count is: 7
Count is: 8
Count is: 9
Count is: 10
$ java DoWhile
tick : 10
tick : 9
tick : 8
tick : 7
tick : 6
tick : 5
tick : 4
tick : 3
tick : 2
tick : 1
Practical 6 :Write Java programs, which make use of control Statement like if, while,
do while.
class IfElseDemo {
public static void main(String[] args) {
class WhileDemo {
public static void main(String[] args){
int count = 1;
while (count < 11) {
System.out.println("Count is: " + count);
count++;
}
}
}
class DoWhile
{
public static void main(String[] args)
{
int n = 10;
do
{
System.out.println("tick : " +n);
n--;
}
while (n>0);
}
}
OUTPUT :
$java This_Final_Super
class A
{
int length ,breadth;
int calculate(int length,int breadth)
{
this.length=length;
this.breadth=breadth;
return (length*breadth);
}
final int a = 50; // constant
final void meth () //Prevent overriding
{
System.out.println("This is a final method");
}
}
class This_Final_Super extends A
{
int a = 200;
void show()
{
System.out.println("Using 'super & final' keyword A : "+super.a);
System.out.println("Without using super keyword A : "+a);
}
/* public void meth()
{
System.out.println("illegal ");
} */
public static void main(String[] args)
{
This_Final_Super object=new This_Final_Super();
object.meth();
int area =object.calculate(5,6);
System.out.println("The area of a Rectangle is(Using This Keyword) : " +area);
object.show();
}
OUTPUT :
class OverloadDemo {
void test() {
System.out.println("No parameters");
}
// Overload test for one integer parameter.
void test(int a) {
System.out.println("a: " + a);
}
// Overload test for two integer parameters.
void test(int a, int b) {
System.out.println("a and b: " + a + " " + b);
}
class Overload {
public static void main(String args[]) {
OverloadDemo ob = new OverloadDemo();
double result;
// call all versions of test()
ob.test();
ob.test(10);
ob.test(10, 20);
result = ob.test(123.2);
System.out.println("Result of ob.test(123.2): " + result);
}
}
Recursion
class Factorial {
int fact(int n)
{
int result;
if ( n ==1) return 1;
result = fact (n-1) * n;
return result;
}
}
class Recursion {
public static void main (String args[])
{
Factorial f =new Factorial();
System.out.println(“Factorial of 3 is “ + f.fact(3));
System.out.println(“Factorial of 3 is “ + f.fact(4));
System.out.println(“Factorial of 3 is “ + f.fact(5));
}
}
OUTPUT :
$ java MethodOverriding
int i;
A(int a, int b)
i = a+b;
void add()
class B extends A
int j;
B(int a, int b, int c)
super(a, b);
j = a+b+c;
void add()
super.add();
class MethodOverriding
b.add();
}
OUTPUT :
$ java exceptions
a=2
20
class exceptions
{
public static void main(String Args[]){
try {
int a = 2;
//int a= 0; divide by zero
System.out.println("a = " +a);
int b= 40/a;
System.out.println(b);
int c[] ={32};
c[5] =99;
System.out.println(" Array value "+ c[1]);
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("Oops, Array index outof bound : " +e);
}
catch(ArithmeticException e)
{
System.out.println("divison by zero Not Possible :" +e);
}
finally
{
System.out.println(" Finally Execute before Ending the try block");
}
System.out.println(" After Try/Catch blocks");
}
}
OUTPUT :
Practical 11 : Write java programs that make usage of java lang.awt package and
design GUI.
import java.awt.*;
import java.applet.*;
class GuiExample extends Applet
{
// A Button to click
Button okButton;
// A textField to get text input
TextField nameField;
// A group of radio buttons
// necessary to only allow one radio button to be selected at the same time.
CheckboxGroup radioGroup;
// The radio buttons to be selected
Checkbox radio1;
Checkbox radio2;
// An independant selection box
Checkbox option;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
add(yes);
add(no);
add(maybe);
yes.addActionListener(this);
no.addActionListener(this);
maybe.addActionListener(this);
}
}
else
{
msg = "you pressed Undecided.";
}
repaint();
}