Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 725071
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T06:17:43+00:00 2026-05-14T06:17:43+00:00

To be honest I’m not quite sure if I understand the task myself :)

  • 0

To be honest I’m not quite sure if I understand the task myself 🙂 I was told to create class MySimpleIt, that implements Iterator and Iterable and will allow to run the provided test code. Arguments and variables of objects cannot be either Collections or arrays.
The code :

 MySimpleIt msi=new MySimple(10,100,
                           MySimpleIt.PRIME_NUMBERS);

 for(int el: msi)
   System.out.print(el+" ");    
 System.out.println();

 msi.setType(MySimpleIterator.ODD_NUMBERS);
 msi.setLimits(15,30);
 for(int el: msi)
   System.out.print(el+" ");    
 System.out.println();

 msi.setType(MySimpleIterator.EVEN_NUMBERS);
 for(int el: msi)
   System.out.print(el+" ");    
 System.out.println();

The result I should obtain :

11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 


15 17 19 21 23 25 27 29 


16 18 20 22 24 26 28 30

And here’s my code :

import java.util.Iterator; 
interface MySimpleIterator{
    static int ODD_NUMBERS=0;
    static int EVEN_NUMBERS = 1;
    static int PRIME_NUMBERS = 2;

    int setType(int i);
}

public class MySimpleIt implements Iterable, Iterator, MySimpleIterator {    
    public MySimple my;

    public MySimpleIt(MySimple m){ 
        my = m;      
    }

    public int setType(int i){
        my.numbers = i;
        return my.numbers;
    }

    public void setLimits(int d, int u){
        my.down = d;
        my.up = u;
    }

    public Iterator iterator(){
        Iterator it = this.iterator();
        return it;
    }

    public void remove(){  
    }

    public Object next(){
        Object o = new Object();
        return o;
    }

    public boolean hasNext(){
        return true;
    }

}

class MySimple {
    public int down;
    public int up;
    public int numbers;

    public MySimple(int d, int u, int n){
        down = d;
        up = u;
        numbers = n;
    }
}

In the test code I have error in line when creating MySimpleIt msi object, as it finds MySimple instead of MySimpleIt. Also I have errors in for-each loops, because compiler wants ‘ints’ there instead of Object. Anyone has any idea on how to solve it ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T06:17:44+00:00Added an answer on May 14, 2026 at 6:17 am

    There are plenty wrong with the design of this assignment.

    Use enum

    The test code contains this snippets:

    MySimpleIt(erator?).PRIME_NUMBERS
    MySimpleIt(erator?).ODD_NUMBERS
    MySimpleIt(erator?).EVEN_NUMBERS
    

    In one place the type is MySimpleIt, in another it’s MySimpleIterator. Either way, the name suggests using an interface to define a bunch of constants. THIS IS NOT A PROPER USE OF an interface!!!

    It would be a much better design to use an enum instead:

    enum SequenceType {
      PRIME_NUMBERS, ODD_NUMBERS, EVEN_NUMBERS;
    }
    

    See: Effective Java 2nd Edition Item 30: Use enums instead of int constants.


    Consider multiple implementations of the interface instead of a monolith with setType

    It looks like your sequencer should be able to switch sequence type on a whim. This will result in that class being a huge blob that must know how to generate every type of sequences. It may work okay just for the 3 types given here, but it’s definitely a poor design if you later want to add more types of sequences.

    Consider just having different implementations of the same interface for the different types of sequences. You may want to define an AbstractIntegerSequencer that defines the basic functionality (resetting bounds, answering hasNext(), iterator(), etc), that delegates to an abstract protected int generateNext() for subclasses to @Override. This way, the specifics of the type of sequence to generate is nicely encapsulated to each subclass.

    You can still keep enum SequenceType for a static factory method that instantiates these different subclasses, one for each sequence type, but those sequences themselves probably shouldn’t be able to switch type on a whim.


    Use generics

    Instead of making your type implements Iterator, you should make it implements Iterator<Integer>.

    From JLS 4.8 Raw Types (emphasis theirs):

    The use of raw types is allowed only as a concession to compatibility of legacy code. The use of raw types in code written after the introduction of genericity into the Java programming language is strongly discouraged. It is possible that future versions of the Java programming language will disallow the use of raw types.

    See also Effective Java 2nd Edition Item 32: Don’t use raw type in new code.


    Don’t confuse Iterator<T> with Iterable<T>.

    Let’s say you have something like this:

    IntegerSequencer seq = new PrimeSequencer(0, 10);
    
    for (int i : seq) {
      System.out.println(i);
    } // prints "2", "3", "5", 7"
    
    for (int i : seq) {
      System.out.println(i);
    } // what should it print???
    

    If you make seq implements Iterable<Integer>, Iterator<Integer> and @Override Iterator<Integer> iterator() to return this;, then the second loop wouldn’t print anything, since seq is its own iterator(), and at that point there is no more hasNext() for seq.

    A proper implementation of Iterable<Integer> should be able to generate as many independent Iterator<Integer> as necessary for the user, and such implementation would again print prime numbers between 0 and 10 in the above code.


    Further readings on stackoverflow

    • Iterator performance contract (and use on non-collections)
      • This question asked about PrimeGenerator implements Iterator<Integer> (among other things)
    • Why is Java’s Iterator not an Iterable?
    • What is the Iterable interface used for?
    • Why can’t I iterate over an iterator?
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

To be honest, I do not really know how to name that problem. I'll
I'll be honest, I am not sure of the terms I used in the
I am using Gamelogic class which implements no one (just object to be honest
I am not sure this is even possible to be honest, I am wondering
To be honest Im not sure how to frame this question, but here goes.
To be honest, I'm not sure if this is an issue with my code
I will be honest here - I am pretty sure I am not approaching
I'm working on my first Android app and to be honest I'm not sure
I must be honest and tell you that I am not good at database
Duplicate: Should I default my website to www.foo or not? To be quite honest,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.