0% found this document useful (0 votes)
14 views17 pages

Rohit Java

Uploaded by

kushamrathee15
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)
14 views17 pages

Rohit Java

Uploaded by

kushamrathee15
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/ 17

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- B.tech (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 java.io.*;
public class revstring{
public static void main(String args[]){
String str= "Hello"; //string to be reversed
char ch ;
String revstring= " ";
System.out.println("Original String is:"+ str);
for(int i=0;i<str.length();i++){
ch=str.charAt(i);
revstring= ch+revstring; //storing value in reverse string
}
System.out.println("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)
{System.out.println();
int n1=0,n2=1,n3,count=10,i;
{System.out.println(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;
{System.out.println(""+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];
System.out.println("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];
}
System.out.print(prod[i][j]+ " " );
}
System.out.println();
}}}

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 java.util.*;
public class pattern {
public static void main(String args[]){
Scanner in = new Scanner(System.in);
System.out.print("no. of pattern lines:"); //pattern size
int n = in.nextInt();
for(int i=0;i<n;i++){ //outer loop to handle rows
for(int j=0;j<=i;j++){ // inner loop to handle columns
System.out.print("*"); //inserting the *
}
System.out.print("\n"); //for a new line
}
}}

8
B. A program to display the pyramid pattern .
*
* *
* * *
* * * *
import java.util.*;
public class pyramidpattern {
public static void main (String args[]){
Scanner sc = new Scanner(System.in);
System.out.println("enter the size of pyramid");
int rows=sc.nextInt();
for(int i=0;i<rows;i++){
for(int j=rows-i;j>1;j--){
System.out.print( " " );
}
for(int j=0;j<=i;j++){
System.out.print("* ");
}
System.out.println();
} }}

9
C. To display the number of days in a particular month and year.

import java.util.*;
public class leapyear {
public static void main(String[]args)
{ Scanner sc= new Scanner(System.in);
int year, month, days=0;
System.out.print("Enter the year=");
year=sc.nextInt();
System.out.print("Enter the no. of month=");
month=sc.nextInt();
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; }
}
System.out.print(days);
}}

1
0
Assignment-4
4. One assignment on Method Overloading.

A. A program to display method overloading.

import java.util.*;
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(System.in);
System.out.println("Enter the value of a =");
int a = sc.nextInt();
System.out.println("Enter the value of b =");
int b = sc.nextInt();
System.out.println("Enter the value of c =");
int c = sc.nextInt();
System.out.println("Enter the value of d =");
int d = sc.nextInt();
System.out.println(ob.add(a,b));
System.out.println(ob.add(a,b,c));
System.out.println(ob.add(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){
System.out.println(e.salary());
}
public static void main (String[]args){
employee obj1=new manager(); // creating object
System.out.println("Manager's salary=");
salary(obj1);
employee obj2=new supervisor(); // creating object
System.out.println("Supervisor's salary=");
salary(obj2);
employee obj3=new clerk(); // creating object
System.out.println("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

System.out.println("GrandFather's class");
}}
class Father extends GrandFather{public void showF(){
System.out.println("Father class has inherited GrandFather class");
}}
class Son extends Father{public void showS(){
System.out.println("Inside son class.");
System.out.println("Son class has inherited Father class");
}}
class Daughter extends Father{public void showD(){
System.out.println("Inside daughter class.");
System.out.println("Daughter class has inherited Father class");
}}
public class multipleInheritance{
public static void main(String args[]){
Son obj = new Son(); //creating object
obj.showS();
obj.showF();
obj.showG();
Daughter obj2 = new Daughter(); //creating object
obj2.showD();
obj2.showF();
obj2.showG();
}}

1
3
Assignment-7
7. One assignment on implemention of Exception handling.

A. A program to display arithmetic exception.


import java.util.Scanner;
public class Exception1 {
public static void main(String[]args){
Scanner sc=new Scanner(System.in);
System.out.print("Enter a=");
int a =sc.nextInt();
System.out.print("Enter b=");
int b =sc.nextInt();
System.out.print("Enter c=");
int c =sc.nextInt();
float d=a/(b+c);
float f=0;
try{
f=a/(b-c); //defining the expression
}
catch(ArithmeticException e )
{
System.out.println("f=cannot be divided by zero."); //exception/condition
System.out.println("Exception:"+ e); //generates exception
}
finally{
System.out.println(d); //display whatever be the result
System.out.println(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(){
System.out.println("Bark-Bark"); }
public void animalsleep(){
System.out.println("7Hours");
}}
class Cat implements Animals{ //cat implements Animal interface
public void animalsound(){ // body of animal sound here
System.out.println("meow-meow");}
public void animalsleep(){ // body of animal sleep here
System.out.println("12hours");
}}
class Goat implements Animals{ //goat implements Animal interface
public void animalsound(){
System.out.println("maii-maii"); }
public void animalsleep(){
System.out.println("8hours");
}}
class Animals{
public static void main(String[]args){
Animals animal=new Dog(); //creating objects
animal.animalsound();
animal.animalsleep();
Animals animal1=new Cat();
animal1.animalsound();
animal1.animalsleep();
Animals animal2=new Goat();
animal2.animalsound();
animal2.animalsleep(); }}

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(){
System.out.println("This is my package");
System.out.println("Package is tested."); }}
public class createpackage{ //name of package
public static void main(String[]args){
Practice obj=new Practice(); //creating object of class
obj.pack();
}}

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=obj.sum(10);
System.out.print(ans);
}}

1
7

You might also like