Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program that will prompt the user to enter a number, n, of men. Fill a L

ID: 650252 • Letter: W

Question

Write a program that will prompt the user to enter a number, n, of men. Fill a LinkedList with n Integer objects numbered 1, 2, , n. (Hint: The Integer value will be equal to the man's position in the original list.)

The Collections class has an algorithm called rotate(List<?> list, int distance) which can be used to rotate a list left or right. Write the logic necessary to eliminate every other Integer beginning with the Integer in the second position. Remember that if you remove an item in the middle of the list, the items which follow it are shifted forward in the list.

Continue eliminating integers until a single Integer is left. Print the list for each step in this process and the survivor's position.

As an example of this process, if we represent five men with a list of integers, [1,2,3,4,5], rotate the list to the left and remove the first man each time, we would see lists like this:


[1, 2, 3, 4, 5]
[3, 4, 5, 1]
[5, 1, 3]
[3, 5]
[3]

Explanation / Answer

// a program that will prompt the user to enter a number(in java language)

import java.util.Scanner;
public class test2
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
System.out.println("Enter a string");
String s = input.nextLine();// methods should be called with () even if have no argument

String lower = "";
String total = "";

for (int i = 0; i < s.length(); i++) {
char thisChar = s.charAt(i);
// you can replace with : if (Character.isLowerCase(thisChar))
if (thisChar >= 97 && thisChar <= 122)
{
lower += thisChar;
}
}//close the for loop here

System.out.println("Total amount of characters: " + s.length() + " - " + s);
System.out.println("Lower case letters: " + lower.length() + " - " + lower);
}
}

// printing ,finding and reversing node in pattern

public class Node {

public int item,p;
public Node next;
for(Node p = head; p != null; p = p.next ) {
System.out.println(p.item);
}

Node p = head;
while( p != null ) {
System.out.println(p.item);
p = p.next;
}
void printList(Node p) {
for( ; p!=null; p = p.next) {
System.out.println(p.item);
}
}
int length(Node h) {
int count = 0;
for(Node p = h.next ; p!=null; p = p.next) {
++count;
}
return count;
}

int n = length(head);
void reverse()
{
if(head == null)
return;
Node p = head.next, q = head, r;
while ( p != null ) {
r = q; // r follows q
q = p; // q follows p
p = p.next; // p moves to next node
q.next = r; // link q to preceding node
}
head.next = null;
head = q;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote