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 2 of 2

Thread: Need help with code

  1. #1
    Junior Member
    Join Date
    Aug 2014
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Need help with code

    Hi All

    I need some help with my code.

    public class MyInteger {
       private int value;
       public MyInteger(int number){
           value = number;
           System.out.println("Constructor created with value of " + value);
       }
       public int getValue(){
           return value;
       }
       public void setValue(int value){
           this.value = value;
       }
     
      public boolean isEven(){
          if(value % 2 == 0)
              return true; 
                      else 
              return false;
      }
      public boolean isOdd(){
          if(value % 2 != 0)
              return true;
          else
              return false;
      }
    }
     
    public class TestMyInteger {
        public static void main(String[] args){
            MyInteger integer1 = new MyInteger();
            integer1.setValue(55);
     
            MyInteger integer2 = new MyInteger();
            integer2.setValue(10);
     
            MyInteger integer3 = new MyInteger(99);
     
            System.out.println("integer1 is even " + integer1.isEven());
            System.out.println("integer1 is odd " + integer1.isOdd());
            System.out.println("integer2 is even " + integer2.isEven());
            System.out.println("integer2 is odd " + integer2.isOdd());
            System.out.println("integer3 is even " + integer3.isEven());
            System.out.println("integer3 is odd " + integer3.isOdd());
    }
    }
    I can't seem to get the value for integer1 and integer2 to pass with the setValue method. I get a runtime error stating the I need to have an int value for these two integers. Can someone help me out?

    thanks.
    Last edited by joejoe; August 10th, 2014 at 04:11 PM.


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Need help with code

    Welcome to the forum! Please read this topic to learn how to post code in code or highlight tags and other useful info for new members.

    Please post your code correctly using code or highlight tags per the above link.

    --- Update ---

    You should be getting a compiler error like:
    Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
    	The constructor MyInteger() is undefined
    	The constructor MyInteger() is undefined
     
    	at TestClass.main(TestClass.java:30)
    (and this is how you should post errors you want help with).

    The reason those errors occur is because the existing constructor MyInteger() requires an int argument. For example, if you changed:

    MyInteger integer1 = new MyInteger();

    to

    MyInteger integer1 = new MyInteger( 5 );

    the first error would be resolved. I don't know if that argument value is correct. I just used it as an example.

Similar Threads

  1. Replies: 3
    Last Post: April 27th, 2013, 07:19 AM
  2. Replies: 4
    Last Post: January 24th, 2013, 11:20 AM
  3. Replies: 7
    Last Post: January 24th, 2013, 10:41 AM
  4. Replies: 5
    Last Post: November 14th, 2012, 10:47 AM
  5. Replies: 3
    Last Post: September 3rd, 2012, 11:36 AM