0% found this document useful (0 votes)
153 views53 pages

Java Lab Manual

This document is a lab assignment submission for a Java programming lab in the 5th semester of a BCA program between 2017-2020. It contains an index of 16 programs completed by the student between July 31st and October 11th, with descriptions of each program and output screenshots. The programs cover topics like printing user details, calculating area of a circle, checking palindromes, converting between binary and decimal, calculating salary, and different Java concepts like constructors, inheritance, abstraction etc.

Uploaded by

manish jat
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
153 views53 pages

Java Lab Manual

This document is a lab assignment submission for a Java programming lab in the 5th semester of a BCA program between 2017-2020. It contains an index of 16 programs completed by the student between July 31st and October 11th, with descriptions of each program and output screenshots. The programs cover topics like printing user details, calculating area of a circle, checking palindromes, converting between binary and decimal, calculating salary, and different Java concepts like constructors, inheritance, abstraction etc.

Uploaded by

manish jat
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 53

School of Computer and Systems Sciences

Lab Assignment

BCA 5th Semester

(Session 2017-2020)

Java Programming Lab


(BCA-552)

Submitted to: Submitted by:

Mr.sheetal kumar dixit Manish Samota

(Assistant Professor) Roll No. - 22


Index File
Sno. Program Name Done on Check on

1 WAP in JAVA, to print username and detail 31/7/19 2/8/19

2 A- WAP in JAVA,to calculate area of circle 31/7/19 2/8/19


A- WAP in JAVA,to check weather the no. is
3 31/7/19 2/8/19
palindrome and also check number of digit input

A- WAP in JAVA, to print info. Of student roll no. ,


4 31/7/19 7/8/19
name, etc

A- WAP in JAVA, to pass argument at command


5 prompt and count how many parameter passed at 7/8/19 9/8/19
command prompt

6 A- WAP in JAVA,to convert binary to decimal 7/8/19 9/8/19

7 A- WAP in JAVA,to convert decimal to binary 7/8/19 9/8/19

8 WAP in JAVA,to calculate gross salary 9/8/19 21/8/19

9 WAP in JAVA, to find sum of digit till single digit 9/8/19 21/8/19

10 WAP in JAVA, to check Armstrong number 16/8/19 21/8/19


WAP in JAVA,to find max & min of four no. using if
11 16/8/19 21/8/19
else ladder or math()

WAP in JAVA,to perform arithmetic and ternary


12 21/8/19 04/8/19
operator using switch case

WAP in JAVA,to perform relational and bitwise


13 21/8/19 04/8/19
operator

14 WAP in JAVA,to convert digit to word 23/8/19 27/8/19

15 WAP in JAVA,to convert word to digit 23/8/19 27/8/19

A- WAP in JAVA,to perform default constructor


16 B- WAP in JAVA,to perform parameterised 27/8/19 30/8/19
constructor

A- WAP in JAVA,to implement copy constructor

17 B- WAP in JAVA,to convert numbers of days input 30/8/19 04/9/19


by user into weeksmonths and year & also check year
is leap or not

A- WAP in JAVA,to perform method overloading


18 B- WAP in JAVA,to check yhe given number is 04/9/19 06/09/19
prime number or not
A- WAP in JAVA,to perform method overriding

19 06/09/19 10/09/19

B- WAP in JAVA,to perform dynamic method


dispatch

WAP in JAVA,to perform Up casting


20 06/09/19 10/09/19

WAP in JAVA,to perform single level inheritance


21 10/09/19 13/09/19

WAP in JAVA,to perform multi level inheritance


22 13/09/19 20/09/19

WAP in JAVA,to perform single hierarchical


23 inheritance 20/09/19 09/10/19

WAP in JAVA,to perform abstraction


24 11/10/19 16/10/19
Program 1
Objective:-Write a program in java to print user
basic information.
classOutput{
publicstaticvoidmain(String[]args){

System.out.println("name= shaarukh”);
System.out.println("address= Jaipur”);

System.out.print("mobile= 98936876687”);
System.out.print("age= 20");
}
}

Output
Program 2
Objective: Write java program to calculate area
of circle by user given values.
import java.util.Scanner;

class area

public static void main(String args[])

int r;

double pi= 3.14,ar ;

Scanner sc=new Scanner(System.in);

System.out.println("enter the value of radius");

r=sc.nextInt();

ar=pi*r*r;

System.out.println("value=" +ar);

}}
output
Program 3
Objective: Write java to check no. is palindrome or
not.

import java.io.*;

import java.util.*;

class pal

public static void main(String args[])

int num,temp,rem,res=0,count=0;

Scanner sc=new Scanner(System.in);

System.out.println("enter the number");

num=sc.nextInt();

temp=num;

while(num>0)

rem=num%10;

res=res*10+rem;

num/=10;

count++;

if(num==temp)

System.out.println("palindrome number");

else

System.out.println("not a palindrome");
}

System.out.println("no. of digits="+count);

}}

output
Program 4
Objective: write java program to print student
detail entered by user

import java.io.*;

import java.util.*;

class data

public static void main(String args[])

String name,fname,cname,roll,id;

Scanner sc=new Scanner(System.in);

System.out.println("enter name");

name=sc.next();

System.out.println("enter father's name");

fname=sc.next();

System.out.println("enter college name");

cname=sc.next();

System.out.println("enter rollno");

roll=sc.next();

System.out.println("enter id");

id=sc.next();

System.out.println("*****Student details*****");

System.out.println("student name: "+name+"\nfather's name: "+fname+"\ncollege name:


"+cname+"\nroll no: "+roll+"\nid: "+id);

output
Program 5
Objective:Write java program to calculate
number of arguments

import java.io.*;

import java.util.*;

class arg

public static void main(String args[])

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

output
Program 6
Objective:Write java codeto convert binary no.
into decimal

import java.io.*;

import java.util.*;

class btd

public static void main(String args[])

int b,d=0,res,rem,i=0,base=1;

Scanner sc=new Scanner(System.in);

System.out.println("enter binary number");

b=sc.nextInt();

while(b>0)

rem=b%10;

res=rem*base;

d+=res;

b/=10;

i++;

base=base*2;

System.out.println("decimal no.="+d);

}
Output
Program 7
Objective: write java code to convert decimal
into binary

import java.io.*;

import java.util.*;

class dtb

public static void main(String args[])

int d,rem=1,r=0,i=1;

Scanner sc=new Scanner(System.in);

System.out.println("enter decimal number");

d=sc.nextInt();

while(d>0)

rem=d%2;

d=d/2;

r+=rem*i;

i*=10;

System.out.println("binary number="+r);

}
Output
Program 8
Objective:Write java code to calculate gross
salary.

import java.io.*;

import java.util.*;

class salary

public static void main(String args[])

double bs,da,hra,sal;

Scanner sc=new Scanner(System.in);

System.out.println("enter basic salary");

bs=sc.nextFloat();

if(bs>5000)

da=bs*0.55;

hra=bs*0.15;

else

da=bs*0.45;

hra=bs*0.10;

sal=bs+da+hra;

System.out.println("total salary="+sal);

}
output
Program 9
Objective : Write code to calculate sum of digits
in number in single digit.

class SumOfDigits
{
public static void main(String arg[])
{
long n,sum;
Scanner sc=new Scanner(System.in);
System.out.println("Enter a number ");
n=sc.nextLong();
for(sum=0 ;n!=0 ;n/=10)
{
sum+=n%10;
}
System.out.println("Sum of digits of a number is "+sum);
}
}

Output:
Program 10
Objective: Write a java code to find greatest of
two numbers.
import java.util.Scanner;

public class JavaProgram


{
public static void main(String args[])
{
int a, b, big;
Scanner scan = new Scanner(System.in);

System.out.print("Enter Two Number : ");


a = scan.nextInt();
b = scan.nextInt();

if(a>b)
{
big = a;
}
else
{
big = b;
}

System.out.print("Largest of Two Number is " +big);


}
}

Output
Program 11
Objective: Write a java code to check no. is
Armstrong or not.

importjava.util.Scanner;
publicclassJavaExample{

publicstaticvoidmain(String[]args){

intnum, number, temp, total =0;


System.out.println("Ënter 3 Digit Number");
Scannerscanner=newScanner(System.in);
num=scanner.nextInt();
scanner.close();
number =num;

for(;number!=0;number /=10)
{
temp = number %10;
total = total + temp*temp*temp;
}

if(total ==num)
System.out.println(num+" is an Armstrong number");
else
System.out.println(num+" is not an Armstrong number");
}
}

Output
Program 12
Objective: Write java code to perform arithmetic
operation & ternary operator.

import java.io.*;

import java.util.*;

class arithmatic

public static void main(String args[])

float num1,num2,num3,num4;

char ch='\0';

Scanner sc=new Scanner(System.in);

System.out.println("enter 2 numbers for arithmatic operation");

num1=sc.nextFloat();

num2=sc.nextFloat();

System.out.println("enter ur choice\n +.add\n-.subtract\n*.multiply\n/.divide\n%.remainder\n");

try{

ch=(char)System.in.read();}

catch(Exception e){}

switch(ch)

case '+':num3=num1+num2;

System.out.println("addition result="+num3);

break;

case '-':num3=num1-num2;

System.out.println("subtraction result="+num3);
break;

case '*':num3=num1*num2;

System.out.println("multiplication result="+num3);

break;

case '/':num3=num1/num2;

System.out.println("divison result="+num3);

break;

case '%':num3=num1+num2;

System.out.println("remainder="+num3);

break;

default:System.out.println("wrong input");

num4=(num1>num2)?num1:num2;

System.out.println(num4+"is greater");

}}

Output
Program 13
Objective: write java code to perform relation &
bitwise operator.

import java.io.*;

import java.util.*;

class relation

public static void main(String args[])

int num1,num2;

Scanner sc=new Scanner(System.in);

System.out.println("enter 2 numbers");

num1=sc.nextInt();

num2=sc.nextInt();

if(num1==num2)

System.out.println(num1+" is equal to "+num2);

else

if(num1>num2)

System.out.println(num1+" is greater than "+num2);

else

System.out.println(num1+" is less than "+num2);

}
int c=num1&num2;

int d=num1|num2;

int e=num1<<2;

int f=num2>>1;

System.out.println("and= "+c);

System.out.println("or= "+d);

System.out.println("lshift= "+e);

System.out.println("rshift= "+f);

Output
Program 14
Objective: Write java code to convert number
into words.
import java.io.*;

import java.util.*;

class conv

public static void main(String args[])

String num;

char ch;

int i;

Scanner sc=new Scanner(System.in);

System.out.println("enter number");

num=sc.next();

for(i=0;i<num.length();i++)

ch=num.charAt(i);

switch(ch)

case '1':System.out.print("one ");

break;

case '2':System.out.print("two ");

break;

case '3':System.out.print("three ");

break;

case '4':System.out.print("four ");

break;
case '5':System.out.print("five ");

break;

case '6':System.out.print("six ");

break;

case '7':System.out.print("seven ");

break;

case '8':System.out.print("eight ");

break;

case '9':System.out.print("nine ");

break;

case '0':System.out.print("zero ");

break;

default:System.out.print(" ");

Output
Program 15
Objective: Write java code to convert words into
digits.

import java.util.*;

class convert1

public static void main(String args[])

Scanner sc=new Scanner(System.in);

StringTokenizeroly=new StringTokenizer(sc.nextline());

while(oly.hasMoreTokens())

System.out.print(getNum(oly.nextToken()));

public static int getNum(String num)

if(num.equals("one"))

return 1;

else if(num.equals("two"))

return 2;

else if(num.equals("three"))

return 3;

else if(num.equals("four"))

