Bsit 2nd Mid Ans
Bsit 2nd Mid Ans
1:A **constructor** initializes an object when it's created and has the same name as the class with no
return type.
2: **Object-Oriented Analysis (OOA)** identifies the objects and requirements of a system, focusing on
"what" it should do.
**Object-Oriented Design (OOD)** defines how the system will work by detailing the interactions and
structure, focusing on "how" it will meet requirements.
3: **Method overloading** is when a class has multiple methods with the same name but different
parameters. It allows methods to perform similar tasks with different inputs.
4: If you don't define any constructor in a class, the compiler automatically provides a **default
constructor** with no parameters, which initializes the object with default values.
5: Yes, **protected members** can be accessed by any class in the same package, as well as by
subclasses, even if they are in different packages.
Long question:
1: Create a Laptop class with attributes brand and price. Add a method updatePrice() to modify the price
attribute. Demonstrate how to use this method.
Answer:
class Laptop {
String brand;
double price;
// Method to set brand and price
this.brand = brand;
this.price = price;
this.price = newPrice;
void displayDetails() {
myLaptop.setDetails("Dell", 1000.00);
myLaptop.updatePrice(1200.00);
myLaptop.displayDetails();
Question 2: Create a Java class named Student with an integer attribute marks. In the main program,
create an object of the Student class, set the marks attribute, and use an if-else statement to print
"Pass" if the marks are 50 or more, or "Fail" if the marks are below 50.
class Student {
student.marks = 65; // You can change this value to test different outcomes
System.out.println("Pass");
} else {
System.out.println("Fail");
Question 3: Write a House class with three constructors: one with no parameters, one with rooms as a
parameter, and another with both rooms and area as parameters.
class House {
House() {
House(int rooms) {
this.rooms = rooms;
this.area = area;
void displayDetails() {
System.out.println("Default House:");
defaultHouse.displayDetails();
customRoomHouse.displayDetails();
System.out.println("\nCustom House:");
customHouse.displayDetails();
}
Question 4:
class WordPrinter {
void printJavaTenTimes() {
System.out.println("Java");
printer.printJavaTenTimes();
Question 5:
hi hi hi
7777