JJJJJJJ
JJJJJJJ
JJJJJJJ
PRROGRAM:
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);
}
OUTPUT:
2|Page
- Dinesh Varma
Input: x=5 Input: n=3 Output: 125
PRROGRAM:
import java.util.Scanner;
// Output
System.out.println("\nResult:: " + b + "^" + e + " = " + r + "\n");
}
}
OUTPUT:
3|Page
- Dinesh Varma
Input: 153 Output: 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");
}
}
}
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);
}
}
}
OUTPUT:
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);
getMaximum(a,b,c);
getMinimum(a,b,c);
}
}
OUTPUT:
PRROGRAM:
6|Page
- Dinesh Varma
public class MatrixMultiplication {
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;
}
}
[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:
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"};
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:
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 {
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{
int en = 0;
String nm = " ";
String gn = " ";
- Dinesh Varma
Student s = new Student();
: "+student_en);
whether this can access the private members of the class or not. [Refer class
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("***********************");
}
}
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;
}
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++;
}
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:
class Ex16
{
public static void main(String args[])
{
// byte data type
byte a = 1;
19 | P a g e
- Dinesh Varma
// wrapping around Float object
Float floatobj = new Float(c);
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;
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();
}
}
Output:
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.]
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************************************");
- 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:
- 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);
*/
- 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.
- 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 :
import java.lang.Math;
- 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);
}
}
- 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