return 4;

else if(num.equals("five"))

return 5;

else if(num.equals("six"))
return 6;

else if(num.equals("seven"))

return 7;

else if(num.equals("eight"))

return 8;

else if(num.equals("nine"))

return 9;

else

return 0;

Output
Program 16(a)
Objective: Write a program to implement default
constructor.
classDemo
{
publicDemo()
{
System.out.println("This is a no argument constructor");
}
publicstaticvoidmain(Stringargs[]){
newDemo();
}
}

Output
Program 16(b)
Objective: Write a program to implement
parameterized constructor.

publicclassEmployee{

intempId;
StringempName;

//parameterized constructor with two parameters


Employee(int id,String name){
this.empId= id;
this.empName= name;
}
voidinfo(){
System.out.println("Id: "+empId+" Name: "+empName);
}

publicstaticvoidmain(Stringargs[]){
Employee obj1 =newEmployee(10245,"Chaitanya");
Employee obj2 =newEmployee(92232,"Negan");
obj1.info();
obj2.info();
}
}

Output
Program 17(a)
Objective: Write a program to implement copy
constructor.
classJavaExample{
String web;
JavaExample(String w){
web = w;
}

/* This is the Copy Constructor, it


* copies the values of one object
* to the another object (the object
* that invokes this constructor)
*/
JavaExample(JavaExample je){
web =je.web;
}
voiddisp(){
System.out.println("Website: "+web);
}

publicstaticvoidmain(Stringargs[]){
JavaExample obj1 =newJavaExample("BeginnersBook");

/* Passing the object as an argument to the constructor


* This will invoke the copy constructor
*/
JavaExample obj2 =newJavaExample(obj1);
obj1.disp();
obj2.disp();
}
}

Output
Program 17(b)
Objective: Write java code to convert numbers of
days inputed by user into weeksmonths and year
& also check year is leap or not.

import java.io.*;

import java.util.*;

class yearcon

public static void main(String args[])

int n;

double year,week,days;

Scanner sc=new Scanner(System.in);

System.out.println("enter value of n");

n=sc.nextInt();

year=(double)n/365;

if(n%4==0)

if(n%100==0)

if(n%400==0)

System.out.println("leap year");

else

System.out.println("not a leap year");

else

System.out.println("leap year");
}

else

System.out.println("not a leap year");

week=(year * 365)/7;

days=year*365;

System.out.println("no of years="+year);

System.out.println("no of weeks="+week);

System.out.println("no of days="+days);

Output
Program 18(a)
Objective:write java code to implement method
overloading.

classDisplayOverloading
{
publicvoiddisp(char c)
{
System.out.println(c);
}
publicvoiddisp(char c,intnum)
{
System.out.println(c +" "+num);
}
}
classSample
{
publicstaticvoidmain(Stringargs[])
{
DisplayOverloadingobj=newDisplayOverloading();
obj.disp('a');
obj.disp('a',10);
}
}

Output
Program 18(b)
Objective: write java code to check number is
prime or not.
import java.io.*;

import java.util.*;

class prime

public static void main(String args[])

int num,i;

Scanner sc=new Scanner(System.in);

System.out.println("enter a number");

num=sc.nextInt();

for(i=2;i<num;i++)

if(num%i==0)

System.out.println("not a prime number");

break;

if(num==i)

System.out.println("prime number");

}
Output
Program 19(a)
Objective: Write java code to perform method
overriding.
class Parent {
// private methods are not overridden
private void m1()
{
System.out.println("From parent m1()");
}

protected void m2()


{
System.out.println("From parent m2()");
}
}

class Child extends Parent {


// new m1() method
// unique to Child class
private void m1()
{
System.out.println("From child m1()");
}

// overriding method
// with more accessibility
@Override
public void m2()
{
System.out.println("From child m2()");
}
}

// Driver class
class Main {
public static void main(String[] args)
{
Parent obj1 = new Parent();
obj1.m2();
Parent obj2 = new Child();
obj2.m2();
}
}
Output
Program 19(b)
Objective: write java code to implement dynamic
method dispatch.

import java.io.*;

import java.util.*;

class dmd

void show()

System.out.println("hello");

class dmd2 extends dmd

void show()

System.out.println("world");

class dmd1

public static void main(String args[])

dmd a=new dmd();

dmd2 b=new dmd2();

dmd ref;

ref=a;

ref.show();
ref=b;

ref.show();

Output
Program 20
Objective: Write java code to perform upcasting.

import java.io.*;

import java.util.*;

class up1

int x=10;

void show()

System.out.println("hello");

class a extends up1

int x=20;

void show()

System.out.println("world");

class up

public static void main(String args[])

up1 obj1=new up1();

a obj2=new a();

obj1=obj2;

obj1.show();
}

Output
Program 21
Objective: write java code to implement single
level inheritance.

import java.io.*;

import java.util.*;

class super1

int a,b;

void input(int x,int y)

a=x;

b=y;

int add()

return(a+b);

class sub1 extends super1

int mul()

return(a*b);

class single1

public static void main(String args[])


{

sub1 s1=new sub1();

s1.input(10,20);

System.out.println("addition="+s1.add());

System.out.println("multiplication="+s1.mul());

Output
Program 22
Objective: write java code to implement
multilevel inheritance.

import java.util.*;

import java.io.*;

class in

int a,b;

public void inp()

Scanner sc=new Scanner(System.in);

System.out.println("enter 2 numbers");

a=sc.nextInt();

b=sc.nextInt();

class csum extends in

int sum;

public void summ()

sum=a+b;

void out1()

System.out.println("sum="+sum);

}
class multilevel extends in

int sub;

public void subb()

sub=a-b;

void out1()

System.out.println("subtraction="+sub);

public static void main(String Args[])

multilevel o=new multilevel();

o.inp();

o.subb();

o.out1();

csum p=new csum();

p.summ();

p.out1();

}
Output
Program 23
Objective: write java code to implement
hierarchical inheritance.

class A
{
publicvoidmethodA()
{
System.out.println("method of Class A");
}
}
class B extends A
{
publicvoidmethodB()
{
System.out.println("method of Class B");
}
}
class C extends A
{
publicvoidmethodC()
{
System.out.println("method of Class C");
}
}
class D extends A
{
publicvoidmethodD()
{
System.out.println("method of Class D");
}
}
classJavaExample
{
publicstaticvoidmain(Stringargs[])
{
B obj1 =newB();
C obj2 =newC();
D obj3 =newD();
//All classes can access the method of class A
obj1.methodA();
obj2.methodA();
obj3.methodA();
}
}
Output
Program 24
Objective: write java code to implement
abstraction.

// Java program to illustrate the

// concept of Abstraction

abstract class Shape

String color;

// these are abstract methods

abstract double area();

public abstract String toString();

// abstract class can have constructor

public Shape(String color) {

System.out.println("Shape constructor called");

this.color = color;

// this is a concrete method

public String getColor() {

return color;

class Circle extends Shape

double radius;
public Circle(String color,double radius) {

// calling Shape constructor

super(color);

System.out.println("Circle constructor called");

this.radius = radius;

@Override

double area() {

return Math.PI * Math.pow(radius, 2);

@Override

public String toString() {

return "Circle color is " + super.color +

"and area is : " + area();

class Rectangle extends Shape{

double length;

double width;

public Rectangle(String color,doublelength,double width) {

// calling Shape constructor

super(color); .3.

System.out.println("Rectangle constructor called");

this.length = length;

this.width = width;
}

@Override

double area() {

return length*width;

@Override

public String toString() {

return "Rectangle color is " + super.color +

"and area is : " + area();

public class Test

public static void main(String[] args)

Shape s1 = new Circle("Red", 2.2);

Shape s2 = new Rectangle("Yellow", 2, 4);

System.out.println(s1.toString());

System.out.println(s2.toString());

}
Output

You might also like