0% found this document useful (0 votes)
9 views6 pages

Bsit 2nd Mid Ans

Fgg hh

Uploaded by

hayamalik3195
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)
9 views6 pages

Bsit 2nd Mid Ans

Fgg hh

Uploaded by

hayamalik3195
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
You are on page 1/ 6

Short answer

1:A **constructor** initializes an object when it's created and has the same name as the class with no
return type.

Differences from a Method:

- **Purpose**: Constructor initializes; method defines behavior.

- **Invocation**: Constructor auto-calls; method needs explicit call.

- **Return Type**: Constructor has none; method can have any.

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

void setDetails(String brand, double price) {

this.brand = brand;

this.price = price;

// Method to update the price

void updatePrice(double newPrice) {

this.price = newPrice;

// Method to display laptop details

void displayDetails() {

System.out.println("Brand: " + brand);

System.out.println("Price: $" + price);

public static void main(String[] args) {

// Create a Laptop object

Laptop myLaptop = new Laptop();

// Set initial details

myLaptop.setDetails("Dell", 1000.00);

// Display initial details


myLaptop.displayDetails();

// Update the price

myLaptop.updatePrice(1200.00);

// Display updated details

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 {

int marks; // Attribute to store marks

public static void main(String[] args) {

// Create a Student object

Student student = new Student();

// Set the marks attribute

student.marks = 65; // You can change this value to test different outcomes

// Use if-else statement to check if the student passed or failed

if (student.marks >= 50) {

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 {

int rooms; // Attribute for number of rooms

double area; // Attribute for area of the house

// Constructor with no parameters

House() {

this.rooms = 1; // Default value for rooms

this.area = 500.0; // Default value for area

// Constructor with rooms as a parameter

House(int rooms) {

this.rooms = rooms;

this.area = 500.0; // Default value for area

// Constructor with both rooms and area as parameters

House(int rooms, double area) {


this.rooms = rooms;

this.area = area;

// Method to display house details

void displayDetails() {

System.out.println("Rooms: " + rooms);

System.out.println("Area: " + area + " sq. ft.");

public static void main(String[] args) {

// Create House objects using different constructors

House defaultHouse = new House();

House customRoomHouse = new House(3);

House customHouse = new House(4, 1500.0);

// Display details of each house

System.out.println("Default House:");

defaultHouse.displayDetails();

System.out.println("\nCustom Room House:");

customRoomHouse.displayDetails();

System.out.println("\nCustom House:");

customHouse.displayDetails();
}

Question 4:

class WordPrinter {

// Method to print "Java" 10 times

void printJavaTenTimes() {

for (int i = 0; i < 10; i++) {

System.out.println("Java");

public static void main(String[] args) {

// Create an object of WordPrinter

WordPrinter printer = new WordPrinter();

// Call the method to print "Java" 10 times

printer.printJavaTenTimes();

Question 5:

hi hi hi

7777

You might also like