Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am working on a circular linked list and I tried to implement an increment method, to do that I created another class that will extend my
circular class of integers as shown in the code. I implemented Increment list method and I have been trying to call it on my driver class but the method is not showing up what should I add into my driver class so the methods in MyCircular_Increment can be called. Also is my implementation correct, is there any changes I should do to incrementlist method.

What I have tried:

Java
public class MyCircular_Increment extends MyCircularLinkedList<integer> {
     
    public MyCircular_Increment() {
        super();
    }
             
    public MyCircular_Increment(Integer[] objects) {
         super(objects);
    }
 
    public void IncrementList(Integer Value)
    {
        Node<integer>
        temp = current;
     
        if (current != null) {
            do {
                temp.element += Value;
                temp = temp.next;
            } while (temp != current);
        }
    }
}



// main class
public class TestMyCircularLinkedList {
	/* Main method */
	public static void main(String[] args) {
		// Create a list for strings
		 Pair 	p1 = new Pair(1,1),     p40 = new Pair(40,40),
				p5 = new Pair(5,5),   p10 = new Pair(10,10),  
              p50 = new Pair(50,50);

		 Pair[] plist1 = new Pair[] {p5, p40, p5, p10, p1, p50, p40, p10, p40, p10}; 
		
		 MyCircularLinkedList<pair> list1 = new MyCircularLinkedList<>(plist1);

         list1.IncrementList(p10);
		 System.out.println("List1 = " + list1);

    }
}
Posted
Updated 17-Nov-21 22:45pm
v3
Comments
phil.o 18-Nov-21 3:25am    
What driver class?
Aimex-oss 18-Nov-21 3:27am    
I meant the main class
phil.o 18-Nov-21 3:29am    
You mean that which you did not show? :)
Aimex-oss 18-Nov-21 3:47am    
My apologies I added the main class, I am trying to call the increment method as shown but it giving me an error

You need to study the concept of inheritance in more detail. A method defined on MyCircular_Increment cannot be called on an instance of MyCircularLinkedList<>.

What Is Inheritance? (The Java™ Tutorials > Learning the Java Language > Object-Oriented Programming Concepts)[^]
 
Share this answer
 
Look at your code:
Java
public class MyCircular_Increment extends MyCircularLinkedList<integer> {
... 
    public void IncrementList(Integer Value)
    {
...
    }
}
		// Create a list for strings
		 Pair 	...,
				...,   p10 = new Pair(10,10),  
                ...;
		 Pair[] plist1 = new Pair[] {p5, p40, p5, p10, p1, p50, p40, p10, p40, p10}; 
		 MyCircularLinkedList<pair> list1 = new MyCircularLinkedList<>(plist1);
         list1.IncrementList(p10);

You don't declare a method called IncrementList that accepts a Pair as a parameter - just an Integer. Since Pair does not inherit from Integer - it's final, nothing can inherit from it - there is no "is a" relationship between the parameter the method expects and the parameter is it called with.
So the system can't find a suitable method to call and - rightly - complains.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900