I get an error when im trying to run this java program, I would appreciate if...

80.2K

Verified Solution

Question

Programming

I get an error when im trying to run this java program, I wouldappreciate if someone helped me asap, I will make sure to leave agood review. thank you in advance!

java class Node

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;
}
  
}

java class LinkedString

public class LinkedString {
Node head;
int length;
  
public LinkedString(char[] characters) {

for(int i=0;iNode 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;iNode 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 = 1; icurr = 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;
}   

}

java class test

public class Test {


public static void main(String[] args) {
char array[]= {'m','a','h','d','i'};
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(\"\nlinkedString Object 2: \");
LinkedString LS2=new LinkedString(\"mazloumi\");
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(\"\nConcatenated 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(\"\nReplaced String: \");
LinkedString ls4=LS3.replace('a', 'o');
System.out.println(ls4.toString());
}

}

Answer & Explanation Solved by verified expert
4.1 Ratings (652 Votes)
class Node private char item private Node next Object getNext public Node item next null public Nodechar newItem setItemnewItem next null public Nodechar newItem Node newNext setItemnewItem setNextnewNext public void setItemchar newItem item newItem public void setNextNode newNext next newNext public char getItem return item public Node getNext return next class LinkedString Node head int length public LinkedStringchar characters for int i 0 i characterslength i Node newNode new Nodecharactersi if head    See Answer
Get Answers to Unlimited Questions

Join us to gain access to millions of questions and expert answers. Enjoy exclusive benefits tailored just for you!

Membership Benefits:
  • Unlimited Question Access with detailed Answers
  • Zin AI - 3 Million Words
  • 10 Dall-E 3 Images
  • 20 Plot Generations
  • Conversation with Dialogue Memory
  • No Ads, Ever!
  • Access to Our Best AI Platform: Flex AI - Your personal assistant for all your inquiries!
Become a Member

Other questions asked by students