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());
}
}
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
Get Answers For Free
Most questions answered within 1 hours.