Question

In: Computer Science

IN JAVA!!! This is called the Josephus problem. Josephus.mp4Play media comment. Use words.txt as input. Use...

IN JAVA!!!

This is called the Josephus problem. Josephus.mp4Play media comment.

Use words.txt as input.

Use the Node class and the Circular Queue class you created in L5b.

In your main program input the words from words.txt into a Queue (a circular queue) - do this by using enqueue .

Then input an integer (n). Then starting at the head of the queue delete every nth node until only one node remains. Output the String in that last node.

Make NO changes to the Node class. In the Queue class you will need a method to delete every nth node.

You will need to print your queue, so make a print method in your Queue class. This should print the ENTIRE queue, not just the head. It is true that at the end only the head should remain, but by printing the entire queue and finding there is only one node printed you are verifying to me (and to yourself) that only the head remains.

When I ran my program these are the answers I got:

n=5 : hostelry

n=29: cliquish

n=7: alienist

SUGGESTION: Try this on a smaller input file first. This one contains just the numbers 1-20    Ex.txt

You can try this by hand, and then verify that your program is correct. For n=5, the answer is "20"

NODE CLASS

class Node<T> {
//Node makes item, next, and prev all private
//and therefore has to provide accessors
//and mutators (gets and sets)
private T item;
private Node next;
private Node prev;

Node(T newItem) {
item = newItem;
next = null;
prev = null;
}

Node(T newItem, Node nextNode, Node prevNode) {
item = newItem;
next = nextNode;
prev = prevNode;
}

/**
* @return the item
*/
public T getItem() {
return item;
}

/**
* @param item the item to set
*/
public void setItem(T item) {
this.item = item;
}

/**
* @return the next
*/
public Node getNext() {
return next;
}

/**
* @param next the next to set
*/
public void setNext(Node next) {
this.next = next;
}

/**
* @return the prev
*/
public Node getPrev() {
return prev;
}

/**
* @param prev the prev to set
*/
public void setPrev(Node prev) {
this.prev = prev;
}

}

QUEUE CLASS

class Queue<T> {

private Node head, tail = null;
int size = 0;
public Queue() {
head = tail = null;
size = 0;
}
//enqueue
public void enqueue(T item) {
Node temp = new Node(item);
if (head == null) {
head = temp;
tail = temp;
} else {
temp.next = head;
tail.next = temp;
tail = temp;
}
size++;
}

//dequeue
public T dequeue(T item) {
Node temp = head;
head = head.next;
tail.next = head;
size--;
return (T) temp.item;
}
//size
public int size() {
if (size == 0) {
}
return size;
}
//peek
public T peek() {
return (T) head.item;
//return item
}
//isEmpty
public boolean isEmpty() {
return (size == 0);
}
//print in queue class
public void print() {
if(head != null) {
Node curr = head;
System.out.println(curr.item);
curr = curr.next;
while (curr != head) {
System.out.println(curr.item);
curr = curr.next;
}
}
}

//Deletes every nth node until only one node remains
public void delete(int num, Queue q) {
Node head = q.head;
Node tail = q.tail;
int count;
Node temp = tail;
int length = q.size;
while((head != tail) && length > 1){
count = 1;
while(count != num){
temp = temp.next;
count++;
}
System.out.println("Deleting " + temp.next.item);
temp.next = temp.next.next;
length--;
}
q.head = temp;
q.tail = temp;
q.size = length;
}
}

Solutions

Expert Solution

file-name:   Node.java
----------------------------
class Node<T> {
    //Node makes item, next, and prev all private
//and therefore has to provide accessors
//and mutators (gets and sets)
    private T item;
    private Node next;
    private Node prev;

    Node(T newItem) {
        item = newItem;
        next = null;
        prev = null;
    }
    Node(T newItem, Node nextNode, Node prevNode) {
        item = newItem;
        next = nextNode;
        prev = prevNode;
    }

    /**
     * @return the item
     */
    public T getItem() {
        return item;
    }
    /**
     * @param item the item to set
     */
    public void setItem(T item) {
        this.item = item;
    }

    /**
     * @return the next
     */
    public Node getNext() {
        return next;
    }

    /**
     * @param next the next to set
     */
    public void setNext(Node next) {
        this.next = next;
    }

    /**
     * @return the prev
     */
    public Node getPrev() {
        return prev;
    }

    /**
     * @param prev the prev to set
     */
    public void setPrev(Node prev) {
        this.prev = prev;
    }
} // END of class Node

=============================================

file-name:     Queue.java
------------------------------
class Queue<T> {

    private Node head, tail = null;
    int size = 0;
    public Queue() {
        head = tail = null;
        size = 0;
    }
    //enqueue
    public void enqueue(T item) {
        Node temp = new Node(item);
        if (head == null) {
            head = temp;
            tail = temp;
        } else {
            temp.setNext(head);
            tail.setNext(temp);
            tail = temp;
        }
        size++;
    }

    //dequeue
    public T dequeue(T item) {
        Node temp = head;
        head = head.getNext();
        tail.setNext(head);
        size--;
        return (T) temp.getItem();
    }
    //size
    public int size() {
        if (size == 0) {
        }
        return size;
    }
    //peek
    public T peek() {
        return (T) head.getItem();
//return item
    }
    //isEmpty
    public boolean isEmpty() {
        return (size == 0);
    }
    //print in queue class
    public void print() {
        if(head != null) {
            Node curr = head;
            System.out.println(curr.getItem());
            curr = curr.getNext();
            while (curr != head) {
                System.out.println(curr.getItem());
                curr = curr.getNext();
            }
        }
    }

    //Deletes every nth node until only one node remains
    public void delete(int num, Queue q) {
        Node head = q.head;
        Node tail = q.tail;
        int count;
        Node temp = tail;
        int length = q.size;
        while((head != tail) && length > 1){
            count = 1;
            while(count != num){
                temp = temp.getNext();
                count++;
            }
            System.out.println("Deleting " + temp.getNext().getItem());
            temp.setNext(temp.getNext().getNext());
            length--;
        }
        q.head = temp;
        q.tail = temp;
        q.size = length;
    } // END of delete
} // END of class Queue

================================================

file-name:  Main.java
-----------------------------
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

public class Main {
    public static void main(String[] args) {

        // creating Queue object
        Queue<String> q = new Queue<>();

        BufferedReader br;
        try {
            FileReader file = new FileReader("/home/rahul/Pictures/words.txt");
            br = new BufferedReader(file);
            String line = br.readLine();
            while(line != null) {
                q.enqueue(line);
                line = br.readLine();
            }
        } // END of try
        catch (FileNotFoundException e) {
            System.out.println("No file with this name");
        } catch (IOException e) {
            e.printStackTrace();
        } // END of catch

        // printing all the names in the queue.
        q.print();
        System.out.println("\n===============================\n");
        // deleting every 5th member in the queue.
        q.delete(5,q);
        // printing the members in the queue after deleting
        System.out.println("\n================================\n");
        q.print();
    } // main( ) method ended
}// END of Main class

-----------------------------------------------------------------------------------------------

OUTPUT:

THANK YOU !! If you have any doubts please ask in comment section. I will modify the answer accordingly. Please UPVOTE if you like my effort and answer.


Related Solutions

The problem is known as the Josephus Problem (or Josephus permutation) and postulates a group of...
The problem is known as the Josephus Problem (or Josephus permutation) and postulates a group of people of size N >= 1 are standing in a circle waiting to be eliminated. Counting begins at a specified point in the circle and proceeds around the circle in a specified direction. After a specified number of M >= 1 people are counted, the M^th person in the circle is eliminated. The procedure is repeated with the remaining people, starting with the next...
c++ The problem is known as the Josephus Problem (or Josephus permutation) and postulates a group...
c++ The problem is known as the Josephus Problem (or Josephus permutation) and postulates a group of people of size N >= 1 are standing in a circle waiting to be eliminated. Counting begins at a specified point in the circle and proceeds around the circle in a specified direction. After a specified number of M >= 1 people are counted, the M^th person in the circle is eliminated. The procedure is repeated with the remaining people, starting with the...
Please use Java language! With as many as comment! ThanksWrite a static method called "evaluate"...
In Java language Write a static method called "evaluate" that takes a string as a parameter. The string will contain a postfix expression, consisting only of integer operands and the arithmetic operators +, -, *, and / (representing addition, subtraction, multiplication, and division respectively). All operations should be performed as integer operations. You may assume that the input string contains a properly-formed postfix expression. The method should return the integer that the expression evaluates to. The method MUST use a stack...
language C++ i need output, Pleases The Josephus problem is named after the historian Flavius Josephus,...
language C++ i need output, Pleases The Josephus problem is named after the historian Flavius Josephus, who lived between the years 37 and 100 CE. Josephus was a reluctant leader of the Jewish revolt against the Roman Empire. When it appeared that Josephus and his band were to be captured, they resolved to kill themselves. Josephus persuaded the group by saying, “Let us commit our mutual deaths to determination by lot. He to whom the first lot falls, let him...
JAVA - PLEASE COMMENT CODE - THANK YOU: Implement a program that can input an expression...
JAVA - PLEASE COMMENT CODE - THANK YOU: Implement a program that can input an expression in postfix notation and output its value.
(Java)// Slightly different problem from the others that use the same input files, it has been...
(Java)// Slightly different problem from the others that use the same input files, it has been adapted from a practice problem// // Also if you can keep the code as simple as possible, try to avoid any advanced techniques, so that I can easily understand what is happening in case I decide to use this for future reference (which most likely I am) that'd be super appreciated// Deposit and Withdrawal Files: Use Notepad or another text editor to create a...
This is the question about the java problem, please give the detail comment and code of...
This is the question about the java problem, please give the detail comment and code of each class. Please write tests for all the code of all the classes Thank you Create a class Mammal with the following UML diagrams: +---------------------------------+ | Mammal | +---------------------------------+ | - name: String | +---------------------------------+ | + Mammal(String name) | | + getName(): String | | + isCookable(): boolean | | + testMammal(): void | (this method is static ) +---------------------------------+ The isCookable method...
In this problem, we provide you with a Java file called Question2.java. Suppose that there is...
In this problem, we provide you with a Java file called Question2.java. Suppose that there is an array and let us call this array, items. The array items contains n integers. These n integers are in a completely random order in the array. What you are asked to do is to reorder the items in the array such that all the negative integers come before all the zeros and all the positive integers appear at the end. Note, this question...
This problem is about java program and please show the detail comment and code in each...
This problem is about java program and please show the detail comment and code in each class. Thank you! Create four classes with the following UML diagrams: (The "-" means private and the testBankAccount() testMobilePhone() testChocolate() testStudent() all static +-----------------------------+ | BankAccount | +-----------------------------+ | - money: int | +-----------------------------+ | + BankAccount(int money) | | + getMoney(): int | | + setMoney(int money): void | | + testBankAccount(): void | +-----------------------------+ +------------------------------------------------+ | MobilePhone     | +------------------------------------------------+ | -...
IN JAVA!!! In this project, you will use radix.txt as the input file, and output the...
IN JAVA!!! In this project, you will use radix.txt as the input file, and output the integers SORTED USING RADIX SORT. You may assume all your input consists of integers <=9999. Your main program will input the integers and put them into a QUEUE. It will then pass this queue to a method called radixSort which will sort the numbers in the queue, passing the sorted queue back to main. The main program will then call another method to print...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT