Welcome to Dream.In.Code
Getting Java Help is Easy!

Join 136,424 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 2,352 people online right now. Registration is fast and FREE... Join Now!




Java Prime Number

2 Pages V  1 2 >  
Reply to this topicStart new topic

Java Prime Number, and leap year question

krod607
14 Oct, 2008 - 05:27 PM
Post #1

New D.I.C Head
*

Joined: 14 Oct, 2008
Posts: 8

Input: A Java int, called n

Output: true is n is prime, else false.

Steps:

Let sqroot be equal to the sqroot of n.

Loop setting a variable x starting at 2; while x<sqroot; add one to x after each loop

If n modulus x is zero then return false

End Loop

Return true



Note: This algorithm uses a basic idea that assumes the number is prime, unless it is proven otherwise.



trying to code this statement below is not working



package csc150;


CODE

public class PrimerNumber {
    private int number;
    public void Prime()
    {
        number=0;
    }

    public void Prime(int num)
    {
    number=num;
    }

    public void setPrime(int num)
    {
   number=num;

    }
    public void isPrime( )
    {
        for(int i=2; i<Math.sqrt(number); i++)
        {
            if(number%i==0)
            {
                System.out.println(number+" is not prime.");
                return;
            }
        }
        System.out.println(number+" is prime.");
    }

    public String toString()
    {
        String output="";  

    return output;

    }
}


*Edited to add the code tags to make code readable
Please: code.gif

This post has been edited by pbl: 14 Oct, 2008 - 08:22 PM
User is offlineProfile CardPM
+Quote Post

Ellie
RE: Java Prime Number
15 Oct, 2008 - 01:47 AM
Post #2

D.I.C Regular
Group Icon

Joined: 17 Jan, 2007
Posts: 422



Thanked: 4 times
Dream Kudos: 150
My Contributions

In what sense does this not work? I've attached the output I get from a quick test of your code. Seems ok to me.




Attached thumbnail(s)
Attached Image
User is offlineProfile CardPM
+Quote Post

krod607
RE: Java Prime Number
15 Oct, 2008 - 03:07 AM
Post #3

New D.I.C Head
*

Joined: 14 Oct, 2008
Posts: 8

QUOTE(Ellie @ 15 Oct, 2008 - 02:47 AM) *

In what sense does this not work? I've attached the output I get from a quick test of your code. Seems ok to me.



Thank you for your help the problem must be in my tester will have to review it
User is offlineProfile CardPM
+Quote Post

Ellie
RE: Java Prime Number
15 Oct, 2008 - 06:04 AM
Post #4

D.I.C Regular
Group Icon

Joined: 17 Jan, 2007
Posts: 422



Thanked: 4 times
Dream Kudos: 150
My Contributions

No worries. If it helps, here is the main method I wrote to test your program.

CODE
   public static void main (String args[]){
        PrimerNumber pn = new PrimerNumber();
        pn.Prime(13);
        pn.isPrime();
        pn.Prime(1300);
        pn.isPrime();
        pn.Prime(2);
        pn.isPrime();
        pn.Prime(2273);
        pn.isPrime();
        pn.Prime(14);
        pn.isPrime();
    }



User is offlineProfile CardPM
+Quote Post

krod607
RE: Java Prime Number
16 Oct, 2008 - 03:01 PM
Post #5

New D.I.C Head
*

Joined: 14 Oct, 2008
Posts: 8

QUOTE(Ellie @ 15 Oct, 2008 - 07:04 AM) *

No worries. If it helps, here is the main method I wrote to test your program.

CODE
   public static void main (String args[]){
        PrimerNumber pn = new PrimerNumber();
        pn.Prime(13);
        pn.isPrime();
        pn.Prime(1300);
        pn.isPrime();
        pn.Prime(2);
        pn.isPrime();
        pn.Prime(2273);
        pn.isPrime();
        pn.Prime(14);
        pn.isPrime();
    }


Thanks for your help so far got another is thats ok its a java code for a leap year program to determine if a it a leap year or not


Having a hard time setting up the if statements wrote it out first to try to see it first in words here is what i got could you take a look at it to see if im in the right direction


if the input module 4 ==0 (That is if the % operator is 0)

if the input module 100 == 0
if the input module 400 ==0
the year is a leap year

else

the year is not a leap year
end if the input module 400

else

the year is not a leap year
end if the input module 100 == 0

else

the year is not a leap year
end if the input module 4 == 0

User is offlineProfile CardPM
+Quote Post

pbl
RE: Java Prime Number
16 Oct, 2008 - 04:06 PM
Post #6

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 3,110



Thanked: 202 times
Dream Kudos: 75
My Contributions
CODE

boolean leap = false;
if(year % 4 == 0) {
   leap = true;
   if(year % 100 == 0) {
       leap = (year % 400) == 0;
   }
}


By the way your algrorithm is not complete... there is also something about the milleniums biggrin.gif
User is offlineProfile CardPM
+Quote Post

krod607
RE: Java Prime Number
16 Oct, 2008 - 05:36 PM
Post #7

New D.I.C Head
*

Joined: 14 Oct, 2008
Posts: 8

QUOTE(pbl @ 16 Oct, 2008 - 05:06 PM) *

CODE

boolean leap = false;
if(year % 4 == 0) {
   leap = true;
   if(year % 100 == 0) {
       leap = (year % 400) == 0;
   }
}


By the way your algrorithm is not complete... there is also something about the milleniums biggrin.gif


Thank you for your help
'
For the assignment my professor wants us to use nested if statements, not sure if the code you showed is for what i wrote above or just the proper way of doing it. Not looking for the code itself just wanted to see if what i had was a step in write direction, very new at java first class, like it so not looking for someone to do it for me i like to learn
User is offlineProfile CardPM
+Quote Post

pbl
RE: Java Prime Number
16 Oct, 2008 - 05:44 PM
Post #8

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 3,110



Thanked: 202 times
Dream Kudos: 75
My Contributions
QUOTE(krod607 @ 16 Oct, 2008 - 06:36 PM) *

For the assignment my professor wants us to use nested if statements, not sure if the code you showed is for what i wrote above or just the proper way of doing it. Not looking for the code itself just wanted to see if what i had was a step in write direction, very new at java first class, like it so not looking for someone to do it for me i like to learn


OK let's remove the boolean = condition

CODE

boolean leap = false;
if(year % 4 == 0) {
   leap = true;
   if(year % 100 == 0) {
       if(year % 400 == 0) {
          leap = false;
       }
   }
}

User is offlineProfile CardPM
+Quote Post

krod607
RE: Java Prime Number
21 Oct, 2008 - 02:25 PM
Post #9

New D.I.C Head
*

Joined: 14 Oct, 2008
Posts: 8

QUOTE(pbl @ 16 Oct, 2008 - 06:44 PM) *

QUOTE(krod607 @ 16 Oct, 2008 - 06:36 PM) *

For the assignment my professor wants us to use nested if statements, not sure if the code you showed is for what i wrote above or just the proper way of doing it. Not looking for the code itself just wanted to see if what i had was a step in write direction, very new at java first class, like it so not looking for someone to do it for me i like to learn


OK let's remove the boolean = condition

CODE

boolean leap = false;
if(year % 4 == 0) {
   leap = true;
   if(year % 100 == 0) {
       if(year % 400 == 0) {
          leap = false;
       }
   }
}



Mine looks alot different they yours      I know mine is not laid out properly


public class LeapYear {
    public static void main (String[] args) {

    
    int theYear = 0;

    
    if (theYear < 100) {
      
        if (theYear > 40) {
        theYear = theYear + 1900;
        }
        else {
        theYear = theYear + 2000;
        }
    }

    if (theYear % 4 == 0) {

      if (theYear % 100 != 0) {
        System.out.println(theYear + " is a leap year.");
        }
        else if (theYear % 400 == 0) {
        System.out.println(theYear + " is a leap year.");
        }

        else {
        System.out.println(theYear + " is not a leap year.");
        }
    }
    
    else {
        System.out.println(theYear + " is not a leap year.");
    }
    }
}



User is offlineProfile CardPM
+Quote Post

pbl
RE: Java Prime Number
21 Oct, 2008 - 03:41 PM
Post #10

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 3,110



Thanked: 202 times
Dream Kudos: 75
My Contributions
And the question is ?
User is offlineProfile CardPM
+Quote Post

krod607
RE: Java Prime Number
21 Oct, 2008 - 05:28 PM
Post #11

New D.I.C Head
*

Joined: 14 Oct, 2008
Posts: 8

QUOTE(pbl @ 21 Oct, 2008 - 04:41 PM) *

And the question is ?



Is this right??. I get no errors but that has happen before and it gets rejected from my professor.
User is offlineProfile CardPM
+Quote Post

pbl
RE: Java Prime Number
21 Oct, 2008 - 07:28 PM
Post #12

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 3,110



Thanked: 202 times
Dream Kudos: 75
My Contributions
QUOTE(krod607 @ 21 Oct, 2008 - 06:28 PM) *

Is this right??. I get no errors but that has happen before and it gets rejected from my professor.

Rejected for what ?

User is offlineProfile CardPM
+Quote Post

2 Pages V  1 2 >
Fast ReplyReply to this topicStart new topic
Time is now: 12/2/08 01:23PM

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month