0% found this document useful (0 votes)
11 views13 pages

CCDATRCLWeek 2

This document provides an introduction to data structures and algorithms, focusing on the importance of data structures in efficiently managing data and solving complex problems. It explains the concept of classes and objects in Java, including examples of creating and using a Car class and an array of Car objects. Additionally, it introduces Abstract Data Types (ADTs) and their characteristics, along with examples like lists, stacks, and queues.

Uploaded by

ta6183821
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)
11 views13 pages

CCDATRCLWeek 2

This document provides an introduction to data structures and algorithms, focusing on the importance of data structures in efficiently managing data and solving complex problems. It explains the concept of classes and objects in Java, including examples of creating and using a Car class and an array of Car objects. Additionally, it introduces Abstract Data Types (ADTs) and their characteristics, along with examples like lists, stacks, and queues.

Uploaded by

ta6183821
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/ 13

DATA STRUCTURES & ALGORITHM

WEEK 2
Introduction to Data Structures

A data structure is a way of organizing and storing data so that it can be


accessed and worked with efficiently.
Common examples include arrays, linked lists, stacks, and queues.

Importance:

Data structures are fundamental to designing efficient algorithms.


They help in managing large amounts of data and are crucial for solving
complex problems in computer science.
What is a Class in Java?

•A class in Java is a blueprint for creating objects.


•It encapsulates data for the object and methods to manipulate that data.

Analogy:
•Think of a class as a template, like a blueprint for a house.
•The class defines the properties (attributes) and behaviors (methods) of
the objects created from it.
Example:
•Consider a Car class that represents the concept of a car with attributes like
make, model, and year. // Class Definition
class Car {
String make;
String model;
int year;

Car(String make, String model, int year) {


this.make = make;
this.model = model;
this.year = year;
}

void displayInfo() {
System.out.println("Make: " + make);
System.out.println("Model: " + model);
System.out.println("Year: " + year);
}
}

Explanation: // Main class to test Car class


public class Main {
Attributes: make, model, and year represent the characteristics of a car. public static void main(String[] args) {
Constructor: The Car constructor initializes these attributes when a new Car // Creating an object of the Car class
object is created. Car myCar = new Car("Toyota", ”Fortuner", 2024);
// Displaying the car's information
Method: The displayInfo() method is used to print the car's details. myCar.displayInfo();
}
}
Understanding Objects

•An object is an instance of a class. It contains real data, and it is through objects that we
interact with classes.

Key Points:
•Objects are created using the new keyword in Java.
•Multiple objects can be created from a single class, each with its own set of attribute
values.

Car car1 = new Car("Honda", "Civic", 2024);


Car car2 = new Car("Ford", "Mustang", 2024);

// Display information about both cars


car1.displayInfo();
car2.displayInfo();
Arrays of Objects: A Simple Data Structure

•An array is a simple data structure that holds a collection of elements, all of the
same type.
•In Java, we can create an array of objects, such as an array of Car objects.
// Creating an array of Car objects
Car[] garage = new Car[3];

// Initializing the array with Car objects


garage[0] = new Car("Tesla", "Model S", 2022);
garage[1] = new Car("BMW", "X5", 2019);
Explanation:
garage[2] = new Car("Audi", "A4", 2021);
•Array: The garage array holds multiple Car objects.
•Looping through the Array: The for-each loop iterates through each
// Display information about all cars in the garage Car in the array, calling displayInfo() on each one.
for (Car car : garage) {
car.displayInfo();
}
Advantages of Using Classes and Objects in Data Structures

Encapsulation:
•Classes allow you to encapsulate data and methods together,
providing a clear structure to your code.
Reusability:
•Once a class is defined, it can be reused to create multiple
objects, reducing redundancy.
Modularity:
•Code can be organized into distinct classes, making it easier to
maintain and understand.
Exercise: Implementing a Custom Data Structure

Task:
1. Create a Book class with attributes like title, author, and ISBN.
2. Write methods to display the book's details and to compare two books by their authors.
3. Create an array of Book objects and display details of all the books.
class Book {
String title;
String author;
String ISBN;

Book(String title, String author, String ISBN) {


this.title = title;
this.author = author;
this.ISBN = ISBN;
}

void displayDetails() {
System.out.println("Title: " + title);
System.out.println("Author: " + author);
System.out.println("ISBN: " + ISBN);
}

boolean isAuthor(String authorName) {


return author.equals(authorName);
}
}

public class Main {


public static void main(String[] args) {
Book[] library = new Book[2];
library[0] = new Book("Java Programming", "John Doe", "123-456");
library[1] = new Book("Data Structures", "Jane Smith", "789-101");

for (Book b : library) {


b.displayDetails();
System.out.println();
}
}
}
Abstract Data Type (ADT)

An Abstract Data Type (ADT) is a model for data structures that defines the type of
data it can hold, as well as the operations that can be performed on the data,
without specifying how these operations will be implemented.

Key Characteristics:

Abstraction:

ADTs focus on what operations are available (e.g., insert, delete, find) and what data types are
supported, rather than how these operations are carried out internally. This abstraction allows you to
work with the ADT without needing to know the details of its implementation.

Interface and Implementation:

The interface of an ADT defines the operations you can perform on the data.
The implementation of an ADT defines how these operations are carried out. Different
implementations can offer varying performance characteristics.
Examples of ADTs:

•List: A collection of elements with operations like insert, delete, and find.

•Stack: A list-like structure where elements are added or removed from one
end (Last-In-First-Out or LIFO).

•Queue: A list-like structure where elements are added at one end and
removed from the other end (First-In-First-Out or FIFO).
Thank You!

You might also like