Question

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 letters of them. ill make sure to leave a positive feedback, thank you in adavance!!

Node.java

public class Node {

private char item;

private Node next;

Object getNext;

public Node(){

  

item = ' ';

next = null;

}

public Node(char newItem) {

setItem(newItem);

next = null;

}

public Node(char newItem, Node newNext){

setItem(newItem);

setNext(newNext);

}

public void setItem(char newItem){

item = newItem;

}

public void setNext(Node newNext){

next = newNext;

}

public char getItem(){

return item;

}

public Node getNext(){

return next;

}

  

}

LinkedString.java

public class LinkedString {

   Node head;

   int length;

  

   public LinkedString(char[] characters) {

   for(int i=0;i<characters.length;i++) {

   Node newNode = new Node(characters[i]);

   if(head==null) {

   head = newNode;

   }

   else

   {

   Node curr=head;

   while(curr.getNext!=null)

   {

   curr=curr.getNext();

   }

   curr = newNode;

   }

   }

   length=characters.length;

   }

   public LinkedString(String str) {

   for(int i=0;i<str.length();i++) {

   Node newNode = new Node(str.charAt(i));

   if(head==null) {

   head = newNode;

   }

else

   {

   Node curr=head;

   while(curr.getNext!=null)

   {

   curr=curr.getNext();

   }

   curr = newNode;

   }

   }

   length=str.length();

   }

  

public char charAt(int index)

   {

Node curr;

curr = head;

if (index == 0){

return curr.getItem();

}else if(index > 0 && index < length){

for (int i = 0; i<index; i++){

if(curr!=null)

curr = curr.getNext();

}

}

   return 0;

   }

  

public LinkedString concat(LinkedString str)

   {

   if(isEmpty())

   {

   length=str.length();

   return str;

   }

   Node curr=head;

   while(curr.getNext!=null)

   {

   curr = curr.getNext();

   }

   curr.setNext(str.head);

   length=length+str.length();

   return this;

   }

  

   public boolean isEmpty() {

   return length==0;

   }

  

   public int length() {

   return length;

   }

  

   public LinkedString replace(char oldChar,char newChar) {

   Node curr = head;

   while(curr!=null) {

   if(curr.getItem()==oldChar)

   curr.setItem(newChar);

   curr = curr.getNext();

   }

  

   return this;

   }

  

public String toString() {

Node curr = head;

String ans = "";

while(curr!=null) {

   ans+=curr.getItem();

   curr = curr.getNext();

}

return ans;

}

}

Test.java

public class Test {

public static void main(String[] args) {

char array[]= {'h','e','l','l','o',' ','w','o','r','l','d','!'};

   System.out.println("String 1: ");

   LinkedString LS1=new LinkedString(array);

   System.out.println("isEmpty: "+LS1.isEmpty());

   System.out.println("Length :"+LS1.length());

   System.out.println("String form: "+LS1.toString());

   System.out.println("Character At index 3: "+LS1.charAt(3));

  

   System.out.println(" String 2: ");

   LinkedString LS2=new LinkedString("Hey Noob");

   System.out.println("isEmpty: "+LS2.isEmpty());

   System.out.println("Length :"+LS2.length());

   System.out.println("String form: "+LS2.toString());

   System.out.println("Character At index 5: "+ LS2.charAt(5));

  

  

   System.out.println(" Concatenated linked String: ");

   LinkedString LS3=LS1.concat(LS2);

   System.out.println("isEmpty: "+LS3.isEmpty());

   System.out.println("Length :"+LS3.length());

   System.out.println("String form: "+LS3.toString());

   System.out.println("Character At index 10: "+LS3.charAt(10));

  

   System.out.println(" Replaced String: ");

   LinkedString ls4=LS3.replace('o', 'e');

   System.out.println(ls4.toString());

   }

}

Homework Answers

Answer #1

Modified Java code

public class Node {

    private char item;

    private Node next;

    public Node() {
        item = ' ';
        next = null;

    }

    public Node(char newItem) {

        setItem(newItem);

        next = null;

    }

    public Node(char newItem, Node newNext) {

        setItem(newItem);

        setNext(newNext);

    }

    public void setItem(char newItem) {

        item = newItem;

    }

    public void setNext(Node newNext) {

        next = newNext;

    }

    public char getItem() {

        return item;

    }

    public Node getNext() {

        return next;

    }

}

// LinkedString.java

public class LinkedString {

    Node head;

    int length;

    public LinkedString(char[] characters) {

        for (int i = 0; i < characters.length; i++) {

            Node newNode = new Node(characters[i]);

            if (head == null) {

                head = newNode;

            }

            else

            {

                Node curr = head;

                while (curr.getNext() != null)

                {

                    curr = curr.getNext();

                }

                curr.setNext(newNode);

            }

        }

        length = characters.length;

    }

    public LinkedString(String str) {

        for (int i = 0; i < str.length(); i++) {

            Node newNode = new Node(str.charAt(i));

            if (head == null) {

                head = newNode;

            }

            else

            {

                Node curr = head;

                while (curr.getNext() != null)

                {

                    curr = curr.getNext();

                }

                curr.setNext(newNode);

            }

        }

        length = str.length();

    }

    public char charAt(int index)

    {

        Node curr;

        curr = head;

        if (index == 0) {

            return curr.getItem();

        } else if (index > 0 && index < length) {

            for (int i = 0; i < index; i++) {

                if (curr != null)

                    curr = curr.getNext();

            }
            return curr.getItem();
        }

        return 0;

    }

    public LinkedString concat(LinkedString str)

    {

        if (isEmpty())

        {

            length = str.length();

            return str;

        }

        Node curr = head;

        while (curr.getNext() != null)

        {

            curr = curr.getNext();

        }

        curr.setNext(str.head);

        length = length + str.length();

        return this;

    }

    public boolean isEmpty() {

        return length == 0;

    }

    public int length() {

        return length;

    }

    public LinkedString replace(char oldChar, char newChar) {

        Node curr = head;

        while (curr != null) {

            if (curr.getItem() == oldChar)

                curr.setItem(newChar);

            curr = curr.getNext();

        }

        return this;

    }

    public String toString() {

        Node curr = head;

        String ans = "";

        while (curr != null) {

            ans += curr.getItem();

            curr = curr.getNext();

        }

        return ans;

    }

}

// Test.java

public class Test {

    public static void main(String[] args) {

        char array[] = { 'h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd', '!' };

        System.out.println("String 1: ");

        LinkedString LS1 = new LinkedString(array);

        System.out.println(LS1);

        System.out.println("isEmpty: " + LS1.isEmpty());

        System.out.println("Length :" + LS1.length());

        System.out.println("String form: " + LS1.toString());

        System.out.println("Character At index 3: " + LS1.charAt(3));

        System.out.println(" String 2: ");

        LinkedString LS2 = new LinkedString("Hey Noob");

        System.out.println(LS2);

        System.out.println("isEmpty: " + LS2.isEmpty());

        System.out.println("Length :" + LS2.length());

        System.out.println("String form: " + LS2.toString());

        System.out.println("Character At index 5: " + LS2.charAt(5));

        System.out.println(" Concatenated linked String: ");

        LinkedString LS3 = LS1.concat(LS2);

        System.out.println(LS3);

        System.out.println("isEmpty: " + LS3.isEmpty());

        System.out.println("Length :" + LS3.length());

        System.out.println("String form: " + LS3.toString());

        System.out.println("Character At index 10: " + LS3.charAt(10));

        System.out.println(" Replaced String: ");

        LinkedString ls4 = LS3.replace('o', 'e');

        System.out.println(ls4);

    }

}


sample output

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
Java Language Add a method (convertToRing ()) that converts the list to a circular. This is,...
Java Language Add a method (convertToRing ()) that converts the list to a circular. This is, which makes the last node instead of pointing to null point to the first. Code: class Node { int value; Node nextNode; Node(int v, Node n) { value = v; nextNode = n; } Node (int v) { this(v,null); } } class LinkedList { Node head; //head = null; LinkedList() { } int length() { Node tempPtr; int result = 0; tempPtr = head;...
Java Language Add a method (convertToRing ()) that converts the list to a circular. This is,...
Java Language Add a method (convertToRing ()) that converts the list to a circular. This is, which makes the last node instead of pointing to null point to the first. Code: class Node { int value; Node nextNode; Node(int v, Node n) { value = v; nextNode = n; } Node (int v) { this(v,null); } } class LinkedList { Node head; //head = null; LinkedList() { } int length() { Node tempPtr; int result = 0; tempPtr = head;...
Please explain code 1 and code 2 for each lines code 1 public class MyQueue {...
Please explain code 1 and code 2 for each lines code 1 public class MyQueue {    public static final int DEFAULT_SIZE = 10;    private Object data[];    private int index; code 2 package test; import java.util.*; /* Class Node */ class Node { protected Object data; protected Node link; /* Constructor */ public Node() { link = null; data = 0; } /* Constructor */ public Node(Object d,Node n) { data = d; link = n; } /*...
(JAVA) I have "cannot be resolved or is not a field" error on node.right = y;...
(JAVA) I have "cannot be resolved or is not a field" error on node.right = y; and node.left = x; //BuildExpressionTree.java import java.util.*; import javax.swing.*; import javax.xml.soap.Node; public class BuildExpressionTree {    private Stack stack = new Stack();    private Node node;       public static boolean isOperator(String token){        if(token == "+" || token == "-" || token == "*" || token == "/"){            return true;        }        JOptionPane.showMessageDialog(null, "Invalid token" +...
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()...
In this code, I build a single-linked list using a node class that has been created....
In this code, I build a single-linked list using a node class that has been created. How could I change this code to take data of type T, rather than int. (PS: ignore the fact that IOHelper.getInt won't work for the type T... ie second half of main). Here's my code right now: public class SLList { public SLNode head = null; public SLNode tail = null; public void add(int a) {// add() method present for testing purposes SLNode newNode...
Code in JAVA The requirements are as follows: The input will be in a text file...
Code in JAVA The requirements are as follows: The input will be in a text file whose name is given by arg[0] of main(). It will contain a fully-parenthesized infix expression containing only: "(", ")", "+", "-" and integers. Need help on the main and fixing the Queue. //Input: ( ( 1 + 2 ) - ( ( 3 - 4 ) + ( 7 - 2 ) ) ) ( ( 1 + 2 ) - ( 3 -...
THIS IS FOR JAVA I have to write a method for a game of Hangman. The...
THIS IS FOR JAVA I have to write a method for a game of Hangman. The word the user is trying to guess is made up of hashtags like so " ###### " If the user guesses a letter correctly then that letter is revealed on the hashtags like so "##e##e##" If the user guesses incorrectly then it increments an int variable named count " ++count; " String guessWord(String guess,String word, String pound) In this method, you compare the character(letter)...
public class PalindromeChecker { /** * Method that checks if a phrase or word is *...
public class PalindromeChecker { /** * Method that checks if a phrase or word is * a Palindrome * * @param str * Represents a string input * * @return true * True if the string is a Palindrome, * false otherwise */ public static boolean isPalindrome(String str) { if (str == null) { return false; } str = str.toLowerCase(); String temp = ""; for (int i = 0; i < str.length(); i++) { char ch = str.charAt(i); if ((ch...
You must alter the Queue class you created in L5 to make it a CIRCULAR Queue...
You must alter the Queue class you created in L5 to make it a CIRCULAR Queue class . Call your class Queue. it must be a template class. public class Queue { } I have put a driver program in the module . It is called CircularQueue.java This driver program should then run with your Queue class (no modifications allowed to the driver program). Your Queue class should have at least the following methods: one or more constructors, enqueue, dequeue,...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT