Object Oriented Programming Project File
Object Oriented Programming Project File
class FactorialExample{
public static void main(String args[]){
int i,fact=1;
int number=5;//It is the number to calculate factorial
for(i=1;i<=number;i++){
fact=fact*i;
}
System.out.println("Factorial of "+number+" is: "+fact);
}
}
OUTPUT :-
Factorial of 5 is: 120
Q-2:- Program to print power of a given no. ?
import java.util.Scanner;
public class PowerOfNumberExample1
{
//function to find the power of a number
static int power(int base, int exponent)
{
int power = 1;
//increment the value of i after each iteration until the condition becomes false
for (int i = 1; i <= exponent; i++)
//calculates power
power = power * base;
//returns power
return power;
}
//driver code
public static void main(String args[])
{
int base, exponent;
Scanner sc=new Scanner(System.in);
System.out.print("Enter the base: ");
base=sc.nextInt();
System.out.print("Enter the exponent: ");
exponent=sc.nextInt();
//calling function
int pow=power(base, exponent);
//prints the result
System.out.println(base +" to the power " +exponent + " is: "+pow);
}
}
OUTPUT :-
Enter the base: 11
Enter the exponent: 3
11 to the power 3 is: 1331
Q-3:- Program to print Fibonacci series ?
class FibonacciExample1{
public static void main(String args[])
{
int n1=0,n2=1,n3,i,count=10;
System.out.print(n1+" "+n2);//printing 0 and 1
OUTPUT :-
0 1 1 2 3 5 8 13 21 34
Q-4:- Program to print largest, middle and
smallest value among three values ?
import java.util.Scanner;
class Small_Large
{
public static void main (String args[])
{
Scanner scan=new Scanner(System.in);
System.out.print("Enter the first number: ");
float num1=scan.nextFloat();//get input from user for num1
System.out.print("Enter the second number: ");
float num2=scan.nextFloat();//get input from user for num2
System.out.print("Enter the third number: ");
float num3=scan.nextFloat();//get input from user for num3
if(num1<=num2 && num1<=num3)
{
System.out.println("\n The Smallest number is: "+num1);
}
else if(num2<=num1 && num2<=num3)
{
System.out.println("\n The Smallest number is: "+num2);
}
else{
System.out.println("\n The Smallest number is: "+num3);
}
if(num1>=num2 && num1>=num3){
System.out.println("\n The Smallest number is: "+num1);
}
else if(num2>=num1 && num2>=num3){
System.out.println("\n The Smallest number is: "+num2);
}
else{
System.out.println("\n The largest number is: "+num3);
}
}
}
OUTPUT :-
import java.util.Scanner;
OUTPUT :-
**********************
**********************
**********************
**********************
**********************
**********************
**********************
Q-6:- Program to implement arrays ?
class Testarray
{
public static void main(String args[])
{
int a[]=new int[5];//declaration and instantiation
a[0]=10;//initialization
a[1]=20;
a[2]=70;
a[3]=40;
a[4]=50;
//traversing array
for(int i=0;i<a.length;i++)//length is the property of array
System.out.println(a[i]);
}
}
OUTPUT :-
10
20
70
40
50
Q-7:- Program to implement type casting using wrapper classes &
methods such as parseInt( ) ?
Q-8:- Program to implement command line arguments?
class A{
public static void main(String args[]){
for(int i=0;i<args.length;i++)
System.out.println(args[i]);
}
}
OUTPUT :-
Your first argument is: sonoo
Q-9:- Program to implement string handling functions ?
class Main {
public static void main(String[] args) {
// create a string
String greet = "Hello! World";
System.out.println("String: " + greet);
OUTPUT :-
class Bike1
{
//creating a default constructor
Bike1(){System.out.println("Bike is created");
}
//main method
public static void main(String args[]){
//calling a default constructor
Bike1 b=new Bike1();
}
}
OUTPUT :-
Bike is created
Q-11:- Write Java Program to implement multilevel inheritance and
hierarchical inheritance ?
Multilevel inheritance:-
class Shape {
public void display() {
System.out.println("Inside display");
}
}
class Rectangle extends Shape {
public void area() {
System.out.println("Inside area");
}
}
class Cube extends Rectangle {
public void volume() {
System.out.println("Inside volume");
}
}
public class Tester {
public static void main(String[] arguments) {
Cube cube = new Cube();
cube.display();
cube.area();
cube.volume();
}
}
OUTPUT :-
Inside display
Inside area
Inside volume
Hierarchical inheritance :-
OUTPUT:-
parentNum * childNum1 = 10
parentNum * childNum2 = 20
parentNum * childNum3 = 30
Q-12:- Program to implement interfaces ?
interface printable{
void print();
}
class A6 implements printable{
public void print(){System.out.println("Hello");}
OUTPUT :-
Hello
Q-13:- Program to implement abstract class and methods ?
OUTPUT :-
running safely
Q-15:- Program to implement multiple inheritance ?
interface PI1 {
// Default method
default void show()
{
System.out.println("Default PI1");
}
}
// Interface 2
interface PI2 {
default void show()
{
System.out.println("Default PI2");
}
}
// Main class
// Implementation class code
class TestClass implements PI1, PI2 {
// Overriding default show method
@Override
public void show()
{
// Using super keyword to call the show
// method of PI1 interface
PI1.super.show();//Should not be used directly in the main method;
OUTPUT:-
Default PI1
Default PI2
Now Executing showOfPI1() showOfPI2()
Default PI1
Default PI2
Q-15:- Program to implement polymorphism by method overriding
and overloading ?
Method Overloading:-
import java.io.*;
class MethodOverloadingEx {
OUTPUT :-
import java.io.*;
class Animal {
void eat()
{
System.out.println("eat() method of base class");
System.out.println("eating.");
}
}
d1.eat();
a1.eat();
OUTPUT :-
// Interface
interface Age {
// Class 1
// Helper class implementing methods of Age Interface
class MyClass implements Age {
// Class 2
// Main class
// AnonymousDemo
class GFG {
// Main driver method
public static void main(String[] args)
{
// Class 1 is implementation class of Age interface
MyClass obj = new MyClass();
OUTPUT :-
Age is 21
Q-17:- Program to implement static methods and variables ?
class Student{
int rollno;//instance variable
String name;
static String college ="ITS";//static variable
//constructor
Student(int r, String n){
rollno = r;
name = n;
}
//method to display the values
void display (){System.out.println(rollno+" "+name+" "+college);}
}
//Test class to show the values of objects
public class TestStaticVariable1{
public static void main(String args[]){
Student s1 = new Student(111,"Karan");
Student s2 = new Student(222,"Aryan");
//we can change the college of all objects by the single line of code
//Student.college="BBDIT";
s1.display();
s2.display();
}
}
OUTPUT :-
111 Karan ITS
222 Aryan ITS
Q-18:- Program to implement final keywords for objects, methods and
classes?
class Bike{
final void run(){System.out.println("running");}
}
OUTPUT :-
Compile Time Error.
OUTPUT :-
Compile Time Error
Q-19:- Program to implement packages ?
package p1;
class C1()
{
public void m1()
{
System.out.println("m1 of C1");
}
public static void main(string args[])
{
C1 obj = new C1();
obj.m1();
}
}
Q-20:- Program to implement access protection such as public,
protected, private and defaults ?
PRIVATE:-
class A{
private int data=40;
private void msg(){System.out.println("Hello java");}
}
PROTECTED:-
//save by A.java
package pack;
public class A{
protected void msg(){System.out.println("Hello");}
}
//save by B.java
package mypack;
import pack.*;
class B extends A{
public static void main(String args[]){
B obj = new B();
obj.msg();
}
}
OUTPUT:- Hello
DEFAULT:-
//save by A.java
package pack;
class A{
void msg(){System.out.println("Hello");}
}
//save by B.java
package mypack;
import pack.*;
class B{
public static void main(String args[]){
A obj = new A();//Compile Time Error
obj.msg();//Compile Time Error
}
}
PUBLIC:-
//save by A.java
package pack;
public class A{
public void msg(){System.out.println("Hello");}
}
//save by B.java
package mypack;
import pack.*;
class B{
public static void main(String args[]){
A obj = new A();
obj.msg();
}
}
OUTPUT:- Hello
Q-21:- Program to implement exception handling with checked
exceptions ?
public class JavaExceptionExample{
public static void main(String args[]){
try{
//code that may raise exception
int data=100/0;
}catch(ArithmeticException e){System.out.println(e);}
//rest code of the program
System.out.println("rest of the code...");
}
}
OUTPUT :-
@Override
public void run() {
System.out.println("Thread has ended");
}
OUTPUT :-
Hii
Thread has ended
Q-24:- Program to implement multithreading using Thread class?
class Table
{
void printTable(int n){
synchronized(this){//synchronized block
for(int i=1;i<=5;i++){
System.out.println(n*i);
try{
Thread.sleep(400);
}catch(Exception e){System.out.println(e);}
}
}
}//end of the method
}
}
class MyThread2 extends Thread{
Table t;
MyThread2(Table t){
this.t=t;
}
public void run(){
t.printTable(100);
}
}
import java.awt.*;
OUTPUT:-
Q-27:- Program to implement to develop calculator application with
event handling?
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
class calculator extends JFrame implements ActionListener {
// create a frame
static JFrame f;
// create a textfield
static JTextField l;
// default constructor
calculator()
{
s0 = s1 = s2 = "";
}
// main function
public static void main(String args[])
{
// create a frame
f = new JFrame("calculator");
try {
// set look and feel
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
catch (Exception e) {
System.err.println(e.getMessage());
}
// create a textfield
l = new JTextField(16);
// equals button
beq1 = new JButton("=");
// create . button
be = new JButton(".");
// create a panel
JPanel p = new JPanel();
f.setSize(200, 220);
f.show();
}
public void actionPerformed(ActionEvent e)
{
String s = e.getActionCommand();
double te;
// convert it to string
s0 = Double.toString(te);
s1 = s2 = "";
}
else {
// if there was no operand
if (s1.equals("") || s2.equals(""))
s1 = s;
// else evaluate
else {
double te;
// convert it to string
s0 = Double.toString(te);
OUTPUT:-