Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

taking extracted numbers and testing them for primness  RSS feed

 
Natasha Sherrod
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi! I have hit a wall for an assignment I have. I have to take a csv file and extract all the numbers, print 5 numbers per line. I have figured that out! I now need to go through those numbers and test if they are prime or not and if they are prime I need to print them out. I know how to test if they are prime; however, I'm just not sure how to go through each number. Do I need to put the extracted numbers in an array and how would I go about doing that. I wanted to just do a nested loop but I'm not sure how to move on to the next number. I tried m1++  Does anyone have any advice or tips?

Here is my code for extracting the numbers and printing them.



Here is my code for finding out if a number is prime

 
Natasha Sherrod
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can I use parseInt in some way

maybe



or will I have to create a method?

 
Piet Souris
Rancher
Posts: 1980
67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Caroline,

what I would advise is to  separate the processes of reading your numbers and displaying them. Reading and storing is most important, you could use an array or better a list.
Having your numbers in an easy to manipulate data structure,  it is next also easy to go through the  data, applying some filter (like prime) and store these as well. Lastly  there is the printing. 
Can you come up with a method that takes an array or list, and a variable that gives the numbers per line? You have already most of that code working.
 
Natasha Sherrod
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Piet Souris wrote:Hi Caroline,

what I would advise is to  separate the processes of reading your numbers and displaying them. Reading and storing is most important, you could use an array or better a list.
Having your numbers in an easy to manipulate data structure,  it is next also easy to go through the  data, applying some filter (like prime) and store these as well. Lastly  there is the printing. 
Can you come up with a method that takes an array or list, and a variable that gives the numbers per line? You have already most of that code working.


How would I go about adding them to a list or array while I am extracting the numbers? I tried adding them to an array first then testing for numbers but I couldn't get it to work. I was working on creating a method for testing is the numbers are prime but I still can't figure out how to assign the numbers a variable to test them....
 
Piet Souris
Rancher
Posts: 1980
67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, you could have a method 'readNumbers', in which you read in the numbers. In that method, create a List or an array large enough. If you find a genuine number in your input (using nextInt/Double or Integer.parseInt, pick one that suits you), store it into your list or array. And when you're done, you will have stored all the input. You can send that list as a return value, or create an instance variable for that purpose.

As said, having such a data structure enables you to perform filtering and printing in a fairly easy way.

 
Natasha Sherrod
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Piet Souris wrote: create a List or an array large enough.


I know I need to create an array or an array list but I'm not sure how to do that using regrex group. can I do count ++ for the group because right now I have m1.group(0).

 
Junilu Lacar
Sheriff
Posts: 11145
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you learned about functional decomposition yet?  Have you learned about methods in Java yet? 

You're experiencing the effects of what's called "tight coupling" in your programs due to a lack of functional decomposition. There are large chunks of functionality that are jammed into the main() method of each program. You have user interaction, display, input/output mixed in as well. All these are tightly bound or coupled together and it's hard for you to see how to combine some of the functionality that exists partly in one program and partly in the other because this tight coupling with other tasks that they are mixed in with. Functional decomposition is how you loosen up the coupling.

The ideal way out of the corner that you've painted yourself into would be if you break down these programs into smaller tasks, with each specific task in its own separate method. For example, the determination of whether or not a number is prime is currently buried deep in a while loop in the one program (lines 16-21).  Ideally, that bit of code would be extracted out to a separate method that you could just pass a single number to check, like so:


This way, the functionality isn't buried deep in a loop or coupled to a specific execution flow. You can call it whenever you need to and it executes independently from whatever larger flow that it was used from. This both promotes modularity and reuse in your code, which is a good thing.

 
Junilu Lacar
Sheriff
Posts: 11145
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Without functional decomposition, you'll be forced to cut and paste pieces of the programs that you already have working and combine them into a third program.

Since you're still putting all your code into static main(), I suppose it would be too much to expect that you have been taught anything about objects in Java. Objects can make functional decomposition even more interesting by assigning specific sets of "responsibilities" to different objects.  See if the following code doesn't make sense, even without you being able to see what happens inside each object:


Line 1 - you create an object that knows how to extract numbers from a CSV file. A lot of the code you've already written could go in the class that defines the behavior of this object.
Line 2 - you call the method on that object that gives back a list of numbers it extracted from the CSV file

