Yarharth 1-7
Yarharth 1-7
1321221
Practical No. 1
Aim: Write a program to accept a number as an input and find out whether
number is prime or not.
Source Code:
import java.util.Scanner;
public class prime_no {
public static void main(String args[])
{
int num;
System.out.print("Enter any Number: ");
Scanner obj=new Scanner(System.in);
num=obj.nextInt();
boolean flag =false;
for(int i=2;i<=num/2;++i)
{
if(num%i==0) {
flag=true;
break;
}
}
if(!flag) {
System.out.println(num+ " is a prime number");
}
else{
System.out.println(num+ " is not a prime number");
}
obj.close();
}
}
1
BCA - 4th (A) Roll No. 1321221
Output:
2
BCA - 4th (A) Roll No. 1321221
Practical No. 2
Aim: Write a program to implement constructors in java.
Source Code:
class Const {
int a=55;
int b=458;
Const(int x,int y)
{
x=a;
y=b;
System.out.print("this is parameterized. . ." +x+" "+y);
}
public static void main(String args[]){
Const obj=new Const(55,85);
}
}
Output:
3
BCA - 4th (A) Roll No. 1321221
Practical No. 3
Aim: Write a program to find out area of rectangle, circle and triangle using
method overloading.
Source Code:
class area{
void area1(){
int r=10;
double area_circle=3.142*r*r;
System.out.println("Area of circle="+area_circle);
}
void area2(int len,int wid){
int area=len*wid;
System.out.println("Area of rectangle="+area);
}
void area3(int base,int height){
int area=(base*height)/2;
System.out.println("Area of triangle="+area);
}
4
BCA - 4th (A) Roll No. 1321221
Output:
5
BCA - 4th (A) Roll No. 1321221
Practical No. 4
Aim: Write a program to elaborate the concept of multilevel inheritance in
java.
Source Code:
class Student{
public void read(){
System.out.println("Inheritance...");
}
}
class student1 extends Student{
public void display (){
System.out.println("Multilevel inheritance...");
}
}
class student3 extends student1{
public void study(){
System.out.println("This is multilevel inheritance...");
}
}
public class teacher{
public static void main(String args[]){
student3 obj=new student3();
obj.read();
obj.display();
obj.study();
}
}
6
BCA - 4th (A) Roll No. 1321221
Output:
7
BCA - 4th (A) Roll No. 1321221
Practical No. 5
Aim: Write a program to find out area of rectangle, circle and triangle using
method overriding.
Source Code:
class shapes{
public void area(){
System.out.println("area of different shapes are: ");
}
}
class circle extends shapes{
public void area(){
int radius=5;
double area=3.143*(radius*radius);
System.out.println("area of circle="+area);
}
}
class rectangle extends shapes{
public void area(){
int length=12;
int width=10;
int area=length*width;
System.out.println("area of rectangle="+area);
}
}
class triangle extends shapes{
public void area(){
int base=10;
int height=50;
int area=(base*height)/2;
8
BCA - 4th (A) Roll No. 1321221
System.out.println("area of triangle="+area);
}
}
public class areas{
public static void main(String args[]){
shapes obj=new circle();
shapes obj1=new rectangle();
shapes obj2=new triangle();
obj.area();
obj1.area();
obj2.area();
}
}
Output:
9
BCA - 4th (A) Roll No. 1321221
Practical No. 6
Aim: Write a program to implement the concept of interfaces in java.
Source Code:
interface simple{
void display(); }
class students implements simple{
public void display(){
int num=1546;
if(num%2==0){
System.out.println("Given number is even.."); }
else{
System.out.println("number is odd..."); }
} }
public class even{
public static void main(String args[]){
students obj=new students();
obj.display(); } }
Output:
1
BCA - 4th (A) Roll No. 1321221
Practical No. 7
Aim: Write a program to create your own packages and utilize those
packages in a class.
Package code:
//User defined package
package classbca;
public class myclass {
public void myfunction(){
System.out.println("This is user defined package.. . .");
}
}
Source code:
import classbca.*;
class student{
public static void main(String args[]){
myclass obj=new myclass();
obj.myfunction();
}
}
Output:
1
BCA - 4th (A) Roll No. 1321221