Practical File: Java

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

PRACTICAL FILE : JAVA

Lab Sessional File

GRAPHIC ERA HILL UNIVERSITY

Master of Computer Application

(MCA)

Submitted To - Submitted By- Sachin singh rawal


Amit Juyal Class - MCA(2B)
Roll No. - 46
PROBLEM STATEMENT 01: Write a java program to create a class student having following data member int
roll_no, string name, double sub1_marks, double sub2_marks , double sub3_marks ,double percentage method ,
parametrized constructor to initialize id ,name and subject marks ,void setdata method will have
parameter(sub1_marks ,sub2_marks ,sub3_marks) ,void show result.

OBJECTIVE: To understand the working of parametrized constructor in java.

JAVA CODE:
class student
{
int roll_no;
String name;
float s1;
float s2;
float s3;
float sum;
float per;
student(int roll_no ,String name ,float s1,float s2,float s3)
{
this.roll_no = roll_no;
this.name=name;
this.s1=s1;
this.s2=s2;
this.s3=s3;
}

float pecentage()
{
sum=s1+s2+s3;
per=sum/3;
return per;
}

void showdata()
{
System.out.println("Name = "+name);
System.out.println("roll_no = "+roll_no);
System.out.println("sub1 = "+s1);
System.out.println("sub2 = "+s2);
System.out.println("sub3 = "+s3);
System.out.println("pecentage = "+per);
System.out.println("");
}
}

class marks
{
public static void main(String[] args)
{
student std1=new student(1,"Raj",90,95,90);
std1.pecentage();
std1.showdata();

student std2=new student(2,"saurav rawat",80,90,70);


std2.pecentage();
std2.showdata();
}
}

OUTPUT:
PROBLEM STATEMENT 02: Write a Java Program to implement simple inheritance.

OBJECTIVE: To understand the concept of inheritance in java.

JAVA CODE:

class Add //parent class

public void add(double a,double b)

System.out.println("sum of two number "+(a+b));

public void sub(double a, double b)

System.out.println("subtract of two number "+(a-b));

class MathOp extends Add //child class

void div(double a,double b)

System.out.println("division of two number "+(a/b));

class Test

public static void main(String[] args)

MathOp m=new MathOp();


m.div(2,3);

m.add(3,4);

m.sub(3,4);

OUTPUT:
PROBLEM STATEMENT 03: Write a Java program to implement method overloading.

OBJECTIVE: To understand the concept of method overloading in java.

JAVA CODE:

class one
{

void method(int n)
{
System.out.println("this is first class");
}

void method(float s)
{
System.out.println("this is second class");
}
}

class overloading
{
public static void main(String[] args)
{
one o1=new one();
o1.method(5); //method overloading
}
}

OUTPUT:-
PROBLEM STATEMENT 04: Write a Java program to implement method overriding.

OBJECTIVE: To understand the concept of method overriding in java.

JAVA CODE:
class parent
{
void m(int n)
{
System.out.println("this is parent class");
}
}

class child extends parent


{
void m(int n)
{
System.out.println("this is child class");
}
}

class overriding //method overriding


{
public static void main(String[] args)
{
child c1=new child();
c1.m(5);
}
}

OUTPUT:
PROBLEM STATEMENT 05: Write a Java program to add two numbers using code line argument.

OBJECTIVE: To understand the use of command line arguments to add two number .

JAVA CODE:

public class Sum

public static void main(String args[])

int x = Integer.parseInt(args[0]); //first arguments

int y = Integer.parseInt(args[1]); //second arguments

int sum = x + y;

System.out.println("sum is " + args[0] + " and " + args[1] + " is: " + sum);

}
OUTPUT:
PROBLEM STATEMENT 06: Write a Java program to find factorial of a number using command line
argument.

OBJECTIVE: To understand the use of command line arguments to find factorial of any number .

JAVA CODE:

class fact

public static void main(String[] args)

int fact=1;

int n=Integer.parseInt(args[0]);

for(int i=1;i<=n;i++)

fact=fact*i;

System.out.println("factorial is" +fact);

OUTPUT:
PROBLEM STATEMENT 07: Write a Java program to print the following pattern:

23

456

7 8 9 10

OBJECTIVE: To print the above pattern and understand the concept of nested loop .

JAVA CODE:

class pattern

public static void main(String[] args)

int c,d,num=1;

for(int i=1;i<5;i++)

for(int j=1;j<=i;j++)

System.out.print(num +" ");

num++;

System.out.println();

}
OUTPUT:
PROBLEM STATEMENT 08: Write a Java program to search an element in an array.

OBJECTIVE: To understand the concepts of array in java.

JAVA CODE:

import java.util.Scanner;

public class Search

public static void main(String args[])

Scanner sc = new Scanner(System.in);

int[] a = new int[5];

int i,c=0;

System.out.println("Enter the array Elements : ");

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

a[i]=sc.nextInt();

System.out.println("Enter the number to be search : ");

int num=sc.nextInt();

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

if(a[i]==num)

c=1;

System.out.println("The number is found in Index : "+ i);

}
if(c!=1)

System.out.println("Number not found");

OUTPUT:
PROBLEM STATEMENT 09: Write a Java program to find largest and second largest element in an array.

OBJECTIVE: To find largest and second largest element in an array.

JAVA CODE:

// Largest and second largest element in an array

import java.util.Scanner;

public class SecondLargest

public static int getSecondLargest(int[] a, int total)

int temp;

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

for (int j = i + 1; j < total; j++)

if (a[i] > a[j])

temp = a[i];

a[i] = a[j];

a[j] = temp;

System.out.println("Largest number: "+ a[total-1]);

return a[total-2];

public static void main(String args[]){


Scanner sc = new Scanner(System.in);

int[] a = new int[5];

int i,c=0;

System.out.println("Enter the array Elements : ");

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

a[i]=sc.nextInt();

System.out.println("The Array elements are : ");

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

System.out.println(a[i]);

System.out.println("Second Largest: "+getSecondLargest(a,5));

OUTPUT:
PROBLEM STATEMENT 10: Write a java program to calculate sum of digits of a numbers.

OBJECTIVE: To calculate sum of digits of a number.

JAVA CODE:

import java.util.Scanner;

class sum

public static void main(String[] args)

Scanner number= new Scanner(System.in);

int sum,c,d;

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

int n1=number.nextInt();

int n2=number.nextInt();

c=n1%10;

d=n2%10;

sum=c+d;

System.out.println("sum is

"+sum);

OUTPUT:
PROBLEM STATEMENT 11: Write a java program to calculate sum of even digit of a given number.

Hint n=12315678

2+4+6+8

20

OBJECTIVE: To calculate sum of even digit of a given number.

JAVA CODE:

import java.util.Scanner;

class even

public static void main(String[] args)

Scanner numbers= new Scanner(System.in);

int sum=0,c;

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

int n1=numbers.nextInt();

while(n1>0)

c=n1%10;

if(c%2==0)

sum+=c;

n1=n1/10;

System.out.println("sum of even number of digit are "+sum);

}
}

OUTPUT:
PROBLEM STATEMENT 12: Write a java program to calculate cyclic sum of given number.

Hint

If a given number =12315

(1+2+3+4+5)+(2+3+4+5)+(3+4+5)+(4+5)+(5)

=>55

OBJECTIVE: To calculate cyclic sum of given number.

JAVA CODE:

import java.util.Scanner;

class cyclic

public static void main(String[] args)

Scanner numbers=new Scanner(System.in);

int n,sum=0,r,temp,count=0;

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

n= numbers.nextInt();

temp=n;

while(temp>0)

count++;

temp=temp/10;

temp=n;

for(int i=1;i<=count;i++)

{
for(int j=1;j<=i;j++)

r=temp%10;

sum+=r;

temp=temp/10;

temp=n;

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

OUTPUT:
PROBLEM STATEMENT 13: Write a java program to generate PIN according following condition.

Hint:-

Step1 if sum of length of input string is single digit then PIN=SUM.

Step2 (if sum in step 1 is two digits)

Calculate again sum of digit

For example

Input string – String s=”abcd abcdef”

First word length = 4

Second word length = 6

4+6=10

If sum of length is single digit then pin=sum

Else again sum=1+0=>pin=1

OBJECTIVE: To understand the logic building for digit of any number and concept of loops.

JAVA CODE:

import java.lang.String;
import java.util.Scanner;
import java.util.*;
public class GeneratePIN
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the String ");
String input1=sc.nextLine();
String[] w = input1.split(" ");
int sum=0;
for(String s: w)
{
sum=sum+s.length();
}
int pin=0; int n=sum; int rem=0; int s=0;
if(sum<0)
{
pin=sum;
}
Else
{
while(true)
{
while(n>0)
{
rem=n%10;
s=s+rem;
n=n/10;
}
if(s<10)
break;
n=s;
}
pin=s;
}
System.out.println(pin);
}
}

OUTPUT:
PROBLEM STATEMENT 14: Create PIN using three given input numbers.

“Secure Assets Private Ltd”, a small company that deal with digital lockers which can be locked and unlocked
using PIN(password).you have been asked to work on that module that is expected to generate PINs using three
input numbers.

Assumptions:- the three given input numbers will always consist three digit i.e each of them in the

Range>=100 and <=999

100<=input1<=999

100<=input2<=999

100<=input3<=999

Below the rules for generating PIN-

-The PIN should be made up of 4 digits.

-The unit(ones)position of the PIN should be the tens position of the three input numbers.

-the hundreds position of the PIN should be the least of the hundred position of the three input numbers.

Example -

Input1=190

Input2=267

Input3=853

Then PIN 9150

OBJECTIVE: To understand the logic building for digit of any number and concept of loops and
made 4 digit PIN number using 3 input numbers.

JAVA CODE:

package demo.com.pkg1;
import java.util.Scanner;
public class PIN
{
public static void main(String[] args)
{

int unit, tens, hundred, greatest, D = 10;


Scanner sc = new Scanner(System.in);
System.out.println("enter the first 3 digit number");
int n1 = sc.nextInt();

System.out.println("enter the second 3 digit number");


int n2 = sc.nextInt();
System.out.println("enter the third 3 digit number");
int n3 = sc.nextInt();

Smallestdigit ob1 = new Smallestdigit();


unit = ob1.smallestdigit(n1, n2, n3, D, 0);

tens = ob1.smallestdigit(n1, n2, n3, D * 10, 1);


hundred = ob1.smallestdigit(n1, n2, n3, D * 100, 2);
Greatestdigit ob4 = new Greatestdigit();
greatest = ob4.greatestdigit(n1, n2, n3);
System.out.println("pin=" + greatest + hundred + tens + unit);

}
}

class Smallestdigit
{
public int smallestdigit(int n1, int n2, int n3, int D, int i)
{

int a, b, c, smallest = 0;

a = n1 % D;
b = n2 % D;
c = n3 % D;
if (i == 1)
{
a = a / 10;
b = b / 10;
c = c / 10;
}
if (i ==
2)
{ a = a / 100;
b = b / 100;
c = c / 100;

if (a < }
b)
{
if (c < a)
{
smallest = c;
}
else
{
smallest = a;
}
}
else
{
if (b < c)
{
smallest = b;
}
else
{
smallest = c;
}
}

return smallest;
}
}

class Greatestdigit
{
public int greatestdigit(int n1, int n2, int n3)
{
int a, b, c, greatest = 0, greatest1 = 0, greatest2 = 0, greatest3 = 0, i, d = 10;

for (i = 1; i <= 3; i++)


{
a = n1 % d;
b = n2 % d;
c = n3 % d;
if (i == 2)
{
a = a / 10;
b = b / 10;
c = c / 10;
}
if (i == 3)
{
a = a / 100;
b = b / 100;
c = c / 100;
}
if (a > b)
{
if (c > a)
{
greatest = c;
}
else
{
greatest = a;
}
}
else
{
if (b > c)
{
greatest = b;
}
else
{
greatest = c;
}
}
if (i == 1)
{
greatest1 = greatest;
}
else
{
if (i == 2)
{
greatest2 = greatest;
}
else
{
greatest3 = greatest;
}
}
d = d * 10;
}
if (greatest1 > greatest2)
{
if (greatest3 > greatest1)
{
greatest = greatest3;
}
else
{
greatest = greatest1;
}
}
else
{
if (greatest2 > greatest3)
{
greatest = greatest2;
}
else
{
greatest = greatest3;
}
}
return greatest;
}
}
OUTPUT:
PROBLEM STATEMENT 15: Write a java program to find nth position Fibonacci number.

OBJECTIVE: to find Nth position Fibonacci number.

JAVA CODE:

import java.util.Scanner;
class fib
{

public static void main(String args[])


{
Scanner ob=new Scanner(System.in);
int f1=0,f2=1,sum,i;
System.out.println("enter number ");
int n= ob.nextInt();

for(i=0;i<(n-1);i++)
{
sum=f1+f2;
f1=f2;
f2=sum;
}
System.out.println(“nth position fibbonacci number is ”+f2);
}
}

OUTPUT:
PROBLEM STATEMENT 16: Create a class box that uses paramerterized constructor to initialize the dimension
of a box . The dimensions of box are width , height , depth. The class should have a method that can return the
volume of the box.create an object of the box class and test the functionalities.

OBJECTIVE: to understand the concept of parametrized constructor.

JAVA CODE:

import java.util.Scanner;

class box

double w,h,d;

box(double width,double height, double depth)

this.w=width;

this.h=height;

this.d=depth;

double volume()

return w*h*d;

public static void main(String[] args)

Scanner sc=new Scanner(System.in);

System.out.println("enter width , height, depth ");

double width=sc.nextDouble();

double height=sc.nextDouble();

double depth=sc.nextDouble();
box b=new box(width,height,depth);

System.out.println("volume is "+b.volume());

OUTPUT:
PROBLEM STATEMENT 17: Create a new class called calculator with the following methods:

1. A static method called powering(int n1,int n2) – this method should return n1 to the power n2.
2. A static method called powerDouble(double n1,int n2) – this method should return n1 to the power n2.

Invoke both the methods and test the functionalities.

OBJECTIVE: To understand the concept of classes in java and different data types.

JAVA CODE:

import java.util.Scanner;
class calculator
{
public static double powering(int n1,int n2)
{
return Math.pow(n1,n2);
}
public static double powerDouble(double n1,int n2)
{
return Math.pow(n1,n2);
}
}
public class cal
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
System.out.println("enter the value of n1 ,n2");
int n1=sc.nextInt();
int n2=sc.nextInt();
System.out.println("powering is "+calculator.powering(n1,n2));
System.out.println("powerDouble is "+calculator.powerDouble(n1,n2));
}
}

OUTPUT:
PROBLEM STATEMENT 18: Crete a class Author with the following infonmation
Member variables:
Name (String), email (String) and gender (char)
Parametrized Constructor; To imitialize the variables
Create a class Book with the following information
Member variables;
Name (String), author (of the class Author you have just created ), price (double) and
qtyinStock(int)
Parameterized Constructor: To initialize the variables
Getters and Setters method for all the member variables.
In the main method create a book object and print all details of the book.

OBJECTIVE: To understand the get and set method and to understand the concept of parameterized
constructor.

JAVA CODE:

class Author
{
String name;
String email;
char gender;
Author(String name,String email,char gender)
{
this.name=name;
this.email=email;
this.gender=gender;
}
public String toString()
{
return "Name of author "+name+" Email "+email+" Gender "+gender;
}
}
class Book
{
String name;
Author author;
double price;
int qty;
public void setName(String name)
{
this.name=name;
}
public String getName()
{
return name;
}
public void setAuthor(Author author)
{
this.author=author;
}
public Author getAuthor()
{
return author;
}
public void setPrice(double price)
{
this.price=price;
}
public double getPrice()
{
return price;
}
public void setQty(int qty)
{
this.qty=qty;
}
public int getQty()
{
return qty;
}
}
public class Bkauthor
{
public static void main(String[] args)
{
Author a1 = new Author("Saurav","[email protected]",'M');
Book b1 = new Book();
b1.setName("Java Programming");
b1.setAuthor(a1);
b1.setPrice(1000);
b1.setQty(17);
System.out.println("Name of the book : "+b1.getName());
System.out.println("Author Details : "+b1.getAuthor());
System.out.println("Price of Book: "+b1.getPrice());
System.out.println("Remaining Books: "+b1.getQty());
}
}

OUTPUT:
PROBLEM STATEMENT 19 : Create a class named Animal which includes method like eat() and
sleep().
Create a child class of Animal named Bird and override the parent class methods. Add a new method
named fly().
Create an instance of Animal class and invoke the eat and sleep method using this object.
Create an instance of Bird class and invoke the eat, sleep and fly method using this object.

OBJECTIVE: To understand the concept of method overriding and objects in java.

JAVA CODE:

class Animal
{
void eat()
{
System.out.println("this is method eat of parent class");
}
void sleep()
{
System.out.println("this is method sleep of parent class");
}
}

class Bird extends Animal


{
void eat()
{
System.out.println("this is method eat of child class");
}
void sleep()
{
System.out.println("this is method sleep of child class");
}
void fly()
{
System.out.println("this is method fly of child class");
}
}
class Lion
{
public static void main(String[] args)
{
Animal A=new Animal();
Bird B=new Bird();
A.eat();
A. sleep();
B. eat();
B.sleep();
B.fly();

}
OUTPUT:

You might also like