• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Returning if a number is prime. (Chapter on Objects and classes. OOP)

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok i search and im' sure there i couldn't find something like this.

Design a class named MyInteger. The class contains:
**An int data field named value taht stores the int value represented by thsi object.
**a constructor that creates a MyInteger object for the specified int value
**A get method that returns the int value.
**Methods isEven(), isOdd(), and isPrime() that return true if the value is even, odd, or prime, repectively.
**Static methods iEven(int), isOdd(int), and isPrime(int) taht return true if the specified value is even, odd, or prime, respectively.
**Static methods isEven(MyInteger), isOdd(MyInteger), and isPrime(MyInteger) that return true if the specified value is even, odd, or prime, respectively.
**Methods equals(int) and equals(MyInteger) taht return true if the value in the object is equal to the specified value.
** a static method parseInt(char[]) that converts an array of numeric characters to an int value.

draw the UML diagram for the class. implement the class. Write a cleint program taht tests all methods in the class.

I"m at the returning Prime if the number is prime.
How do i prove it's prime? I know how to prove if it's even or odd but prime is only divisible by one and itself.

 
Bartender
Posts: 1849
15
Eclipse IDE Spring VI Editor Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I just did one of these.... I used this as my outline for the algorithm....

http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes

Hope that helps.
 
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
google "what is prime number"

http://en.wikipedia.org/wiki/List_of_prime_numbers

http://www.mathforum.org/dr.math/faq/faq.prime.num.html
 
Ranch Hand
Posts: 492
Firefox Browser VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Janeice's idea of using the Sieve of Eratosthenes will work really well but you could always use loops as well. And search for all possible factors of a number.

-Hunter
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To check if number n is prime or not,see if it is divisible by numbers ranging from 2 to n/2.If it is divisible then it is not prime.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your isEven() method does not even slightly fulfill your requirements. I don't think it will compile. Ditto isOdd().

Please don't anybody give any more hints, otherwise you will overstep the line between helping and doing somebody else's assignment.

You are not going about things correctly. You should not be creating a whole class. You should create a little class with an isEven method only, execute and test that, then add an isOdd method, and so on. You will find it much easier if you develop in tiny bits.

Draw your UML diagrams first. They can easily be drawn from the specification you are given and you can pick up 10% of your marks in 20 minutes.
 
Too many men are afraid of being fools - Henry Ford. Foolish tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic