Question

In: Computer Science

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

I get an error when im trying to run this java program, I would appreciate if someone helped me asap, I will make sure to leave a good 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;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 = 1; i<index; i++){
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;
}   

}

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

}

Solutions

Expert Solution

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

}

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 = 1; i < index; i++) {
                                //added null check before caling getNext()
                                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;
        }

}

public class TestLin {

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

}

Note : Please comment below if you have concerns. I am here to help you

If you like my answer please rate and help me it is very Imp for me


Related Solutions

BSTree.java:99: error: reached end of file while parsing } I get this error when i run...
BSTree.java:99: error: reached end of file while parsing } I get this error when i run this code. can someone help me out? I can't figure out how to make this work. public class BSTree<T extends Comparable<T>> { private BSTreeNode<T> root = null; // TODO: Write an addElement method that inserts generic Nodes into // the generic tree. You will need to use a Comparable Object public boolean isEmpty(){ return root == null; } public int size(){ return node;} public...
Im trying to get a GUI interface in java where there is four text fields for...
Im trying to get a GUI interface in java where there is four text fields for the first name, last name, department, and phone number of employee in a program. Then also have a radio button below for Gender (Male/Female/Other) and a list for the Title (Mr./Ms./Mrs./Dr./Col./Prof.). At the very bottom of the frame there has to be buttons for printing, submitting and exiting but for whatever reason when I tried it nothing appears in the frame regardless of what...
This is in Python I am getting an error when I run this program, also I...
This is in Python I am getting an error when I run this program, also I cannot get any output. Please help! #Input Section def main(): name=input("Please enter the customer's name:") age=int(input("Enter age of the customer: ")) number_of_traffic_violations=int(input("Enter the number of traffic violations: ")) if age <=15 and age >= 105: print('Invalid Entry') if number_of_traffic_violations <0: print('Invalid Entry') #Poccessing Section def Insurance(): if age < 25 and number_of_tickets >= 4 and riskCode == 1: insurancePrice = 480 elif age >=...
when i run the program on eclipse it gives me this error: Exception in thread "main"...
when i run the program on eclipse it gives me this error: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 0 out of bounds for length 0    at SIM.main(SIM.java:12) how do I fix that ? (please fix it ) import java.util.*; import java.util.Scanner; import java.util.ArrayList; import java.io.File; import java.io.FileNotFoundException; import java.lang.Math; public class SIM{    public static void main(String[] args) throws FileNotFoundException {       int cacheSize = Integer.parseInt( args[1] ); int assoc = Integer.parseInt( args[2] ); int replacement = Integer.parseInt(...
I need this code translated from C++ to Java. Im personally still trying to learn Java,...
I need this code translated from C++ to Java. Im personally still trying to learn Java, so if you can include screenshots of your IDE/output that would be helpful. Much appreciated! #include <iostream> #include <string> using namespace std; class pizza { public:    string ingrediants, address;    pizza *next;    pizza(string ingrediants, string address)    {        this->address = address;        this->ingrediants = ingrediants;        next = NULL;    } }; void enqueue(pizza **head, pizza **tail, pizza...
I am writing a shell program in C++, to run this program I would run it...
I am writing a shell program in C++, to run this program I would run it in terminal like the following: ./a.out "command1" "command2" using the execv function how to execute command 1 and 2 if they are stored in argv[1] and argv[2] of the main function?
Im trying to run Nmap vulnerability scrip on Kali to my Metasploitable VM. Every time I...
Im trying to run Nmap vulnerability scrip on Kali to my Metasploitable VM. Every time I use : Nmap --script vulnerability (IP address) I get back following response: |_clamav-execl: error: Script execution failed (use -d to debug) Trying to solve this issue please help. Ps: I already updated my Nmap and still gives the same issue.
HI. I have been trying to run my code but I keep getting the following error....
HI. I have been trying to run my code but I keep getting the following error. I can't figure out what I'm doing wrong. I also tried to use else if to run the area of the other shapes but it gave me an error and I created the private method. Exception in thread "main" java.util.InputMismatchException at java.base/java.util.Scanner.throwFor(Scanner.java:939) at java.base/java.util.Scanner.next(Scanner.java:1594) at java.base/java.util.Scanner.nextInt(Scanner.java:2258) at java.base/java.util.Scanner.nextInt(Scanner.java:2212) at project2.areacalculation.main(areacalculation.java:26) My code is below package project2; import java.util.Scanner; public class areacalculation { private static...
Whenever I am attempting to write a simple program on C++ I get an error message...
Whenever I am attempting to write a simple program on C++ I get an error message that reads "cout was not declared in this scope". Literally every time. This has become frustrating because I have even written my code the exact same way as some of my classmates who got theirs to compile and run with no sign of this error at all, and yet min gives this answer. I will leave an example of a code where this error...
I am trying to write a java program that determines if an inputted year is a...
I am trying to write a java program that determines if an inputted year is a leap year or not, but I am not able to use if else statements how would I do it. I don't need the code just advice.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT