0% found this document useful (0 votes)
2 views5 pages

Java Lab Program

The document contains Java code for three programs: a College class with constructors and methods to manage class details, an Animal interface with implementations for Dog and Cat classes demonstrating interface usage, and an ExceptionHandlingDemo class showcasing exception handling with try-catch blocks. Each program includes main methods to create objects and demonstrate functionality. The code illustrates object-oriented programming concepts such as encapsulation, inheritance, and exception management.
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)
2 views5 pages

Java Lab Program

The document contains Java code for three programs: a College class with constructors and methods to manage class details, an Animal interface with implementations for Dog and Cat classes demonstrating interface usage, and an ExceptionHandlingDemo class showcasing exception handling with try-catch blocks. Each program includes main methods to create objects and demonstrate functionality. The code illustrates object-oriented programming concepts such as encapsulation, inheritance, and exception management.
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

5.

Create a Class Named College having data members Name of the class (BCA,
BCom, BSc), Name of the staff ,No of the students in the class, use constructors

public class College {


// Data members
private String className; // Name of the class (BCA, BCom, BSc)
private String staffName; // Name of the staff
private int numberOfStudents; // No of the students in the class

// Default constructor
public College() {
[Link] = "";
[Link] = "";
[Link] = 0;
}

// Parameterized constructor
public College(String className, String staffName, int numberOfStudents) {
[Link] = className;
[Link] = staffName;
[Link] = numberOfStudents;
}

// Getter and setter methods


public String getClassName() {
return className;
}
public void setClassName(String className) {
[Link] = className;
}

public String getStaffName() {


return staffName;
}

public void setStaffName(String staffName) {


[Link] = staffName;
}

public int getNumberOfStudents() {


return numberOfStudents;
}

public void setNumberOfStudents(int numberOfStudents) {


[Link] = numberOfStudents;
}

// Method to display the details of the college


public void displayDetails() {
[Link]("Class Name: " + className);
[Link]("Staff Name: " + staffName);
[Link]("Number of Students: " + numberOfStudents);
}
public static void main(String[] args) {
// Creating an object using the default constructor
College college1 = new College();
[Link]("BCA");
[Link]("[Link]");
[Link](150);
[Link]();

// Creating an object using the parameterized constructor


College college2 = new College("BCom", "[Link]", 120);
[Link]();
}
}

[Link] to demonstrate interface in java program.

// Define an interface
interface Animal {
void sound();
}

// Implement the interface in a class


class Dog implements Animal {
public void sound() {
[Link]("Woof");
}
}

class Cat implements Animal {


public void sound() {
[Link]("Meow");
}
}

// Test the implementation


class InterfaceDemo {
public static void main(String[] args) {
Animal dog = new Dog();
Animal cat = new Cat();

[Link](); // Output: Woof


[Link](); // Output: Meow
}
}

[Link] to Demonstrate exception handling in java.

public class ExceptionHandlingDemo {

public static void main(String[] args) {


try {
// Code that might throw an exception
int divideByZero = 5 / 0; // This will throw an ArithmeticException
[Link]("This line will not be executed.");
} catch (ArithmeticException e) {
// Handling the exception
[Link]("ArithmeticException caught: " + [Link]());
} finally {
// This block always executes
[Link]("Finally block executed.");
}

try {
// Code that might throw an exception
String str = null;
[Link]([Link]()); // This will throw a
NullPointerException
} catch (NullPointerException e) {
// Handling the exception
[Link]("NullPointerException caught: " + [Link]());
} finally {
// This block always executes
[Link]("Finally block executed.");
}

}
}

You might also like