0% found this document useful (0 votes)
18 views6 pages

OOP Exercise I

The document provides 10 exercises on OOP concepts in Java including arrays, constructors, inheritance, polymorphism and interfaces. The exercises involve writing Java programs to demonstrate concepts like finding min/max of arrays, sorting arrays, generating patterns using nested loops, constructor overloading, static variables, method overloading, inheritance, polymorphism and interfaces.

Uploaded by

yabera528
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views6 pages

OOP Exercise I

The document provides 10 exercises on OOP concepts in Java including arrays, constructors, inheritance, polymorphism and interfaces. The exercises involve writing Java programs to demonstrate concepts like finding min/max of arrays, sorting arrays, generating patterns using nested loops, constructor overloading, static variables, method overloading, inheritance, polymorphism and interfaces.

Uploaded by

yabera528
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

OOP Exercise

1. Write a java application that finds the smallest and largest of several integers using one
dimensional array. Assume that the first value read specifies the number of values to input
from the user.
2. Write a java application that swaps and sorts one dimensional array element in ascending
order. Consider the following unsorted array values {60, 40, 20, 30, 50, 10 } as an input.
3. Write a java application that displays the following patterns separately, one below the
other. Use a nested for loops to generate the patterns.

4. Write the following java program on Netbeans IDE and analyze its outputs.

package teststudent;
class Student{
int id;
String name;
Student(int id,String name){
this.id=id;
this.name=name;
}
void display(){
System.out.println("The student info");
System.out.println("===============");
System.out.print(" "+name+" "+id);
System.out.print("\n ");
}
}
public class TestStudent {
public static void main(String[] args) {
Student s1=new Student(101,"Dave");
s1.display(); }
}
a. Modify the above program using a default constructor.
b. Apply constructor overloading and getter and setter methods adding
member fields as you like.
5. Write the following java program on Netbeans IDE and analyze its outputs.
package counter;
public class Counter {

1|Page
OOP Exercise

//int count=0;//will get memory when instance is created


static int count=0;//will get memory when instance is created
Counter(){
count++;
System.out.println(count);
}
public static void main(String args[]){

Counter c1=new Counter();


Counter c2=new Counter();
Counter c3=new Counter();
}}
Note the use of static keyword
6. Write the following java program on Netbeans IDE and analyze its outputs.

package factorial;
public class Factorial {
void fact(int n){
int fact=1;
for(int i=1;i<=n;i++){
fact=fact*i;
}
System.out.println("factorial is "+fact);
}
void fact1(int n){
int x = 1;
int result=1;
while( x <=n ) {
result*=x;
System.out.println("value of x : " + x );
x++;
}
System.out.println("The result= : " + result );

}
public static void main(String args[]){
new Factorial().fact(5);//calling method with annonymous object
new Factorial().fact1(5);
} }

7. Write the following java program on Netbeans IDE and analyze its outputs.

package decimaltoprime;
class decimal
{
int dec,ib,ic,ih;
int[] bi=new int[15];
int[] oc=new int[10];
char[] hex=new char[10];

2|Page
OOP Exercise

void calbin()
{
System.out.println("decimal\t"+"bin eqvt.\t"+"octal
eqvt\t"+"hexa eqvt");
for(int i=1;i<256;i++)
{
dec=i;
ib=0;
ic=0;
ih=0;
int dev,com;
while(dec>0)
{
bi[++ib]=dec%2;
dev=dec/2;
dec=dev;
}
System.out.print(i+"\t");
for(int j=ib;j>0;j--)
{
System.out.print(bi[j]);
}
dec=i;
while(dec>0)
{
oc[++ic]=dec%8;
dev=dec/8;
dec=dev;
}
System.out.print("\t"+"\t");
for(int j=ic;j>0;j--)
{
System.out.print(oc[j]);
}
dec=i;
while(dec>0)
{
com=dec%16;
if(com==0)
hex[++ih]='0';
else if(com==1)
hex[++ih]='1';
else if(com==2)
hex[++ih]='2';
else if(com==3)

3|Page
OOP Exercise

hex[++ih]='3';
else if(com==4)
hex[++ih]='4';
else if(com==5)
hex[++ih]='5';
else if(com==6)
hex[++ih]='6';
else if(com==7)
hex[++ih]='7';
else if(com==8)
hex[++ih]='8';
else if(com==9)
hex[++ih]='9';
else if(com==10)
hex[++ih]='A';
else if(com==11)
hex[++ih]='B';
else if(com==12)
hex[++ih]='C';
else if(com==13)
hex[++ih]='D';
else if(com==14)
hex[++ih]='E';
else
hex[++ih]='F';
dev=dec/16;
dec=dev;
}
System.out.print("\t"+"\t");
for(int j=ih;j>0;j--)
{
System.out.print(hex[j]);
}
System.out.println();
} } }
public class DecimalToPrime {
public static void main(String[] args) {
// TODO code application logic here
decimal dd=new decimal();
dd.calbin();
}}
8. Write the following java program on Netbeans IDE and analyze its outputs.
package employee;
public class Employee {
float salary=40000;
public static void main(String args[]){
Programmer p=new Programmer();

4|Page
OOP Exercise

System.out.println("Programmer salary is:"+p.salary);


System.out.println("Bonus of Programmer is:"+p.bonus);
}
}
public class Programmer extends Employee {
int bonus=10000;
}
public class Officer extends Employee {
double allowance;
}
9. Write the following java program on Netbeans IDE and analyze its outputs.

package polysplendor;
public class Bike {
void run(){
System.out.println("This is Bike");
System.out.println("_________________");
System.out.println(" It is running safely with 50km");
}}
public class PolySplendor extends Bike {
void run(){
System.out.println("This is Splendor");
System.out.println("_________________");
System.out.println("It is running safely with 60km");
}
public static void main(String[] args) {
Bike b= new PolySplendor();
b.run();
Bike b1= new Bike();
b.run();
}}
Note the concept of polymorphism

10. Write the following java program on Netbeans IDE and analyze its outputs
c. Association of a class with an interface
package testinterface;
interface Printable{
void print();
}
interface Showable{
void show();
}
public class TestInterface implements Printable,Showable {
public void print(){System.out.println("Hello");}
public void show(){System.out.println("Welcome");}
public static void main(String[] args) {
// TODO code application logic here
TestInterface obj = new TestInterface();
obj.print();
obj.show();

5|Page
OOP Exercise

}
}
///////////////////////////////////////////////////////////////////////////
d. Association of a class with an interface

package testinterface;
interface Printable{
void print();
}
interface Showable extends Printable {
void show();
}
public class TestInterface implements Showable {
public void print(){System.out.println("Hello");}
public void show(){System.out.println("Welcome");}
public static void main(String[] args) {
// TODO code application logic here
TestInterface obj = new TestInterface();
obj.print();
obj.show();
}
}

6|Page

You might also like