GURU JAMBHESHWAR UNIVERSITY OF
SCIENCE AND TECHNOLOGY
HISAR -HARYANA
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
Practical file
of
Object Oriented Programming using Java
(PCC-CSEAI202-P)
SUBMITTED TO : SUBMITTED BY:
Dr. Seema Yodha Rohit
Department of CSE Roll no.-220010150048
GJUS&T, HISAR Class- [Link] (CSE-AI&ML)
INDEX
S. TOPIC PAGE SIGN
NO. NO.
Use Java Platform, create a test
project, create a test class and run it
to see how you can use auto
1.
suggestions and auto fill
functionalities. Try a simple program
of code formatter and code 5
refractoring like renaming variables,
methods and classes. Try a program
which contains at least one if else
condition and a for loop.
A. A program to display reverse of a
string.
Two assignment illustrating
class, objects, methods, arrays, 4
2.
various data types.
A. A program to display fibonacci 6-7
series upto 10 numbers.
B. A program to display product of
two matrices.
Assignments on the use of control,
looping statements and user defined
Functions.
3.
A. A program to display the
following pattern.
*
**
8-10
***
****
B. A program to display the
pyramid pattern.
*
* *
* * *
* * * *
C. To display the number of
days in a particular month and year
2
One assignment on Method
Overloading.
4.
A. A program to display method
overloading. 11
One assignment on method
overriding and polymorphism.
5. A. A program to display method 12
overriding.
Assignment illustrating the
implementation of various forms
of inheritance. 13
6.
A. A program to display multiple
inheritance.
7. One assignment on
implemention of Exception
handling.
7. 14
A. A program to display
arithmetic exception
One assignment to illustrate
interfaces in java.
A. A program to display use of
8.
interfaces in java. 15
One assignment to create packages
in java.
9.
A. A program to display a
package for statements. 16-17
B. A program to display
package for mathematical formula.
3
4
Assignment-1
1. Use Java Platform, create a test project, create a test class and run it to see how you
can use auto suggestions and auto fill functionalities. Try a simple program of code
formatter and code refractoring like renaming variables, methods and classes. Try a
program which contains at least one if else condition and a for loop.
A. A program to display reverse of a string.
import [Link].*;
public class revstring{
public static void main(String args[]){
String str= "Hello"; //string to be reversed
char ch ;
String revstring= " ";
[Link]("Original String is:"+ str);
for(int i=0;i<[Link]();i++){
ch=[Link](i);
revstring= ch+revstring; //storing value in reverse string
}
[Link]("New string is: "+revstring); //printing reverse string
}}
5
Assignment -2
2. Two assignment illustrating class, objects, methods, arrays, 4 various data types.
A. A program to display fibonacci series upto 10 numbers.
//fibonacci series program
public class fibonacci {
public static void main (String[]args)
{[Link]();
int n1=0,n2=1,n3,count=10,i;
{[Link](n1+""+n2); //printing first two elements 0 and 1
for(i=2;i<count;i++) //starting loop from 2 because 0 and1already printed
{n3=n1+n2;
{[Link](""+n3);
n1=n2; n2=n3;
}}
}}}
6
B. A program to display product of two matrices.
public class matrixmultiplication {
public static void main (String args[]){
//initialising two matrix
int a[][]={{5,7,9},{3,2,6},{3,1,3}}; //first matrix
int b[][]={{7,9,4},{4,5,2},{9,7,2}}; //second matrix
//prod is product of two matrix
int prod [][]=new int [3][3];
[Link]("multiplied matrix is:");
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
prod[i][j]=0; for(int k=0;k<3;k++)
{ prod[i][j]+=a[i][k]*b[k][j];
}
[Link](prod[i][j]+ " " );
}
[Link]();
}}}
7
Assignment – 3
3. Assignments on the use of control, looping statements and user defined
Functions.
A. A program to display the following pattern.
*
**
***
****
import [Link].*;
public class pattern {
public static void main(String args[]){
Scanner in = new Scanner([Link]);
[Link]("no. of pattern lines:"); //pattern size
int n = [Link]();
for(int i=0;i<n;i++){ //outer loop to handle rows
for(int j=0;j<=i;j++){ // inner loop to handle columns
[Link]("*"); //inserting the *
}
[Link]("\n"); //for a new line
}
}}
8
B. A program to display the pyramid pattern .
*
* *
* * *
* * * *
import [Link].*;
public class pyramidpattern {
public static void main (String args[]){
Scanner sc = new Scanner([Link]);
[Link]("enter the size of pyramid");
int rows=[Link]();
for(int i=0;i<rows;i++){
for(int j=rows-i;j>1;j--){
[Link]( " " );
}
for(int j=0;j<=i;j++){
[Link]("* ");
}
[Link]();
} }}
9
C. To display the number of days in a particular month and year.
import [Link].*;
public class leapyear {
public static void main(String[]args)
{ Scanner sc= new Scanner([Link]);
int year, month, days=0;
[Link]("Enter the year=");
year=[Link]();
[Link]("Enter the no. of month=");
month=[Link]();
switch(month){
case 1: case 3: case 5: case 7: case 8: // case for 31 days
case 10: case 12:
days=31;
break;
case 4: case 6: case 9: case 11: //case for 30 days
days=30;
break;
case 2:
if(year%4==0){ // case for 28/29 days
days=29;}
else{
days=28; }
}
[Link](days);
}}
1
0
Assignment-4
4. One assignment on Method Overloading.
A. A program to display method overloading.
import [Link].*;
public class overloading {
public int add(int a, int b) //this add contains two int parameters.
{return a+b;}
public int add(int a,int b,int c) // this add contains three int parameters.
{return a+b+c;}
public int add (int a ,int b,int c, int d) //this add contains four int parameters.
{return a+b+c+d;}
public static void main(String[]args){
overloading ob= new overloading(); //creating object of class
Scanner sc =new Scanner([Link]);
[Link]("Enter the value of a =");
int a = [Link]();
[Link]("Enter the value of b =");
int b = [Link]();
[Link]("Enter the value of c =");
int c = [Link]();
[Link]("Enter the value of d =");
int d = [Link]();
[Link]([Link](a,b));
[Link]([Link](a,b,c));
[Link]([Link](a,b,c,d));
}}
1
1
Assignment-5
5. One assignment on method overriding and polymorphism.
A. A program to display method overriding.
class employee{ //creating a parent class
int salary(){
return 0; }}
class manager extends employee{ //creating a child class
int salary(){
return 80000;}}
class supervisor extends employee{ //creating a child class
int salary(){
return 50000;}}
class clerk extends employee{ //creating a child class
int salary(){
return 30000;}}
public class overriding{
static void salary(employee e){
[Link]([Link]());
}
public static void main (String[]args){
employee obj1=new manager(); // creating object
[Link]("Manager's salary=");
salary(obj1);
employee obj2=new supervisor(); // creating object
[Link]("Supervisor's salary=");
salary(obj2);
employee obj3=new clerk(); // creating object
[Link]("Clerk's salary=");
salary(obj3); } }
1
2
Assignment -6
6. Assignment illustrating the implementation of various forms of inheritance.
A. A program to display multiple inheritance.
class GrandFather{public void showG(){ //first class
[Link]("GrandFather's class");
}}
class Father extends GrandFather{public void showF(){
[Link]("Father class has inherited GrandFather class");
}}
class Son extends Father{public void showS(){
[Link]("Inside son class.");
[Link]("Son class has inherited Father class");
}}
class Daughter extends Father{public void showD(){
[Link]("Inside daughter class.");
[Link]("Daughter class has inherited Father class");
}}
public class multipleInheritance{
public static void main(String args[]){
Son obj = new Son(); //creating object
[Link]();
[Link]();
[Link]();
Daughter obj2 = new Daughter(); //creating object
[Link]();
[Link]();
[Link]();
}}
1
3
Assignment-7
7. One assignment on implemention of Exception handling.
A. A program to display arithmetic exception.
import [Link];
public class Exception1 {
public static void main(String[]args){
Scanner sc=new Scanner([Link]);
[Link]("Enter a=");
int a =[Link]();
[Link]("Enter b=");
int b =[Link]();
[Link]("Enter c=");
int c =[Link]();
float d=a/(b+c);
float f=0;
try{
f=a/(b-c); //defining the expression
}
catch(ArithmeticException e )
{
[Link]("f=cannot be divided by zero."); //exception/condition
[Link]("Exception:"+ e); //generates exception
}
finally{
[Link](d); //display whatever be the result
[Link](f);
} }}
1
4
Assignment -8
8. One assignment to illustrate interfaces in java.
A. A program to display use of interfaces in java.
interface Animals{ //interface
public void animalsound(); // interface method(does not have body)
public void animalsleep();} // interface method
class Dog implements Animals{ //dog implements Animal interface
public void animalsound(){
[Link]("Bark-Bark"); }
public void animalsleep(){
[Link]("7Hours");
}}
class Cat implements Animals{ //cat implements Animal interface
public void animalsound(){ // body of animal sound here
[Link]("meow-meow");}
public void animalsleep(){ // body of animal sleep here
[Link]("12hours");
}}
class Goat implements Animals{ //goat implements Animal interface
public void animalsound(){
[Link]("maii-maii"); }
public void animalsleep(){
[Link]("8hours");
}}
class Animals{
public static void main(String[]args){
Animals animal=new Dog(); //creating objects
[Link]();
[Link]();
Animals animal1=new Cat();
[Link]();
[Link]();
Animals animal2=new Goat();
[Link]();
[Link](); }}
1
5
Assignment-9
9. One assignment to create packages in java.
A. A program to display a package for statements.
package mypackage; //name of folder in which package to be created
class Practice //class of package
{
public void pack(){
[Link]("This is my package");
[Link]("Package is tested."); }}
public class createpackage{ //name of package
public static void main(String[]args){
Practice obj=new Practice(); //creating object of class
[Link]();
}}
1
6
B. A program to display package for mathematical formula.
package mypackage; //name of folder in which package to be created
class practice{ //class of package
public int sum (int a)
{
return((a+2)*(a+1));
//expression
}}
public class Package { //name of package
public static void main(String[]args){
practice obj=new practice(); //creating object.
int ans=[Link](10);
[Link](ans);
}}
1
7