Results 1 to 13 of 13

Thread: help on program

  1. #1
    BenHart1121 is offline Member
    Join Date
    Apr 2015
    Posts
    4
    Rep Power
    0

    Default help on program

    Design a class named MyInteger. The class contains:

    An int data field named value that stores the int value of an integer.

    A constructor that creates a MyInteger object for the specified int value.

    A get method that returns the int value.

    A method, isPrime() that returns true if the value is a prime number. See section 4.10 of the text for Java code that detects prime numbers (this may vary depending on the edition you have).

    A static, isPrime(MyInteger) that returns true if the value is a prime number. Note that this method takes an object reference variable (rather than the value) as a parameter.

    Implement the class and write a program that tests all methods in the class. Your output should look like the following:

    Testing Instance method, is Prime

    isPrime: 997 is prime

    -----------------------------------

    Testing Class method (that takes a reference variable)is Prime

    isPrime: 997 is prime

    Testing Instance method, is Prime

    isPrime: 998 is not prime

    -----------------------------------

    Testing Class method (that takes a reference variable)is Prime

    isPrime: 998 is not prime

    Java Code:
    Java Code:
    public static void main(String[] args) { 
    MyInteger A = new MyInteger(997); //creates new integers, 997 & 998 
    MyInteger B = new MyInteger(998); 
    
    
    System.out.print("Testing class method (that uses reference variable), Is Prime"+"\n Is prime: "+A+MyInteger.isPrime(A)+"\n ------------------------"+"\n Testing instance method, Is Prime /n Is Prime: "+A+A.isPrime(B)); 
    
    System.out.println(); 
    System.out.print("Testing class method (that uses reference variable), Is Prime"+"\n Is prime: "+B+MyInteger.isPrime(B)+"\n------------... Testing instance method, Is Prime /n Is Prime: "+B+B.isPrime(A));	
    
    
    
    
    
    } 
    
    } 
    
    class MyInteger{ 
    
    int value; 
    
    MyInteger(int value2){ //constructor that creates a MyInteger object 
    value2 = value; 
    } 
    
    int getValue(){ //get method that determines int value 
    return value; 
    } 
    
    static boolean isPrime(MyInteger MyIntegerPrime){ //static method that determines whether an integer is prime 
    for (int i = 2; i < MyIntegerPrime.getValue(); i++){ 
    if(MyIntegerPrime.getValue() % 2 == 0) 
    return false; 
    } 
    return true; 
    } 
    
    boolean isPrime(){ //instance method that determines whether an integer is prime 
    for ( int i = 2; i < value; i++){ 
    if(value % 2 == 0) 
    return false; 
    } 
    return true; 
    } 
    }
    Could someone please explain why my code is not working properly? Thanks!

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,717
    Rep Power
    17

    Default Re: help on program

    What make you think that it is not working?

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    25

    Default Re: help on program

    Quote Originally Posted by BenHart1121 View Post
    help on program
    Please go through the Forum Rules, especially the third paragraph.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    BenHart1121 is offline Member
    Join Date
    Apr 2015
    Posts
    4
    Rep Power
    0

    Default Re: help on program

    oh sorry is there a way to change the title and my program dosent print out this isPrime: 997 is prime

    Testing Instance method, is Prime

    isPrime: 997 is prime

    -----------------------------------

    Testing Class method (that takes a reference variable)is Prime

    isPrime: 997 is prime it just keep returning the name of the program

  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,717
    Rep Power
    17

    Default Re: help on program

    Here's the code you are using to print:

    Java Code:
    System.out.print(
            "Testing class method (that uses reference variable), Is Prime"
            +"\n Is prime: "+A+MyInteger.isPrime(A)
            +"\n ------------------------"
            +"\n Testing instance method, Is Prime /n Is Prime: "
            +A
            +A.isPrime(B)); 
    
    System.out.println(); 
    System.out.print(
            "Testing class method (that uses reference variable), Is Prime"
            +"\n Is prime: "+B+MyInteger.isPrime(B)
            +"\n------------... Testing instance method, Is Prime /n Is Prime: "
            +B
            +B.isPrime(A));
    it just keep returning the name of the program
    The clearest way to describe the output is to copy and post it. I strongly suspect that you are seeing "MyInteger" in the output when you print B for example. That's because the MyInteger class does not override the toString() method and so its string form consists of "MyInteger" and a rather random looking number to distinguish the particular MyInteger instance you are printing.

    If you want to print 998 instead of the unusual looking string use

    Java Code:
    System.out.println("B is " + B.getValue());
        // not this
    //System.out.println("B is " + B);
    ---

    It would be a very good idea if you changed your variables so that they start with a lowercase letter: so myIntegerPrime for example. The code is much more readable that way. (Also on my monitor it is rather hard to read very long lines of code, so it's a good idea to break them.)

    Once you have corrected how the program is printing the MyInteger variables, if there are other problems post the updated code.

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,423
    Blog Entries
    7
    Rep Power
    27

    Default Re: help on program

    Your isPrime( ... ) methods suck; i.e. if a number isn't even, they consider it a prime number ... (so, 9, 15, 21, 25 ... are considered prime numbers).

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  7. #7
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: help on program

    Quote Originally Posted by JosAH View Post
    Your isPrime( ... ) methods suck; i.e. if a number isn't even, they consider it a prime number ... (so, 9, 15, 21, 25 ... are considered prime numbers).
    And it would also report that 2 is not a prime. Clearly, the OP didn't implement the supplied algorithm correctly.

    Hey OP, here's a hint. Why are you using a loop?

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  8. #8
    BenHart1121 is offline Member
    Join Date
    Apr 2015
    Posts
    4
    Rep Power
    0

    Default Re: help on program

    Quote Originally Posted by jim829 View Post
    And it would also report that 2 is not a prime. Clearly, the OP didn't implement the supplied algorithm correctly.

    Hey OP, here's a hint. Why are you using a loop?

    Regards,
    Jim
    for some reason the site wont let me reply to the thread

    Java Code:
    public class Assignment3{
    	public static void main(String[] args) { 
    
    MyInteger A = new MyInteger(997); //creates new integers, 997 & 998 
    
    MyInteger B = new MyInteger(998); 
    
    System.out.println("A is " + A.getValue());
    
    System.out.println("B is " + B.getValue());
    	}
    	}
    class MyInteger{ 
    
    		int value; 
    
    		MyInteger(int value2){ //constructor that creates a MyInteger object 
    		value= value2; 
    		} 
    
    		
    			boolean isPrime() { 
    
    				if (value<2) { 
    				return false;	
    				} 
    
    				int sqrt = (int) Math.sqrt(value);	
    				for (int i=2; i<=sqrt; i++) {
    				if (value % i == 0) {	
    				return false;	
    				} 
    				} 
    
    				return true;	
    				
    			}
    }
    here it still printing out null men i feel so stupid i cant figure out what i'm doing wrong

  9. #9
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: help on program

    Examine your constructor.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  10. #10
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,423
    Blog Entries
    7
    Rep Power
    27

    Default Re: help on program

    Quote Originally Posted by jim829 View Post
    Examine your constructor.
    Maybe I'm blind, but I don't see anything wrong with that constructor ...

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  11. #11
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: help on program

    Quote Originally Posted by JosAH View Post
    Maybe I'm blind, but I don't see anything wrong with that constructor ...

    kind regards,

    Jos
    The OP is assigning the instance field to the argument.

    Regards,
    Jim
    Last edited by jim829; 04-08-2015 at 06:08 PM.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  12. #12
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,423
    Blog Entries
    7
    Rep Power
    27

    Default Re: help on program

    Quote Originally Posted by jim829 View Post
    The OP is assigning the instance field to the argument.
    Ah, yes, but that was in the OP; it was fixed in reply #8:

    Java Code:
            MyInteger(int value2){ //constructor that creates a MyInteger object 
            value= value2; 
            }
    kind regards,

    Jos (<--- pfew, doesn't need glasses)
    Build a wall around Donald Trump; I'll pay for it.

  13. #13
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: help on program

    Quote Originally Posted by JosAH View Post
    Ah, yes, but that was in the OP; it was fixed in reply #8:

    Java Code:
            MyInteger(int value2){ //constructor that creates a MyInteger object 
            value= value2; 
            }
    kind regards,

    Jos (<--- pfew, doesn't need glasses)

    I had to go back to the original post to see the getValue method. That is where I noticed the constructor problem. So when I copied the getValue method to the new code, it worked. I'll bet the OP didn't change the constructor in the actual code (or did something with the getValue method that wasn't posted).

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Replies: 6
    Last Post: 12-15-2013, 06:40 AM
  2. Replies: 4
    Last Post: 11-21-2012, 01:19 PM
  3. Replies: 2
    Last Post: 04-02-2011, 12:57 PM
  4. changing my program to array working program
    By Chewart in forum New To Java
    Replies: 39
    Last Post: 11-18-2009, 06:53 PM
  5. Replies: 0
    Last Post: 04-04-2008, 02:40 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •