KEMBAR78
Java File | PDF | Java (Programming Language) | Java Virtual Machine
0% found this document useful (0 votes)
15 views66 pages

Java File

JAVA PRACTICAL FILE

Uploaded by

Sunaina 0000
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views66 pages

Java File

JAVA PRACTICAL FILE

Uploaded by

Sunaina 0000
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 66

Programming in Java

Simple

Object-Oriented Portable

Distributed
Java
Complied and Interpreted
Features

Robust
High Performance

Secure Multi Threaded

Architectural Neutral Dynamic & Extensible

LKC/DCSIT/BCA 5th SEM/10721504936 Page 1


Programming in Java

Qus.1 What is Java? Explain the feature of Java.

Ans. Java is a general purpose language and the most popular object oriented programming
language. Due to its simplicity and easy to learn features , it is the first choice of programmers
today. The java programming language is related to Cand C++ but is organized rather
differently, with many sources of bugs and complex features of C and C++ omitted and a few
ideas from other languages included. In addition to the object oriented features, it also provides
features such as platform independence, security , multithreading , portability etc. which makes it
well suited for the web and network services , application , platform-independent desktop ,
robotics and any other embedded devices. These feature together makes java no longer just a
programming language but a platform on its own. The java platform has benefits for the end
user as well as development and support personnel.
Java was developed by James Gosling and his colleagues at Sun Microsystem in the
early 1990’s . Unlike conventional language which are generally designed either to be complied
to machine code , or to interpreted from source code at run time, Java application are intended to
be complied to bytecode that can run on any java Virtual Machine(JVM) regardless of the
computer architecture.
FEATURES OF JAVA: Java is a very powerful , cross-platform , object-oriented programming
language which is designed to solve a number of problems in modern programming practice. It
supports many interesting features that makes it an ideal language for software development .
Java supports the following features that are a part of the official website of the Sun
Microsystem:
Simple: Java is a compared and simple language. Program are easy to write and debug as it
omits many clumsy , poorly understood and confusing features of other programming languages
such as C++. These primarily include exclusive of header files, pointers , implicit type-casting ,
structure, union , operator overloading, multiple inheritance , virtual base classes, templates etc.
Object-Oriented: Java is a fully object- oriented language because programming in java is
centered on creating objects , manipulating objects and making objects work together. In Java ,
everything is an object i.e.
All data types with an exception of few primitive types are rendered as object .
All of GUI building blocks in java are objects.
All function are associated with objects and there can be no free floating function as there are in
C++.
Distributed: Java is a distributed language which means that the programs can be designed to
run on computer networks. Java provides an extensive library of classes for communicating
using TCP/IP protocols such as HTTP and FTP. This makes creating network connections much
easier than in C/C++. You can read and write data from and to a file. This helps the programmers
at remote location to work together on the same project.
Robust: Java is designed for writing programs that are highly robust , we mean reliable. Java
puts a lot of emphasis on early checking of possible errors which would otherwise be detected at

LKC/DCSIT/BCA 5th SEM/10721504936 Page 2


Programming in Java

LKC/DCSIT/BCA 5th SEM/10721504936 Page 3


Programming in Java

execution time in other language. Java also provides strong type checking feature and eliminates
errors prone programming constructs such as pointer.
Java also has a runtime exception handling feature to provide programming supports for
robustness. Java forces the programmer to write the code to deal with exceptions . It can catch
and respond to an exceptional situation so that program can continue its normal execution and
terminate gracefully when runtime error occurs.
The feature of automatic garbage collection makes the programs reliable as it relieves the
programmers from worry of manually and freeing all the dynamic memory.
Secure: Java was designed with the security in mind. As Java is intended to be used in
networked/distributed environments so it implements several security mechanisms to protect you
against malicious code that might try to invade your file security.
Architectural Neutral: One of the key feature of Java that makes it different from other
programming language is architectural neutral. This means that the programs written on one
platform can run on any other platform without having to rewrite or recompile them. In other
words , it follows ‘Write –once-run-anywhere’ approach. This is a significant advantage when
developing applets or applications that are down-loaded from the internet and are needed to run
on different system.
Portable: The portability actually comes from architecture-neutrality. In C/C++ , source code
may run slightly differently on different hardware platform because of how these platforms
implement arithmetic operations. In Java , it has been simplified.
Interpreted: Unlike most of the programming language which are either complied or
interpreted, Java is both complied and interpreted The Java complier translates a java source a
java source file to bytecodes and the Java interpreter executes the translated byte codes directly
on the system that implements the Java Virtual Machine. These two steps of compilation and
interpretation allow extensive code checking and improved security.
High performance : Java programs are complied to portable intermediate form kown as
bytecodes , rather than to native machine level instructions and JVM executes Java bytecode on
any machine on which it is installed. This architecture means that Java programs are faster than
program or scripts written in purely interpreted languages but slower than C and C++ programs
that complied to native machine language.
Multithreaded: Java is also a multithreaded programming language . It allows you to write a
program that can do many tasks simultaneously . Multithreaded enables you to write very
efficient program that makes maximum use of CPU, because idle time can be kept to minimum.
This feature makes Java most suited for the interactive , GUI based networked environment
where idle is common.
Dynamic: Java is designed to be dynamic. Classes are stored in separate files and are loaded into
Java Interpretator only when they are needed. Thus application can decide as it is running what
classes it needs and loaded them when required. In other words , a program can dynamically
extend itself by loading the classes it needs to expand its functionality.

LKC/DCSIT/BCA 5th SEM/10721504936 Page 4


Programming in Java

LKC/DCSIT/BCA 5th SEM/10721504936 Page 5


Programming in Java

Qus2. Write a program in java find the greatest of three numbers using conditional
operators.
Ans.
import java.util.Scanner;
public class Greatest
{
public static void main(String[] args)
{
int a, b,c,result;
Scanner input=new Scanner(System.in);
System.out.print("Enter the three number------>");
a=input.nextInt();
b=input.nextInt();
c=input.nextInt();
result=(a>b)?((a>c)?a:c):((b>c)?b:c);
System.out.println(result+ "is Greatest");
}
}

LKC/DCSIT/BCA 5th SEM/10721504936 Page 6


Programming in Java

LKC/DCSIT/BCA 5th SEM/10721504936 Page 7


Programming in Java

Qus.3 Calculate percentage and grade of student using nested if else statement.
Ans.
import java.util.Scanner;
import java.util.*;
class Per1
{
public static void main(String[] args)
{
int m1,m2,m3;
long sum;
double perc;
System.out.println("Enter marks of student:");
Scanner a=new Scanner(System.in);
m1=a.nextInt();
m2=a.nextInt();
m3=a.nextInt();
sum=m1+m2+m3;
perc=sum/3;
System.out.println("Sum is:="+sum );
System.out.println("Percentage is="+perc);
if((perc>=80 ) && (perc<=100))
{
System.out.println("Grade A");
}
else
if((perc>=60) && (perc<=79))
{
System.out.println("Grade B");
}
if((perc>=50) && (perc<=59))
{
System.out.println("Grade C");
}
else
if((perc>=40 )&& (perc<=49))
{
System.out.println("Grade D");
}
else

LKC/DCSIT/BCA 5th SEM/10721504936 Page 8


Programming in Java

if(perc<40)

LKC/DCSIT/BCA 5th SEM/10721504936 Page 9


Programming in Java

{
System.out.println("Failed");
}
}
}

LKC/DCSIT/BCA 5th SEM/10721504936 Page 10


Programming in Java

LKC/DCSIT/BCA 5th SEM/10721504936 Page 11


Programming in Java

Qus.4. Calculate sum of digit of a number.


Ans.
import java.util.Scanner;
class digitsum
{
public static void main(String[] args)
{
int n , rem , sum=0;
Scanner b=new Scanner(System.in);
System.out.println("Enter number:");
n=b.nextInt();
while(n>0)
{
rem=n%10;
sum=sum+rem;
n=n/10;
}
System.out.println("sum of digits are:"+sum);
}
}

LKC/DCSIT/BCA 5th SEM/10721504936 Page 12


Programming in Java

LKC/DCSIT/BCA 5th SEM/10721504936 Page 13


Programming in Java

Qus.5 Write a program in java to check whether a given number is palindrome or not.
Ans.
import java.util.Scanner;
class Revn
{
public static void main(String[] args)
{
int r , n,rev=0,n1;
Scanner b=new Scanner(System.in);
System.out.println("Enter number:");
n=b.nextInt();
n1=n;
while(n>0)
{
rev=n%10;
rev=rev*10+rev;
n=n/10;
}
if(rev==n1)
System.out.println("Number is Palindrome");
else
System.out.println("Number is not Paindrome");
}
}

LKC/DCSIT/BCA 5th SEM/10721504936 Page 14


Programming in Java

LKC/DCSIT/BCA 5th SEM/10721504936 Page 15


Programming in Java

Qus.6. Write a program in java to display pyramid as follows.


a) 1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
b) 1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
c) A
B C
D E F
G H I J
K L M N O
Ans.
a) import java.util.Scanner;
class pattern1
{
public static void main(String[] args)
{
int i, j, n;
Scanner b=new Scanner(System.in);
System.out.print("Enter value for n:");
n=b.nextInt();
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
System.out.print(j+" ");
}
System.out.println(" ");
}
}
}

LKC/DCSIT/BCA 5th SEM/10721504936 Page 16


Programming in Java

LKC/DCSIT/BCA 5th SEM/10721504936 Page 17


Programming in Java

b) import java.util.Scanner;
class pattern1
{
public static void main(String[] args)
{
int i, j, n;
Scanner b=new Scanner(System.in);
System.out.print("Enter value for n:");
n=b.nextInt();
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
{
System.out.print(i+" ");
}
System.out.println(" ");
}
}
}

LKC/DCSIT/BCA 5th SEM/10721504936 Page 18


Programming in Java

LKC/DCSIT/BCA 5th SEM/10721504936 Page 19


Programming in Java

c)
import java.util.Scanner;
class Pyramid
{
public static void main(String [] args)
{
Scanner input=new Scanner(System.in);
int i,j,n;
char ch='A';
System.out.println("enter the number of lines you want to enter" );
n=input.nextInt();
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
{
System.out.print(ch);
ch++;
}
System.out.println(" ");
}
}
}

LKC/DCSIT/BCA 5th SEM/10721504936 Page 20


Programming in Java

LKC/DCSIT/BCA 5th SEM/10721504936 Page 21


Programming in Java

Qus. 7 Illustrate the functionality and use of string methods in java.


Ans.
class Demostring
{
public static void main(String args[])
{
String str="we are going to work with string!";
int len=str.length();
System.out.println("length of string is:"+len);
str=str.toLowerCase();
System.out.println("\nIn Lower case:"+str);
str=str.toUpperCase();
System.out.println("\nIn Upper case:"+str);
char c=str.charAt(5);
System.out.println("\ncharacter at 5th index is:"+c);
int index=str.indexOf('G');
System.out.println("\ncharacter 'G' is found at index:"+index);
str=str.replace('W','T');
System.out.println("\nAfter replacing 'W' with 'T':"+str);
String str1="vivek";
String str2="and manish are brothers!";
str1=str1.concat(str2);
System.out.println("\n concatenation of String:"+str1);
}
}

LKC/DCSIT/BCA 5th SEM/10721504936 Page 22


Programming in Java

LKC/DCSIT/BCA 5th SEM/10721504936 Page 23


Programming in Java

Qus. 8 Perform linear search in java with help of arrays.


Ans.
import java.util.Scanner;
public class LinearSearch
{
public static void main(String[] args)
{
int [] numArray=new int[5];
int n, i , item, loc=-1;
boolean exist=false;
Scanner input=new Scanner(System.in);
System.out.println("Enter how many elements------>");
n=input.nextInt();
System.out.println("Enter the array elements---->");
for(i=0;i<n;i++)
numArray[i]= input.nextInt();
System.out.print("Enter the item to be searched ------>");
item=input.nextInt();
for(i=0;i<n;i++)
{
if(numArray[i]==item)
{
loc=i;
exist=true;
break;
}
}
if(exist)
System.out.println("Item is present at location ----->" +(loc+1));
else
System.out.println("Item is not present at any location");
}
}

LKC/DCSIT/BCA 5th SEM/10721504936 Page 24


Programming in Java

LKC/DCSIT/BCA 5th SEM/10721504936 Page 25


Programming in Java

Qus. 9 Write a program in java using switch case that performs following operation:
I. To check whether a number is prime or not
II. To print table of a number
III. To check whether a given number is Armstrong or not
IV. To print Fibonacci series.
Ans.
import java.util.*;
import java.io.*;
class Switchcase1
{
public static void main(String[] args)
{
int i,n,c,f=0,s=1,t,tab,flag=1,arm=0,rev;
System.out.println("Enter your option:");
System.out.println("1. To check wheater a given number is prime or not:");
System.out.println("2.To print table of a number:");
System.out.println("3.To check wheater a given number as armstrong or not :");
System.out.println("4.To print fabbonic series:");
System.out.println("5. To exit from program:");
Scanner a=new Scanner(System.in);
c=a.nextInt();
switch(c)
{
case 1:{
System.out.println("Enter the number to check prime:");
n=a.nextInt();
for(i=2;i<=n;i++)
{
if(n%i==0)
{
flag=0;
break;
}
}
if(flag==1)
System.out.println(n+"is prime number");
else
System.out.println(n+"is not a prime number");
break;
}

LKC/DCSIT/BCA 5th SEM/10721504936 Page 26


Programming in Java

case 2:
{

LKC/DCSIT/BCA 5th SEM/10721504936 Page 27


Programming in Java

System.out.println("Enter the number write you want to print table:");


n=a.nextInt();
for(i=1;i<=10;i++)
{
tab=i*n;
System.out.println(n+"*"+i+"="+tab);
}
break;
}
case 3:
{
int d,rev1;
System.out.println("Enter number to check armstrong or not:");
n=a.nextInt();
rev1=n;
while(n>0)
{
d=n%10;
rev1=d*d*d;
arm=arm+rev1;
n=n/10;
}
if(rev1==arm)
System.out.println("Given number is armstrong");
else
System.out.println("Given number is not armstrong");
break;
}
case 4:
{
System.out.println("Enter till how much you want to print

Fibonic series:");
n=a.nextInt();
System.out.println(f);
System.out.println(s);
for(i=1;i<=n-2;i++)
{

LKC/DCSIT/BCA 5th SEM/10721504936 Page 28


Programming in Java

t=f+s;
System.out.println(t);
f=s;
s=t;

LKC/DCSIT/BCA 5th SEM/10721504936 Page 29


Programming in Java

}
break;
}
default: System.out.println("You have entered wrong option plz

try again later");


}
}
}

LKC/DCSIT/BCA 5th SEM/10721504936 Page 30


Programming in Java

Qus.10 Write a program to compute Multiplication , addition and subtraction of complex


numbers.

LKC/DCSIT/BCA 5th SEM/10721504936 Page 31


Programming in Java

import java.util.Scanner;
class Complex
{
private float real,imag;
Complex(){}
Complex(float r, float i)
{
real=r;
imag=i;
}
void show()
{
if(imag>0)
System.out.println(real+"+"+imag+"i");
else
System.out.println(real+imag+"i");
}
void add(Complex cc1,Complex cc2)
{
real=cc1.real+cc2.real;
imag=cc1.imag+cc2.imag;
}
void subtract(Complex cc1,Complex cc2)
{
real=cc1.real-cc2.real;
imag=cc1.imag-cc2.imag;
}
void multiply(Complex cc1,Complex cc2)
{
real=(cc1.real*cc2.real)-(cc1.imag*cc2.imag);
imag=(cc1.real*cc2.imag)+(cc1.imag*cc2.real);
}
void divide(Complex cc1,Complex cc2)
{
float den=cc2.real*cc2.real+cc2.imag+cc2.imag;
real=(cc1.real*cc2.real+cc1.imag*cc2.imag)/den;
imag=(cc1.imag*cc2.real-cc1.real*cc2.imag)/den;
}};

LKC/DCSIT/BCA 5th SEM/10721504936 Page 32


Programming in Java

class Complexdemo
{
public static void main(String[] args)
{

LKC/DCSIT/BCA 5th SEM/10721504936 Page 33


Programming in Java

Complex c1=new Complex(2,3);


Complex c2=new Complex(5,1);
Complex c3=new Complex();
int choice;
Scanner input=new Scanner(System.in);
do
{
System.out.println("\n various complex number operations are");
System.out.println("1.Add operation");
System.out.println("2.subtract operation");
System.out.println("3.multiply operation");
System.out.println("4.divide operation");
System.out.println("5.quit");
System.out.println("enter your choice");
choice=input.nextInt();
switch(choice)
{
case 1: c3.add(c1,c2);
c3.show();
break;
case 2: c3.subtract(c1,c2);
c3.show();
break;
case 3: c3.multiply(c1,c2);
c3.show();
break;
case 4: c3.divide(c1,c2);
c3.show();
break;
case 5:System.out.println("exiting from program");
break;
default:System.out.println("invalid choice.try again");
}
}
while(choice!=5);
}}

LKC/DCSIT/BCA 5th SEM/10721504936 Page 34


Programming in Java

Qus.11 Write a program in java to show the concept of copy constructor.

LKC/DCSIT/BCA 5th SEM/10721504936 Page 35


Programming in Java

Ans. import java.util.Scanner;


class Rectangle
{
int length;
int breadth;
Rectangle(int l,int b)
{
length = l ;
breadth = b ;
}
Rectangle(Rectangle obj)
{
System.out.println("copy constructor invoked");
length = obj.length;
breadth= obj.breadth;
}
int area()
{
return(length*breadth);
}
}
class CopyConstructorDemo
{
public static void main(String[] args)
{
Rectangle firstRect = new Rectangle (5,6);
Rectangle secondRect = new Rectangle(firstRect);
System.out.println("area of first rectangle="+firstRect.area());
System.out.println("area of second rectangle="+secondRect.area());
}
}

LKC/DCSIT/BCA 5th SEM/10721504936 Page 36


Programming in Java

LKC/DCSIT/BCA 5th SEM/10721504936 Page 37


Programming in Java

Qus.12 Write a program using constructor to transfer amount from one account to
another.
Ans.
import java.util.Scanner;
class Account
{
private int acctno;
private double balance;
Account(int acc, double b)
{
acctno=acc;
balance=b;
}
void transfer(Account obj , double amt)
{
balance=balance-amt;
obj.balance=obj.balance+amt;
}
void show()
{
System.out.println("Account Number is ="+acctno);
System.out.println("Balance is ="+balance);
}
}
class TransferMoney
{
public static void main(String[] args)
{
Account acct1= new Account(101,5000);
System.out.println("Information about Acct1.....");
acct1.show();
Account acct2=new Account(102,2000);
System.out.println("Information about Acct2....");
acct2.show();
Scanner input=new Scanner(System.in);
System.out.println("Enter amount to be transferred..>");
double amt=input.nextDouble();
acct1.transfer(acct2,amt);
System.out.println("Information after money is transfered....");
acct1.show();

LKC/DCSIT/BCA 5th SEM/10721504936 Page 38


Programming in Java

acct2.show();
}

LKC/DCSIT/BCA 5th SEM/10721504936 Page 39


Programming in Java

LKC/DCSIT/BCA 5th SEM/10721504936 Page 40


Programming in Java

Qus.13 Write a program to calculate transpose of a matrix.


Ans.
import java.util.Scanner;
public class Transpose
{
public static void main(String[] args)
{
int [] [] table={{5,6,} , {3,4,}};
int [] [] result;
System.out.println("Matrix before transpose:");
for(int i=0; i<table.length;i++)
{
for(int j=0;j<table.length;j++)
System.out.print(table[i][j]+" ");
System.out.println();
}
result=transpose(table);
System.out.println("Matrix After transpose:");

LKC/DCSIT/BCA 5th SEM/10721504936 Page 41


Programming in Java

for(int i=0;i<table.length;i++)
{
for(int j=0;j<table.length;j++)
System.out.print(result[i][j]+ " ");
System.out.println();
}
}
static int[][] transpose(int [] []a)
{
int [] [] temp=new int[a[0].length][a.length];
for(int i=0;i<a[0].length;i++)
{
for(int j=0;j<a.length;j++)
temp[i][j]=a[j][i];
}
return temp;
}
}

LKC/DCSIT/BCA 5th SEM/10721504936 Page 42


Programming in Java

Qus.14. Write a program to calculate multiplication of a matrix.

Ans.
import java.util.Scanner;
class Matmul
{
public static void main(String args[])
{
int a[][]=new int[3][3];
int b[][]=new int[3][3];
int c[][]=new int[3][3];
System.out.println("Enter the first matrix:");
Scanner input=new Scanner(System.in);
for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
a[i][j]=input.nextInt();

System.out.println("Enter the second matrix:");


for(int i=0;i<3;i++)

LKC/DCSIT/BCA 5th SEM/10721504936 Page 43


Programming in Java

for(int j=0;j<3;j++)
b[i][j]=input.nextInt();
System.out.println("Matrix multiplication is as follows:");
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
c[i][j]=0;
for(int k=0;k<3;k++)
{
c[i][j]+=a[i][k]*b[k][j];
}
}
}
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
System.out.print(a[i][j]+"\t");
}
System.out.println("\n");
}

LKC/DCSIT/BCA 5th SEM/10721504936 Page 44


Programming in Java

System.out.println("\n");
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
System.out.print(b[i][j]+"\t");
}
System.out.println("\n");
}
System.out.println("\n");
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
System.out.print(c[i][j]+"\t");
}
System.out.println("\n");
}
}
}

LKC/DCSIT/BCA 5th SEM/10721504936 Page 45


Programming in Java

LKC/DCSIT/BCA 5th SEM/10721504936 Page 46


Programming in Java

Qus.15 Calculate areas of different objects in java using polymorphism/method


overloading.
Ans.
import java.util.Scanner;
class sumtwonum
{
void sum(int x , int y)
{
System.out.println("sum of two integer is " +(x+y));
}
void sum(double x , double y)
{
System.out.println("sum of two double number is " +(x+y));
}
void sum(char x , char y)
{
System.out.println("Sum of two character are=" +(x+y));
}
}
class overloaddemo
{

LKC/DCSIT/BCA 5th SEM/10721504936 Page 47


Programming in Java

public static void main(String[] args)


{
sumtwonum obj=new sumtwonum();
obj.sum(10.5 , 20.8);
obj.sum('A' , 'B');
obj.sum(10, 20);
}
}

LKC/DCSIT/BCA 5th SEM/10721504936 Page 48


Programming in Java

Qus.16 Show the use of abstract class and abstract methods in java.
Ans.
import java.util.Scanner;
abstract class Shape
{
abstract void area();
abstract void circumference();
}
class Rectangle extends Shape
{
private double length,breadth;
Rectangle(double x,double y)
{
length=x; breadth=y;
}
public void area()
{
System.out.println("area of rectangle is="+ (length*breadth));
}
public void circumference()
{
System.out.println("Circumference of Rectangle is="+2*(length+breadth));
}

LKC/DCSIT/BCA 5th SEM/10721504936 Page 49


Programming in Java

}
class Circle extends Shape
{
private double radius;
Circle(double r)
{
radius=r;
}
public void area()
{
System.out.println("Area of circle is ="+(Math.PI*radius*radius));
}
public void circumference()
{
System.out.println("Circumference of circle is="+2*Math.PI*radius);
}
}

LKC/DCSIT/BCA 5th SEM/10721504936 Page 50


Programming in Java

class AbstractDemo
{
public static void main( String[] args)
{
Shape s;
Rectangle r= new Rectangle(10,20);
s=r;
s.area();
s.circumference();
Circle c=new Circle(5);
s=c;
s.area();
s.circumference();
}
}

LKC/DCSIT/BCA 5th SEM/10721504936 Page 51


Programming in Java

LKC/DCSIT/BCA 5th SEM/10721504936 Page 52


Programming in Java

Qus. 17 Implement multiple inheritance with help of interface in java.


Ans.
abstract class Shape
{
abstract double area();
abstract double circumference();
}
class Rectangle extends Shape
{
private double length , breadth;
Rectangle(double x, double y)
{
length=x;
breadth=y;
}
public double getLength()
{
return length;
}
public double getBreadth()
{
return breadth;
}
public double area()

LKC/DCSIT/BCA 5th SEM/10721504936 Page 53


Programming in Java

{
return (length*breadth);
}
public double circumference()
{
return(2*(length+breadth));
}
}
class Circle extends Shape
{
private double radius;
Circle(double r)
{
radius=r;
}
public double getRadius()

LKC/DCSIT/BCA 5th SEM/10721504936 Page 54


Programming in Java

{
return radius;
}
public double area()
{
return(Math.PI*radius*radius);
}
public double circumference()
{
return(2*Math.PI*radius);
}
}
interface CenteredShape
{
public void setCenter(double x, double y);
public double getCenterx();
public double getCentery();
}
class CenteredRectangle extends Rectangle implements CenteredShape
{
private double centerx , centery;
public CenteredRectangle(double cx, double cy , double l, double b)
{
super(l,b);
centerx=cx;
centery=cy;

LKC/DCSIT/BCA 5th SEM/10721504936 Page 55


Programming in Java

}
public void setCenter(double x, double y)
{
centerx=x;
centery=y;
}
public double getCenterx()
{
return centerx;
}
public double getCentery()
{
return centery;
}

LKC/DCSIT/BCA 5th SEM/10721504936 Page 56


Programming in Java

}
class MultipleInheritenceDemo
{
public static void main(String[] args)
{
Shape s;
CenteredRectangle r=new CenteredRectangle(2,3,4,5);
s=r;
System.out.println("Area of centered rectangle is="+s.area());
CenteredRectangle r1=(CenteredRectangle)s;
System.out.println("coordinates are=("+r1.getCenterx()+","+r1.getCentery()+")");
}
}

LKC/DCSIT/BCA 5th SEM/10721504936 Page 57


Programming in Java

LKC/DCSIT/BCA 5th SEM/10721504936 Page 58


Programming in Java

Qus.18 Write a program in java to demonstrate the use of packages.


Ans.
import static java.lang.System.out;
import static java.lang.Math.*;
class StaticImportDemo
{
public static void main(String[] args)
{
out.println("sqrt(3.0)="+sqrt(4.0));
out.println("pow(2.0,4.0)="+pow(2.0,4.0));
out.println("cbrt(3.0)="+cbrt(4.0));
out.println("Value of PI="+PI);
}
}

LKC/DCSIT/BCA 5th SEM/10721504936 Page 59


Programming in Java

LKC/DCSIT/BCA 5th SEM/10721504936 Page 60


Programming in Java

Qus.19 Write a program in java to catch multiple exception.


Ans.
import java.util.Scanner;
public class MultipleExceptionDemo
{
public static void main(String[] args)
{
int [] num={12 , 24, 36,48,60,72};
int [] den={2,4,0,6};
for(int i=0;i<num.length;i++)
{
try
{
System.out.println(num[i]+"/" +den[i]+"=" +num[i]/den[i]);
}
catch(ArithmeticException e)
{
System.out.println("integer division by 0 not possible");
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("Array Index of Bound");
}
}
}
}

LKC/DCSIT/BCA 5th SEM/10721504936 Page 61


Programming in Java

LKC/DCSIT/BCA 5th SEM/10721504936 Page 62


Programming in Java

Qus.20 Write a program in java to overload constructor.


Ans.
import java.util.Scanner;
class Rectangle
{
int length ;
int breadth;
Rectangle()
{
System.out.println("Constructor with zero parameter called");
length=breadth=0;
}
Rectangle(int side)
{
System.out.println("Constructor with one parameter called");
length=breadth=side;
}
Rectangle(int l,int b)
{
System.out.println("Constructor with two parameters called");
length=l;
breadth=b;
}
int area()
{
return(length*breadth);
}
}
class ConstructorOverload
{
public static void main(String[] args)

LKC/DCSIT/BCA 5th SEM/10721504936 Page 63


Programming in Java

{
Rectangle r1=new Rectangle();
Rectangle r2=new Rectangle(5);
Rectangle r3=new Rectangle(7,8);
System.out.println("Area of first rectangle is "+r1.area());
System.out.println("Area of square is "+r2.area());
System.out.println("Area of second rectangle is "+r3.area());
}
}

LKC/DCSIT/BCA 5th SEM/10721504936 Page 64


Programming in Java

Qus.21 Write a program in java to create multiple threads in java by extending Thread
class.
class simpleThread extends Thread
{
public simpleThread(String str)
{
super(str);
}
public void run()
{
for(int i=1;i<5;i++)
{
System.out.println(i+" "+getName());
try
{
int delay=(int)Math.random()*1000;
sleep(delay);
}
catch(InterruptedException e) {}
}
System.out.println("DONE!" + getName());
}
}
class multiplethread
{
public static void main(String args[])
{
simpleThread thread1=new simpleThread("manu");
thread1.start();
simpleThread thread2=new simpleThread("Hanu");
thread2.start();
}

LKC/DCSIT/BCA 5th SEM/10721504936 Page 65


Programming in Java

LKC/DCSIT/BCA 5th SEM/10721504936 Page 66

You might also like