I'm in a beginner Java class at school and I'm having a hard time getting this to work.

Most of our problems require JOptionPane and for or while loops and if statements and those kinds of things...

For #6, I'm thinking about using a while loop and a for loop inside of that and then I'll use the mod function to make sure that a prime number is a number with no remainder...

For #7 and #8, I honestly have no idea how to start... I've tried looking up examples on the internet... but I'm still having a hard time putting it all together.

Thank you for anyone that's willing to help!!

6.
Write a program that repeatedly prompts the user to enter a positive integer > 1. The program should then take this integer and determine whether it is prime. (A prime integer is evenly divisible only by itself and 1.) Then display whether the integer is prime or composite (not prime). If the user enters 0, then exit from the program.

Hint: Use a for loop and the mod function.

7.
Consider the sum 1/2 + 1/4 + 1/8 + …. The first term of this sum is 1/2 = .5. The second term is 1/2 + 1/4 = .75. The third term is 1/2 + 1/4 + 1/8 = .875. Create a program that repeatedly prompts the user for a positive integer, n. Then have the program calculate the nth term of the above sum as a decimal and display it to the user. For example, if the user enters 4, then your program must compute 1/2 + 1/4 + 1/8 + 1/16 = .9375. If the user enters 0, then exit from the program.

Hint: Use a for loop.

8.
This problem is like problem 7, but instead of asking the user to enter an integer, n, we ask the user to enter a decimal, d, greater than 0 and less than 1. We then calculate what n is for the first term that causes the sum to be larger than d. Display this value of n to the user. For example, if the user enters .9 for d, then we should display 4 for n, since term 4 is the first term making the sum larger than .9. (Term 3 is only .875, which is less than .9)

Hint: Use a while loop.

## All 9 Replies

For number 6, that seems like a sound start.

For #7, a for loop seems like it'll be useful. For # 8, I'm thinking a while loop.

I hope that helps a little.

Generally, it's useful to think out a problem like this on paper. Suppose for #7 I give you 10 as the value for n. What would you have to do to get the answer?

Guide for Problem 7:

x = y = 0

if ( n < 1 ) exit

for x running through 1 to n
{
y = y + 1/ 2^x
}

Output y

I have #6 mostly right... but when I put in 15, it's wrong... And I'm stuck on #7... I'm not sure what to do next...

``````import javax.swing.JOptionPane;

public class M3_2E6 {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

//declare variables
String response;
int num = 0;

//show input box
response = JOptionPane.showInputDialog("Please enter a positive integer > 1: ");

//convert response to an integer
num = Integer.parseInt(response);

//loops
while (num > 0){

//check if 2 is entered.
if (num == 2){
JOptionPane.showMessageDialog(null, "You entered a prime number.");
response = JOptionPane.showInputDialog("Please enter a positive integer > 1: ");
}

for (int i=2; i<=num; i++){
int n = num%i;

if (n == 0){
JOptionPane.showMessageDialog(null, "You entered a composite number. ");
response = JOptionPane.showInputDialog("Please enter a positive integer > 1: ");
num = Integer.parseInt(response);

if (num == 0){
System.exit(0);
}

}
if (n != 0){
JOptionPane.showMessageDialog(null, "You entered a prime number. ");
response = JOptionPane.showInputDialog("Please enter a positive integer > 1: ");
num = Integer.parseInt(response);

if (num == 0){
System.exit(0);
}

break;

}

}
}

}

}``````
``````import javax.swing.JOptionPane;

public class M3_2E7 {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

//declare variables

String response;
int n, x, y = 0;
x=y;

//Input

response = JOptionPane.showInputDialog("Please enter a number: ");
n = Integer.parseInt(response);

//loop
if (n<1){
System.exit(0);
}

for (x=1; x>n; x++){
y = y + (1/(2^x));
}
JOptionPane.showMessageDialog(null, "The nth term is: " + y );

System.exit(0);

}

}``````

I think I see the problem with 15, and you're probably also getting a false prime for 9 as well, no? And if you entered 39, you'd probably also get a prime.

You'll see it too, if you walk through the while loop code carefully, with num == 15 (or 9, or 39 - any of them will do). Take a piece of paper and make a column for each of your variables and note each time they change, and execute each decision as though your were the computer.
It won't take you very long to spot the bug, I think.

Overall, there is some tightening you could do on this code, but nothing major. No need to check whether num==0 in a while loop when the condition for the loop is (num>0) and num is invariant within the loop. Your outer loop never gets you all the way back around to running the second iteration, unless I'm missing something. Since continuous input is part of the spec, that's a little more of an issue.

The main issue that's making this difficult is the structure is a bit messy, but I'm guessing that you haven't dealt with methods yet, so you get a pass on that one.

With number 7, you might want to look at the difference between integer and floating-point division.
In java, 5/2 != 5.0/2.0

This is probably causing you some trouble. Otherwise, your logic looks reasonable.

You can cut your time in half on #6 with a bit of logic shift.

``````public boolean isPrime(int value) {
for (double i = 2, max = value; i < max; i++) {
if (value % i == 0)
return false;
max = value / i;
}
return true;
}``````

You really only need to check 1/2 the numbers between 1 and n because x * y = y * x.

Remember what they say about premature optimization... first make sure it works, then worry about whether you can save time.

re Problem 7: I can see that you have followed through my logic well. However, you have not converted the user input integer into float, hence the problem. (I deliberately not write out the entire program otherwise you would not have a chance to practise by yourself).

If you still cannot get the right answer, let us know what the error output is.

Thanks for all the help guys... I think I kind of see where my error is.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.