You are on page 1of 34

OUT PUT :

$ java ReadArgs 1 2

Add Two Numbers :

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

modifiers like private, public,protected, default.

class BaseClass {

public int x = 10;


private int y = 10;
protected int z = 10;
int a = 10; //Implicit Default Access Modifier
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
private int getY() {
return y;
}
private void setY(int y) {
this.y = y;
}
protected int getZ() {
return z;
}
protected void setZ(int z) {
this.z = z;
}
int getA() {
return a;
}
void setA(int a) {
this.a = a;
}
}
public class SubclassInSamePackage extends BaseClass {

public static void main(String args[]) {


BaseClass rr = new BaseClass();
rr.z = 0;
SubclassInSamePackage subClassObj = new SubclassInSamePackage();
//Access Modifiers - Public
System.out.println("Value of x is : " + subClassObj.x);
subClassObj.setX(20);
System.out.println("Value of x is : " + subClassObj.x);
//Access Modifiers - Public
// If we remove the comments it would result in a compilaton
// error as the fields and methods being accessed are private
/* System.out.println("Value of y is : "+subClassObj.y);

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

Postfix ++ operator resulted in 5

Prefix ++ operator resulted in 11

Unary operator resulted in -6

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

Practical 4 :Writing programs which make use of Arithmetic Operators, Comparison


Operators, Logical Operators, Bit wise Operators.

public class operators {


public operators( ) {
int z, y ;
// Assignment Operators
z = 10; // z gets the value 10.
y = 5; // y gets the value 5.
System.out.println("z is : "+z);
System.out.println("y is : "+y);
//Arithmetic operators
int x;
x = y + z;
System.out.println("+ operator resulted in " + x);
x = y - z;
System.out.println("- operator resulted in " + x);
x = y * z;
System.out.println("* operator resulted in " + x);
x = y / z;
System.out.println("/ operator resulted in " + x);
x = y % z;
System.out.println("% operator resulted in " + x);
x = y++;
System.out.println("Postfix ++ operator resulted in " + x);
x = ++z;
System.out.println("Prefix ++ operator resulted in " + x);
x = -y;
System.out.println("Unary operator resulted in " + x);

//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 AND value2 is 2

value1 is 1 OR value2 is 1

Practical 5 :Writing programs which make use of && and II operators.

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 :

The output from the program If-Else:


Grade = C

Output of progran WHILE:

$ 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

Output of program DO-WHILE :

$ 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) {

int testscore = 76;


char grade;

if (testscore >= 90) {


grade = 'A';
} else if (testscore >= 80) {
grade = 'B';
} else if (testscore >= 70) {
grade = 'C';
} else if (testscore >= 60) {
grade = 'D';
} else {
grade = 'F';
}
System.out.println("Grade = " + grade);
}
}

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

This is a final method

The area of a Rectangle is(Using This Keyword) : 30

Using 'super & final' keyword A : 50

Without using super keyword A : 200


Practical 7 :Using super, this, Final in Java Programs.

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 :

This program generates the following output:


No parameters
a: 10
a and b: 10 20
double a: 123.2
Result of ob.test(123.2): 15178.24

The output from this program is shown here:


Factorial of 3 is 6
Factorial of 4 is 24
Factorial of 5 is 120

Practical 8 :Write code snippets which make usage of Method


Overloading,recursion.

Demonstrate method overloading.

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);
}

// overload test for a double parameter


double test(double a) {
System.out.println("double a: " + a);
return a*a;
}
}

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

Sum of a and b is: 30

Sum of a, b and c is: 60


Practical 9 :Write code snippets which make usage of Method Overriding.
class A

int i;

A(int a, int b)

i = a+b;

void add()

System.out.println("Sum of a and b is: " + i);

class B extends A

int j;
B(int a, int b, int c)

super(a, b);

j = a+b+c;

void add()

super.add();

System.out.println("Sum of a, b and c is: " + j);

class MethodOverriding

public static void main(String args[]) {

B b = new B(10, 20, 30);

b.add();

}
OUTPUT :

$ java exceptions

a=2

20

Oops, Array index outof bound : java.lang.ArrayIndexOutOfBoundsException: 5

Finally Execute before Ending the try block

After Try/Catch blocks


Practical 10 : Write Java Programs, which make usage of Exception handling

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;

public void init()


{
// Tell the applet not to use a layout manager.
setLayout(null);
// initialze the button and give it a text.
okButton = new Button("A button");
// text and length of the field
nameField = new TextField("A TextField",100);
// initialize the radio buttons group
radioGroup = new CheckboxGroup();
radio1 = new Checkbox("Radio1", radioGroup,false);
// same but selected
radio2 = new Checkbox("Radio2", radioGroup,true);
// Label and state of the checkbox
option = new Checkbox("Option",false);
// now we will specify the positions of the GUI components.
// this is done by specifying the x and y coordinate and
//the width and height.
okButton.setBounds(20,20,100,30);
nameField.setBounds(20,70,100,40);
radio1.setBounds(20,120,100,30);
radio2.setBounds(140,120,100,30);
option.setBounds(20,170,100,30);
// now that all is set we can add these components to the applet
add(okButton);
add(nameField);
add(radio1);
add(radio2);
add(option);
}
}
Practical 12 : Usage of event handling in Java GUI (Graphical user interface)
programs.

import java.awt.*;
import java.awt.event.*;
import java.applet.*;

class ButtonDemo extends Applet implements ActionListener {


String msg = " ";
Button yes, no, maybe;

public void init() {


yes = new Button("yes");
no = new Button("no");
maybe = new Button("Undecided");

add(yes);
add(no);
add(maybe);

yes.addActionListener(this);
no.addActionListener(this);
maybe.addActionListener(this);
}

public void actionPerformed (ActionEvent ae ) {


String str = ae.getActionCommand();
if(str.equals("Yes")) {
msg = " You Pressed yes.";
}
else if (str.equals("No")) {
msg = "You pressed No.";

}
else
{
msg = "you pressed Undecided.";
}
repaint();
}

public void paint(Graphics g)


{
g.drawString(msg,6,100);
}
}

You might also like