Line 4 - you create an object that knows how to display N things per line
Line 5 - you tell that object to display 5 items per line. It could have also been 7 or whatever you wanted.

Line 7 - you pass the Displayer object a list of numbers to print out and the object does its thing, printing out 5 numbers per line, just like you told it to in line 5.

Line 9 - you create an object that knows how to check for prime numbers. A lot of the code you've already written could go in the class that defines the behavior of this object.
Line 10 - you pass it the list of numbers extracted by the NumberExtractor object and get back another list that contains the primes from the list passed in

Line 12 - you give this new list to the display object to go and do it's 5 items per line thing again, only this time, do it with the list of prime numbers you just got from the PrimeChecker object.

Does that make sense? This is more like what a Java program should look like. If you can imagine it, it's like you're a magician who can conjure up objects using the new incantation, then "tell" those objects to do your bidding, and they will dutifully go and do whatever it is you've asked them to do.

I think it would be great if they taught you to organize your programs and your problem solving approach this way. Right now, you're developing bad habits that you'll have to correct later so that you can program and think the way I show here. Sorry if this has kind of a negative tone to it but I just want you to be aware of what you should be aiming for later as you learn more because frankly, the road you're on right now is leading you to a lot of confusion and frustration.
 
Junilu Lacar
Sheriff
Posts: 11145
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And the nice thing with decomposing functionality into small, relatively independent units of work like this is that it becomes much easier to adapt to new requirements.

For example, let's say you were given an additional assignment to print out the non-prime numbers in the CSV file. With the programs you've written now, that's going to be another complication, right? But with these kinds of objects I showed, you'd only have to go to one place and add a new capability. You could then write something like this:

When organized around objects, extending your programs to have new capabilities is very logical and systematic.

Here's what your thought process would be like:

"Which object should I use to get a list of non-primes?"

"Well, since this PrimeChecker object knows how to check for primes, wouldn't it make sense that conversely, it would also know when a number is not prime?"

"Ok, let me just add another method to PrimeChecker that's similar to filterPrimes, only this one does kind of the opposite..."

(Modify class PrimeChecker to add a filterNonPrimes() method that takes a list of numbers and gives back a list of all the non-prime numbers in the given list)

"There, now I can ask a PrimeChecker object to not only give me back a list of primes, I can also ask it to give me back a list of non-primes!"

"Then, since I already have this Displayer object that knows how to show a list of things the way I want it to (5 items per line), I'll just reuse it to show this other list as well."

"Wow, that was easier than I thought it would be." 

Ok, that last line was just for dramatic effect but it really could be that easy to make your program do more, if necessary.
 
Junilu Lacar
Sheriff
Posts: 11145
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you're wondering what this PrimeChecker class would look like, it's really very similar to what you've already written before, except it's been decomposed into small chunks of independent units of work.


You should have a good idea of what code goes in the isPrime() method since you've already solved that problem before. This isn't even the best implementation that I show here. There are a number of ways you can make this code even shorter and more elegant. However, it's self-contained enough and the details of the implementation have been hidden (abstracted away) from the rest of the code that uses it. This allows you to change the details of the implementation as much as you want, as long as you keep the way the calls are made and the return values the same (also known as the class API), you won't have to worry much about screwing up any code that uses it.

You might notice the little method on lines 26-28 and wonder what that's for. These kinds of helper methods make the code easier to read. The ! (negation) operator is kind of difficult to see so I will often hide it inside a method that spells out "not" in its name. Notice how it simply negates the result of the isPrime() method that was already written before. There are some, maybe even many, who will balk at doing this kind of thing because of a perceived hit on program performance and efficiency but when considering a tradeoff between readability vs performance, 98% of the time I will favor readability. Only when it can be objectively shown that there's a significant performance hit that matters will I consider favoring performance instead.
 
Ranga Ilandara
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[MODERATOR EDIT: deleted solution code]
 
Junilu Lacar
Sheriff
Posts: 11145
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch, Ranga Ilandara!

We don't allow members to post solution code in response to a question in the Beginning Java forum. The purpose is to give students a chance to do their own homework.  Also, we don't want the ranch to be a code mill.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!