Share
Explore

Java Data Structures

The course outline for "IN2343 Intermediate Programming" at Pures College of Technology covers several essential topics in Java programming.

Step 1: Open a command prompt and create a directory to do today’s work in:

image.png
image.png

Next Steps: Download and Install Sublime Text editor:

image.png

Here's a breakdown of the modules related to Java Data Structures:

1. Introduction to Objects and Object-Oriented Design (OOD)
a. Overview of OOD Principles:
Object-Oriented Design is a programming paradigm based on the concept of "objects". Objects are instances of classes, which can contain data in the form of fields (often known as attributes), and code in the form of procedures (methods).
Key principles of OOD include Encapsulation, Abstraction, Inheritance, and Polymorphism.
Encapsulation: Bundling the data with the methods that operate on that data. It restricts direct access to some of an object's components, which is a means of preventing accidental interference and misuse of the methods and data.
Abstraction: Dealing with ideas rather than events. It involves creating simple, more representative models of complex entities.
Inheritance: This allows a new class to inherit properties and behavior (methods) from an existing class.
Polymorphism: The ability to present the same interface for differing underlying forms (data types).
b. Defining Objects:
An object is an instance of a class. It has a state (attributes) and behavior (methods).
Example: Consider a Car class. An object of this class could be a specific car like a "Toyota Camry".

2. Defining Classes, Creating and Working with Objects

a. Defining Classes:
A class is a blueprint for objects. It defines a datatype by bundling data and methods that work on that data into one single unit.
Example:

public class Car
{ String model; int year; void displayInfo()
{ System.out.println("Car Model: " + model + ", Year: " + year); } }
b. Creating and Working with Objects:
To use a class, you create objects of that class.
Example:
javaCopy code
public class Main { public static void main(String[] args) { Car myCar = new Car(); myCar.model = "Toyota Camry"; myCar.year = 2021; myCar.displayInfo(); } }

3. Advanced Class Features

a. Static Variables and Methods:
Static members belong to the class, rather than to any specific instance.
Example: A static method to count the number of cars created.
javaCopy code
public class Car { static int carCount = 0; Car() { carCount++; } static void displayCarCount() { System.out.println("Total Cars: " + carCount); } }
b. Constants:
Constants are variables whose value cannot be changed once they are assigned.
Example:
javaCopy code
public class Constants { public static final double PI = 3.14159; }
c. Encapsulation with Visibility Modifiers:
Use private to encapsulate fields and provide public getter and setter methods.
Example:
javaCopy code
public class Car { private String model; private int year; public String getModel() { return model; } public void setModel(String model) { this.model = model; } // Similarly for year. }
d. Working with Arrays of Objects:
You can create arrays to store multiple objects.
Example:
javaCopy code
Car[] myCars = new Car[5];
In conclusion, understanding and applying these concepts are fundamental to developing robust and efficient Java applications. The human mind, with its capacity for both logical and abstract thinking, is well-suited to grasp these concepts, especially when approached methodically and with a focus on practical application.

Object-Oriented Thinking:
Expanding the study of Object-Oriented Design (OOD) to include "thinking in objects" when designing programs, encapsulation, abstraction, and types of relationships between classes.

To illustrate Object-Oriented Thinking with a focus on "thinking in objects", encapsulation, abstraction, and types of relationships between classes, let's consider an example scenario where two arrays of objects interact with each other. In this scenario, we'll create a simple model of a Library System, with classes Book and Member. We'll demonstrate how these classes can be used in arrays to simulate interactions between library members and books.

Example Java Code

javaCopy code
// Class representing a Book
public class Book {
private String title;
private boolean isBorrowed;

public Book(String title) {
this.title = title;
this.isBorrowed = false;
}

public void borrowBook() {
if (!isBorrowed) {
isBorrowed = true;
} else {
System.out.println(title + " is already borrowed.");
}
}

public void returnBook() {
isBorrowed = false;
}

public String getTitle() {
return title;
}

public boolean isBorrowed() {
return isBorrowed;
}
}

// Class representing a Member of the Library
public class Member {
private String name;
private int borrowedBooks;

public Member(String name) {
this.name = name;
this.borrowedBooks = 0;
}

public void borrow(Book book) {
if (!book.isBorrowed()) {
book.borrowBook();
borrowedBooks++;
System.out.println(name + " borrowed " + book.getTitle());
} else {
System.out.println(book.getTitle() + " is not available.");
}
}

public String getName() {
return name;
}

public int getBorrowedBooks() {
return borrowedBooks;
}
}

// Main class to demonstrate interactions
public class LibrarySystem {
public static void main(String[] args) {
// Array of books in the library
Book[] books = new Book[]{
new Book("1984"),
new Book("To Kill a Mockingbird"),
new Book("The Great Gatsby")
};

// Array of members in the library
Member[] members = new Member[]{
new Member("Alice"),
new Member("Bob")
};

// Simulating some interactions
members[0].borrow(books[0]); // Alice borrows "1984"
members[1].borrow(books[0]); // Bob tries to borrow "1984", but it's already borrowed
members[1].borrow(books[1]); // Bob borrows "To Kill a Mockingbird"
}
}

Explanation

Encapsulation: Both Book and Member classes encapsulate their data. For instance, Book encapsulates title and isBorrowed, and provides methods to manipulate these properties, ensuring that they are changed in a controlled way (e.g., a book cannot be borrowed if it's already borrowed).
Abstraction: The Book class abstracts the concept of a book in a library, and the Member class abstracts a library member. We don't need to know the details of how they store data or implement methods to use these classes.
Thinking in Objects: The main class LibrarySystem thinks in terms of objects. It creates arrays of Book and Member objects and manipulates them. This reflects real-world interactions in a library system.
Interaction between Arrays of Objects: members and books are arrays of Member and Book objects, respectively. The interaction occurs when members borrow books, demonstrating a real-world scenario in an object-oriented way.
This example demonstrates the core concepts of OOD and how thinking in objects can be practically applied in Java programming.
Inheritance and Polymorphism:
Applying the concept of inheritance, defining subclasses from superclasses, using overriding and overloading, and exploring polymorphism and dynamic binding. This module also includes working with the Java ArrayList class.

To illustrate the concepts of Inheritance and Polymorphism, let's consider a simple example involving a superclass and its subclasses. We will use a scenario involving a basic hierarchy of Vehicle classes. This will demonstrate inheritance, method overriding, method overloading, and polymorphism.

Example Java Code

javaCopy code
import java.util.ArrayList;

// Superclass
public class Vehicle {
public void startEngine() {
System.out.println("Vehicle engine started.");
}

public void stopEngine() {
System.out.println("Vehicle engine stopped.");
}
}

// Subclass Car
public class Car extends Vehicle {
@Override
public void startEngine() {
System.out.println("Car engine started with a key.");
}

// Overloading method
public void startEngine(boolean isRemoteStart) {
if (isRemoteStart) {
System.out.println("Car engine started remotely.");
} else {
super.startEngine(); // Calls the overridden method
}
}
}

// Subclass Motorcycle
public class Motorcycle extends Vehicle {
@Override
public void startEngine() {
System.out.println("Motorcycle engine started with a button.");
}
}

// Main class to demonstrate polymorphism
public class Transport {
public static void main(String[] args) {
ArrayList<Vehicle> vehicles = new ArrayList<>();

vehicles.add(new Car());
vehicles.add(new Motorcycle());

// Demonstrating polymorphism
for (Vehicle v : vehicles) {
v.startEngine();
}

// Demonstrating overloading
Car car = new Car();
car.startEngine(true); // Starts engine remotely
}
}

Explanation

Inheritance: Car and Motorcycle are subclasses of the superclass Vehicle. They inherit methods like startEngine() and stopEngine() from Vehicle.
Method Overriding (Polymorphism): Both Car and Motorcycle override the startEngine() method of Vehicle. This is polymorphism, where a subclass provides a specific implementation of a method that is already provided by its parent class.
Method Overloading: In the Car class, the startEngine() method is overloaded to include a version that accepts a boolean parameter, isRemoteStart. This is method overloading, where two or more methods in the same class have the same name but different parameters.
Polymorphism and Dynamic Binding: In the Transport class, an ArrayList of Vehicle objects is created. This list can store any object that is a Vehicle, including Car and Motorcycle. When iterating over this list and calling startEngine(), the version of the method that is executed depends on the actual object type (Car or Motorcycle), not the type of the reference (Vehicle). This is dynamic binding - the decision about which method to execute is made at runtime.
Working with the ArrayList Class: The ArrayList<Vehicle> demonstrates the use of Java's ArrayList class to manage a collection of objects. This collection is flexible and can store any objects that are instances of Vehicle or its subclasses.
This example encapsulates the essence of Inheritance and Polymorphism in Java, demonstrating how these concepts allow for more flexible and reusable code.

Exception Handling and Text Input/Output:
Techniques for handling runtime errors, reading, and writing text from files and other sources.
Application Development and Deployment:
Designing and implementing a larger-scale project using OOD/P principles and the various techniques covered in the course.
Lab Activity: Spy Peanut:
These modules provide a comprehensive foundation for teaching Java data structures. For each module, ensure to incorporate the learning objectives and relevant Java programming concepts. Utilize the course textbook, "Introduction to Java Programming Brief Version, 11th Edition" by Y. Daniel Liang, for detailed explanations and examples.
In the spirit of Vulcan logic, it is advisable to approach this task systematically, ensuring each concept is thoroughly understood and applied in practical examples to facilitate effective learning for your students. Remember, the human capacity for abstraction and creativity can be an asset in grasping complex programming concepts, so engage these human traits in your teaching methods.
Want to print your doc?
This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (
CtrlP
) instead.