• Post Reply Bookmark Topic Watch Topic
  • New Topic

Need to implement methods through ArrayList  RSS feed

 
Ryan Gordon
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to write a program that loops through some numbers and will print whether the number is odd, prime, etc. I need to use a method public static ArrayList<String> interate().
I don't know how to use ArrayList to print these values, I need to print the results with 1 number on each line with the specific values it contains like
0 is prime, odd
1 is prime
2 is prime, odd
etc

This is what I have



Thank you for any help
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't create a class called Number because there already is one by that name in the java.lang package. Also you don't have a class representing a number, because it hasn't got any number fields. You actually appear to be writing a utility class. If that is what you want, call it Numbers or Integers, and give it a private constructor to prevent anybody creating any instances.
Don't write if (something) return true; else return false;
Look in the old Sun style guide for what you should write.
There are much better ways to work out whether a number is prime.
Don't have lots of methods for divisibility. Look how much repeated code you have. Have one method taking the divisor as a parameter.
Look very carefully at your isOdd method and see if you can't see anything odd about it.
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do you want the List for? I can't just at the moment see how that would fit with a utility class.
 
Ryan Gordon
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
'm not 100% if I want a utility class or not, this is a homework question, it just asked to loop these methods and display each number on its own line saying if its prime and odd etc.
I have to use those methods for divisibility its a requirement for the homework question. I fixed the odd method i think, I removed the unnecessary brackets if that's what you meant. I think the main point of this assignment is to make use of ArrayList and implement these methods through ArrayList which i haven't done before.
 
Carey Brown
Saloon Keeper
Posts: 3310
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ArrayLists can't hold ints but they can hold Integers. Iterators are almost invisible now with the advent of the enhanced for() loops, but they are still there if you want to use them. First you need to create an Iterator object of the correct type, then use the hasNext() method to see if there's any more data, and finally, use the next() method to retrieve the data.
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ryan Gordon wrote:'m not 100% if I want a utility class or not . . .
In that case you probably want a proper concrete class. Remove all the instances of the keyword static except before the main method.
I have to use those methods for divisibility its a requirement for the homework question. . . .
Which looks to me like being given homework to write poor quality code
I removed the unnecessary brackets if that's what you meant.
No, it isn't. Don't go thinking that brackets are unnecessary or can be removed right left and centre. Sometimes brackets are optional, but no, my suggestion for change had nothing to do with brackets. Did you find that style guide link all right? Or wouldn't it open? Did you even find out what I thought was odd about the method I named?
I think the main point of this assignment is to make use of ArrayList and implement these methods through ArrayList which i haven't done before.
Read the assignment again and see what it says to do with the List. Also have a look at the Java™ Tutorials and see what you can find out about Lists. Note that if you create MyInteger instances or whatever the class is called, you can put them into your List.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!