0% found this document useful (0 votes)
22 views

CSE215.4 Lab Manual 6 & 7 Summer 2024

Uploaded by

Ajom Khan
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)
22 views

CSE215.4 Lab Manual 6 & 7 Summer 2024

Uploaded by

Ajom Khan
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
You are on page 1/ 10

‭North South University‬

‭Department of Electrical and Computer Engineering‬


‭CSE 215L (Programming Language II Lab)‬

‭1. Class Creation and UML‬

‭Theory‬
‭ class is a blueprint for objects in Java, defining their properties (attributes) and behaviours‬
A
‭(methods). A class encapsulates data for the object and provides methods to interact with that‬
‭data. The key principles of a class include:‬

‭●‬ A ‭ ttributes (Fields): Variables that represent the state of an object. For example, in a‬
‭class Car, attributes might include color, model, and speed.‬
‭●‬ ‭Methods: Functions that define the behaviors of an object. Methods allow interaction‬
‭with an object's attributes. For instance, a Car class might have methods like‬
‭accelerate() or brake().‬
‭●‬ ‭Constructors: Special methods used to initialize objects. Constructors can be overloaded‬
‭to provide multiple ways of initializing an object.‬

‭UML Class Diagram‬


‭ ML (Unified Modeling Language) class diagrams visually represent the structure of classes,‬
U
‭including their attributes, methods, and relationships with other classes. Here's a basic example:‬

‭UML Diagram Description:‬


‭‬ C
● ‭ lass Name: The name of the class is placed at the top of the box.‬
‭●‬ ‭Attributes: Listed in the middle section, showing data types and visibility modifiers (+ for‬
‭public, - for private).‬
‭●‬ ‭Methods: Listed in the bottom section, showing method signatures.‬

‭Example:‬
Car: The name of the‬‭
‭ class‬
.‬

‭ttributes‬
A :‬

color‬‭
‭ (‬private‬
‭ ,‬‭
‭ String‬
):‬‭
‭ Represents‬‭
the‬
color‬‭
‭ of‬‭the‬‭
car‬.‬

model‬‭
‭ (‬private‬
‭ ,‬‭
‭ String‬
):‬‭
‭ Represents‬‭
the‬
model‬‭
‭ of‬‭the‬‭
car‬.‬

speed‬‭
‭ (‬private‬
‭ ,‬‭
‭ int‬
):‬‭
‭ Represents‬‭
the‬
speed‬‭
‭ of‬‭the‬‭
car‬.‬

‭ethods‬
M :‬

accelerate‬
‭ ():‬‭
‭ Increases‬‭
the‬‭car‬'‭
‭s
‬‬‭
speed‬
.‬

brake‬
‭ ():‬‭
‭ Decreases‬‭ the‬‭
car‬
'‬
‭ s‬‭
‭ speed‬.‬

displayInfo‬
‭ ():‬‭
‭ Prints‬‭
the‬‭
car‬'‬
‭ s‬‭
‭ details‬
.‬

‭Coding example:‬
‭/ Car.java‬
/
public‬‭
‭ class‬‭
Car‬‭
{‬
private‬‭
‭ String color;‬
private‬‭
‭ String model;‬
private‬‭
‭ int‬‭
speed;‬

‭/ Constructor‬
/
public‬‭
‭ Car‬(String color, String model,‬‭
‭ int‬‭
speed)‬‭
{‬
this‬
‭ .color = color;‬

this‬
‭ .model = model;‬

this‬
‭ .speed = speed;‬

}‬

‭/ Method to accelerate the car‬


/
public‬‭
‭ void‬‭
accelerate‬
() {‬

speed +=‬‭
‭ 10‬
;‬

System.out.println(‬
‭ "The car is accelerating.‬‭
‭ Speed: "‬‭
+ speed);‬
}‬

‭/ Method to brake the car‬


/
public‬‭
‭ void‬‭
brake‬
() {‬

speed -=‬‭
‭ 10‬
;‬

System.out.println(‬
‭ "The car is braking. Speed:‬‭
‭ "‬‭
+ speed);‬
}‬

‭/ Method to display car information‬
/
public‬‭
‭ void‬‭
displayInfo‬
() {‬

System.out.println(‬
‭ "Car Model: "‬‭
‭ + model +‬‭
", Color: "‬‭
+ color +‬‭
",‬
Speed: "‬‭
‭ + speed);‬
}‬

public‬‭
‭ static‬‭
void‬‭main‬
(String[] args) {‬

Car myCar =‬‭
‭ new‬‭Car(‬
"Red"‬
‭ ,‬‭
‭ "Toyota"‬
,‬‭
‭ 0‬);‬

myCar.displayInfo();‬

myCar.accelerate();‬

myCar.brake();‬

}‬

}‬

‭Output:‬

‭ar Model: Toyota, Color: Red, Speed:‬‭


C 0‬
The car is accelerating. Speed:‬‭
‭ 10‬
The car is braking. Speed:‬‭
‭ 0‬

‭2. Class with Polymorphic Function and Constructor‬

‭Theory‬
‭ olymorphism is a key concept in OOP that allows objects to be treated as instances of their‬
P
‭parent class, providing the ability to call methods defined in a superclass while referring to a‬
‭subclass object. Polymorphism enables flexibility and integration of different behaviors through‬
‭method overriding and method overloading.‬

‭●‬ M ‭ ethod Overloading: Occurs when multiple methods in the same class have the same‬
‭name but different parameters. It allows a class to perform different tasks based on‬
‭different method signatures.‬
‭●‬ ‭Method Overriding: Allows a subclass to provide a specific implementation of a method‬
‭already defined in its superclass. This ensures that the correct method is called for an‬
‭object, depending on its actual subclass type, not the type of reference used to call the‬
‭method.‬
‭●‬ ‭Constructors and Polymorphism: Constructors can also be overloaded to allow different‬
‭ways of initializing an object.‬
‭UML Class Diagram Description‬
‭Example of a polymorphic class hierarchy with constructors:‬

Animal‬‭
‭ Class‬
:‬

‭ttributes:‬
A
name (‬
‭ public‬
‭ , String) - Name‬‭
‭ of‬‭
the animal.‬

‭ethods:‬
M
Animal(name):‬‭
‭ Constructor‬‭to‬‭
initialize‬‭
the‬
name‬
‭ .‬

makeSound()‬
‭ : A‬‭
‭ method‬‭
that‬‭
will‬‭
be‬
overridden‬‭
‭ in‬‭
the‬‭subclasses‬
.‬

Dog‬‭
‭ Class‬
:‬

Inheritance: Inherits‬‭
‭ from‬‭
Animal.‬

‭ethods:‬
M
Dog(name): Calls the parent‬‭
‭ constructor‬‭
to‬‭
set‬‭
the‬‭
animal‬
'‭
‭s
‬‬‭
name‬
.‬

makeSound()‬
‭ : Overrides the parent‬‭
‭ method‬‭
to‬‭
provide‬‭
a‬‭
specific‬‭
sound‬
("Bark")‬
‭ .‬

‭Coding Example‬
‭/ Animal.java‬
/
class‬‭
‭ Animal‬‭
{‬
protected‬‭
‭ String name;‬

‭/ Constructor‬
/
public‬‭
‭ Animal‬(String name) {‬

this‬
‭ .name = name;‬

}‬

‭/ Method to be overridden‬
/
public‬‭
‭ void‬‭
makeSound‬
() {‬

System.out.println(‬
‭ "Some generic animal sound"‬
‭ );‬

}‬

}‬

// Dog.java‬

class‬‭
‭ Dog‬‭
extends‬‭
Animal‬‭
{‬

‭/ Constructor‬
/
public‬‭
‭ Dog‬(String name) {‬

super‬
‭ (name);‬

}‬

‭/ Overridden method‬
/
@Override‬

public‬‭
‭ void‬‭
makeSound‬
() {‬

System.out.println(name +‬‭
‭ " says: Bark"‬
);‬

}‬

public‬‭
‭ static‬‭
void‬‭
main‬(String[] args) {‬

Animal myAnimal =‬‭
‭ new‬‭Animal(‬
"Generic Animal"‬
‭ );‬

myAnimal.makeSound();‬

‭og myDog =‬‭


D new‬‭
Dog(‬
"Buddy"‬
‭ );‬

myDog.makeSound();‬

}‬

}‬

‭Output:‬

‭ome generic animal sound‬


S
Buddy says: Bark‬

‭3. Inheritance and Method Overriding‬

‭Theory‬
I‭nheritance is a mechanism where a new class (subclass) is derived from an existing class‬
‭(superclass). The subclass inherits all the non-private attributes and methods of the superclass,‬
‭allowing code reuse and the creation of a hierarchical relationship between classes.‬

‭●‬ S ‭ uperclasses and Subclasses: The superclass is the general class (e.g., Animal), while‬
‭subclasses are more specific (e.g., Dog, Cat). Subclasses extend superclasses to inherit‬
‭their behaviors.‬
‭●‬ ‭Method Overriding: Subclasses can override methods of the superclass to provide‬
‭specialized behavior while maintaining the same method signature.‬
‭UML Class Diagram Description‬
‭Example of inheritance with method overriding:‬

Vehicle‬‭
‭ Class‬
:‬

‭ttributes:‬
A
speed (‬
‭ public‬
‭ , int) - Represents the speed‬

of‬‭
‭ the vehicle.‬

‭ethods:‬
M
Vehicle(speed):‬‭
‭ Constructor‬‭
to‬‭
initialize‬
speed‬
‭ .‬

move()‬
‭ : A generic movement‬‭
‭ method‬
.‬

Car‬‭
‭ Class‬:‬

Inheritance: Inherits‬‭
‭ from‬‭
Vehicle.‬

‭ethods:‬
M
Car(speed): Calls the parent‬‭
‭ constructor‬.‬

move()‬
‭ : Overrides the move()‬‭
‭ method‬‭
to‬
provide‬‭
‭ specific‬‭
movement‬‭
behavior‬‭
for‬‭the‬
Car‬
‭ .‬

‭Coding Example‬
‭/ Vehicle.java‬
/
class‬‭
‭ Vehicle‬‭
{‬
protected‬‭
‭ int‬‭
speed;‬

‭/ Constructor‬
/
public‬‭
‭ Vehicle‬(‬
‭ int‬‭
‭ speed) {‬
this‬
‭ .speed = speed;‬

}‬

‭/ Method to be overridden‬
/
public‬‭
‭ void‬‭
move‬
() {‬

System.out.println(‬
‭ "The vehicle is moving‬‭
‭ at speed: "‬‭
+ speed);‬
}‬

}‬

‭/ Car.java‬
/
class‬‭
‭ Car‬‭
extends‬‭
Vehicle‬‭
{‬
‭/ Constructor‬
/
public‬‭
‭ Car‬(‭
‭i
‬nt‬‭
speed) {‬
super‬
‭ (speed);‬

}‬

‭/ Overridden method‬
/
@Override‬

public‬‭
‭ void‬‭
move‬
() {‬

System.out.println(‬
‭ "The car is driving at‬‭
‭ speed: "‬‭
+ speed);‬
}‬

public‬‭
‭ static‬‭
void‬‭
main‬
(String[] args) {‬

Vehicle myVehicle =‬‭
‭ new‬‭
Vehicle(‬
50‬
‭ );‬

myVehicle.move();‬

‭ar myCar =‬‭


C new‬‭
Car(‬
100‬
‭ );‬

myCar.move();‬

}‬

}‬

‭Output:‬

‭he vehicle is moving at speed:‬‭


T 50‬
The car is driving at speed:‬‭
‭ 100‬

‭4. Abstract Method, Class, and Interface‬

‭Theory‬
‭ bstract classes and interfaces are used in Java to define common templates for other classes.‬
A
‭They help achieve abstraction, one of the four fundamental OOP concepts, allowing the creation‬
‭of complex applications with simple, defined structures.‬

‭●‬ A ‭ bstract Class: A class that cannot be instantiated and often includes abstract methods‬
‭(methods without implementation). Subclasses must provide implementations for‬
‭abstract methods.‬
‭●‬ ‭Abstract Methods: Methods declared without a body in an abstract class. Subclasses‬
‭must implement them.‬
‭●‬ ‭Interfaces: A completely abstract class that only includes abstract methods (before Java‬
‭8) and static final variables. Classes implementing an interface must provide‬
‭implementations for all the methods defined in the interface.‬

‭UML Class Diagram Description‬


‭Example of an abstract class and interface implementation:‬

Appliance‬‭
‭ Class‬
:‬

‭ttributes:‬
A
power (boolean) - Indicates‬‭
‭ if‬‭
the appliance‬‭
is‬
on‬‭
‭ or‬‭
off.‬

‭ethods:‬
M
turnOn(): Turns the appliance‬‭
‭ on‬
.‬

turnOff(): Turns the appliance off.‬

adjust():‬‭
‭ Abstract‬‭
method‬‭
to‬‭
be‬‭implemented‬‭
by‬
subclasses‬
‭ .‬

Fan‬‭
‭ Class‬
:‬

Inheritance: Inherits‬‭
‭ from‬‭
Appliance.‬

‭ethods:‬
M
adjust(): Provides specific adjustment‬

functionality.‬

Playable‬‭
‭ Interface‬
:‬

‭ethods:‬
M
play(): A‬‭
‭ method‬‭
that‬‭
must‬‭be‬‭
implemented‬‭
by‬
any‬‭
‭ class‬‭
that‬‭
implements‬‭
the‬‭interface‬
.‬

VideoGame‬‭
‭ Class‬
:‬

Implements‬
‭ :‬‭
‭ Implements‬‭
the Playable‬‭
interface‬
.‬

‭ethods:‬
M
play(): Provides‬‭
‭ implementation‬‭
specific‬‭
to‬‭
a‬
video game.‬

‭Coding Example‬
‭/ Appliance.java‬
/
abstract‬‭
‭ class‬‭
Appliance‬‭
{‬
protected‬‭
‭ boolean‬‭
power;‬

‭/ Method to turn on the appliance‬


/
public‬‭
‭ void‬‭
turnOn‬
() {‬

power =‬‭
‭ true‬
;‬

System.out.println(‬
‭ "Appliance is now ON."‬
‭ );‬

}‬

‭/ Method to turn off the appliance‬


/
public‬‭
‭ void‬‭
turnOff‬() {‬

power =‬‭
‭ false‬
;‬

System.out.println(‬
‭ "Appliance is now OFF."‬
‭ );‬

}‬

‭/ Abstract method‬
/
public‬‭
‭ abstract‬‭
void‬‭
adjust‬
();‬

}‬

‭/ Fan.java‬
/
class‬‭
‭ Fan‬‭
extends‬‭
Appliance‬‭
{‬

‭/ Implementation of abstract method‬


/
@Override‬

public‬‭
‭ void‬‭
adjust‬
() {‬

System.out.println(‬
‭ "Fan speed adjusted."‬
‭ );‬

}‬

public‬‭
‭ static‬‭
void‬‭
main‬(String[] args) {‬

Fan myFan =‬‭
‭ new‬‭Fan();‬
myFan.turnOn();‬

myFan.adjust();‬

myFan.turnOff();‬

}‬

}‬

‭/ Playable.java‬
/
interface‬‭
‭ Playable‬‭
{‬
void‬‭
‭ play‬();‬

}‬

// VideoGame.java‬

class‬‭
‭ VideoGame‬‭
implements‬‭
Playable‬‭
{‬

‭/ Implementation of play method‬


/
@Override‬

public‬‭
‭ void‬‭
play‬
() {‬

System.out.println(‬
‭ "Playing the video game."‬
‭ );‬

}‬

public‬‭
‭ static‬‭
void‬‭
main‬
(String[] args) {‬

VideoGame game =‬‭
‭ new‬‭
VideoGame();‬
game.play();‬

}‬

}‬

‭Output:‬

‭ppliance is now ON.‬


A
Fan speed adjusted.‬

Appliance is now OFF.‬

Playing the video game.‬

You might also like