• Post Reply Bookmark Topic Watch Topic
  • New Topic

Writing a static method that calculates whether the number in an array is a prime  RSS feed

 
Greenhorn
Posts: 25
1
Android
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
I seem to be having a hard time understanding how to do this. The assignment requires us to " Write a method isPrime that determines if a number is prime. If the number is prime the method returns true, otherwise it returns false". Then in main, we have to create an array and call the method to find each prime number and store it in the array. This is what I wrote so far, but I am getting this error (will post as an attachment). How would I use the method to determine if the number is prime and then store that number in the array?

Screenshot-(104).png
[Thumbnail for Screenshot-(104).png]
 
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

The isPrime() method takes a long value, and returns a boolean value.  Your are trying to pass in a long array instance, and trying to assign the result to a long array reference variable.

Neither operation is allowed, as (1) a long array is *not* a long, and (2) a boolean is *not* a long array.

Henry
 
Andrea Menjivar
Greenhorn
Posts: 25
1
Android
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
The isPrime() method takes a long value, and returns a boolean value.  Your are trying to pass in a long array instance, and trying to assign the result to a long array reference variable.

Neither operation is allowed, as (1) a long array is *not* a long, and (2) a boolean is *not* a long array.

Henry


Which is exactly what confuses me. But the instructions in my assignment say, " Write a method isPrime that determines if a number is prime. If the number is prime the method returns true, otherwise it returns false".
Unless I am misinterpreting that, I would love if someone could help clarify this for me?
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Andrea Menjivar wrote:
Which is exactly what confuses me. But the instructions in my assignment say, " Write a method isPrime that determines if a number is prime. If the number is prime the method returns true, otherwise it returns false".
Unless I am misinterpreting that, I would love if someone could help clarify this for me?


First, I didn't dive into your code, so can't tell you that it works correctly or not ... However, it does look like you did what the assignment said. The method does take a number. And the method also returns either true or false. This is not the problem.

The problem (which the compiler is complaining about) is how you use the method that you implemented.  You did not give the method a number -- you gave the method an array. You don't expect a true or false to be returned -- you expect an array to be returned.

Henry
 
Andrea Menjivar
Greenhorn
Posts: 25
1
Android
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
Andrea Menjivar wrote:
Which is exactly what confuses me. But the instructions in my assignment say, " Write a method isPrime that determines if a number is prime. If the number is prime the method returns true, otherwise it returns false".
Unless I am misinterpreting that, I would love if someone could help clarify this for me?


First, I didn't dive into your code, so can't tell you that it works correctly or not ... However, it does look like you did what the assignment said. The method does take a number. And the method also returns either true or false. This is not the problem.

The problem (which the compiler is complaining about) is how you use the method that you implemented.  You did not give the method a number -- you gave the method an array. You don't expect a true or false to be returned -- you expect an array to be returned.

Henry


Hi, so I rewrote my code but I am still confused as to how to place the prime numbers in the array. I am getting the error  "incompatible types: possible lossy conversion from long to int" for the line
primes[number] = number;

 
Sheriff
Posts: 4289
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Think about this line of code.  The first prime number is 2, so it will evaluate to

What about elements primes[0] and primes[1]?  You don't want to use the number as the index into your array.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!