Practical File: Java
Practical File: Java
Practical File: Java
(MCA)
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();
OUTPUT:
PROBLEM STATEMENT 02: Write a Java Program to implement simple inheritance.
JAVA CODE:
class Test
m.add(3,4);
m.sub(3,4);
OUTPUT:
PROBLEM STATEMENT 03: Write a Java program to implement method overloading.
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.
JAVA CODE:
class parent
{
void m(int n)
{
System.out.println("this is parent class");
}
}
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:
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
int fact=1;
int n=Integer.parseInt(args[0]);
for(int i=1;i<=n;i++)
fact=fact*i;
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
int c,d,num=1;
for(int i=1;i<5;i++)
for(int j=1;j<=i;j++)
num++;
System.out.println();
}
OUTPUT:
PROBLEM STATEMENT 08: Write a Java program to search an element in an array.
JAVA CODE:
import java.util.Scanner;
int i,c=0;
for(i=0;i<a.length;i++)
a[i]=sc.nextInt();
int num=sc.nextInt();
for(i=0;i<a.length;i++)
if(a[i]==num)
c=1;
}
if(c!=1)
OUTPUT:
PROBLEM STATEMENT 09: Write a Java program to find largest and second largest element in an array.
JAVA CODE:
import java.util.Scanner;
int temp;
temp = a[i];
a[i] = a[j];
a[j] = temp;
return a[total-2];
int i,c=0;
for(i=0;i<a.length;i++)
a[i]=sc.nextInt();
for(i=0;i<a.length;i++)
System.out.println(a[i]);
OUTPUT:
PROBLEM STATEMENT 10: Write a java program to calculate sum of digits of a numbers.
JAVA CODE:
import java.util.Scanner;
class sum
int sum,c,d;
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
JAVA CODE:
import java.util.Scanner;
class even
int sum=0,c;
int n1=numbers.nextInt();
while(n1>0)
c=n1%10;
if(c%2==0)
sum+=c;
n1=n1/10;
}
}
OUTPUT:
PROBLEM STATEMENT 12: Write a java program to calculate cyclic sum of given number.
Hint
(1+2+3+4+5)+(2+3+4+5)+(3+4+5)+(4+5)+(5)
=>55
JAVA CODE:
import java.util.Scanner;
class cyclic
int n,sum=0,r,temp,count=0;
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:-
For example
4+6=10
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
100<=input1<=999
100<=input2<=999
100<=input3<=999
-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
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)
{
}
}
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;
JAVA CODE:
import java.util.Scanner;
class fib
{
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.
JAVA CODE:
import java.util.Scanner;
class box
double w,h,d;
this.w=width;
this.h=height;
this.d=depth;
double volume()
return w*h*d;
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.
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","sauravrwt647@gmail.com",'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.
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");
}
}
}
OUTPUT: