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;...
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 -...
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...
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()...
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)...
[Java] I'm not sure how to implement the code. Please check my code at the bottom....
[Java] I'm not sure how to implement the code. Please check my code at the bottom. In this problem you will write several static methods to work with arrays and ArrayLists. Remember that a static method does not work on the instance variables of the class. All the data needed is provided in the parameters. Call the class Util. Notice how the methods are invoked in UtilTester. public static int min(int[] array) gets the minimum value in the array public...
Java, I get a index out of bounds exception with this method, can you spot where...
Java, I get a index out of bounds exception with this method, can you spot where the method goes out of bounds from this code? public void showEarliestAttack(){ String earliestAttack; for (int i = 0; i < attackArray.size(); i++) { earliestAttack= attackArray.get(i).toString(); if(attackArray.get(i+1).getDate().compareTo(attackArray.get(i).getDate())<0){ earliestAttack = attackArray.get(i+1).toString(); } System.out.println(earliestAttack); } }
How can I alter this Java method so that I convert and then return every sentence...
How can I alter this Java method so that I convert and then return every sentence in the array of strings instead of just the first sentence? public static char[] charConvert ( String [] sentences ) { int totLength = sentences[0].length() + sentences[1].length() + sentences[2].length() + sentences[3].length() + sentences[4].length(); char [] letter = new char[totLength]; int x = 0; int y = 0;    while ( y < sentences[x].length() ) { switch ( sentences[x].charAt(y) ) { case 'a': case 'A':...
I am a beginner when it comes to java codeing. Is there anyway this code can...
I am a beginner when it comes to java codeing. Is there anyway this code can be simplified for someone who isn't as advanced in coding? public class Stock { //fields private String name; private String symbol; private double price; //3 args constructor public Stock(String name, String symbol, double price) { this.name = name; this.symbol = symbol; setPrice(price); } //all getters and setters /** * * @return stock name */ public String getName() { return name; } /** * set...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT