Share
Explore

Assignment 1 Data Structures

Data Structures in Java:

In a more advanced Java Programming: you would learn a topic called DSA
Data Structures and Algorithms.
These data structures today are in-memory data structures.
Benefit of in-memory data structures:
→ Our goal as programmers: Think about to push as much of the work as possible to our Data Structures: And let the Data Structures do the processing for us.
Next assigment (2): (Next Week): We will learn how to persist Data into a SQL database.

How to submit :

Put your code into a GitHub Repository
Instructions will be provided on how to upload your code.

Learning Outcome 1:

Learn how to use GitHUB

First Step in using GitHub.
→ Learn how to set up a GITHUB.com Account.
Make an Account to LOGIN:
Use personal email
User Name

image.png
Linked lists, stacks, and queues.
These exercises involve storing and retrieving objects from data structures.
The examples are from interesting verticals to make the exercises more engaging.

Assignment: Understanding Objects in Java through Data Structures

Objective: Explore how Java objects can be stored, manipulated, and retrieved using different types of data structures such as linked lists, stacks, and queues.

Exercise 1: Implement a Linked List of Student Objects

// Your hand in requirement for exercise 1:
// get the linked list working: with methods to print out the linked list forward and backward
Make a new method: printStudentsInReverse()
image.png
A linked list is a linear data structure where each element is a separate object. Each element (node) of a list is made up of two items: the data and a reference to the next node.
Task: Create a linked list of Student objects. Each student object should have the following properties: name, id, and grade.
Example:
public class Student {
String name;
int id;
float grade;
}
Here's a complete Java code example that demonstrates the creation of a linked list of Student objects with the properties: name, id, and grade. In this example, we'll create a linked list of three students and display their information.

// Node class represents a single node in the linked list
class Node {
Student data; // Student object
Node next; // Reference to the next node

public Node(Student student) {
data = student;
next = null;
}
}

// Student class represents a student object
class Student {
String name;
int id;
int grade;

public Student(String name, int id, int grade) {
this.name = name;
this.id = id;
this.grade = grade;
}
}

// LinkedList class represents the linked list data structure
class LinkedList {
Node head; // Reference to the first node in the linked list

// Method to add a student to the linked list
public void addStudent(Student student) {
Node newNode = new Node(student);
if (head == null) {
head = newNode;
} else {
Node current = head;
while (current.next != null) {
current = current.next;
}
current.next = newNode;
}
}

// Method to display the student information in the linked list
public void displayStudents() {
Node current = head;
while (current != null) {
Student student = current.data;
System.out.println("Name: " + student.name);
System.out.println("ID: " + student.id);
System.out.println("Grade: " + student.grade);
System.out.println("----------------------");
current = current.next;
}
}
}


In this example, we create a Student class to represent a student object with name, id, and grade properties. The Node class represents a single node in the linked list, where each node holds a reference to a Student object and the reference to the next node.
The LinkedList class encapsulates the functionality to add students to the linked list and display their information. The addStudent method adds a student by creating a new Node object and appending it to the end of the linked list. The displayStudents method traverses the linked list and prints out the information for each student.
In the main method, we create a LinkedList object, instantiate three Student objects, add them to the linked list using the addStudent method, and finally, display the student information using the displayStudents method.
This example demonstrates the basic functionality of creating a linked list of student objects in Java and displaying their information. You can expand on this code to include additional operations such as searching for a specific student, removing a student from the linked list, etc., based on your requirements.

Exercise 2: Implement a Stack of Book Objects

A stack is a linear data structure that follows the Last In First Out (LIFO) principle.
Task: Implement a stack of Book objects.
Each book object should have title, author, and isbn properties.
Example:
public class Book {
String title;
String author;
String isbn;
}

// Complete Code:

// Here's an example of how you can implement a stack of Book objects
// in Java, along with the data management object that
// provides the push and pop stack handling methods:

import java.util.Stack;

class Book {
String title;
String author;
String isbn;

public Book(String title, String author, String isbn) {
this.title = title;
this.author = author;
this.isbn = isbn;
}
}

class BookStack {
private Stack<Book> stack;

public BookStack() {
stack = new Stack<>();
}

public void push(Book book) {
stack.push(book);
}

public Book pop() {
return stack.pop();
}

public boolean isEmpty() {
return stack.isEmpty();
}

public int size() {
return stack.size();
}

public Book peek() {
return stack.peek();
}
}
// In the code above, we have a Book class representing a book with title,
// author, and isbn properties. Then, we have a BookStack class that uses the
// Stack data structure provided by Java to manage the stack of books.
//
// The push method allows you to add a book to the top of the stack, and the pop
// method removes and returns the book at the top of the stack. The isEmpty
// method checks if the stack is empty, and the size method returns the number
// of books in the stack. Finally, the peek method returns the book at the top
// of the stack without removing it.
// You can use the BookStack class as follows:

public class Main {
public static void main(String[] args) {
BookStack bookStack = new BookStack();

// Pushing books to the stack
Book book1 = new Book("Title 1", "Author 1", "ISBN 1");
Book book2 = new Book("Title 2", "Author 2", "ISBN 2");
bookStack.push(book1);
bookStack.push(book2);

System.out.println("Stack size: " + bookStack.size());

// Popping a book from the stack
Book poppedBook = bookStack.pop();
System.out.println("Popped book: " + poppedBook.title);
System.out.println("Stack size: " + bookStack.size());
// Checking if the stack is empty
System.out.println("Is stack empty? " + bookStack.isEmpty());

// Peeking at the top book
Book topBook = bookStack.peek();
System.out.println("Top book: " + topBook.title);
System.out.println("Is stack empty? " + bookStack.isEmpty());
System.out.println("Stack size: " + bookStack.size());
System.out.println("Popped book: " + poppedBook.title);
}
}
Exercise 2: What you need to do for the Assignment:
Make a Stack with 10 Books
Make a method to walk over the Stack, and report on the Titles of Each Book in your Stack of Books

Exercise 3: Implement a Queue of Athlete Objects

A queue is a type of data structure that follows the First In First Out (FIFO) principle.
Task: Implement a queue of Patient objects. Each patient object should have name, age, and ailment properties.
Example:
public class Athlete{
String name;
int age;
int runningDistanceEachDay;
}

// Here's an implementation of a queue of Athlete objects

// in Java, providing all standard Java Queue methods:

import java.util.LinkedList;
import java.util.Queue;

class Athlete {
String name;
int age;
int runningDistanceEachDay;

public Athlete(String name, int age, int runningDistanceEachDay) {
this.name = name;
this.age = age;
this.runningDistanceEachDay = runningDistanceEachDay;
}
}

class QueueManager {
private Queue<Athlete> queue;

public QueueManager() {
queue = new LinkedList<>();
}

public void enqueue(Athlete athlete) {
queue.add(athlete);
}

public Athlete dequeue() {
return queue.poll();
}

public boolean isEmpty() {
return queue.isEmpty();
}

public int size() {
return queue.size();
}

public Athlete peek() {
return queue.peek();
}
}
//In the code above, we have an Athlete class representing an athlete with name, age, and runningDistanceEachDay properties. Then, we have an AthleteQueue class that uses the LinkedList data structure provided by Java to manage the queue of athletes.

//The enqueue method allows you to add an athlete to the end of the queue, and the dequeue method removes and returns the athlete at the front of the queue. The isEmpty method checks if the queue is empty, and the size method returns the number of athletes in the queue. Finally, the peek method returns the athlete at the front of the queue without removing it.

//You can use the AthleteQueue class as follows:

public class Main {
public static void main(String[] args) {
QueueManager athleteQueue = new QueueManager();

// Enqueuing athletes
Athlete athlete1 = new Athlete("Athlete 1", 25, 10);
Athlete athlete2 = new Athlete("Athlete 2", 30, 8);
athleteQueue.enqueue(athlete1);
athleteQueue.enqueue(athlete2);

System.out.println("Queue size: " + athleteQueue.size());

// Dequeuing an athlete
Athlete dequeuedAthlete = athleteQueue.dequeue();
System.out.println("Dequeued athlete: " + dequeuedAthlete.name);

// Checking if the queue is empty
System.out.println("Is queue empty? " + athleteQueue.isEmpty());

// Peeking at the front athlete
Athlete frontAthlete = athleteQueue.peek();
System.out.println("Front athlete: " + frontAthlete.name);

Athlete dequeuedAthlete2 = athleteQueue.dequeue();
System.out.println("Dequeued athlete: " + dequeuedAthlete2.name);
// Checking if the queue is empty
System.out.println("Is queue empty? " + athleteQueue.isEmpty());

}
}
// This implementation provides all the standard Java Queue methods (enqueue, dequeue, isEmpty, size, and peek) to manage the queue of Athlete objects. Again, depending on your specific requirements, you may need to add additional functionality or error handling to the AthleteQueue class.
What you need to do for Exercise 3 for the Assignment:
Create 10 Athetes
Make LOOP to iterate over the List
And print out the Details of Each Athlete
Requirement: At the END I must have the same Athletes in the Queue that I had at the beginning

Exercise 4: Retrieve Objects from a Linked List

Task: Retrieve specific Student objects from your linked list based on a given condition. For example, retrieve all students with grades above a certain threshold.
Example:
java
Copy code
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.