Java help - winner gets a prize!

hopesfall

CAGiversary!
Feedback
14 (100%)
Ok, I'm working on a problem for my Java class and I have no idea what the hell I am doing. I am supposed to write a program that will make the user enter in an integer, and it will then print a list of all the prime integers less than or equal to the user's input. I am supposed to use a for loop, and I can't use any created methods. Keep in mind I'm a total idiot with this stuff, so maybe my terms aren't the best. Anyway, he gave us an example using a created method. Here it is:

public class PrintPrimes {
public static void main(String[ ] args) {
int x;
Scanner input = new Scanner(System.in);
System.out.println("Please enter a positive integer");
x = input.nextInt( );
System.out.println(
"The prime numbers less than or equal to " + x + " are ");
for (int i=2; i
 
I'm not a very good programmer, but if I were looking at this, I'd probably tell the isPrime method to print all numbers evaluating to true into a String object, and then have that String print itself out in the first method.

So if the number was 8, the primes are 3, 5, and 7. The first method counts 3-7, takes all those numbers down into the isPrime, and determines those 3 are prime. As it evaluates to true, it then appends those values into the String.

So like...String list.

isPrime(3) = true = list + " " + number
isPrime(4) = false = terminates this loop
isPrime(5) = true = list + " " + number

And so on.

Now I know that code isn't exactly written correctly, but basically you'd create "3 5 7" which could then be passed into the first method and printed out.

However, now that I look at the code, it calls for the isPrime method in the loop itself, and then prints out if it evaluates to true. Which almost makes me think the code is good to go already.

But again, I'm really bad at this, and there's probably many more people here who can help you better.
 
[quote name='Strell']I'm not a very good programmer, but if I were looking at this, I'd probably tell the isPrime method to print all numbers evaluating to true into a String object, and then have that String print itself out in the first method.

So if the number was 8, the primes are 3, 5, and 7. The first method counts 3-7, takes all those numbers down into the isPrime, and determines those 3 are prime. As it evaluates to true, it then appends those values into the String.

So like...String list.

isPrime(3) = true = list + " " + number
isPrime(4) = false = terminates this loop
isPrime(5) = true = list + " " + number

And so on.

Now I know that code isn't exactly written correctly, but basically you'd create "3 5 7" which could then be passed into the first method and printed out.

However, now that I look at the code, it calls for the isPrime method in the loop itself, and then prints out if it evaluates to true. Which almost makes me think the code is good to go already.

But again, I'm really bad at this, and there's probably many more people here who can help you better.[/QUOTE]

You forgot to tell him

Nick_BurnsYW.jpg
 
haha, trust me, you can't be any worse than I am. The only problem with your posts is that I can't actually use the isPrime method; I have to do everything in the main method. So he gave us the isPrime portion as sort of a "guideline" to what we should be doing with our program, if that makes sense. Does that make it any easier/less complicated to work with?
 
I believe the point of the assignment is to demonstrate that any bit of code invoked via a method could also simply be copied and pasted inline to the methods that call it. So you just copy the guts of isPrime to where it is called in your main method, rename the variables as you can't use i twice and there is nothing called 'number' in your main function and do something in place of the return lines in your main function. Hint 'number' in your main function would be the variable being passed to isPrime.

I could code it out, but you should be able to catch on and you'll get a lot more out of it if you work out the details yourself.
 
Ahh, then yea, do what wubb suggested. You can just copy the code from isPrime into the main function, right under the function call.

Which is really a backwards way of teaching people how to program. The point is to have a nice, tidy small main function with a lot of calls so that the main program looks efficient and clean, where allt he dirty work is being done outside of it.
 
[quote name='Strell']Ahh, then yea, do what wubb suggested. You can just copy the code from isPrime into the main function, right under the function call.

Which is really a backwards way of teaching people how to program. The point is to have a nice, tidy small main function with a lot of calls so that the main program looks efficient and clean, where allt he dirty work is being done outside of it.[/QUOTE]

Yeah, I also thought it was interesting that the prof set the assignment up like this rather than giving the example code as one big method w/ the task being to split out some of the work to a 2nd method. I guess it would just add complexity with everyone trying to figure out what would make sense to break out.
 
I think I see what you guys are saying. So basically just use the code from the isPrime method in place of where it is being called in the main method, and switch the variables around so that they are relevant to the main method, right? That makes sense. I kinda tried that earlier, but I think where I got tangled up is that I don't really understand I guess the "placement" of things. For example, when I looked at replacing the call to isPrime, I would want to take this:

for (int i=2; i
 
That's it, so you have a for loop nested inside the existing for loop, but you can't use i as the iterator for both for loops...
 
It is rather strange that all the code has to be in the main method. A slightly more elegant way to handle it, if you were able to do so, would be to have a recursive call inside the isPrime method- you could eliminate both loop structures. However, your only option now is to use nested loops. I think everyone else here already has you on the right track with that...
 
So I need to set up a new variable as the iterator for the second for loop (say "y" for example), but what do I do about the return false/return true part of the isPrime method? How do I "replace" that? Thanks again for the help guys...starting to feel like this problem is at least going places! :)
 
Instead of returning false when you hit the % = 0 case, why not set a boolean value that you then reference AFTER that inner for loop to determine if you need to println or not? That variable will need to be initialized BEFORE the inner for loop begins.
 
Thanks for all the help guys. However, I still haven't come up with the right code. An afternoon and evening spent on one simple problem is very...frustrating. But I do appreciate the help from everyone.

If anyone wants to write a Java program in the fashion I need, I'd make it worth your while...I can't afford to take a C on this project :cry:.
 
I don't have the Scanner class on my comp, so I tried another way to get the input. The code below should work anyway.

public class PrintPrimes {
public static void main(String[ ] args) {
int x;
Scanner input = new Scanner(System.in);
System.out.println("Please enter a positive integer");
x = input.nextInt( );
System.out.println("The prime numbers less than or equal to " + x + " are ");

for (int i=2; i
 
Replace "...." with spaces/tabs.

import java.util.Scanner;

public class PrintPrimes {
....public static void main(String[ ] args) {
........int x,i,j;
........boolean foundDivisor;
........
........Scanner input = new Scanner(System.in);
........System.out.println("Please enter a positive integer");
........x = input.nextInt( );
........System.out.println("The prime numbers less than or equal to " + x + " are ");
........for (i=2; i
 
I don't know how much more efficient that code is, but you created another variable foundDivisor; it might run faster, but it uses up more memory. Always a tradeoff between speed and memory :\ And what does the j*j really do?
 
[quote name='Squee']I don't know how much more efficient that code is, but you created another variable foundDivisor; it might run faster, but it uses up more memory. Always a tradeoff between speed and memory :\ And what does the j*j really do?[/quote]

It's a boolean variable. The memory usage is negligible.
 
Well extending the case to something like Fourier transforms and the like, I don't know how many variables you'd like to have, not to mention having to handle garbage collection. Just saying. Anyway, what does j*j do? I can't quite figure that out.:whistle2:k
 
Right.

First, my code uses _less_ memory, because storing instructions uses the same memory that would store variables, and I'm using less of those. But this is such a minor, trivial issue not even worth thinking about; and it would be a simple matter to modify the code to *not* use it, but it would make it slightly less readable.

Readability is so much more important in real world software development. Seriously.

It would be more efficient to use a labeled continue, but that's something that would not be used in real code, and would just confuse you.

Now, for the j*j part.

Suppose you want to check if a number slightly smaller than one million is a prime.

What you don't want to do is check it against every number between two and a million; it's enough to check up to the square root of the number (or around one thousand in our case) since one of the divisors would always be less than the square root (can you see why?). So in the worst-case scenario (the number is prime) the code only performs one thousandth of the work. Now, that's something worth thinking about.
 
That's cool, thanks for sharing the knowledge :). Just curious, what are you majoring in? I'm an EE.
 
If the OP can't figure out how to convert an invoked method to inline code I doubt the prof will believe he figured out how to optimize it (and in such a slick way) on his own.

And eldad is out of college and working in this field, I believe.

[quote name='eldad9']
Readability is so much more important in real world software development. Seriously.
[/QUOTE]

QFT.
 
bread's done
Back
Top