In: Computer Science
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());
}
}
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