Rohit Java
Rohit Java
Practical file
of
Object Oriented Programming using Java
(PCC-CSEAI202-P)
2
One assignment on Method
Overloading.
4.
A. A program to display method
overloading. 11
7. One assignment on
implemention of Exception
handling.
7. 14
A. A program to display
arithmetic exception
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.
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.
6
B. A program to display product of two matrices.
7
Assignment – 3
3. Assignments on the use of control, looping statements and user defined
Functions.
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.
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.
1
2
Assignment -6
6. Assignment illustrating the implementation of various forms of inheritance.
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.
1
4
Assignment -8
8. One assignment to illustrate interfaces in java.
1
5
Assignment-9
9. One assignment to create packages in java.
A. A program to display a package for statements.
1
6
B. A program to display package for mathematical formula.
1
7