OOps with Java
OOps with Java
QS -1. Create a class named 'Student' with String variable 'name' and integer variable 'roll_no'.
Assign the value of roll_no as '2' and that of name as "John" by creating an object of the class
Student.
class Student{
String name;
int roll_no;
class Ans{
s.name = "John";
s.roll_no = 2;
QS- 2. The Matrix class has methods for each of the following:
1 - get the number of rows
2 - get the number of columns
3 - set the elements of the matrix at given position (i,j)
4 - adding two matrices. If the matrices are not addable, "Matrices cannot be added" will be
displayed.
5 - multiplying the two matrices
class Matrix{
int row;
int column;
int[][] a;
row = r;
column = c;
a = new int[row][column];
return row;
return column;
return a[r][c];
a[r][c] = element;
for(int i = 0;i<m.row;i++){
for(int j = 0;j<m.column;j++){
m.setElement(i,j,(x.getElement(i,j)+y.getElement(i,j)));
return m;
else{
for(int j = 0;j<x.row;j++){
for(int i = 0;i<y.column;i++){
int sum = 0;
for(int k = 0;k<x.column;k++){
sum = sum+(x.getElement(j,k)*y.getElement(k,i));
m.setElement(j,i,sum);
return m;
System.out.println("Matrix is :");
for(int i = 0;i<row;i++){
for(int j = 0;j<column;j++){
System.out.print(a[i][j]+"\t");
}
System.out.println("");
class Ans{
int k = 1;
for(int i = 0;i<3;i++){
m.setElement(i,j,k);
k++;
n.setElement(i,j,k);
k++;
m.printMatrix();
n.printMatrix();
Matrix o = Matrix.add(m,n);
o.printMatrix();
Matrix p = Matrix.product(m,n);
p.printMatrix();
QS- 3. Write a program to print the area of a rectangle by creating a class named 'Area' taking
the values of its length and breadth as parameters of its constructor and having a method named
'returnArea' which returns the area of the rectangle. Length and breadth of rectangle are entered
through keyboard.
import java.util.*;
class Area{
int length;
int breadth;
length = l;
breadth = b;
return length*breadth;
class Ans{
int l,b;
System.out.println("Enter length");
l = s.nextInt();
System.out.println("Enter breadth");
b = s.nextInt();
Area a = new Area(l,b);
System.out.println("Area : "+a.getArea());
QS 4. Print the sum, difference and product of two complex numbers by creating a class named
'Complex' with separate methods for each operation whose real and imaginary parts are entered
by user.
import java.util.*;
class Complex{
int real;
int imag;
real = r;
imag = i;
}
public void printComplex(){
System.out.println(imag+"i");
System.out.println(real);
else{
System.out.println(real+"+"+imag+"i");
class Ans{
Complex e = Complex.add(c,d);
Complex f = Complex.diff(c,d);
Complex g = Complex.product(c,d);
e.printComplex();
f.printComplex();
g.printComplex();
}
}
QS- 5-Write a program to print the area of two rectangles having sides (4,5) and (5,8)
respectively by creating a class named 'Rectangle' with a method named 'Area' which returns the
area and length and breadth passed as parameters to its constructor.
class Rectangle{
int length;
int breadth;
length = l;
breadth = b;
return length*breadth;
return 2*(length+breadth);
class Ans{
}
METHODS:-
QS-1 Write a Java method to find the smallest number among three numbers.
Test Data:
Input the first number: 25
Input the Second number: 37
Input the third number: 29
import java.util.Scanner;
double x = in.nextDouble();
double y = in.nextDouble();
double z = in.nextDouble();
Sample Output:
QS-2 Write a Java method to compute the average of three numbers. Go to the editor
Test Data:
import java.util.Scanner;
double x = in.nextDouble();
double y = in.nextDouble();
double z = in.nextDouble();
return (x + y + z) / 3;
}
}
Sample Output:
QS-3. Write a Java method to display the middle character of a string. Go to the editor
Note: a) If the length of the string is odd there will be two middle characters.
b) If the length of the string is even there will be one middle character.
Test Data:
import java.util.Scanner;
int position;
int length;
if (str.length() % 2 == 0)
{
position = str.length() / 2 - 1;
length = 2;
else
position = str.length() / 2;
length = 1;
import java.util.Scanner;
int count = 0;
count++;
return count;
Sample Output:
import java.util.Scanner;
int count = 0;
count++;
count = count + 1;
return count; // returns 0 if string starts or ends with space " ".
Sample Output:
Input the string: The quick brown fox jumps over the lazy dog
METHOD OVERLOADING:-
// Normal main()
}
// Overloaded main methods
MetodOverloadingExample1.main("DataFlair");
return (x + y);
}
return (x + y + z);
return (x + y);
// Driver code
System.out.println(s.sum(10, 20));
System.out.println(s.sum(10.5, 20.5));
Output :
30
60
31.0
3. class Multiply {
m.mul(6, 10);
m.mul(10, 6, 5);
output:-
Sum of two=60
Sum of three=300
4. class DispOvrload
class Main
o1.show('G');
Output:-
class DispOvrload
}
}
class Main
o1.show('G', 62);
o1.show(46, 'S');
output:-
MRTHOD OVERRIDING:-
Figure figref;
figref = f;
System.out.println("Area is :"+figref.area());
figref = r;
System.out.println("Area is :"+figref.area());
class Figure {
double dim1;
double dim2;
Figure(double a , double b) {
dim1 = a;
dim2 = b;
Double area() {
return(dim1*dim2);
Rectangle(double a, double b) {
super(a ,b);
Double area() {
return(dim1*dim2);
output:-
Area is :100.0
Area is :45.0
2. class Animal{
@Override
Output:
@Override
@Override
Output:
4. class IceCreamPricesWithOverriding
{
ic.flavor = "Pista";
ic.numberOfScoops = 2;
fs.flavor = "Chocolate";
fs.numberOfScoops = 1;
fs.gramsOfFruitSalad = 50;
kkm.flavor = "Vanila";
kkm.numberOfScoops = 1;
kkm.gramsOfKhubaniKaMeeta = 75;
class IceCream
String flavor;
int numberOfScoops;
double getPrice()
int gramsOfFruitSalad;
double getPrice()
}
class KhubaniKaMeetaWithIceCream extends IceCream
int gramsOfKhubaniKaMeeta;
double getPrice()
OUTPUT
5. /**
* @author CodesJava
*/
class Student {
/**
* @author CodesJava
*/
/**
* @author CodesJava
*/
//main method
obj.show();
Output:
1. class Shape {
System.out.println("Inside display");
}
System.out.println("Inside area");
rect.display();
rect.area();
Output
Inside display
Inside area
Inherit_Single() {
}
class SubClass extends Inherit_Single {
SubClass() {
void display()
System.out.println(str);
class MainClass {
obj.display();
Sample Output
3. class Faculty
{
float salary=30000;
float bonous=2000;
System.out.println("Salary is:"+obj.salary);
System.out.println("Bonous is:"+obj.bonous);
Output
MULTILEVEL INHARITANCE:-
class Car{
public Car()
System.out.println("Class Car");
}
class Maruti extends Car{
public Maruti()
System.out.println("Class Maruti");
System.out.println("Brand: Maruti");
System.out.println("Max: 90Kmph");
public Maruti800()
System.out.println("Max: 80Kmph");
obj.brand();
obj.speed();
Output:
Class Car
Class Maruti
Brand: Maruti
Max: 80Kmph
2. class Person
Person()
System.out.println("Person constructor");
void nationality()
{
System.out.println("Indian");
void place()
System.out.println("Mumbai");
Emp()
System.out.println("Emp constructor");
}
void organization()
System.out.println("IBM");
void place()
System.out.println("New York");
Manager()
System.out.println("Manager constructor");
}
void subordinates()
System.out.println(12);
void place()
System.out.println("London");
class Check
m.nationality();
m.organization();
m.subordinates();
m.place();
Output:
Person constructor
Emp constructor
Manager constructor
Indian
IBM
12
London
3. package management;
import java.util.Scanner;
class teacher{
void tech(){
System.out.println("!!!Subject!!!\t\t!!Empid!!\n1)java\t\t\t101\n2)php\t\t\t102\n3)Android\t\
t103\n\n");
void admin(){
System.out.println("!!!Salary!!!\t\t!!Shift!!\n1)rs25,000\t\t9:00 AM to 5:00PM\
n2)rs30,000\t\t9:00 AM to 5:30PM)\n3)rs45,000\t\t5:00 PM to 9:00AM\n\n");
void manager(){
System.out.println("!!!HR Salary!!!\t\t!!!FinanceSalary!!!\n
1)Rs35,000\t\t40,000\n2)Rs45,000\t\t47,000\n1)Rs55,000\t\t57,000\n");
String dept=s.nextLine();
if(dept.equals("teacher"))
{ m.tech();
}
else if(dept.equals("admin"))
{ m.admin();
else if(dept.equals("manager"))
{ m.manager();
else
4. class Student {
/**
* @author CodesJava
*/
//method call
obj.showDetail();
Output:
String name="anu";
int age=20;
int m1=30,m2=30,m3=30;
int total;
void calc()
total=m1+m2+m3;
void show()
System.out.println("NAME:" +name+"\nAGE:"+age+"\nMARK1="+m1+"\nMARK2="+m2+"\
nMARK3="+m3+"\nTOTAL:"+total);
class multilevel
ob.calc();
ob.show();
MULTITHREADING:-
1. package com.techbeamers.multithreading;
class NameMyThread
MyThread mt;
if (args.length == 0)
else
mt.start ();
MyThread ()
}
public void run ()
Output.
Java NameMyThread
Output:
threadName = msg;
try
catch (InterruptedException e)
if (thread == null)
thread.start ();
thread1.start();
p1.start();
c1.start();
class Shop
try
wait();
available = false;
notifyAll();
return materials;
try
wait();
ie.printStackTrace();
}
materials = value;
available = true;
notifyAll();
Shop = c;
this.number = number;
int value = 0;
value = Shop.get();
Shop = c;
this.number = number;
Shop.put(i);
try
sleep((int)(Math.random() * 100));
ie.printStackTrace();
}
4. Write a program that creates 2 threads - each displaying a message (Pass the message as a
parameter to the constructor). The threads should display the messages continuously till the user
presses ctrl+c.
Thread1.java
Thread1(String msg)
this.msg = msg;
try
while (true)
System.out.println(msg);
Thread.sleep(300);
ex.printStackTrace();
Thread2.java
Thread2(String msg)
this.msg = msg;
try
while (true)
System.out.println(msg);
Thread.sleep(400);
}
ex.printStackTrace();
ThreadDemo.java
class ThreadDemo
t1.start();
t2.start();
Output:
5. Q. Write a JAVA program which will generate the threads:
Fibonacci.java
import java.io.*;
try
int n = Integer.parseInt(br.readLine());
System.out.println("\n=================================");
System.out.println("Fibonacci series:");
while (n>0)
System.out.print(c+" ");
a=b;
b=c;
c=a+b;
n=n-1;
}
}
ex.printStackTrace();
Reverse.java
try
System.out.println("\n=================================");
System.out.println("=================================");
System.out.print(i+" ");
System.out.println("\n=================================\n\n");
{
ex.printStackTrace();
MainThread.java
class MainThread
try
fib.start();
fib.sleep(4000);
rev.start();
ex.printStackTrace();
}
Output: