Question

Write three classes: Student.java, StudentList.java and Course.java with using the concept of linked list.

Write three classes: Student.java, StudentList.java and Course.java with using the concept of linked list.

0 0
Add a comment Improve this question Transcribed image text
Answer #1
import java.util.HashMap;
import java.util.Objects;

public class StudentList {
    private HashMap<Student, CourseList> studentList;

    public StudentList() {
        this.studentList = new HashMap<>();
    }

    public void add(Student student, Course course) {
        if (this.studentList.containsKey(student)) {
            CourseList courseListOfStudent = studentList.get(student);
            courseListOfStudent.add(course);
            return;
        }
        CourseList courseList = new CourseList();
        courseList.add(course);
        studentList.put(student, courseList);
    }

    public void display() {
        for (Student student : studentList.keySet()) {
            System.out.println("Student Name ::" + student);
            studentList.get(student).courseList();
            System.out.println();
        }
    }

    public static void main(String[] args) {
        StudentList studentList = new StudentList();
        studentList.add(new Student("srinu"),new Course("MATHS"));
        studentList.add(new Student("srinu"),new Course("Physics"));
        studentList.add(new Student("srinu"),new Course("IT"));

        studentList.add(new Student("ram"),new Course("MATHS"));
        studentList.add(new Student("ram"),new Course("Chemistry"));

        studentList.add(new Student("John"),new Course("IT"));
        studentList.display();
    }
}

class Course {
    private final String courseName;

    public Course(String courseName) {
        this.courseName = courseName;
    }

    @Override
    public String toString() {
        return courseName;
    }
}

class Student {
    private final String name;

    public Student(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return name;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Student student = (Student) o;
        return Objects.equals(name, student.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name);
    }
}

class CourseList {
    private Node head;

    public CourseList() {
        this.head = null;
    }

    public void add(Course course) {
        if (head == null) {
            head = new Node(course);
            return;
        }
        Node temp = new Node(course);
        temp.next = head;
        head = temp;
    }

    public void courseList() {
        System.out.printf("Course List:: ");
        Node temp = head;
        while (temp != null) {
            System.out.printf("%s ", temp.getCourse());
            temp = temp.next;
        }
        System.out.println();
    }
}

class Node {

    private Course course;
    Node next;

    public Node(Course course) {
        this.course = course;
        this.next = null;
    }

    public Course getCourse() {
        return course;
    }
}
Student Name::srinu Course List:: IT Physics MATHS Student Name::Johrn Course List: IT Student Name ::ram Course List:: Chemi
Add a comment
Know the answer?
Add Answer to:
Write three classes: Student.java, StudentList.java and Course.java with using the concept of linked list.
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT