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

Constructor Lecture

Uploaded by

Aysha Safdar
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)
22 views5 pages

Constructor Lecture

Uploaded by

Aysha Safdar
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

Constructor:

1. Default Constructor Scenario

Scenario:
You are building a Book class for a library system. You want each book to have a default title
"Unknown" and price 0.0 if no information is provided when the object is created.

Question:

 Implement the class using a default constructor.

class Book {
String title;
double price;

Book() {
title = "Unknown";
price = 0.0;
}

void display() {
[Link]("Title: " + title + ", Price: " + price);
}
}

2. Parameterized Constructor Scenario

Scenario:
You're making a Student class where each student must have a name and roll number at the
time of creation.

Question:

 Write a constructor that accepts these two values.

class Student {
String name;
int rollNo;

Student(String name, int rollNo) {


[Link] = name;
[Link] = rollNo;
}

void display() {
[Link]("Name: " + name + ", Roll No: " + rollNo);
}
}
3. Copy Constructor Scenario

Scenario:
You are creating a game with a Player class. You want to duplicate a player with the same stats.

Question:

 Create a copy constructor for the Player class.


 How is it different from using assignment (=)?

class Player {
String name;
int score;

Player(String name, int score) {


[Link] = name;
[Link] = score;
}

Player(Player p) {
[Link] = [Link];
[Link] = [Link];
}

void display() {
[Link](name + ": " + score);
}
}

4. Constructor Overloading Scenario

Scenario:
You’re designing a Car class. Sometimes, you know only the brand. Other times, you also know
the model and price.

Question:

 Implement constructor overloading to handle different sets of data.


 What benefit does constructor overloading provide in real applications?

class Car {
String brand;
String model;
double price;

Car(String brand) {
[Link] = brand;
}

Car(String brand, String model, double price) {


[Link] = brand;
[Link] = model;
[Link] = price;
}

void display() {
[Link]("Brand: " + brand + ", Model: " + model + ", Price:
" + price);
}
}

Scenario 1: Ride Booking App — UberClone


Context:
You're designing a Ride class for a ride-booking app like Uber. You need to handle:

➤ Constructor Requirements:

1. A default constructor initializes unknown ride (for testing).


2. A parameterized constructor for setting source, destination, and distance.
3. A copy constructor that copies an existing ride.

➤ Method Overloading Requirements:

Overload a method calculateFare():

 One version that uses only distance (base fare: 10/km).


 Another that accepts distance and ride type ("premium", "standard") — premium fare is
15/km.
 Another that accepts distance, ride type, and time of day (night charges apply).

Question:

Implement the class Ride with:

 All three types of constructors.


 At least 3 overloaded calculateFare() methods.
 Create 3 objects to demonstrate all constructor types.
 Call all calculateFare() versions.

Code:
class Ride {
String source, destination;
double distance;

// Default constructor
Ride() {
source = "Unknown";
destination = "Unknown";
distance = 0;
}

// Parameterized constructor
Ride(String source, String destination, double distance) {
[Link] = source;
[Link] = destination;
[Link] = distance;
}

// Copy constructor
Ride(Ride r) {
[Link] = [Link];
[Link] = [Link];
[Link] = [Link];
}

double calculateFare() {
return distance * 10;
}

double calculateFare(String type) {


return [Link]("premium") ? distance * 15 : distance * 10;
}

double calculateFare(String type, boolean isNight) {


double fare = calculateFare(type);
return isNight ? fare + 50 : fare;
}
}

Scenario 2: University Management System — StudentRecord


You're designing a Student class for a university. The system should handle:

➤ Constructor Requirements:

1. A default constructor that assigns default values ("Unnamed", 0).


2. A parameterized constructor that accepts name and rollNo.
3. A copy constructor to clone another student object.

➤ Method Overloading Requirements:

Overload calculateGPA():

 With 2 grades.
 With 3 grades.
 With 3 grades and weightages.

Question:
Create a Student class:

 Implement all three types of constructors.


 Overload calculateGPA() with increasing complexity.
 Show how method overloading helps flexibility.
 Demonstrate constructor chaining to reduce code.

Code:
java
CopyEdit
class Student {
String name;
int rollNo;

// Default constructor
Student() {
this("Unnamed", 0); // constructor chaining
}

// Parameterized constructor
Student(String name, int rollNo) {
[Link] = name;
[Link] = rollNo;
}

// Copy constructor
Student(Student s) {
[Link] = [Link];
[Link] = [Link];
}

double calculateGPA(double g1, double g2) {


return (g1 + g2) / 2;
}

double calculateGPA(double g1, double g2, double g3) {


return (g1 + g2 + g3) / 3;
}

double calculateGPA(double g1, double w1, double g2, double w2, double g3,
double w3) {
return (g1 * w1 + g2 * w2 + g3 * w3) / (w1 + w2 + w3);
}
}

You might also like