0% found this document useful (0 votes)
3 views11 pages

Java Programs: OOP Concepts & Exceptions

The document contains multiple Java programs demonstrating concepts of classes, objects, method and constructor overloading, inheritance, and exception handling. It includes examples of creating objects, setting dimensions, and calculating volumes, as well as handling exceptions and demonstrating polymorphism through method overriding. Each section provides code snippets along with comments on expected outputs.

Uploaded by

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

Java Programs: OOP Concepts & Exceptions

The document contains multiple Java programs demonstrating concepts of classes, objects, method and constructor overloading, inheritance, and exception handling. It includes examples of creating objects, setting dimensions, and calculating volumes, as well as handling exceptions and demonstrating polymorphism through method overriding. Each section provides code snippets along with comments on expected outputs.

Uploaded by

octaneg21
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

//1a:-Program on classes and objects

class Company

int registrationNo;

String companyName;

void insertRecord(int r, String n)

registrationNo = r;

companyName = n;

void displayInfo()

[Link]("Registration No.: "+registrationNo+",Company Name:


"+companyName);

public static void main(String[] args)

Company c1 = new Company();

[Link](2006,"Google");

[Link]();

//Output

//1b:-Programs on classes and objects


class Box

double width;

double height;

double depth;

double volume()

return width*height*depth;

void setDim(double w, double h, double d)

width = w;

height = h;

depth = d;

public class Creatingobjects1

public static void main(String args[])

Box obj1 = new Box();

Box obj2 = new Box();

[Link](3, 5, 10);

[Link](10, 15, 20);

[Link]("Vol of Box1: " + [Link]());

[Link]("Vol of Box2: " + [Link]());

}
}

//Output

//2:-Programs on method and constructor overloading

class Student {

String name;

int roll_no;

double marks;

// Default constructor

Student() {

[Link]("Default Constructor Called");

// Constructor with 2 parameters

Student(String n, int r) {

name = n;

roll_no = r;

[Link]("Name: " + name + ", Roll No: " + roll_no);

// Constructor with 3 parameters

Student(String n, int r, double m) {

name = n;

roll_no = r;

marks = m;
[Link]("Name: " + name + ", Roll No: " + roll_no + ", Marks:
" + marks);

class B {

// Method overloading

void area(int r) {

[Link]("Area of Circle: " + (3.14 * r * r));

void area(int l, int b) {

[Link]("Area of Rectangle: " + (l * b));

public class Class2 {

public static void main(String[] args) {

// Constructor Overloading

new Student();

new Student("Sweety", 86, 88.5);

new Student("siya", 125);

// Method Overloading

B obj = new B();

[Link](5); // Circle with radius 5

[Link](4, 6); // Rectangle with length 4 and breadth 6

//Output
//3a:- Programs on various types of inheritance and Exception
handling

class A

private int i;

private String s;

public A()

[Link]("Inside Default Constructor of A");

public void setI(int i)

this.i=i;

public void setS(String s)

this.s=s;

public int get()

return i;

}
public String getS()

return s;

public void display()

[Link]("display function Inside class A");

class B extends A

public B()

[Link]("Inside Default Constructor of B");

public void display()

[Link]("display function Inside class B");

class C extends B

public C()

[Link]("Inside constructor of C");

public void display()


{

[Link]("display function Inside class C");

interface D1

public void display1();

interface D2

public void display2();

class D extends A implements D1,D2

public void display1()

[Link]("display1 of interface D1");

public void display2()

[Link]("display2 of interface D2");

public class Class3

public static void main(String arg[])

{
[Link]("Calling constructor of A");

A a=new A();

[Link]("Calling constructor of B");

B b=new B();

[Link]("Calling constructor of C");

C c=new C();

[Link]("Calling constructor of D");

D d=new D();

[Link]();

d.display1();

d.display2();

[Link]("akshay");

[Link](12);

[Link]();

[Link]();

[Link]();

//Output
//3b:-Program on Exception Handling

class ExceptionHandling1

public static void main(String[] args)

int d,a;

try{

d=0;

a=42/d;

catch(ArithmeticException e)

{
[Link]("By printStackTrace() method");

[Link]();

[Link](e);

[Link]("by toString() method");

[Link]([Link]());

[Link]("By getMessage()");

[Link]([Link]());

//Output

//3c:-Programs on various types of inheritance and Exception


handling.

class Animal {

public void eat() {

[Link]("Animal is eating...");
}

class Dog extends Animal {

@Override

public void eat() {

[Link]("Dog is eating...");

public class Main {

public static void main(String[] args) {

Animal myAnimal = new Animal();

[Link]();

Dog myDog = new Dog();

[Link]();

Animal animalDog = new Dog();

[Link]();

//Output

You might also like