Question

The class CourseList implements a simple database of students registered in a course. Complete the method...

The class CourseList implements a simple database of students registered in a course. Complete the method countAttendees in the code below such that it returns the number of students in the instance of CourseList who have not attended any classes. Your response may be written in Java or pseudocode. Your code cannot modify the linked list. The class Node is similar to the linked list node object discussed in lecture. You can use helper methods, but this is not necessary.

class Student {

    protected int studentNumber;
    protected int classesAttended;

    public Student (int newStudentNumber) {
        studentNumber = newStudentNumber;
        classesAttended = 0;
    }

    public int getClassesAttended () { return classesAttended; }

    public void attendClass () { classesAttended++; }
}

class Node<E> {

    protected E value;
    protected Node<E> next;

    public Node (E newValue, Node<E> newNext) {
        value = newValue;
        next = newNext;
    }

    public E getValue () { return value; }
    public Node<E> getNext () { return next; }
}

public class CourseList {

    Node<Student> head;

    public CourseList () { head = null; }

    public void addStudent (Student newStudent) { head = new Node<Student>(newStudent, head); }

    public int countAttendees () {

Homework Answers

Answer #1

Solution:

  • Look for comments in the code to understand it line-by-line.
  • Check for student attendance and increment count accordingly.

Java code:

public int countAttendees () {
    // Make a new object storing head
    Node<Student> p = head;

    // Initialise count to 0        
    int count = 0;
    
    // Iterate over list of student nodes, till p becomes null
    while(p!=null){
        // Fetch student details from node
        Student s = p.getValue();
        
        // If student attended no class increment count
        if(s.getClassesAttended() == 0)
            count += 1;

        // Move to next student in the list                 
        p = p.getNext();
    }
    
    // return number of students who have not attended any class
    return count;
}

Screenshot of code to understand indentation:

PS: Let me know if you have any doubt

Know the answer?
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
Similar Questions
Given this definition of a generic Linked List node: public class LLNode {     private T...
Given this definition of a generic Linked List node: public class LLNode {     private T data;     private LLNode next;     public LLNode(T data, LLNode next) {           this.data = data;           this.next = next;     }     public void setNext(LLNode newNext){ next = newNext; }     public LLNode getNext(){ return next; }     public T getData() {return data;} } Write the findMinimumNode method body. This method returns the linked list node that contains the minimum value in the...
1. Design and implement a CircularLinkedList, which is essentially a linked list in which the next...
1. Design and implement a CircularLinkedList, which is essentially a linked list in which the next reference of the tail node is set to refer back to the head of the list (rather than null) . Start from the SinglyLinkedList provided in the Lecture (not the built-in Java LinkedList class). 2. Starting from  SinglyLinkedList you will need to modify or complete methods: first(), last(), addFirst(), addLast(), removeFirst(), toString(), and also create a new rotate() method which will move the tail to...
public class DoublyLinkedList { Node Head; // head of Doubly Linked List //Doubly Linked list Node...
public class DoublyLinkedList { Node Head; // head of Doubly Linked List //Doubly Linked list Node class Node { int value; Node prev; Node next; // Constructor to create a new node Node(int d) { value = d; } } // Inserting a node at the front of the list public void add(int newData) { // allocate node and put in the data Node newNode = new Node(newData); // Make the next of new node as head // and previous...
Question: I get a Segmentation fault error sometimes when I addElementBack or print. Am I using...
Question: I get a Segmentation fault error sometimes when I addElementBack or print. Am I using pointers correctly and deleting memory properly? #ifndef DYNAMICARRAY_H #define DYNAMICARRAY_H #include <cstdlib> #include <iostream> using namespace std; // Node class class Node { int data; Node* next; Node* prev; public: Node(); Node(int); void SetData(int newData) { data = newData; }; void SetNext(Node* newNext) { next = newNext; }; void SetPrev(Node* newPrev) { prev = newPrev; }; int getData() { return data; }; Node* getNext()...
(Please use Java Eclipse) QUESTION 1: For the question below, assume the following implementation of LinkedQueue:...
(Please use Java Eclipse) QUESTION 1: For the question below, assume the following implementation of LinkedQueue: public static final class LinkedQueue<T> implements QueueInterface<T> {     private Node<T> firstNode;     private Node<T> lastNode;     public LinkedQueue() {         firstNode = null;         lastNode = null;     }     @Override     public T getFront() {         if (isEmpty()) {             return null;         }         return firstNode.getData();     }     @Override     public boolean isEmpty() {         return firstNode == null;    ...
Q1; Write a method in class SLL called public SLL reverse() that produces a new linked...
Q1; Write a method in class SLL called public SLL reverse() that produces a new linked list with the contents of the original list reversed. Make sure not to use any methods like addToHead() or addToTail(). In addition, consider any special cases that might arise. What is the big-O complexity of your method in terms of the list size n Supplementary Exercise for Programming (Coding) [Singly Linked Lists] Download and unpack (unzip) the file SinglyLinkedList.rar. Compile and execute the class...
This is the java code that I have, but i cannot get the output that I...
This is the java code that I have, but i cannot get the output that I want out of it. i want my output to print the full String Form i stead of just the first letter, and and also print what character is at the specific index instead of leaving it empty. and at the end for Replaced String i want to print both string form one and two with the replaced letters instead if just printing the first...
based on the code below, answer the questions Question 1: The LinkedList class uses another class...
based on the code below, answer the questions Question 1: The LinkedList class uses another class called Node (which is defined inside LinkedList.java). What are the fields in the Node class? Question 2: The Node class uses generics. Why? Question 3: The linkFirst method contains the following lines of code: if (f == null) last = newNode; else f.prev = newNode; What is the value of the linked list’s size attribute at this point in the code if f ==...
Use Java: Also: Please include screenshots if possible. Create a class called AbstractStackTest. Write an abstract...
Use Java: Also: Please include screenshots if possible. Create a class called AbstractStackTest. Write an abstract method called makeStack that returns a Stack of Strings. Use the Stack interface as the return type, not a specific implementation! Write a class named NodeStackTest that extends your AbstractStackTest class. Implement the makeStack method to return a NodeStack. Repeat parts 1 and 2 for the Queue interface and the NodeQueue implementation. Write a new stack implementation, ArrayStack. It should be generic and use...
IntNode class I am providing the IntNode class you are required to use. Place this class...
IntNode class I am providing the IntNode class you are required to use. Place this class definition within the IntList.h file exactly as is. Make sure you place it above the definition of your IntList class. Notice that you will not code an implementation file for the IntNode class. The IntNode constructor has been defined inline (within the class declaration). Do not write any other functions for the IntNode class. Use as is. struct IntNode { int data; IntNode *next;...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT