Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

JJJJJJJ

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 37

[1] Write a simple “Hello World” java program, compilation, debugging,

executing using java compiler and interpreter.

PRROGRAM:

public class HelloWorld


{ public static void main(String args[])
{
System.out.println("Hello World");
}
}

OUTPUT:

[2] Write a program to pass Starting and Ending limit and print all prime
numbers and
Fibonacci numbers between this range.

PRROGRAM:

class Fibonacci
{ public static void main(String args[])
{
int n1=0,n2=1,n3,i,count=10;
System.out.print(n1+" "+n2);//printing 0 and 1
for(i=2;i<count;++i

{ n3=n1+n2;
System.out.print(" "+n3);
n1=n2; n2=n3;
}
}
}
OUTPUT:

1|Page

- Dinesh Varma
[3] Write a java program to check whether number is palindrome or not.
Input: 528 Output: It is not palindrome number
Input: 545 Output: It is not palindrome number
PRROGRAM:

import java.util.*;
public class Palindrome
{ public static void printPalin(int n)
{ int r,sum=0,temp;
temp=n;
while(n>0)
{ r=n%10; //getting remainder
sum=(sum*10)+r; n=n/10;
} if(temp==sum)
System.out.println("[ "+temp+" ] is palindrome
number"); else
System.out.println("[ "+temp+" ] is not palindrome");

}
public static void main(String args[])
{ int n=0; Scanner ob = new
Scanner(System.in);

System.out.println("Enter the Number : ");


n = ob.nextInt(); printPalin(n);
}

}
OUTPUT:

[4] Write a java program to print value of x^n.

2|Page

- Dinesh Varma
Input: x=5 Input: n=3 Output: 125

PRROGRAM:

import java.util.Scanner;

public class FindingCube {


public static void main(String[] args) { Scanner in = new
Scanner(System.in); System.out.println("Enter the base &
exponent values::\n");

int b = in.nextInt(); int e = in.nextInt(); int r = 1;


int i = 1; // b = base
// e = exponent
// r = result

/* finding power of base value by equiping exponent value */ while(i


<= e) { r *=
b; i++; }

// Output
System.out.println("\nResult:: " + b + "^" + e + " = " + r + "\n");
}
}

OUTPUT:

[5] Write a java program to check Armstrong number.

3|Page

- Dinesh Varma
Input: 153 Output: Armstrong number

Input: 22 Output: not Armstrong number

PRROGRAM:

import java.util.*;
public class Armstrong
{ public static void printArmstrong(int n)
{ int c=0,a,temp;
temp=n;
while(n>0)
{ a=n%10;
n=n/10;
c=c+(a*a*a);
}
if(temp==c)
System.out.println("armstrong number"); else
System.out.println("Not armstrong number");
}

public static void main(String args[])


{ int n=0; Scanner ob = new
Scanner(System.in);

System.out.println("Enter the Number : ");


n = ob.nextInt(); printArmstrong(n);

}
}

OUTPUT:

[6] Write a java program which should display maximum number of given
4 numbers.

PRROGRAM:
4|Page

- Dinesh Varma
import java.util.*;
public class Maximum
{ public static void getMaximum(int a,int b, int c)
{ int d = c > (a > b ? a : b) ? c : ((a > b) ? a : b);
System.out.println("Largest Number:"+d);
}

public static void main(String args[])


{ int a,b,c; Scanner ob = new
Scanner(System.in);

System.out.println("Enter the Number A : ");


a = ob.nextInt();
System.out.println("Enter the Number B : ");
b = ob.nextInt();
System.out.println("Enter the Number C : ");
c = ob.nextInt(); getMaximum(a,b,c);

}
}

OUTPUT:

[7] Write a program in Java to find minimum of three numbers using


conditional operator.

PRROGRAM:

5|Page

- Dinesh Varma
import java.util.*;
public class MaxiMin
{ public static void getMaximum(int a,int b, int c)
{ int d = c > (a > b ? a : b) ? c : ((a > b) ? a : b);
System.out.println("Largest Number:"+d);
}
public static void getMinimum(int a,int b, int c)
{ int d = c < (a < b ? a : b) ? c : ((a < b) ? a : b);
System.out.println("Minimum Number:"+d);
}
public static void main(String args[])
{ int a,b,c; Scanner ob = new
Scanner(System.in);

System.out.println("Enter the Number A : ");


a = ob.nextInt();
System.out.println("Enter the Number B : ");
b = ob.nextInt();
System.out.println("Enter the Number C : ");
c = ob.nextInt();

getMaximum(a,b,c);
getMinimum(a,b,c);

}
}

OUTPUT:

[8] Write a program in Java to multiply two matrix.

Declare a class Matrix where 2D array is declared as instance variable


and array should be initialized, within class.

PRROGRAM:

6|Page

- Dinesh Varma
public class MatrixMultiplication {

public static void main(String[] args)


{ int[][] m1 = { {1, 2, 3}, {4, 5,
6} }; int[][] m2 = { {7, 8}, {9, 10}, {11,
12} }; // Mutliplying Two matrices

int sum = 0;
int[][] r = new int[2][2];
for(int i = 0; i < 2; i++)
{
for (int j = 0; j < 2; j++)
{
for (int k = 0; k < 3; k++)
{
sum += m1[i][k] * m2[k][j];
} r[i][j]=sum;
//sum=0;
}
}

// Display result matrix


System.out.println("Sum of two matrices : ");
for(int i = 0; i < 2; i++)
{
for (int j = 0; j < 2; j++)
{
System.out.print(r[i][j] + " ");
}
System.out.println();
}
}
}
OUTPUT:

[9] Write a java program to create a class “Matrix” that would contain
integer values
having varied Numbers of columns for each row. Print row-wise sum of
the integer values for each row.

7|Page

- Dinesh Varma
PRROGRAM:

import java.util.Scanner; class


MUlMatrix { public static void
main(String args[])
{ int r1, r2,c1,c2,i,j,k,sum;
Scanner in = new Scanner(System.in);

System.out.println("Enter the number of rows of matrix1");


r1 = in.nextInt();

System.out.println("Enter the number columns of matrix 1");


c1 = in.nextInt();
System.out.println("Enter the number of rows of matrix2");
r2 = in.nextInt();

System.out.println("Enter the number of columns of matrix 2");


c2 = in.nextInt();
if(c1==r2)
{

int mat1[][] = new int[r1][c1]; int


mat2[][] = new int[r2][c2]; int
res[][] = new int[r1][c2];
System.out.println("Enter the elements of matrix1");
for ( i= 0 ; i < r1 ; i++ ) { for ( j= 0 ; j <
c1 ;j++ ) mat1[i][j] = in.nextInt();
}
System.out.println("Enter the elements of matrix2");
for ( i= 0 ; i < r2 ; i++ ){ for ( j= 0 ; j < c2 ;j++ )
mat2[i][j] = in.nextInt();
}

8|Page

- Dinesh Varma
System.out.println("\n\noutput matrix:-"); for
( i= 0 ; i < r1 ; i++ ){ for ( j= 0 ; j
<c2;j++){ sum=0; for ( k= 0 ; k <r2;k++ )
{ sum +=mat1[i][k]*mat2[k][j] ; } res[i]
[j]=sum;
} for ( i= 0 ; i < r1; i++ ) { for
( j=0 ; j < c2;j++ )
System.out.print(res[i][j]+" ");
System.out.println();
}
}
else
System.out.print("multipication does not exist ");
}
}

OUTPUT:

9|Page

- Dinesh Varma
[10] Write a Java application which takes several command line
arguments, which are supposed to be names of students and prints
output as given below:
(Suppose we enter 3 names then output should be as
follows).. Number of arguments = 3 1.: First Student
Name is = Arun
2.: Second Student Name is = Hiren
3.Third Student Name is = Hitesh

PRROGRAM:

10 | P a g e

- Dinesh Varma
import java.util.*;
public class CLArg
{ public static void main(String args[])
{
String arr[] = {"First", "Second", "Third",
"Fourth", "Fifth", "Sixth",
"Seventh", "Eighth", "Nineth",
"tenth", "eleventh","twelveth",
"thirteenth","fourteenth",

"fifteenth","sixteenth","Seventeenth",

"Eighteenth","Nineteenth","twentyth"};

System.out.println("Number of arguments = "+args.length);


for(int i=0;i<args.length;i++)
{
System.out.println((i+1)+": "+ arr[i]+
" Student Name is = "+args[i]);
}
}
}

OUTPUT:

[11] Write a Java application to count and display frequency of letters and
digits from the String given by user as command-line argument.

PRROGRAM:

public class Ex11


{
public static void main(String[] args)
{ if(args.length<=0)
{
System.out.println("Enter a string using command
line");
}
else
{
String txt = args[0]; int
letters = 0, digits = 0;

txt = txt.toLowerCase();
11 | P a g e

- Dinesh Varma
for(int i = 0; i < txt.length(); ++i)
{
// returns char value for the particular
index char ch = txt.charAt(i);// char charAt(int
index) if((ch >= 'a' && ch <= 'z'))
{ letters++;
}
else if( ch >= '0' && ch <= '9')
{ digits ++;
}
}
System.out.println("String : " + args[0]);
System.out.println("Letters : " + letters);
System.out.println("Digits : " + digits );
}
}
}
OUTPUT:

[12] Create a class Student that would contain enrollmentNo, name, and
gender as data members.Create appropriate getter and setter methods for the
Student class and constructors to initialize the data members. Also
demonstrate constructor chaining.

PRROGRAM:

package Student;

class Student {

private int enrollmentNo;


private String name;
private String gender;

public Student()
{ enrollmentNo=0;
name="undefine";
gender="male";
}
public Student(int en,String nm, String gn)
{ setEnrollmentNo(en);
setName(nm);
setGender(gn);
}
12 | P a g e

- Dinesh Varma
/* enrollmentNo */
public int getEnrollmentNo()
{ return(enrollmentNo);
}
public void setEnrollmentNo(int en)
{ enrollmentNo=en;
}
/* name */
public String getName()
{ return(name);
}
public void setName(String nm)
{ name=nm.toUpperCase();
}
/* gender */
public String getGender()
{ return(gender);
}
public void setGender(String gn)
{ gender=gn.toUpperCase();
}
public void displayStudent()
{
System.out.println("***********************");
System.out.println("Enrollment No : "+getEnrollmentNo());
System.out.println("Student Name : "+getName());
System.out.println("Gender : "+getGender());
System.out.println("***********************");
}
}
package ex12; import
java.util.Scanner;
public class ex12{

public static void main(String args[])


{
Scanner scan = new Scanner(System.in);

int en = 0;
String nm = " ";
String gn = " ";

System.out.println("Enroll number : ");


int student_en=scan.nextInt();

System.out.println("Student Name : ");


String student_nm=scan.next();

System.out.println("Student Gender : ");


String student_gn=scan.next();
13 | P a g e

- Dinesh Varma
Student s = new Student();

s.show_detail(); System.out.println("Enroll number

: "+student_en);

System.out.println("Student Name : "+student_nm);

System.out.println("Student Gender : "+student_gn);


}
}
OUTPUT:

[13] Write a program in Java to demonstrate use of this keyword. Check

whether this can access the private members of the class or not. [Refer class

student in Q12 to perform the task]

PRROGRAM:

class Student
{ private int enrollmentNo;
private String name;
private String gender;

public Student()
{ enrollmentNo=0;
name="undefine";
gender="male";
}
public Student(int en,String nm, String gn)
{
//this.enrollmentNo=en;
setEnrollmentNo(en);
//this.name=nm.toUpperCase();
setName(nm);

14 | P a g e

- Dinesh Varma
//this.gender=gn.toUpperCase();
setGender(gn);
}
/* enrollmentNo */
public int getEnrollmentNo()
{ return(this.enrollmentNo);
}
public void setEnrollmentNo(int en)
{ this.enrollmentNo=en;
}
/* name */
public String getName()
{ return(this.name);
}
public void setName(String nm)
{ this.name=nm.toUpperCase();
}
/* gender*/
public String getGender()
{ return(this.gender);
}
public void setGender(String gn)
{ this.gender=gn.toUpperCase();
}
public void displayStudent()
{
System.out.println("***********************");
System.out.println("Enrollment No :
"+this.getEnrollmentNo());
System.out.println("Student Name : "+this.getName());
System.out.println("Gender : "+this.getGender());
System.out.println("***********************");
}
}

public class Ex13


{ public static void main(String args[])
{
Student s = new Student();
s.displayStudent();
Student s1 = new Student(1,"bipin","male");
s1.displayStudent();
}
}

OUTPUT:

15 | P a g e

- Dinesh Varma
[14] Create a class Rectangle that would contain length and width as data
members.
Define constructors [constructor overloading (default, parameterized
and copy)]
to initialize the data members. Define the member functions to find area
and to display the number of objects created.
[ Note:
define initializer block, static initializer block and the static data member
and member function.
Also demonstrate the sequence of execution of initializer block and static

initializer block]

PRROGRAM:

class Rectangle
{
//data member int
length=fieldInit(); int
width=-1; static int
numOfObject=-1;

//initializer block
{
System.out.println("Initializer block execut");
length=0; width=0;
}

//static initializer block


static
{

16 | P a g e

- Dinesh Varma
System.out.println("Static block initialize");
numOfObject=0;
}

//default constructor
public Rectangle()
{ length=1;
width=1;
System.out.println("Default constructor invoked");
numOfObject++;
}
//parameterized constructor
public Rectangle(int l, int b)
{
System.out.println("Parameterized constructor invoked");
length=l; width=b; numOfObject++;
}

//copy constructor
public Rectangle(Rectangle obj)
{
System.out.println("Copy constructor invoked");
length=obj.length; width=obj.width;
numOfObject++;
}

//method to calculate area of rectangle


public void getArea()
{
System.out.println("Length : "+length);
System.out.println("Width : "+width);
System.out.println("Area : "+length*width);
}

//count number of objects created using static field


public static void getNumOfObject()
{ System.out.println("Number of object created :
"+numOfObject);
}

public int fieldInit()


{
System.out.println("Field initialize"); return(-
1);
}
}

class Ex14
{ public static void main(String args[])
{ System.out.println("\n********************************\n");

17 | P a g e

- Dinesh Varma
Rectangle firstRect=new Rectangle();
firstRect.getArea();
Rectangle.getNumOfObject();
System.out.println("\n********************************\n");
Rectangle secondRect1=new Rectangle(firstRect);
System.out.println("\n********************************\n");
Rectangle secondRect=new Rectangle(6,3);
secondRect.getArea(); Rectangle.getNumOfObject();
System.out.println("\n********************************\n");
}
}
OUTPUT:

18 | P a g e

- Dinesh Varma
[15] Write a java program static block which will be executed before main ( )
method in a class.
class Ex15

PRROGRAM:

class Ex15
{ static
{
System.out.println("static block is invoked");
}
public static void main(String args[])
{
System.out.println("main method is invoked");
}
}

OUTPUT:

16) Write programs in Java to use Wrapper class of each primitive


data types.

class Ex16
{
public static void main(String args[])
{
// byte data type
byte a = 1;

// wrapping around Byte object


Byte byteobj = new Byte(a);

// int data type


int b = 10;

//wrapping around Integer object


Integer intobj = new Integer(b);

// float data type


float c = 18.6f;

19 | P a g e

- Dinesh Varma
// wrapping around Float object
Float floatobj = new Float(c);

// double data type


double d = 250.5;

// Wrapping around Double object


Double doubleobj = new Double(d);

// char data type


char e='a';

// wrapping around Character object


Character charobj=e;

// printing the values from objects


System.out.println(" Values of Wrapper objects (printing
as objects)");
System.out.println(" Byte object byteobj: " + byteobj);
System.out.println(" Integer object intobj: " + intobj);
System.out.println(" Float object floatobj: " +
floatobj);
System.out.println(" Double object doubleobj: " +
doubleobj);
System.out.println(" Character object charobj: " +
charobj);

// objects to data types (retrieving data types from


objects)
// unwrapping objects to primitive data types
byte bv = byteobj;
int iv = intobj;
float fv = floatobj;
double dv = doubleobj;
char cv = charobj;

// printing the values from data types


System.out.println(" Unwrapped values (printing as data
types)");
System.out.println(" byte value, bv: " + bv);
System.out.println(" int value, iv: " + iv);
System.out.println(" float value, fv: " + fv);
System.out.println(" double value, dv: " + dv);
System.out.println(" char value, cv: " + cv);
}
}

OUTPUT:

20 | P a g e

- Dinesh Varma
17) Write a class “circle” with radius as data member and count the number of
instances created using default constructor only. [Constructor Chaining]
class circle
{
static int i;
int radius;
double ar;
static
{
i=0;
}
circle()
{
this.i++;
}
circle(int r)
{
this();
radius=r;
}
void area()
{
ar=3.14*radius*radius;
System.out.println("Area= " +ar);
}
void display()
{
System.out.println("Radius= " +radius);
}
static void count()
{
21 | P a g e

- Dinesh Varma
System.out.println("\nObject created= " +i);
}
}

class precticle17
{
public static void main(String args[])
{
System.out.println("\nObject one");
circle c1=new circle();
c1.display();

System.out.println("\nObject Two");
circle c2=new circle(5);
c2.display();
c2.area();

System.out.println("\nObject Three");
circle c3=new circle(10);
c3.display();
c3.area();

circle.count();
}
}

OUTPUT:

22 | P a g e

- Dinesh Varma
18. Create a class “Vehicle” with instance variable vehicle_type. Inherit the class in a class
called “Car” with instance model_type, company name etc. display the information of
the
vehicle by defining the display() in both super and sub class [ Method Overriding].

class vehicle
{
String vehicle_type;
public vehicle(String vt)
{
vehicle_type=vt;
}
void display()
{
System.out.println("\nVehicle Information");
System.out.println("Vehicle type = "+vehicle_type);
}
}
class car extends vehicle
{
String model_type;
String company_name;

public car(String b,String mt,String cn)


{
super(b);
model_type=mt;
company_name=cn;
}
public void display()
{
super.display();
System.out.println("Vehicle model_type = "+model_type);
System.out.println("Vehicle type = "+company_name);
}
}

23 | P a g e

- Dinesh Varma
class vehicleEX
{
public static void main(String args[])
{
car c1=new car("car","Tata Nexon","TATA");
c1.display();

car c2=new car("Bike","Royal Enfield","Royal Enfield Motors


Limited.");
c2.display();

}
}

Output:

19. Create a class “Account” containing accountNo, and balance as an instance


variable.
Derive the Account class into two classes named “Savings” and “Current”. The

24 | P a g e

- Dinesh Varma
“Savings” class should contain instance variable named interestRate, and the
“Current” class should contain instance variable called overdraftLimit. Define
appropriate methods for all the classes to enable functionalities to check balance,
deposit, and withdraw amount in Savings and Current account. [Ensure that the
Account class cannot be instantiated.]

abstract class Account


{
long accountNo=0;
double balance=0.0;
Account()
{
this.accountNo=0;
this.balance=0.0;
}
Account(long x, double y)
{
this.accountNo=x;
this.balance=y;
}
public abstract void checkBalance();
}
class Saving extends Account
{

double interestRate=0.0;
final int minimumBalance=1000;
Saving()
{
this.accountNo=0;
this.balance=0.0;
this.interestRate=4.0;
}
Saving(long a,double b, double i)
{
this.accountNo=a;
this.balance=b;
this.interestRate=i;
}
public void checkBalance()
{
System.out.println("\
n******************************************");
System.out.println("\nSaving Acount Balance:"+this.balance);
System.out.println("\
n******************************************");
}
public void depositBalance(double x)
{
this.balance=this.balance+x;
}
public void withdrawBalance(double x)
{
if((this.balance-x) >= this.minimumBalance)
25 | P a g e

- Dinesh Varma
{
this.balance=this.balance-x;
}
else
{
System.out.println("\
n************************************");
System.out.println("\nYou can not withdraw "+x);
System.out.println("\ninsufficient balance in your
account,\n\nPlease
maintain minumum balace "+this.minimumBalance);
System.out.println("\nSaving Account balance :
"+this.balance);
System.out.println("\
n************************************"); }
}
}
class Current extends Account
{
double overdraftLimit;
double overdraft;
Current()
{
this.accountNo=0;
this.balance=0.0;
this.overdraftLimit=100000;
this.overdraft=0;
}
Current(long a,double b, double o)
{
this.accountNo=a;
this.balance=b;
this.overdraftLimit=o;
this.overdraft=0;
}
public void checkBalance()
{
System.out.println("\n************************************");
System.out.println("\nCurrent Acount
Balance:"+this.balance);
System.out.println("\n************************************");

public void depositBalance(double x)


{
this.balance=this.balance+x;
}
public void withdrawBalance(double x)
{
if((this.balance-x) >= 0)
{
this.balance=this.balance-x;
26 | P a g e

- Dinesh Varma
}
else
{
System.out.println("\
n************************************");
System.out.println("\nyou can not withdraw "+x+"
balance");
System.out.println("\ncurrent balance : "+this.balance);

System.out.println("\
n************************************"); }
}
public void takeOverDraft(double x)
{
//under development
if((this.balance-x) >= 0)
{
this.balance=this.balance-x;
}
else
{
System.out.println("\
n************************************");
System.out.println("\nyou can not withdraw "+x+" balance");
System.out.println("\ncurrent balance : "+this.balance);

System.out.println("\
n************************************"); }
}
}

class Ex19
{
public static void main(String args[])
{
/*
Saving s = new Saving(1,1000,4);
s.depositBalance(100000);
s.checkBalance();
s.withdrawBalance(5000000);
*/

27 | P a g e

- Dinesh Varma
Current c = new Current(1,1000,100000);
//c.depositBalance(100000);
//c.checkBalance();
//c.withdrawBalance(1000);
c.checkBalance();
c.withdrawBalance(1000);
c.checkBalance();

}
}

Output:

20. Write a program in Java in which a subclass constructor invokes the


constructor of the super class and instantiate the values. [ refer class Account and
sub classes savingAccount and CurrentAccount in Q 19 for this task]

abstract class Account


{
long accountNo=0;
double balance=0.0;
Account()
{
this.accountNo=0;
this.balance=0.0;
}
28 | P a g e

- Dinesh Varma
Account(long x, double y)
{
this.accountNo=x;
this.balance=y;
}
public abstract void checkBalance();
}
class Saving extends Account
{

double interestRate=0.0;
final int minimumBalance=1000;
Saving()
{
this.accountNo=0;
this.balance=0.0;
this.interestRate=4.0;
}
Saving(long a,double b, double i)
{
this.accountNo=a;
this.balance=b;
this.interestRate=i;
}
public void checkBalance()
{
System.out.println("\n************************************");
System.out.println("\nSaving Acount
Balance:"+this.balance);
System.out.println("\n************************************");
}
public void depositBalance(double x)
{
this.balance=this.balance+x;
}
public void withdrawBalance(double x)
{
if((this.balance-x) >= this.minimumBalance)
{
this.balance=this.balance-x;
}
else
{
System.out.println("\
n************************************");
System.out.println("\nYou can not withdraw "+x);
System.out.println("\ninsufficient balance in your
account,\n\nPlease
maintain minumum balace
"+this.minimumBalance);
System.out.println("\nSaving Account balance :
"+this.balance);
System.out.println("\
n************************************");
29 | P a g e

- Dinesh Varma
}

}
}
class Current extends Account
{
double overdraftLimit;
double overdraft;
Current()
{
this.accountNo=0;
this.balance=0.0;
this.overdraftLimit=100000;
this.overdraft=0;
}
Current(long a,double b, double o)
{
this.accountNo=a;
this.balance=b;
this.overdraftLimit=o;
this.overdraft=0;
}
public void checkBalance()
{
System.out.println("\n************************************");
System.out.println("\nCurrent Acount
Balance:"+this.balance);
System.out.println("\n************************************");

}
public void depositBalance(double x)
{
this.balance=this.balance+x;
}
public void withdrawBalance(double x)
{
if((this.balance-x) >= 0)
{
this.balance=this.balance-x;
}
else
{
System.out.println("\
n************************************");
System.out.println("\nyou can not withdraw "+x+"
balance");
System.out.println("\ncurrent balance : "+this.balance);

System.out.println("\
n************************************"); }
}
public void takeOverDraft(double x)
{
//under development
30 | P a g e

- Dinesh Varma
if((this.balance-x) >= 0)
{
this.balance=this.balance-x;
}
else
{
System.out.println("\
n************************************");
System.out.println("\nyou can not withdraw "+x+"
balance");
System.out.println("\ncurrent balance : "+this.balance);
System.out.println("\
n************************************"); }
}
}

class Ex20
{
public static void main(String args[])
{
/*
Saving s = new Saving(1,1000,4);
s.depositBalance(100000);
s.checkBalance();
s.withdrawBalance(5000000);
*/

Current c = new Current(1,1000,100000);


//c.depositBalance(100000);
//c.checkBalance();
//c.withdrawBalance(1000);
c.checkBalance();
31 | P a g e

- Dinesh Varma
c.withdrawBalance(1000);
c.checkBalance();

}
}

Output :

21. Write a program in Java to demonstrate the use of 'final' keyword in the field
declaration. How it is accessed using the objects.

final class X // final class


{
final int x=90;//final variable
final public void disp() //final method
{
//x=99; // ERROR : final variable value can not be change
System.out.println("final variable : "+x);
}
}
/*
class Y extends X //final class can not be inherited
{
public void disp() //final method can not be override
{
32 | P a g e

- Dinesh Varma
System.out.println("disp of Y : ");
}
}
*/

class FinalDemo
{
public static void main(String args[])
{
X obj=new X();
System.out.println("final variable : "+obj.x);
}
}

Output:

22. Write a java program to illustrates how to access a hidden variable. Class A
declares a static variable x. The class B extends A and declares an instance
variable x. display ( ) method in B displays both of these variables.

class A
{
static int a=10;
A()
{
a=200;
}
}
class B extends A
{
int a;
B()
{
a=1000;
}
33 | P a g e

- Dinesh Varma
void display()
{
System.out.println("\n Local a= "+a);
System.out.println("\n Static a= "+super.a);
}
}
class Ex22
{
public static void main(String args[])
{
B ob=new B();
ob.display();
}
}

Output :

23. Describe abstract class called Shape which has three


subclasses say Triangle, Rectangle,
and Circle. Define one method area () in the abstract class
and override this area () in
these three subclasses to calculate for specific object
i.e. area () of Triangle subclass
should calculate area of triangle etc. Same for Rectangle
and Circle.

import java.lang.Math;

abstract class Shape


{
double area;
Shape()
{
this.area=0;
}
abstract void area();
}
34 | P a g e

- Dinesh Varma
class Triangle extends Shape
{
double base=0,height=0;
Triangle()
{
this.base=0;
this.height=0;
}
Triangle(double height,double base)
{
this.height=height;
this.base=b;
}
void area()
{
area = (base*height)/2;
System.out.println("area of Triangle :"+area);
}
}

class Rectangle extends Shape


{
double width=0,height=0;
Rectangle()
{
this.height=0;
this.width=0;
}
Rectangle(double height,double width)
{
this.height=height;
this.width=width;
}
void area()
{
area = width*height;
System.out.println("area of Rectangle :"+area);
}
}

class Circle extends Shape


35 | P a g e

- Dinesh Varma
{
double radius=0;
Circle()
{
this.radius=0;
}
Circle(double x)
{
this.radius=x;
}
void area()
{
area = Math.PI * (radius * radius);
System.out.println("area of Circle :"+area);
}
}

class Ex23
{
public static void main(String [] args)
{
Triangle t= new Triangle(50,15);
Rectangle r =new Rectangle(70,20);
Circle c =new Circle(5);

t.area();
r.area();
c.area();
}
}

Output :

36 | P a g e

- Dinesh Varma
37 | P a g e

- Dinesh Varma

You might also like