Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: Confused with constructor

  1. #1
    Junior Member
    Join Date
    Mar 2013
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Confused with constructor

    basically it is giving me an error at the 2nd line new MyInterger(5)
    Im not sure how to make the constructor to get rid of the errors when im calling myInteger, isEven,isOdd, isPrime in the main method.
    Can someone please help me figure out what to change in the class MyInteger to make the program run smooth.
    <public class Classwork {
     
     
    	public static void main(String[] args) {
    		MyInteger n1 = new MyInteger(5);
    	    System.out.println("n1 is even? " + n1.isEven());
    	    System.out.println("n1 is prime? " + n1.isPrime());
    	    System.out.println("15 is prime? " + MyInteger.isPrime(15));
     
    	    char[] chars = {'3', '5', '3', '9'};
    	    System.out.println(MyInteger.parseInt(chars));
     
    	    String s = "3539";
    	    System.out.println(MyInteger.parseInt(s));
     
    	    MyInteger n2 = new MyInteger(24);
    	    System.out.println("n2 is odd? " + n2.isOdd());
    	    System.out.println("45 is odd? " + MyInteger.isOdd(45));
    	    System.out.println("n1 is equal to n2? " + n1.equals(n2));
    	    System.out.println("n1 is equal to 5? " + n1.equals(5));
    	  }
    	}
     
    class MyInteger {
     
     
     
    public MyInteger(int value) {
    	int number =  value;
    }
     
    public static boolean isPrime(int number){
    for (int divisor = 2; divisor <= number / 2; divisor++){
    	if (number % divisor == 0){
    		return false;
    	}
    }
    return true;
    }
    public static boolean isEven(int number){
     
    	if (number % 2 == 0)
    	return true;
     
     return false;
    									}
     
    public static boolean isOdd(int n2){
     
    	if (n2 % 2 == 1)
    	return true;
     
     return false;
    									}
     
    }
    >


  2. #2
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 139 Times in 134 Posts

    Default Re: Confused with constructor

    I think that we kind of sort of need to see the error messages in full.

  3. #3
    Junior Member
    Join Date
    Mar 2013
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Confused with constructor

    ok sorry here is the error message.
    You can ignore the parseInt messages.

    Exception in thread "main" java.lang.Error: Unresolved compilation problems:
    The method isEven(int) in the type MyInteger is not applicable for the arguments ()
    The method isPrime(int) in the type MyInteger is not applicable for the arguments ()
    The method parseInt(char[]) is undefined for the type MyInteger
    The method parseInt(String) is undefined for the type MyInteger
    The method isOdd(int) in the type MyInteger is not applicable for the arguments ()

    at Classwork.main(Classwork.java:7)

  4. #4
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 139 Times in 134 Posts

    Default Re: Confused with constructor

    Do the offending methods really need parameters? Are you wanting to do tests on the parameteres, or are you instead wanting to get the parameters out of the picture and do tests on the number held by the class (aha!)?

  5. #5
    Junior Member
    Join Date
    Mar 2013
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Confused with constructor

    well my instructor said i need to include the following things in my MyInteger class


    • An int data field named value that stores the int value represented by this 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, respectively.
    • Static methods isEven(int), isOdd(int), and isPrime(int) that 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) that 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.
    • A static method parseInt(String) that converts a string into an int value.

    I guess I need parameters but Im not really sure.

  6. #6
    Junior Member
    Join Date
    Mar 2013
    Posts
    13
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Default

    You need to declare int number as a global variable, not local to your constructor, your int number is being destroyed as soon as constructor finishes... Once that is fixed all your other methods can be written without any parameters...

  7. #7
    Junior Member
    Join Date
    Mar 2013
    Posts
    13
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Default

    Sorry writing this on my phone so missed all of the problem, you haven't written a method isEven(), isOdd(), or isPrime(), which take no parameters, but will be working on the int that is assigned by the constructor once you've made it global scope... With the static methods, you won't be required to create a second instance of MyInteger, simply call the methods with an int parameter... Hope that helps

  8. #8
    Junior Member Fabgio's Avatar
    Join Date
    Feb 2013
    Location
    Padua, Italy
    Posts
    8
    My Mood
    Fine
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Red face Re: Confused with constructor

    Basically you need to define a non-static context if you want to use constructors.
    Here is a modified version of the code.
    Hope this will work properly
    public class ClassWork {
        public static void main(String[] args) {
            MyInteger n1 = new MyInteger(5);
            System.out.println("n1 is even? " + n1.isEven());
            System.out.println("n1 is prime? " + n1.isPrime());
            System.out.println("n1 is odd? " + n1.isOdd());
            MyInteger n2 = new MyInteger(24);
            System.out.println("n2 is even? " + n2.isEven());
            System.out.println("n2 is prime " + n2.isPrime());
            System.out.println("n1 is odd? " + n2.isOdd());
            System.out.println("n1 is equal to n2?"+n1.equals(n2)); 
        }
    }
    // Hint: define a non-static context
    class MyInteger {
        int number;
         MyInteger(int value) {
            number = value;
        }
     
        public  boolean isPrime() {
            for (int divisor = 2; divisor <= number / 2; divisor++) 
                if (number % divisor == 0) 
                    return false; 
            return true;
        }
        public  boolean isEven() {
            if (number % 2 == 0) 
                return true;
            return false;
        }
     
        public boolean isOdd() {
            if (number % 2==1)
                return true;
            return false;  
        }
     
        public boolean equals (MyInteger o) { //hint: define a   "MyInteger" object as  parameter to compare objects 
            if(o.number==number) 
                return true;
            return false;
        }
    }
    Last edited by Fabgio; March 24th, 2013 at 11:44 AM. Reason: indentation error

Similar Threads

  1. So confused, I need help!
    By sessypeanut in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 2nd, 2012, 10:45 PM
  2. which class has a default constructor? (Req. guidance on constructor)
    By DragBall in forum Java Theory & Questions
    Replies: 1
    Last Post: June 27th, 2012, 04:42 PM
  3. I'm confused
    By Wonderlandslost in forum What's Wrong With My Code?
    Replies: 7
    Last Post: March 2nd, 2012, 12:33 PM
  4. help me , im confused!!!!
    By sephskie in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 11th, 2011, 10:52 AM
  5. Confused..
    By jadowers in forum What's Wrong With My Code?
    Replies: 0
    Last Post: October 5th, 2011, 12:56 PM