• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Jj Roberts
  • Al Hobbs
  • Piet Souris

How to call methods from an Integer class to the main driver class

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
Marshal
Posts: 74341
334
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well indented code Well, at least part of it is indented correctly.
That spares you one of the problems we sometimes see; I can read badly indented code and not know which part is which. But please use spaces rather than tabs. Get a decent text editor, e.g. for Windows®, “Notepad++”, and enable the options for automatic indentation and automatic conversion tab→4 spaces.

Aimex Ai wrote:. . . integers

Do you mean integer or Integer? The two are different.

. . . Also is my implementation correct

No.
I am afraid that, without even looking at your algorithm, I can see all sorts of problems. Let's start with some style things.
  • Make sure that all ClassNames start with CapitalLetters.
  • Make sure all methodNames and variablleNames start with lowerCaseLetters.
  • Don't use_underscores (except in CONSTANT_NAMES).
  • Those conventions make it impossible to confuse ClassNames and variableNames, and make it easier for people trying to help to read your code.

    . . . is there any changes I should do to incrementlist method.

    Careful about spelling; Java® is a case‑sensitive language and almost everywhere the difference between i and I is significant. We see all sorts of subtle errors caused by forgetting such spellings.
    * * * * * * * * * * * * * * * * * * * *
    I am afraid I can see some design problems, and the name of your class probably hides a design problem. The names of classes should usually be singular nouns. And increment (noun) means the amount something else has increased by. So, even without the _ style thing, you have a confusing name. It tells me there is an increase, but not what has increased. Can you think of a better name, in English or any other language? I thought of CircularListWithIncrement, but that reads strangely. If I can't think of a good name for the class, I start wondering why the class is there at all. Is it appropriate to create a subtype of your List at all?
    Why have you got a no‑arguments constructor? What does it do? I have not read the code for the superclass. Maybe that constructor creates an empty List, in which case it is correct, but often no‑arguments constructors permit creation of an incomplete object.
    Why have you attributed the superclass a formal type parameter <Integer>? If you want to make your current type generic, you would declare it something like this: class XYZ<E> extends ABC<E>. If you always want to store Integers or ints in your list, there is no need for any type parameters. If you want to store any type in the list, then you have problems with the increment method because you can only add an Integer to an int, or to another Integer by unboxing conversion. So the subtype only fulfils the “IS‑A” relationship with the supertype if the supertype only takes Integers.
    You are using the variable name current. That suggests you have a non‑private field in the superclass, which isn't good design. It is often possible to gain access to a superclass' data via a getXXX() method, but that won't work because it will allow access to the node from outside code. I think my conclusion is that you shouldn't subclass your list class, and also I think you should hide your node by making it a nested class.Give the node all the methods it had before, with private access, and use them only in the list class. Yes, that will compile normally. Put the increment method in the list class.

    I think the algorithm for incrementing every element is correct, but the method name should read incrementEveryElement(...) or similar.
     
    Saloon Keeper
    Posts: 13366
    295
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    The problem is likely that your "driver class" uses a reference of type MyCircularLinkedList<Integer>, and not a reference of type MyCircular_Increment. That means the method won't be visible to that class. Change the type of the variable it is using.

    There are several issues with your code:

  • Class names should be named using a noun, not a verb.
  • Don't use underscores in class or method names.
  • Why prefix your class names with "My"? Are there other circularly linked lists in your code base?
  • Classes should be final unless you explicitly designed them to be extensible.
  • Method and variable names should start with a lowercase letter.
  • Indent your code properly.
  • Prefer collections over arrays.
  • Use accurate names. You can't increment a list. You increment its elements.
  • Don't use Integer where int will suffice.

  • Let's have a look at the code after fixing this:

    I would say your implementation is correct, as long as current != null is really just a check to see if the list is empty. If it is, I'd prefer it if you had added a method isEmpty() to the CircularLinkedList class.  I also don't like that the fields CircularLinkedList.current, Node.element and Node.next are not private.

    Performing a transformation on each element in the collection is a common operation. Instead of writing a subclass to do one specific operation for a circularly linked list of integers, I would add a replaceAll() operation to the CircularLinkedList class that applies a mapping function to each element:

    Instead of creating a subclass specially for integers, you can create a static utility class that contains useful operations on various types of circularly linked lists:
     
    Campbell Ritchie
    Marshal
    Posts: 74341
    334
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Has somebody given me a cow? Thank you whoever it was.
     
    reply
      Bookmark Topic Watch Topic
    • New Topic