Question

Objective: The goal of this assignment is to practice recursion. ignment: The assignment requires writing recursive methods for some linked list operations. The use of loops in the recursive methods is strictly prohibited in this assignment. That is, you cannot use for, while, and do-while in the recursive methods you will write. You can only use a loop when you will initiate values for a linked list in the main method. You will need to write a method to generate a random String. You can use loops in that method too. 0 Consider that you are given the following linked list node. public class StringNode t public String head: public StringNode next StringNode ) StringNode (String s) heads StringNode (String s, StringNode tail) f heads next-tail: Clearly, this class can be used to create a linked list. Write a class named Operations that will contain the methods described below. In addition, some instructions are given as comments of the partial code provided later in this assignment. 1. Write a method named getRandstring(int length) that will return a String of length given in the parameter. This method need not be a recursive one. The returned 2. Write a recursive method named printMyList (StringNode m) to print all the 3. Write a recursive method named countKLenghthStrings (StringNode m 4. Write a recursive method named longeststringofMyList (StringNode m) 5. Write a recursive method named lengthofMyList (StringNode m) that will 6. Write a recursive method named StringNode reverseMyList (StringNode 7. Write a recursive method named String may only contain English letters in capital (that is, letters from A to Z). strings in the linked list m. Notice that m is the head of the linked list. int k) that will return number of Strings with length k in the given linked list m. that will return the longest String in a linked list. compute and return the length of a given linked list m. m) to reverse a linked list. Return the head of the reversed linked list
media%2F588%2F58852c9d-b2da-45df-ab4f-56
media%2Fb01%2Fb015add4-02e6-4437-8e38-e1
media%2F0a3%2F0a3e223d-711f-402f-8baf-e0
media%2Fa03%2Fa0328a1b-3e29-4824-a1d0-00
Java language
Any use of java.util.LinkedList is prohibited
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here is the code :

import java.util.Random;

class StringNode {

public String head;

public StringNode next;

StringNode() {}

StringNode(String s) {

head =s ;

}

StringNode(String s, StringNode tail) {

head = s;

next = tail;

}

}

public class Operations {

  

static String getRandString(int length) {

String SALTCHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

StringBuilder salt = new StringBuilder();

Random rnd = new Random();

while (salt.length() < length) { // length of the random string.

int index = (int) (rnd.nextFloat() * SALTCHARS.length());

salt.append(SALTCHARS.charAt(index));

}

String saltStr = salt.toString();

return saltStr;

}

public static void printMyList(StringNode m) {

if (m != null) {

System.out.println(m.head);

printMyList(m.next);

}

}

  

public static int countKLengthString(StringNode m, int k) {

if (m != null) {

if (m.head.length()>k) {

return 1 + countKLengthString(m.next, k);

}else {

return 0 + countKLengthString(m.next, k);

}

}else {

return 0;

}

}

  

public static String longestStringOfMyList(StringNode m) {

String str;

  

if (m!=null) {

str = longestStringOfMyList(m.next);

if (m.head.length() > str.length() ) {

return m.head;

}else {

return str;

}

}else {

return "";

}

}

  

public static int lengthOfMyList (StringNode m) {

if (m!=null) {

return 1+lengthOfMyList(m.next);

}else {

return 0;

}

}

  

public static StringNode reverseMyList (StringNode m) {

StringNode newLL;

if (m==null) {

  

return null;

}else if (m.next == null){

return m;

}else {

newLL = reverseMyList(m.next);

m.next.next = m;

m.next = null;

return newLL;

}

}

  

public static StringNode removeAStringFromMyList(StringNode m, String removee) {

StringNode newLL = new StringNode();

if(m!=null) {

if (m.head == removee) {

return removeAStringFromMyList(m.next, "");

}else {

newLL.head = m.head;

newLL.next = removeAStringFromMyList(m.next, removee);

return newLL;

}

}else {

return null;

}

  

}

  

public static boolean isListInOrder(StringNode m) {

if (m.next != null) {

if ( m.head.compareTo(m.next.head) < 0)

return true & isListInOrder(m.next);

else

return false;

}else{

return true;

}

}

  

public static StringNode insertAStringIntoMyList(StringNode m, String insertee, int position) {

  

if (position == 0) {

StringNode newNode = new StringNode(insertee);

newNode.next = m;

return newNode;

}

if (m!=null) {

m.next = insertAStringIntoMyList(m.next, insertee, position-1);

return m;

}else {

return null;

}

}

  

  

public static boolean isPalindrome(String s) {

if(s.length() == 0 || s.length() == 1)

return true;

if(s.charAt(0) == s.charAt(s.length()-1))

return isPalindrome(s.substring(1, s.length()-1));

return false;

}

  

public static int countPalindromes(StringNode m) {

if (m!=null) {

if (isPalindrome(m.head.toLowerCase()))

return 1 + countPalindromes(m.next);

else

return countPalindromes(m.next);

}

else

return 0;

}

  

public static void main(String[] args) {

StringNode L = new StringNode("0"+getRandString(2+(int)(Math.random()*5)));

StringNode temp = L;

for ( int i = 1; i <=9; i++) {

temp.next = new StringNode(i+getRandString(2+(int)(Math.random()*5)));

temp = temp.next;

}

  

System.out.println("All Strings in the list");

printMyList(L);

System.out.println();

  

  

boolean b = isListInOrder(L);

System.out.println("List is ordered: "+b);

System.out.println();

  

System.out.println("Count of k-length strings");

System.out.println("k\tNo. of String with lenght k");

for ( int k = 0; k <7; k++) {

System.out.println(k+"\t"+countKLengthString(L,k));

}

  

System.out.println("Longest Strings="+longestStringOfMyList(L));

System.out.println("Length="+lengthOfMyList(L));

  

L = reverseMyList(L);

System.out.println("All string in the reversed list: ");

printMyList(L);

System.out.println();

  

System.out.println("Remove a given String");

StringNode LL = removeAStringFromMyList(L, L.next.next.head);

  

System.out.println("All strings in the new list: ");

printMyList(LL);

System.out.println();

  

System.out.println("All strings in the previous list: ");

printMyList(L);

System.out.println();

  

System.out.println("Insert a string in a position of the new list:");

LL = insertAStringIntoMyList(LL, "Hello world", 3);

printMyList(LL);

System.out.println();

  

b = isListInOrder(L);

System.out.println("List is ordered: "+b);

System.out.println();

  

LL=insertAStringIntoMyList(LL,"ABBA", 3);

LL=insertAStringIntoMyList(LL,"DoGeeseSeeGod", 3);

  

int c = countPalindromes(LL);

System.out.println("Found "+c+" palindromes");

}

  

}

Sample Output

Remove a given String All strings in the new list: 5 J İGYAD 1LNGL All strings in the previous list: 53 1GYAD 1LNGL Insert a

Add a comment
Know the answer?
Add Answer to:
Java language Any use of java.util.LinkedList is prohibited Objective: The goal of this assignment is to...
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
  • Java, can you help me out thanks. CSCI 2120 Introduction For this assignment you will implement...

    Java, can you help me out thanks. CSCI 2120 Introduction For this assignment you will implement two recursive methods and write JUnit tests for each one. You may write all three methods in the same class file. You are required to write Javadoc-style documentation for all of your methods, including the test methods. Procedure 1) Write method!! a recursive method to compare two Strings using alphabetical order as the natural order (case insensitive, DO NOT use the String class built-in...

  • JAVA Recursion: For this assignment, you will be working with various methods to manipulate strings using...

    JAVA Recursion: For this assignment, you will be working with various methods to manipulate strings using recursion. The method signatures are included in the starter code below, with a more detailed explanation of what function the method should perform. You will be writing the following methods: stringClean() palindromeChecker() reverseString() totalWord() permutation() You will be using tools in the String class like .substring(), .charAt(), and .length() in all of these methods, so be careful with indices. If you get stuck, think...

  • Need the answers to these RECURSIVE practice problems using Linked Lists in JAVA . Thank you. Complete the Link cl...

    Need the answers to these RECURSIVE practice problems using Linked Lists in JAVA . Thank you. Complete the Link class by writing methods described below. Do not use loops, or create any more methods (other than those specified), class or instance variables. public class Link private Link next; /null if this is the last link private int value; public Link(Link n, int v) nextn valuev; Do not use loops, or create any more methods (other than those specified), class or...

  • PLEASE WRITE IN JAVA AND ADD COMMENTS TO EXPLAIN write a class NameCard.java which has •Data...

    PLEASE WRITE IN JAVA AND ADD COMMENTS TO EXPLAIN write a class NameCard.java which has •Data fields: name (String), age(int), company(String) •Methods: •Public String getName(); •Public int getAge(); •Public String getCom(); •Public void setName(String n); •Public void setAge(int a); •Public void setCom(String c); •toString(): \\ can be used to output information on this name card 2, write a class DoublyNameCardList.java, which is a doubly linked list and each node of the list a name card. In the DoublyNameCardList.java: •Data field:...

  • A java program for this question please! Recursion: A word is considered elfish if it contains...

    A java program for this question please! Recursion: A word is considered elfish if it contains the letters: e, l, and f in it, in any order. For example, we would say that the following words are elfish: whiteleaf, tasteful, unfriendly, and waffles, because they each contain those letters. Write a recursive method called elfish(), that, given a word, tells us whether or not that word is elfish. The signature of the method should be: public static boolean elfish(String word)...

  • Programming Assignment #7 (Recursion) This assignment is to write some methods that perform simple array operations...

    Programming Assignment #7 (Recursion) This assignment is to write some methods that perform simple array operations recursively. Specifically, you will write the bodies for the recursive methods of the ArrayRecursion class, available on the class web page. No credit will be given if any changes are made to ArrayRecursion.java, other than completing the method bodies Note that the public methods of ArrayRecursion – contains(), getIndexOfSmallest(), and sort() – cannot be recursive because they have no parameters. Each of these methods...

  • Assignment Description. In this assignment, you will design a class to represent a book of recipes...

    Assignment Description. In this assignment, you will design a class to represent a book of recipes using a doubly linked list. Your recipe book will support methods to add a recipe and get a recipe at a position; your main will use these to print all the names of all the recipes in the book. Create a C++ class called RecipeBook. Your class must implement the following variables, constructors, and methods: Create a private inner class called Node for implementing...

  • Study the recursive instance method length()of MyLinkedList2 class and its associated auxiliary method. Then solve (a),...

    Study the recursive instance method length()of MyLinkedList2 class and its associated auxiliary method. Then solve (a), (b), and (c) below in a similar way: (a)   Convert the method public Element find(Object obj)of MyLinkedList2 class to a recursive one. Use an auxiliary private method. (b)   Convert the method public String toString( )of MyLinkedList2 class to a recursive one such that it returns the string representation of a linked list in the form: {ValueDatum1  valueDatum2 valueDatum3  . . .  valueDatumN-1   valueDatumN} Use an auxiliary private method....

  • Here is the IntegerLinkedList_incomplete class: public class IntegerLinkedList { static class Node { /** The element...

    Here is the IntegerLinkedList_incomplete class: public class IntegerLinkedList { static class Node { /** The element stored at this node */ private int element; // reference to the element stored at this node /** A reference to the subsequent node in the list */ private Node next; // reference to the subsequent node in the list /** * Creates a node with the given element and next node. * * @param e the element to be stored * @param n...

  • In java Write a method public void printReverse() that prints the elements of a doubly linked...

    In java Write a method public void printReverse() that prints the elements of a doubly linked list in reverse. Write a method public void delete5FromTheEnd() which deletes the 5th element from end of the list. Note that if you reach the end then you have to reverse the direction of counting. In the main() method of the test class, create a randomly generated Doubly-Linked list of 10 Integers. Next, call the delete5FromTheEnd() method and print the lists iteratively until the...

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